Skip to content

Commit

Permalink
Added format commands
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Sep 1, 2023
1 parent 7508600 commit 7954c1e
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 24 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ Many of the commands take arguments and return values that can only be used with
Remove/delete the active file.
- `andreas.moveFile()`
Move active file to new directory.
- `andreas.formatWorkspaceFiles()`
Format workspace files
- `andreas.formatSelectedFiles()`
Format selected files

### Edit commands

Expand All @@ -46,7 +50,7 @@ Many of the commands take arguments and return values that can only be used with
- `andreas.selectTo(line: number)`
Select from current location to specified line.
- `andreas.lineMiddle()`
Move curser to middle of the current line.
Move cursor to middle of the current line.

### Text commands

Expand Down
73 changes: 54 additions & 19 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.32.2",
"version": "3.33.0",
"publisher": "AndreasArvidsson",
"license": "MIT",
"main": "./out/extension.js",
Expand Down Expand Up @@ -95,7 +95,8 @@
{
"command": "andreas.getFilename",
"category": "Andreas (File)",
"title": "Get filename of active file."
"title": "Get filename of active file.",
"enablement": "false"
},
{
"command": "andreas.copyFilename",
Expand Down Expand Up @@ -127,6 +128,17 @@
"category": "Andreas (File)",
"title": "Move active file to new directory."
},
{
"command": "andreas.formatWorkspaceFiles",
"category": "Andreas (File)",
"title": "Format workspace files"
},
{
"command": "andreas.formatSelectedFiles",
"category": "Andreas (File)",
"title": "Format",
"enablement": "false"
},
{
"command": "andreas.generateRange",
"category": "Andreas (Edit)",
Expand All @@ -145,89 +157,112 @@
{
"command": "andreas.openEditorAtIndex",
"category": "Andreas (Nav)",
"title": "Open editor/tab at given index."
"title": "Open editor/tab at given index.",
"enablement": "false"
},
{
"command": "andreas.focusTab",
"category": "Andreas (Nav)",
"title": "Focus tab by hint."
"title": "Focus tab by hint.",
"enablement": "false"
},
{
"command": "andreas.selectTo",
"category": "Andreas (Nav)",
"title": "Select from current location to specified line."
"title": "Select from current location to specified line.",
"enablement": "false"
},
{
"command": "andreas.lineMiddle",
"category": "Andreas (Nav)",
"title": "Move curser to middle of the current line."
"title": "Move cursor to middle of the current line."
},
{
"command": "andreas.getDocumentText",
"category": "Andreas (Text)",
"title": "Get document text."
"title": "Get document text.",
"enablement": "false"
},
{
"command": "andreas.getSelectedText",
"category": "Andreas (Text)",
"title": "Get selected text."
"title": "Get selected text.",
"enablement": "false"
},
{
"command": "andreas.getDictationContext",
"category": "Andreas (Text)",
"title": "Get text before and after selection."
"title": "Get text before and after selection.",
"enablement": "false"
},
{
"command": "andreas.getClassName",
"category": "Andreas (Text)",
"title": "Get class name."
"title": "Get class name.",
"enablement": "false"
},
{
"command": "andreas.getGitFileURL",
"category": "Andreas (Git)",
"title": "Get URL to Git repository file webpage."
"title": "Get URL to Git repository file webpage.",
"enablement": "false"
},
{
"command": "andreas.getGitRepoURL",
"category": "Andreas (Git)",
"title": "Get URL to Git repository webpage."
"title": "Get URL to Git repository webpage.",
"enablement": "false"
},
{
"command": "andreas.getGitIssuesURL",
"category": "Andreas (Git)",
"title": "Get URL to Git repository issues webpage."
"title": "Get URL to Git repository issues webpage.",
"enablement": "false"
},
{
"command": "andreas.getGitNewIssueURL",
"category": "Andreas (Git)",
"title": "Get URL to Git repository new issue webpage."
"title": "Get URL to Git repository new issue webpage.",
"enablement": "false"
},
{
"command": "andreas.getGitPullRequestsURL",
"category": "Andreas (Git)",
"title": "Get URL to Git repository pull requests webpage."
"title": "Get URL to Git repository pull requests webpage.",
"enablement": "false"
},
{
"command": "andreas.getSetting",
"category": "Andreas (Other)",
"title": "Get setting from vscode"
"title": "Get setting from vscode",
"enablement": "false"
},
{
"command": "andreas.setSetting",
"category": "Andreas (Other)",
"title": "Set setting for vscode"
"title": "Set setting for vscode",
"enablement": "false"
},
{
"command": "andreas.executeCommands",
"category": "Andreas (Other)",
"title": "Sequentially execute multiple commands."
"title": "Sequentially execute multiple commands.",
"enablement": "false"
},
{
"command": "andreas.printCommands",
"category": "Andreas (Other)",
"title": "Print available commands."
}
]
],
"menus": {
"explorer/context": [
{
"command": "andreas.formatSelectedFiles",
"title": "Format"
}
]
}
},
"scripts": {
"build": "vsce package",
Expand Down
4 changes: 3 additions & 1 deletion src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export const commandDescriptions = {
renameFile: visible("File", "Rename active file.", undefined, "(name?: string)"),
removeFile: visible("File", "Remove/delete the active file."),
moveFile: visible("File", "Move active file to new directory."),
formatWorkspaceFiles: visible("File", "Format workspace files"),
formatSelectedFiles: hidden("File", "Format", " selected files"),

// Edit commands
generateRange: visible(
Expand All @@ -76,7 +78,7 @@ export const commandDescriptions = {
undefined,
"(line: number)"
),
lineMiddle: visible("Nav", "Move curser to middle of the current line."),
lineMiddle: visible("Nav", "Move cursor to middle of the current line."),

// Text commands
getDocumentText: hidden("Text", "Get document text.", undefined, "(): string | null"),
Expand Down
85 changes: 85 additions & 0 deletions src/commands/formatFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as vscode from "vscode";
import fs from "node:fs";

export async function formatWorkspaceFiles() {
const uris = await vscode.workspace.findFiles("**/*");
await formatDocuments(uris);
}

export async function formatSelectedFiles(clickedFile: vscode.Uri, selectedFiles: vscode.Uri[]) {
const uris = await recursivelyGetFileUris(selectedFiles);
await formatDocuments(uris);
}

async function recursivelyGetFileUris(uris: vscode.Uri[]) {
const result: vscode.Uri[] = [];

for (const uri of uris) {
if (!fs.existsSync(uri.fsPath)) {
continue;
}
if (fs.lstatSync(uri.fsPath).isDirectory()) {
const children = await vscode.workspace.findFiles(
new vscode.RelativePattern(uri, "**/*")
);
result.push(...children);
} else {
result.push(uri);
}
}

return result;
}

async function formatDocuments(uris: vscode.Uri[]) {
const increment = 100 / uris.length;

await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Formatting files",
cancellable: true
},
async (progress, cancellationToken) => {
for (let i = 0; i < uris.length; ++i) {
const uri = uris[i];

if (cancellationToken.isCancellationRequested) {
break;
}

progress.report({ message: `${i + 1} / ${uris.length}` });

await formatDocument(uri);

progress.report({ increment });
}
}
);
}

async function formatDocument(uri: vscode.Uri) {
try {
const editor = await vscode.window.showTextDocument(uri, {
preserveFocus: false,
preview: true
});

await vscode.commands.executeCommand("editor.action.formatDocument", uri);

if (editor.document.isDirty) {
await vscode.commands.executeCommand("workbench.action.files.save", uri);
}

await vscode.commands.executeCommand("workbench.action.closeActiveEditor", uri);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);

// This message will be thrown for binary files
if (!message.endsWith("Detail: File seems to be binary and cannot be opened as text")) {
void vscode.window.showWarningMessage(
`Could not format file: ${uri.fsPath}. ${message}`
);
}
}
}
3 changes: 3 additions & 0 deletions src/commands/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { newFile } from "./files/newFile";
import { removeFile } from "./files/removeFile";
import { renameFile } from "./files/renameFile";
import { focusTab } from "./focusTab";
import { formatSelectedFiles, formatWorkspaceFiles } from "./formatFiles";
import { generateRange } from "./generateRange";
import { decrement, increment } from "./incrementDecrement";
import { lineMiddle } from "./lineMiddle";
Expand Down Expand Up @@ -42,6 +43,8 @@ export function registerCommands(
renameFile,
removeFile,
moveFile,
formatWorkspaceFiles,
formatSelectedFiles,
// Edits
generateRange,
increment,
Expand Down
1 change: 1 addition & 0 deletions src/language/TalonFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class TalonFormatter implements LanguageFormatterTree {
case "noise(":
case "identifier":
case "variable":
case "binary_operator":
case "string":
case "integer":
case "float":
Expand Down
6 changes: 3 additions & 3 deletions src/meta-updater/updatePackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export function updatePackageJson(content: PackageJson): PackageJson {
}

function getCommands(): Command[] {
return Object.entries(commandDescriptions).map(([command, { category, title }]) => ({
return Object.entries(commandDescriptions).map(([command, { category, title, isVisible }]) => ({
command: getFullCommand(command as CommandId),
category: `Andreas (${category})`,
title
// ...(isVisible ? {} : { enablement: "false" })
title,
...(isVisible ? {} : { enablement: "false" })
}));
}

0 comments on commit 7954c1e

Please sign in to comment.