Skip to content

Commit

Permalink
feat: support multi cursor insert debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
libondev committed Jul 30, 2023
1 parent 4ba4b47 commit 05cf481
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 130 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"vite",
"Vitesse",
"vitest"
]
],
"commentTranslate.source": "DarkCWK.youdao-youdao"
}
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/libondev/debugger-for-console"
},
"main": "./dist/index.js",
"main": "./dist/extension.js",
"publisher": "banlify",
"icon": "res/icon.png",
"files": [
Expand All @@ -23,7 +23,7 @@
"url": "https://github.com/libondev/debugger-for-console/issues"
},
"scripts": {
"build": "npm run typecheck && tsup src/index.ts --external vscode",
"build": "npm run typecheck && tsup src/extension.ts --external vscode",
"dev": "nr build --watch",
"lint": "eslint . --fix",
"vscode:prepublish": "nr build",
Expand All @@ -38,7 +38,6 @@
"@types/node": "^18.17.1",
"@types/vscode": "1.80.0",
"@vscode/vsce": "^2.19.0",
"acorn": "^8.10.0",
"bumpp": "^9.1.1",
"eslint": "^8.45.0",
"pnpm": "^6.35.1",
Expand Down Expand Up @@ -129,10 +128,10 @@
"default": "'",
"description": "Insert the quoted text in the debug statement."
},
"debugger-for-console.fileName": {
"type": "boolean",
"default": true,
"description": "Insert the filename in the debug statement."
"debugger-for-console.fileDepth": {
"type": "number",
"default": 2,
"description": "The path depth of the current file. (Set to 0 to disable the path)."
},
"debugger-for-console.autoSave": {
"type": "boolean",
Expand Down
11 changes: 1 addition & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 26 additions & 8 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { window } from 'vscode'
import { getDebuggerStatement } from '../utils/index'
import { Position, WorkspaceEdit, window, workspace } from 'vscode'
import { getDebuggerStatement, getInsertLineIndents } from '../utils/index'
import { documentAutoSaver, getScopeSymbols } from '../features'

export async function createDebuggers() {
async function create(direction: 'before' | 'after' = 'after') {
const editor = window.activeTextEditor!

// const logger = await getDebuggerStatement(editor, 'this is a debugger statement.')
const fileUri = editor.document.uri
const workspaceEdit = new WorkspaceEdit()
const selectionsLength = editor.selections.length

// console.log(await getDebuggerStatement(editor, 'this is a debugger statement.'))
const scopeSymbols = await getScopeSymbols(editor)

console.log(await getDebuggerStatement(editor))
}
for (let i = 0; i < selectionsLength; i++) {
const selection = editor.selections[i]
const line = selection.end.line + (direction === 'before' ? 0 : 1)
const indents = getInsertLineIndents(editor, line)
const debuggerStatement = getDebuggerStatement(editor, selection, scopeSymbols)

workspaceEdit.insert(
fileUri,
new Position(line, 0),
`${indents}${debuggerStatement}`,
)
}

export async function createDebuggersBefore() {
await workspace.applyEdit(workspaceEdit)

documentAutoSaver(editor)
}

export const createDebuggers = create.bind(null, 'after')

export const createDebuggersBefore = create.bind(null, 'before')
5 changes: 3 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createDebuggers, createDebuggersBefore } from './create'
import { removeDebuggers } from './remove'
import { toggleDebuggers } from './toggle'
import { commentDebuggers, uncommentDebuggers } from './toggle'
import { updateUserConfig } from './update'

export const commandsMapping = {
'debugger-for-console.comment': commentDebuggers,
'debugger-for-console.uncomment': uncommentDebuggers,
'debugger-for-console.create': createDebuggers,
'debugger-for-console.before': createDebuggersBefore,
'debugger-for-console.toggle': toggleDebuggers,
'debugger-for-console.remove': removeDebuggers,
'debugger-for-console.update': updateUserConfig,
} as const
42 changes: 41 additions & 1 deletion src/commands/remove.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
export function removeDebuggers() {
import type { Range, TextDocument } from 'vscode'
import { WorkspaceEdit, window, workspace } from 'vscode'
import { getLanguageStatement } from '../utils'
import { documentAutoSaver } from '../features'

function getStatements(document: TextDocument, regexp: RegExp) {
const text = document.getText()

let range: Range
const statements = [...text.matchAll(regexp)].reduce((acc, match) => {
range = document.lineAt(document.positionAt(match.index!).line).range

if (!range.isEmpty) {
acc.push(range)
}

return acc
}, [] as Range[])

return statements
}

export async function removeDebuggers() {
const editor = window.activeTextEditor!

const matchStatements = getStatements(editor.document, new RegExp(getLanguageStatement(editor), 'gm'))

if (!matchStatements.length) {
window.showInformationMessage('No debugger statements found.')
return
}

const workspaceEdit = new WorkspaceEdit()
matchStatements.forEach((statement) => {
const lineRange = statement.with(statement.start.with(statement.start.line + 1, 0))
workspaceEdit.delete(editor.document.uri, statement)
workspaceEdit.delete(editor.document.uri, lineRange)
})

await workspace.applyEdit(workspaceEdit)

documentAutoSaver(editor)
}
6 changes: 5 additions & 1 deletion src/commands/toggle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function toggleDebuggers() {
export function commentDebuggers() {

}

export function uncommentDebuggers() {

}
8 changes: 6 additions & 2 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { workspace } from 'vscode'
import { resolvedConfig } from '../index'
import { resolvedConfig } from '../extension'
import { quote, semi } from '../features/optional'

export function updateUserConfig() {
Object.assign(
const config = Object.assign(
resolvedConfig,
workspace.getConfiguration('debugger-for-console'),
)

quote.update(config.get('quote')!)
semi.update(config.get('semi') ? ';' : '')
}
File renamed without changes.
33 changes: 33 additions & 0 deletions src/features/auto-resolving.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Position, Selection, TextDocument } from 'vscode'

const BREAK_CHARACTER = [
' ', '\n', '\'', '"', '`', '=', '\\', '(', ',',
')', '+', '-', '*', '/', '%', '{', '<', '>',
]

function getWordAtPosition(document: TextDocument, position: Position): string {
const word = document.getWordRangeAtPosition(position)
if (!word)
return ''

const lineContent = document.lineAt(position.line).text
let start = word.start.character

while (start > 0 && !BREAK_CHARACTER.includes(lineContent[start - 1])) {
start--
}

return lineContent.slice(start, word.end.character)
}

export function getVariables({ document }: ActiveTextEditor, selection: Selection): string {
if (selection.isEmpty) {
return getWordAtPosition(document, selection.anchor)
}

const start = selection.start.character
const end = selection.end.character
const lineContent = document.lineAt(selection.start.line).text

return lineContent.slice(start, end)
}
16 changes: 16 additions & 0 deletions src/features/file-depth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { sep } from 'node:path'
import { workspace } from 'vscode'
import { resolvedConfig } from '../extension'

export function getFileDepth(editor: ActiveTextEditor) {
const depths = resolvedConfig.get('fileDepth')

if (depths) {
const relationFilePath = workspace.asRelativePath(editor.document.fileName)
const splitFilePaths = relationFilePath.split(new RegExp(`\\${sep}`))

return ` ${splitFilePaths.slice(-depths).join('/')}`
}

return ''
}
4 changes: 4 additions & 0 deletions src/features/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './file-depth'
export * from './optional'
export * from './symbols'
export * from './auto-resolving'
27 changes: 27 additions & 0 deletions src/features/optional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Selection } from 'vscode'
import { lazyValue } from '../utils/index'
import { resolvedConfig } from '../extension'

const EMOJIS = [
'🚀', '🎈', '🎆', '🎇', '✨', '🎉', '🎊', '🎃', '🎄', '🎍', '🎏',
'🎐', '🎑', '🎡', '👑', '🧶', '⚽', '🥎', '🏀', '🏐', '🎮', '📦',
]

export const semi = lazyValue<string>()
export const quote = lazyValue<string>()

export function getRandomEmoji() {
return resolvedConfig.get('emoji') ? EMOJIS[Math.floor(Math.random() * EMOJIS.length)] : ''
}

export function getLineNumber(selection: Selection) {
return resolvedConfig.get('lineNumber') ? `:${selection.active.line + 1}` : ''
}

export function documentAutoSaver(editor: ActiveTextEditor) {
if (!resolvedConfig.get('autoSave')) {
return
}

editor.document.save()
}
20 changes: 16 additions & 4 deletions src/utils/symbol.ts → src/features/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import type { DocumentSymbol, Position } from 'vscode'
import { commands } from 'vscode'
import type { ActiveTextEditor } from '../types/global'
import { resolvedConfig } from '../extension'

export async function getCurrentScopeSymbol({ document: { uri }, selection: { active } }: ActiveTextEditor) {
export async function getCurrentScopeSymbol({
document: { uri },
selection: { active },
}: ActiveTextEditor) {
const symbols = await commands.executeCommand<DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', uri)

if (!symbols) {
return ''
return []
}

let scopeSymbols: DocumentSymbol[] = []
Expand All @@ -18,7 +21,8 @@ export async function getCurrentScopeSymbol({ document: { uri }, selection: { ac
}
}

return scopeSymbols.map(({ name }) => name).join(' > ')
return scopeSymbols.reduce((acc, { name }) => `${acc} > ${name}`, '').slice(3)
// return scopeSymbols
}

function findNestedSymbols(symbol: DocumentSymbol, position: Position): DocumentSymbol[] {
Expand All @@ -33,3 +37,11 @@ function findNestedSymbols(symbol: DocumentSymbol, position: Position): Document

return nestedSymbols
}

export async function getScopeSymbols(editor: ActiveTextEditor) {
if (!resolvedConfig.get('symbols')) {
return ''
}

return `@${await getCurrentScopeSymbol(editor)}`
}
5 changes: 5 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { window } from 'vscode'

declare global {
type ActiveTextEditor = Exclude<typeof window.activeTextEditor, undefined>;
}
1 change: 1 addition & 0 deletions src/syntax/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './javascript'
Loading

0 comments on commit 05cf481

Please sign in to comment.