Skip to content

Commit

Permalink
Added dialog window for missing command arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Sep 2, 2023
1 parent 3f577f7 commit c9fecb0
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 19 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Many of the commands take arguments and return values that can only be used with
- `andreas.formatWorkspaceFiles()`
Format workspace files
- `andreas.formatSelectedFiles()`
Format selected files
Format selected files. Used by file explorer context menu.

### Edit commands

Expand Down Expand Up @@ -162,12 +162,18 @@ generate range [from <number_small>]:

![Tab view](images/tab_view.png)

### Format selected files context menu

<img src="images/format_context.png" height="500" />

## Demo

[YouTube - On hover and go to definition](https://youtu.be/UdMLNVLkBkg)

[YouTube - Tab view](https://youtu.be/35yRJwSjTCk)

[YouTube - Format selected files](https://youtu.be/UiWT6GI86Nw)

## Build

```bash
Expand Down
Binary file added images/format_context.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions 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.33.3",
"version": "3.34.0",
"publisher": "AndreasArvidsson",
"license": "MIT",
"main": "./out/extension.js",
Expand Down Expand Up @@ -156,20 +156,17 @@
{
"command": "andreas.openEditorAtIndex",
"category": "Andreas",
"title": "Open editor/tab at given index.",
"enablement": "false"
"title": "Open editor/tab at given index."
},
{
"command": "andreas.focusTab",
"category": "Andreas",
"title": "Focus tab by hint.",
"enablement": "false"
"title": "Focus tab by hint."
},
{
"command": "andreas.selectTo",
"category": "Andreas",
"title": "Select from current location to specified line.",
"enablement": "false"
"title": "Select from current location to specified line."
},
{
"command": "andreas.lineMiddle",
Expand Down
12 changes: 8 additions & 4 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ export const commandDescriptions = {
removeFile: visible("File", "Remove/delete the active file."),
moveFile: visible("File", "Move active file to new directory."),
formatWorkspaceFiles: visible("File", "Format workspace files"),
formatSelectedFiles: visible("File", "Format", "selected files"),
formatSelectedFiles: visible(
"File",
"Format",
"selected files. Used by file explorer context menu."
),

// Edit commands
generateRange: visible(
Expand All @@ -65,14 +69,14 @@ export const commandDescriptions = {
decrement: visible("Edit", "Decrement selected number.", undefined, "(value?: number)"),

// Navigation commands
openEditorAtIndex: hidden(
openEditorAtIndex: visible(
"Navigation",
"Open editor/tab at given index.",
"Negative indices are counted from the back.",
"(index: number)"
),
focusTab: hidden("Navigation", "Focus tab by hint.", "Hints range [A-ZZ].", "(hint: string)"),
selectTo: hidden(
focusTab: visible("Navigation", "Focus tab by hint.", "Hints range [A-ZZ].", "(hint: string)"),
selectTo: visible(
"Navigation",
"Select from current location to specified line.",
undefined,
Expand Down
27 changes: 26 additions & 1 deletion src/commands/focusTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { TabGroup, commands, window } from "vscode";
import { focusViewColumn } from "../util/focusViewColumn";
import { hintToIndex } from "../util/hints";

export async function focusTab(hint: string): Promise<void> {
export async function focusTab(hint?: string): Promise<void> {
if (hint == null) {
hint = await showInputBox();
if (hint == null) {
console.warn("Can't focus tab: Missing hint argument.");
return;
}
}

const index = hintToIndex(hint);
const tabInfo = getTabInfo(index);

Expand All @@ -29,3 +37,20 @@ function getTabInfo(index: number): { group: TabGroup; index: number } | null {

return null;
}

async function showInputBox(): Promise<string | undefined> {
const value = await window.showInputBox({
placeHolder: "Tab hint: [a-zA-Z]{1,2}",
ignoreFocusOut: true,
validateInput: (value) => {
if (/^[a-zA-Z]{1,2}$/.test(value.trim())) {
return null;
}
return "Must be one or two letters: [a-zA-Z]{1,2}";
}
});
if (value != null) {
return value.trim();
}
return undefined;
}
33 changes: 29 additions & 4 deletions src/commands/openEditorAtIndex.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { window, commands } from "vscode";
import { commands, window } from "vscode";

export function openEditorAtIndex(index: number): Thenable<void> {
// Negative index starts from the back
export async function openEditorAtIndex(index?: number): Promise<void> {
if (index == null) {
index = await showInputBox();
if (index == null) {
console.warn("Can't open editor: Missing index argument.");
return;
}
}

// Negative indices starts from the back
if (index < 0) {
index += window.tabGroups.activeTabGroup.tabs.length;
}

return commands.executeCommand("workbench.action.openEditorAtIndex", index);
await commands.executeCommand("workbench.action.openEditorAtIndex", index);
}

async function showInputBox(): Promise<number | undefined> {
const value = await window.showInputBox({
placeHolder: "Editor index (0 offset)",
ignoreFocusOut: true,
validateInput: (value) => {
if (/^-?\d+$/.test(value.trim())) {
return null;
}
return "Must be integer";
}
});
if (value != null) {
return parseInt(value.trim());
}
return undefined;
}
27 changes: 26 additions & 1 deletion src/commands/selectTo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Selection, window } from "vscode";

export function selectTo(lineNumber: number): void {
export async function selectTo(lineNumber?: number): Promise<void> {
const editor = window.activeTextEditor;
if (!editor) {
return;
}

if (lineNumber == null) {
lineNumber = await showInputBox();
if (lineNumber == null) {
console.warn("Can't select to: Missing line number argument.");
return;
}
}

const { start, end } = editor.selection;
const line = editor.document.lineAt(lineNumber);
if (line.range.start.isBefore(start)) {
Expand All @@ -14,3 +22,20 @@ export function selectTo(lineNumber: number): void {
editor.selection = new Selection(start, line.range.end);
}
}

async function showInputBox(): Promise<number | undefined> {
const value = await window.showInputBox({
placeHolder: "Line number (0 offset)",
ignoreFocusOut: true,
validateInput: (value) => {
if (/^\d+$/.test(value.trim())) {
return null;
}
return "Must be positive integer";
}
});
if (value != null) {
return parseInt(value.trim());
}
return undefined;
}
9 changes: 8 additions & 1 deletion src/util/showNewNameInputBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ export async function showNewNameInputBox(
): Promise<string | undefined> {
const filename = await window.showInputBox({
prompt: "New name",
placeHolder: "New name",
value: `${suggestedName}${suggestedExt}`,
valueSelection: [0, suggestedName.length],
ignoreFocusOut: true
ignoreFocusOut: true,
validateInput: (value) => {
if (value.trim()) {
return null;
}
return "Can't be empty";
}
});
return filename ? filename.trim() : undefined;
}

0 comments on commit c9fecb0

Please sign in to comment.