From 68d8c0218d24bf17b324df48da237d94fe6e1fe7 Mon Sep 17 00:00:00 2001 From: Andreas Arvidsson Date: Sun, 26 Nov 2023 00:41:08 +0100 Subject: [PATCH] Added go to line command --- README.md | 2 ++ package.json | 7 ++++++- src/commands/commands.ts | 1 + src/commands/goToLine.ts | 22 ++++++++++++++++++++++ src/commands/registerCommands.ts | 2 ++ src/test/commands.test.ts | 26 -------------------------- src/test/goToLine.test.ts | 17 +++++++++++++++++ src/test/testUtil/getFullFixture.ts | 2 +- 8 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 src/commands/goToLine.ts delete mode 100644 src/test/commands.test.ts create mode 100644 src/test/goToLine.test.ts diff --git a/README.md b/README.md index 95e0199..5c76eed 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ Many of the commands take arguments and return values that can only be used with Open editor/tab at given index. Negative indices are counted from the back. - `andreas.focusTab(hint: string)` Focus tab by hint. Hints range [A-ZZ]. +- `andreas.goToLine(line: number)` + Go to line number. - `andreas.selectTo(line: number)` Select from current location to specified line. - `andreas.lineMiddle()` diff --git a/package.json b/package.json index bd1b1d2..161ab8a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "andreas-talon", "displayName": "Andreas Talon", "description": "VSCode extension used by Talon Voice", - "version": "3.39.0", + "version": "3.40.0", "publisher": "AndreasArvidsson", "license": "MIT", "main": "./out/extension.js", @@ -163,6 +163,11 @@ "category": "Andreas", "title": "Focus tab by hint." }, + { + "command": "andreas.goToLine", + "category": "Andreas", + "title": "Go to line number." + }, { "command": "andreas.selectTo", "category": "Andreas", diff --git a/src/commands/commands.ts b/src/commands/commands.ts index 2e1a0dd..c8ad8ae 100644 --- a/src/commands/commands.ts +++ b/src/commands/commands.ts @@ -76,6 +76,7 @@ export const commandDescriptions = { "(index: number)" ), focusTab: visible("Navigation", "Focus tab by hint.", "Hints range [A-ZZ].", "(hint: string)"), + goToLine: visible("Navigation", "Go to line number.", undefined, "(line: number)"), selectTo: visible( "Navigation", "Select from current location to specified line.", diff --git a/src/commands/goToLine.ts b/src/commands/goToLine.ts new file mode 100644 index 0000000..68bac10 --- /dev/null +++ b/src/commands/goToLine.ts @@ -0,0 +1,22 @@ +import { Position, Selection, window } from "vscode"; + +export function goToLine(line: number): void { + const editor = window.activeTextEditor; + + if (!editor) { + return; + } + + const documentLine = editor.document.lineAt(line); + + if (!documentLine) { + throw Error(`No line ${line} in document`); + } + + const position = new Position( + documentLine.lineNumber, + documentLine.firstNonWhitespaceCharacterIndex + ); + + editor.selection = new Selection(position, position); +} diff --git a/src/commands/registerCommands.ts b/src/commands/registerCommands.ts index ce21404..ebbc0c1 100644 --- a/src/commands/registerCommands.ts +++ b/src/commands/registerCommands.ts @@ -17,6 +17,7 @@ import { renameFile } from "./files/renameFile"; import { focusTab } from "./focusTab"; import { formatSelectedFiles, formatWorkspaceFiles } from "./formatFiles"; import { generateRange } from "./generateRange"; +import { goToLine } from "./goToLine"; import { decrement, increment } from "./incrementDecrement"; import { lineMiddle } from "./lineMiddle"; import { openEditorAtIndex } from "./openEditorAtIndex"; @@ -52,6 +53,7 @@ export function registerCommands( // Navigation openEditorAtIndex, focusTab, + goToLine, selectTo, lineMiddle, // Text diff --git a/src/test/commands.test.ts b/src/test/commands.test.ts deleted file mode 100644 index 890ec89..0000000 --- a/src/test/commands.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { runTest } from "./testUtil/runTest"; - -const command = "generateRange"; - -suite(command, () => { - runTest({ - title: command, - command, - pre: { - content: "a\nb\nc", - selections: [ - [0, 1], - [1, 1], - [2, 1] - ] - }, - post: { - content: "a1\nb2\nc3", - selections: [ - [0, 2], - [1, 2], - [2, 2] - ] - } - }); -}); diff --git a/src/test/goToLine.test.ts b/src/test/goToLine.test.ts new file mode 100644 index 0000000..e74ee8a --- /dev/null +++ b/src/test/goToLine.test.ts @@ -0,0 +1,17 @@ +import { runTest } from "./testUtil/runTest"; + +const command = "goToLine"; + +suite(command, () => { + runTest({ + title: command, + command: { id: command, args: [1] }, + pre: { + content: "a\n b", + selections: [0, 0] + }, + post: { + selections: [1, 2] + } + }); +}); diff --git a/src/test/testUtil/getFullFixture.ts b/src/test/testUtil/getFullFixture.ts index d778fa7..d459bab 100644 --- a/src/test/testUtil/getFullFixture.ts +++ b/src/test/testUtil/getFullFixture.ts @@ -29,7 +29,7 @@ function getCallback(fixture: TestFixture): () => Thenable { const { command } = fixture; const id = typeof command === "object" ? command.id : command; - const args: unknown[] = typeof command === "object" ? command.args ?? [] : []; + const args: unknown[] = typeof command === "object" ? command.args : []; return () => { return commands.executeCommand(getFullCommand(id), ...args);