-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multi cursor insert debugger
- Loading branch information
Showing
20 changed files
with
293 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ | |
"vite", | ||
"Vitesse", | ||
"vitest" | ||
] | ||
], | ||
"commentTranslate.source": "DarkCWK.youdao-youdao" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export function toggleDebuggers() { | ||
export function commentDebuggers() { | ||
|
||
} | ||
|
||
export function uncommentDebuggers() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './javascript' |
Oops, something went wrong.