Skip to content

Commit 965bd16

Browse files
author
sutong.527608
committed
Add Sort Imports command to VS Code extension
Integrate organize imports feature: - Add executeCommand method to LSP client - Register typescript.native-preview.sortImports command - Add Sort Imports to command palette and quick pick menu - Support for TypeScript and JavaScript files
1 parent 01f192a commit 965bd16

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

_extension/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
"title": "Show LSP Trace",
8686
"enablement": "typescript.native-preview.serverRunning",
8787
"category": "TypeScript Native Preview"
88+
},
89+
{
90+
"command": "typescript.native-preview.sortImports",
91+
"title": "Sort Imports",
92+
"enablement": "typescript.native-preview.serverRunning && editorLangId =~ /^(javascript|typescript|javascriptreact|typescriptreact)$/",
93+
"category": "TypeScript Native Preview"
8894
}
8995
]
9096
},

_extension/src/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,14 @@ export class Client {
158158
this.client.restart();
159159
return new vscode.Disposable(() => {});
160160
}
161+
162+
async executeCommand(command: string, ...args: any[]): Promise<any> {
163+
if (!this.client) {
164+
throw new Error("Language client is not initialized");
165+
}
166+
return this.client.sendRequest("workspace/executeCommand", {
167+
command,
168+
arguments: args,
169+
});
170+
}
161171
}

_extension/src/commands.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export function registerLanguageCommands(context: vscode.ExtensionContext, clien
3333

3434
disposables.push(vscode.commands.registerCommand("typescript.native-preview.showMenu", showCommands));
3535

36+
disposables.push(vscode.commands.registerCommand("typescript.native-preview.sortImports", async () => {
37+
return sortImports(client);
38+
}));
39+
3640
return disposables;
3741
}
3842

@@ -53,11 +57,41 @@ async function updateUseTsgoSetting(enable: boolean): Promise<void> {
5357
await vscode.commands.executeCommand("workbench.action.restartExtensionHost");
5458
}
5559

56-
/**
57-
* Shows the quick pick menu for TypeScript Native Preview commands
58-
*/
60+
async function sortImports(client: Client): Promise<void> {
61+
const editor = vscode.window.activeTextEditor;
62+
if (!editor) {
63+
vscode.window.showErrorMessage("No active editor");
64+
return;
65+
}
66+
67+
const document = editor.document;
68+
const languageId = document.languageId;
69+
70+
// Check if the file is TypeScript or JavaScript
71+
if (!["typescript", "javascript", "typescriptreact", "javascriptreact"].includes(languageId)) {
72+
vscode.window.showErrorMessage("Sort Imports is only available for TypeScript and JavaScript files");
73+
return;
74+
}
75+
76+
try {
77+
// Execute the sort imports command on the server via LSP
78+
await client.executeCommand(
79+
"typescript-go.organizeImports",
80+
document.uri.toString()
81+
);
82+
vscode.window.showInformationMessage("Imports sorted successfully");
83+
} catch (error) {
84+
vscode.window.showErrorMessage(`Failed to sort imports: ${error}`);
85+
}
86+
}
87+
5988
async function showCommands(): Promise<void> {
6089
const commands: readonly { label: string; description: string; command: string; }[] = [
90+
{
91+
label: "$(symbol-namespace) Sort Imports",
92+
description: "Sort imports in the current file",
93+
command: "typescript.native-preview.sortImports",
94+
},
6195
{
6296
label: "$(refresh) Restart Server",
6397
description: "Restart the TypeScript Native Preview language server",

0 commit comments

Comments
 (0)