@@ -463,7 +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
+ registerLanguageServiceDocumentRequestHandler (handlers , lsproto .TextDocumentCodeActionInfo , (* Server ).handleCodeAction )
467
467
468
468
return handlers
469
469
})
@@ -641,9 +641,11 @@ func (s *Server) handleInitialize(ctx context.Context, params *lsproto.Initializ
641
641
DocumentHighlightProvider : & lsproto.BooleanOrDocumentHighlightOptions {
642
642
Boolean : ptrTo (true ),
643
643
},
644
- ExecuteCommandProvider : & lsproto.ExecuteCommandOptions {
645
- Commands : []string {
646
- "typescript-go.organizeImports" ,
644
+ CodeActionProvider : & lsproto.BooleanOrCodeActionOptions {
645
+ CodeActionOptions : & lsproto.CodeActionOptions {
646
+ CodeActionKinds : & []lsproto.CodeActionKind {
647
+ lsproto .CodeActionKindSourceOrganizeImports ,
648
+ },
647
649
},
648
650
},
649
651
},
@@ -850,64 +852,51 @@ func (s *Server) handleDocumentHighlight(ctx context.Context, ls *ls.LanguageSer
850
852
return ls .ProvideDocumentHighlights (ctx , params .TextDocument .Uri , params .Position )
851
853
}
852
854
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 {}, errors .New ("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
- err = json .Unmarshal (argBytes , & documentURI )
873
- if err != nil {
874
- return lsproto.LSPAnyOrNull {}, fmt .Errorf ("invalid document URI: %w" , err )
875
- }
876
-
877
- languageService , err := s .session .GetLanguageService (ctx , documentURI )
878
- if err != nil {
879
- return lsproto.LSPAnyOrNull {}, err
880
- }
881
-
882
- edits , err := languageService .OrganizeImports (ctx , documentURI , ls .OrganizeImportsModeAll )
883
- if err != nil {
884
- return lsproto.LSPAnyOrNull {}, err
885
- }
886
-
887
- if len (edits ) == 0 {
888
- return lsproto.LSPAnyOrNull {}, nil
855
+ func (s * Server ) handleCodeAction (ctx context.Context , languageService * ls.LanguageService , params * lsproto.CodeActionParams ) (lsproto.CodeActionResponse , error ) {
856
+ // Check if the client is requesting source.organizeImports
857
+ requestedOrganizeImports := false
858
+ if params .Context != nil && params .Context .Only != nil {
859
+ for _ , kind := range * params .Context .Only {
860
+ if kind == lsproto .CodeActionKindSourceOrganizeImports || kind == lsproto .CodeActionKindSource {
861
+ requestedOrganizeImports = true
862
+ break
863
+ }
864
+ }
865
+ } else {
866
+ // If no specific kinds are requested, include organize imports
867
+ requestedOrganizeImports = true
889
868
}
890
869
891
- editPtrs := make ([]* lsproto.TextEdit , len (edits ))
892
- for i := range edits {
893
- editPtrs [i ] = & edits [i ]
894
- }
870
+ var actions []lsproto.CommandOrCodeAction
871
+ if requestedOrganizeImports {
872
+ edits , err := languageService .OrganizeImports (ctx , params .TextDocument .Uri , ls .OrganizeImportsModeAll )
873
+ if err != nil {
874
+ return lsproto.CommandOrCodeActionArrayOrNull {}, err
875
+ }
895
876
896
- workspaceEdit := & lsproto. WorkspaceEdit {
897
- Changes : & map [lsproto. DocumentUri ][] * lsproto.TextEdit {
898
- documentURI : editPtrs ,
899
- },
900
- }
877
+ if len ( edits ) > 0 {
878
+ editPtrs := make ([] * lsproto.TextEdit , len ( edits ))
879
+ for i := range edits {
880
+ editPtrs [ i ] = & edits [ i ]
881
+ }
901
882
902
- _ , err = s .sendRequest (ctx , lsproto .MethodWorkspaceApplyEdit , & lsproto.ApplyWorkspaceEditParams {
903
- Label : ptrTo ("Organize Imports" ),
904
- Edit : workspaceEdit ,
905
- })
906
- if err != nil {
907
- return lsproto.LSPAnyOrNull {}, fmt .Errorf ("failed to apply workspace edit: %w" , err )
883
+ kind := lsproto .CodeActionKindSourceOrganizeImports
884
+ action := lsproto.CommandOrCodeAction {
885
+ CodeAction : & lsproto.CodeAction {
886
+ Title : "Organize Imports" ,
887
+ Kind : & kind ,
888
+ Edit : & lsproto.WorkspaceEdit {
889
+ Changes : & map [lsproto.DocumentUri ][]* lsproto.TextEdit {
890
+ params .TextDocument .Uri : editPtrs ,
891
+ },
892
+ },
893
+ },
894
+ }
895
+ actions = append (actions , action )
896
+ }
908
897
}
909
898
910
- return lsproto.LSPAnyOrNull { }, nil
899
+ return lsproto.CommandOrCodeActionArrayOrNull { CommandOrCodeActionArray : & actions }, nil
911
900
}
912
901
913
902
func (s * Server ) Log (msg ... any ) {
0 commit comments