@@ -429,8 +429,11 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
429
429
430
430
var ideCommand * lsp.Command
431
431
if clangItem .Command != nil {
432
- c := ls .cpp2inoCommand (logger , * clangItem .Command )
433
- ideCommand = & c
432
+ c := ls .clang2IdeCommand (logger , * clangItem .Command )
433
+ if c == nil {
434
+ continue // Skit item with unsupported command convertion
435
+ }
436
+ ideCommand = c
434
437
}
435
438
436
439
ideCompletionList .Items = append (ideCompletionList .Items , lsp.CompletionItem {
@@ -771,60 +774,74 @@ func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Co
771
774
return ideDocSymbols , ideSymbolsInformation , nil
772
775
}
773
776
774
- func (ls * INOLanguageServer ) TextDocumentCodeActionReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , inoParams * lsp.CodeActionParams ) ([]lsp.CommandOrCodeAction , * jsonrpc.ResponseError ) {
777
+ func (ls * INOLanguageServer ) TextDocumentCodeActionReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.CodeActionParams ) ([]lsp.CommandOrCodeAction , * jsonrpc.ResponseError ) {
775
778
ls .readLock (logger , true )
776
779
defer ls .readUnlock (logger )
777
780
778
- inoTextDocument := inoParams .TextDocument
779
- inoURI := inoTextDocument .URI
780
- logger .Logf ("--> codeAction(%s:%s)" , inoTextDocument , inoParams .Range .Start )
781
+ ideTextDocument := ideParams .TextDocument
782
+ ideURI := ideTextDocument .URI
783
+ logger .Logf ("--> codeAction(%s:%s)" , ideTextDocument , ideParams .Range .Start )
781
784
782
- cppParams := * inoParams
783
- cppTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , inoTextDocument )
785
+ cppTextDocument , err := ls .ide2ClangTextDocumentIdentifier (logger , ideTextDocument )
784
786
if err != nil {
785
787
logger .Logf ("Error: %s" , err )
786
788
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
787
789
}
788
- cppParams .TextDocument = cppTextDocument
789
790
791
+ clangParams := & lsp.CodeActionParams {
792
+ TextDocument : cppTextDocument ,
793
+ WorkDoneProgressParams : ideParams .WorkDoneProgressParams ,
794
+ PartialResultParams : ideParams .PartialResultParams ,
795
+ Range : ideParams .Range ,
796
+ Context : ideParams .Context ,
797
+ }
790
798
if cppTextDocument .URI .AsPath ().EquivalentTo (ls .buildSketchCpp ) {
791
- cppParams .Range = ls .sketchMapper .InoToCppLSPRange (inoURI , inoParams .Range )
792
- for i , inoDiag := range inoParams .Context .Diagnostics {
793
- cppParams .Context .Diagnostics [i ].Range = ls .sketchMapper .InoToCppLSPRange (inoURI , inoDiag .Range )
799
+ clangParams .Range = ls .sketchMapper .InoToCppLSPRange (ideURI , ideParams .Range )
800
+ for i , inoDiag := range ideParams .Context .Diagnostics {
801
+ clangParams .Context .Diagnostics [i ].Range = ls .sketchMapper .InoToCppLSPRange (ideURI , inoDiag .Range )
794
802
}
795
803
}
796
- logger .Logf (" --> codeAction(%s:%s)" , cppParams .TextDocument , inoParams .Range .Start )
804
+ logger .Logf (" --> codeAction(%s:%s)" , clangParams .TextDocument , ideParams .Range .Start )
797
805
798
- cppResp , cppErr , err := ls .Clangd .conn .TextDocumentCodeAction (ctx , & cppParams )
806
+ clangCommandsOrCodeActions , clangErr , err := ls .Clangd .conn .TextDocumentCodeAction (ctx , clangParams )
799
807
if err != nil {
800
808
logger .Logf ("clangd communication error: %v" , err )
801
809
ls .Close ()
802
810
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
803
811
}
804
- if cppErr != nil {
805
- logger .Logf ("clangd response error: %v" , cppErr .AsError ())
806
- return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : cppErr .AsError ().Error ()}
812
+ if clangErr != nil {
813
+ logger .Logf ("clangd response error: %v" , clangErr .AsError ())
814
+ return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : clangErr .AsError ().Error ()}
807
815
}
808
816
809
817
// TODO: Create a function for this one?
810
- inoResp := []lsp.CommandOrCodeAction {}
811
- if cppResp != nil {
812
- logger .Logf (" <-- codeAction(%d elements)" , len (cppResp ))
813
- for _ , cppItem := range cppResp {
814
- inoItem := lsp.CommandOrCodeAction {}
815
- switch i := cppItem .Get ().(type ) {
816
- case lsp.Command :
817
- logger .Logf (" > Command: %s" , i .Title )
818
- inoItem .Set (ls .cpp2inoCommand (logger , i ))
819
- case lsp.CodeAction :
820
- logger .Logf (" > CodeAction: %s" , i .Title )
821
- inoItem .Set (ls .cpp2inoCodeAction (logger , i , inoURI ))
818
+ ideCommandsOrCodeActions := []lsp.CommandOrCodeAction {}
819
+ if clangCommandsOrCodeActions != nil {
820
+ return ideCommandsOrCodeActions , nil
821
+ }
822
+ logger .Logf (" <-- codeAction(%d elements)" , len (clangCommandsOrCodeActions ))
823
+ for _ , clangItem := range clangCommandsOrCodeActions {
824
+ ideItem := lsp.CommandOrCodeAction {}
825
+ switch i := clangItem .Get ().(type ) {
826
+ case lsp.Command :
827
+ logger .Logf (" > Command: %s" , i .Title )
828
+ ideCommand := ls .clang2IdeCommand (logger , i )
829
+ if ideCommand == nil {
830
+ continue // Skip unsupported command
822
831
}
823
- inoResp = append (inoResp , inoItem )
832
+ ideItem .Set (* ideCommand )
833
+ case lsp.CodeAction :
834
+ logger .Logf (" > CodeAction: %s" , i .Title )
835
+ ideCodeAction := ls .clang2IdeCodeAction (logger , i , ideURI )
836
+ if ideCodeAction == nil {
837
+ continue // Skip unsupported code action
838
+ }
839
+ ideItem .Set (* ideCodeAction )
824
840
}
825
- logger . Logf ( "<-- codeAction(%d elements)" , len ( inoResp ) )
841
+ ideCommandsOrCodeActions = append ( ideCommandsOrCodeActions , ideItem )
826
842
}
827
- return inoResp , nil
843
+ logger .Logf ("<-- codeAction(%d elements)" , len (ideCommandsOrCodeActions ))
844
+ return ideCommandsOrCodeActions , nil
828
845
}
829
846
830
847
func (ls * INOLanguageServer ) TextDocumentFormattingReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.DocumentFormattingParams ) ([]lsp.TextEdit , * jsonrpc.ResponseError ) {
@@ -1346,39 +1363,47 @@ func (ls *INOLanguageServer) ino2cppVersionedTextDocumentIdentifier(logger jsonr
1346
1363
return res , err
1347
1364
}
1348
1365
1349
- func (ls * INOLanguageServer ) cpp2inoCodeAction (logger jsonrpc.FunctionLogger , codeAction lsp.CodeAction , uri lsp.DocumentURI ) lsp.CodeAction {
1350
- inoCodeAction := lsp.CodeAction {
1351
- Title : codeAction .Title ,
1352
- Kind : codeAction .Kind ,
1353
- Edit : ls .cpp2inoWorkspaceEdit (logger , codeAction .Edit ),
1354
- Diagnostics : codeAction .Diagnostics ,
1355
- }
1356
- if codeAction .Command != nil {
1357
- inoCommand := ls .cpp2inoCommand (logger , * codeAction .Command )
1358
- inoCodeAction .Command = & inoCommand
1366
+ func (ls * INOLanguageServer ) clang2IdeCodeAction (logger jsonrpc.FunctionLogger , clangCodeAction lsp.CodeAction , origIdeURI lsp.DocumentURI ) * lsp.CodeAction {
1367
+ ideCodeAction := & lsp.CodeAction {
1368
+ Title : clangCodeAction .Title ,
1369
+ Kind : clangCodeAction .Kind ,
1370
+ Diagnostics : clangCodeAction .Diagnostics ,
1371
+ IsPreferred : clangCodeAction .IsPreferred ,
1372
+ Disabled : clangCodeAction .Disabled ,
1373
+ Edit : ls .cpp2inoWorkspaceEdit (logger , clangCodeAction .Edit ),
1374
+ }
1375
+ if clangCodeAction .Command != nil {
1376
+ inoCommand := ls .clang2IdeCommand (logger , * clangCodeAction .Command )
1377
+ if inoCommand == nil {
1378
+ return nil
1379
+ }
1380
+ ideCodeAction .Command = inoCommand
1359
1381
}
1360
- if uri .Ext () == ".ino" {
1361
- for i , diag := range inoCodeAction .Diagnostics {
1362
- _ , inoCodeAction .Diagnostics [i ].Range = ls .sketchMapper .CppToInoRange (diag .Range )
1382
+ if origIdeURI .Ext () == ".ino" {
1383
+ for i , diag := range ideCodeAction .Diagnostics {
1384
+ _ , ideCodeAction .Diagnostics [i ].Range = ls .sketchMapper .CppToInoRange (diag .Range )
1363
1385
}
1364
1386
}
1365
- return inoCodeAction
1387
+ return ideCodeAction
1366
1388
}
1367
1389
1368
- func (ls * INOLanguageServer ) cpp2inoCommand (logger jsonrpc.FunctionLogger , command lsp.Command ) lsp.Command {
1369
- inoCommand := lsp.Command {
1370
- Title : command .Title ,
1371
- Command : command .Command ,
1372
- Arguments : command .Arguments ,
1373
- }
1374
- if command .Command == "clangd.applyTweak" {
1375
- for i := range command .Arguments {
1390
+ func (ls * INOLanguageServer ) clang2IdeCommand (logger jsonrpc.FunctionLogger , clangCommand lsp.Command ) * lsp.Command {
1391
+ switch clangCommand .Command {
1392
+ case "clangd.applyTweak" :
1393
+ logger .Logf ("> Command: clangd.applyTweak" )
1394
+ ideCommand := & lsp.Command {
1395
+ Title : clangCommand .Title ,
1396
+ Command : clangCommand .Command ,
1397
+ Arguments : clangCommand .Arguments ,
1398
+ }
1399
+ for i := range clangCommand .Arguments {
1376
1400
v := struct {
1377
1401
TweakID string `json:"tweakID"`
1378
1402
File lsp.DocumentURI `json:"file"`
1379
1403
Selection lsp.Range `json:"selection"`
1380
1404
}{}
1381
- if err := json .Unmarshal (command .Arguments [0 ], & v ); err == nil {
1405
+
1406
+ if err := json .Unmarshal (clangCommand .Arguments [0 ], & v ); err == nil {
1382
1407
if v .TweakID == "ExtractVariable" {
1383
1408
logger .Logf (" > converted clangd ExtractVariable" )
1384
1409
if v .File .AsPath ().EquivalentTo (ls .buildSketchCpp ) {
@@ -1393,10 +1418,13 @@ func (ls *INOLanguageServer) cpp2inoCommand(logger jsonrpc.FunctionLogger, comma
1393
1418
if err != nil {
1394
1419
panic ("Internal Error: json conversion of codeAcion command arguments" )
1395
1420
}
1396
- inoCommand .Arguments [i ] = converted
1421
+ ideCommand .Arguments [i ] = converted
1397
1422
}
1423
+ return ideCommand
1424
+ default :
1425
+ logger .Logf ("ERROR: could not convert Command '%s'" , clangCommand .Command )
1426
+ return nil
1398
1427
}
1399
- return inoCommand
1400
1428
}
1401
1429
1402
1430
func (ls * INOLanguageServer ) cpp2inoWorkspaceEdit (logger jsonrpc.FunctionLogger , cppWorkspaceEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
0 commit comments