Skip to content

Commit

Permalink
Added go to line command
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Nov 25, 2023
1 parent 24f6aed commit 68d8c02
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
22 changes: 22 additions & 0 deletions src/commands/goToLine.ts
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -52,6 +53,7 @@ export function registerCommands(
// Navigation
openEditorAtIndex,
focusTab,
goToLine,
selectTo,
lineMiddle,
// Text
Expand Down
26 changes: 0 additions & 26 deletions src/test/commands.test.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/test/goToLine.test.ts
Original file line number Diff line number Diff line change
@@ -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]
}
});
});
2 changes: 1 addition & 1 deletion src/test/testUtil/getFullFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getCallback(fixture: TestFixture): () => Thenable<unknown> {

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);
Expand Down

0 comments on commit 68d8c02

Please sign in to comment.