@@ -33,6 +33,10 @@ export function registerLanguageCommands(context: vscode.ExtensionContext, clien
33
33
34
34
disposables . push ( vscode . commands . registerCommand ( "typescript.native-preview.showMenu" , showCommands ) ) ;
35
35
36
+ disposables . push ( vscode . commands . registerCommand ( "typescript.native-preview.sortImports" , async ( ) => {
37
+ return sortImports ( client ) ;
38
+ } ) ) ;
39
+
36
40
return disposables ;
37
41
}
38
42
@@ -53,11 +57,41 @@ async function updateUseTsgoSetting(enable: boolean): Promise<void> {
53
57
await vscode . commands . executeCommand ( "workbench.action.restartExtensionHost" ) ;
54
58
}
55
59
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
+
59
88
async function showCommands ( ) : Promise < void > {
60
89
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
+ } ,
61
95
{
62
96
label : "$(refresh) Restart Server" ,
63
97
description : "Restart the TypeScript Native Preview language server" ,
0 commit comments