From 4716c5d2def9c23e413c8df5d3eeb3c531f63471 Mon Sep 17 00:00:00 2001 From: Thomas Foskolos Date: Thu, 17 Nov 2022 09:26:15 +0200 Subject: [PATCH] added relative go to negative with the minus sign prefilled --- package.json | 1 + src/extension.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/package.json b/package.json index 9b83a20..7f78e3e 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "shortcuts", "relative", "goto", + "gotoNegative", "lines", "jump" ], diff --git a/src/extension.ts b/src/extension.ts index 2d2452f..6e7b910 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -74,6 +74,63 @@ export async function activate(context: vscode.ExtensionContext) { } ); +// The command has been defined in the package.json file + // Now provide the implementation of the command with registerCommand + // The commandId parameter must match the command field in package.json + const registeredCommandGotoNegative = vscode.commands.registerCommand( + "relativity.gotoNegative", + async () => { + // Get the current editor + const editor: vscode.TextEditor | undefined = + vscode.window.activeTextEditor; + + // If the current editor is undefined (meaning user focus not on a editor), just ignore the command + if (!editor) return; + + // Get input from user + const input: string | undefined = await vscode.window.showInputBox({ + value: "-", + prompt: "Jump using relative number of lines", + validateInput: newPeekline(editor), + }); + // Delete highlight generated in preview function + deleteHighlight(editor); + + // End if input box closed after losing focus or if user pressed esc or if user pressed enter with no input + if (input === undefined || input === "") { + // Replace visible range/viewPort with how it was before the preview + editor.revealRange( + new vscode.Range(editor.selection.active, editor.selection.active), + vscode.TextEditorRevealType.InCenterIfOutsideViewport + ); + + return; + } + + // Get lines to jump and characters to jump from input + const { linesToJump, charactersToJump } = parseInput(input); + + // Create the new end position using linesToJump + const newPosition: vscode.Position = createNewPosition( + editor, + linesToJump, + charactersToJump + ); + + // Create new selection object where the start and end positions are the same, to make it a singular cursor movement + const newSelection: vscode.Selection = new vscode.Selection( + newPosition, + newPosition + ); + + // Set new selection onto current text editor + editor.selection = newSelection; + + // Shifts visible range if needed + shiftVisibleRange(editor, newPosition); + } + ); + const registeredCommandSelect = vscode.commands.registerCommand( "relativity.select", async () => { @@ -130,6 +187,7 @@ export async function activate(context: vscode.ExtensionContext) { ); context.subscriptions.push(registeredCommandGoto); + context.subscriptions.push(registeredCommandGotoNegative); context.subscriptions.push(registeredCommandSelect); }