@@ -463,6 +463,7 @@ var handlers = sync.OnceValue(func() handlerMap {
463
463
registerLanguageServiceDocumentRequestHandler (handlers , lsproto .TextDocumentDocumentHighlightInfo , (* Server ).handleDocumentHighlight )
464
464
registerRequestHandler (handlers , lsproto .WorkspaceSymbolInfo , (* Server ).handleWorkspaceSymbol )
465
465
registerRequestHandler (handlers , lsproto .CompletionItemResolveInfo , (* Server ).handleCompletionItemResolve )
466
+ registerRequestHandler (handlers , lsproto .WorkspaceExecuteCommandInfo , (* Server ).handleExecuteCommand )
466
467
467
468
return handlers
468
469
})
@@ -640,6 +641,11 @@ func (s *Server) handleInitialize(ctx context.Context, params *lsproto.Initializ
640
641
DocumentHighlightProvider : & lsproto.BooleanOrDocumentHighlightOptions {
641
642
Boolean : ptrTo (true ),
642
643
},
644
+ ExecuteCommandProvider : & lsproto.ExecuteCommandOptions {
645
+ Commands : []string {
646
+ "typescript-go.organizeImports" ,
647
+ },
648
+ },
643
649
},
644
650
}
645
651
@@ -844,6 +850,65 @@ func (s *Server) handleDocumentHighlight(ctx context.Context, ls *ls.LanguageSer
844
850
return ls .ProvideDocumentHighlights (ctx , params .TextDocument .Uri , params .Position )
845
851
}
846
852
853
+ func (s * Server ) handleExecuteCommand (ctx context.Context , params * lsproto.ExecuteCommandParams , _ * lsproto.RequestMessage ) (lsproto.ExecuteCommandResponse , error ) {
854
+ switch params .Command {
855
+ case "typescript-go.organizeImports" :
856
+ return s .handleOrganizeImportsCommand (ctx , params )
857
+ default :
858
+ return lsproto.LSPAnyOrNull {}, fmt .Errorf ("unknown command: %s" , params .Command )
859
+ }
860
+ }
861
+
862
+ func (s * Server ) handleOrganizeImportsCommand (ctx context.Context , params * lsproto.ExecuteCommandParams ) (lsproto.ExecuteCommandResponse , error ) {
863
+ if params .Arguments == nil || len (* params .Arguments ) == 0 {
864
+ return lsproto.LSPAnyOrNull {}, fmt .Errorf ("organizeImports command requires a document URI argument" )
865
+ }
866
+
867
+ var documentURI lsproto.DocumentUri
868
+ argBytes , err := json .Marshal ((* params .Arguments )[0 ])
869
+ if err != nil {
870
+ return lsproto.LSPAnyOrNull {}, fmt .Errorf ("failed to marshal argument: %w" , err )
871
+ }
872
+ if err := json .Unmarshal (argBytes , & documentURI ); err != nil {
873
+ return lsproto.LSPAnyOrNull {}, fmt .Errorf ("invalid document URI: %w" , err )
874
+ }
875
+
876
+ languageService , err := s .session .GetLanguageService (ctx , documentURI )
877
+ if err != nil {
878
+ return lsproto.LSPAnyOrNull {}, err
879
+ }
880
+
881
+ edits , err := languageService .OrganizeImports (ctx , documentURI , ls .OrganizeImportsModeAll )
882
+ if err != nil {
883
+ return lsproto.LSPAnyOrNull {}, err
884
+ }
885
+
886
+ if len (edits ) == 0 {
887
+ return lsproto.LSPAnyOrNull {}, nil
888
+ }
889
+
890
+ editPtrs := make ([]* lsproto.TextEdit , len (edits ))
891
+ for i := range edits {
892
+ editPtrs [i ] = & edits [i ]
893
+ }
894
+
895
+ workspaceEdit := & lsproto.WorkspaceEdit {
896
+ Changes : & map [lsproto.DocumentUri ][]* lsproto.TextEdit {
897
+ documentURI : editPtrs ,
898
+ },
899
+ }
900
+
901
+ _ , err = s .sendRequest (ctx , lsproto .MethodWorkspaceApplyEdit , & lsproto.ApplyWorkspaceEditParams {
902
+ Label : ptrTo ("Organize Imports" ),
903
+ Edit : workspaceEdit ,
904
+ })
905
+ if err != nil {
906
+ return lsproto.LSPAnyOrNull {}, fmt .Errorf ("failed to apply workspace edit: %w" , err )
907
+ }
908
+
909
+ return lsproto.LSPAnyOrNull {}, nil
910
+ }
911
+
847
912
func (s * Server ) Log (msg ... any ) {
848
913
fmt .Fprintln (s .stderr , msg ... )
849
914
}
0 commit comments