Skip to content

Commit 525f6a8

Browse files
committed
Some refactoring of CodeActions handling, still work in progres...
1 parent 76cda4a commit 525f6a8

File tree

1 file changed

+85
-57
lines changed

1 file changed

+85
-57
lines changed

ls/ls.go

+85-57
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,11 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
429429

430430
var ideCommand *lsp.Command
431431
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
434437
}
435438

436439
ideCompletionList.Items = append(ideCompletionList.Items, lsp.CompletionItem{
@@ -771,60 +774,74 @@ func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Co
771774
return ideDocSymbols, ideSymbolsInformation, nil
772775
}
773776

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) {
775778
ls.readLock(logger, true)
776779
defer ls.readUnlock(logger)
777780

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)
781784

782-
cppParams := *inoParams
783-
cppTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, inoTextDocument)
785+
cppTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, ideTextDocument)
784786
if err != nil {
785787
logger.Logf("Error: %s", err)
786788
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
787789
}
788-
cppParams.TextDocument = cppTextDocument
789790

791+
clangParams := &lsp.CodeActionParams{
792+
TextDocument: cppTextDocument,
793+
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
794+
PartialResultParams: ideParams.PartialResultParams,
795+
Range: ideParams.Range,
796+
Context: ideParams.Context,
797+
}
790798
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)
794802
}
795803
}
796-
logger.Logf(" --> codeAction(%s:%s)", cppParams.TextDocument, inoParams.Range.Start)
804+
logger.Logf(" --> codeAction(%s:%s)", clangParams.TextDocument, ideParams.Range.Start)
797805

798-
cppResp, cppErr, err := ls.Clangd.conn.TextDocumentCodeAction(ctx, &cppParams)
806+
clangCommandsOrCodeActions, clangErr, err := ls.Clangd.conn.TextDocumentCodeAction(ctx, clangParams)
799807
if err != nil {
800808
logger.Logf("clangd communication error: %v", err)
801809
ls.Close()
802810
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
803811
}
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()}
807815
}
808816

809817
// 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
822831
}
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)
824840
}
825-
logger.Logf("<-- codeAction(%d elements)", len(inoResp))
841+
ideCommandsOrCodeActions = append(ideCommandsOrCodeActions, ideItem)
826842
}
827-
return inoResp, nil
843+
logger.Logf("<-- codeAction(%d elements)", len(ideCommandsOrCodeActions))
844+
return ideCommandsOrCodeActions, nil
828845
}
829846

830847
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
13461363
return res, err
13471364
}
13481365

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
13591381
}
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)
13631385
}
13641386
}
1365-
return inoCodeAction
1387+
return ideCodeAction
13661388
}
13671389

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 {
13761400
v := struct {
13771401
TweakID string `json:"tweakID"`
13781402
File lsp.DocumentURI `json:"file"`
13791403
Selection lsp.Range `json:"selection"`
13801404
}{}
1381-
if err := json.Unmarshal(command.Arguments[0], &v); err == nil {
1405+
1406+
if err := json.Unmarshal(clangCommand.Arguments[0], &v); err == nil {
13821407
if v.TweakID == "ExtractVariable" {
13831408
logger.Logf(" > converted clangd ExtractVariable")
13841409
if v.File.AsPath().EquivalentTo(ls.buildSketchCpp) {
@@ -1393,10 +1418,13 @@ func (ls *INOLanguageServer) cpp2inoCommand(logger jsonrpc.FunctionLogger, comma
13931418
if err != nil {
13941419
panic("Internal Error: json conversion of codeAcion command arguments")
13951420
}
1396-
inoCommand.Arguments[i] = converted
1421+
ideCommand.Arguments[i] = converted
13971422
}
1423+
return ideCommand
1424+
default:
1425+
logger.Logf("ERROR: could not convert Command '%s'", clangCommand.Command)
1426+
return nil
13981427
}
1399-
return inoCommand
14001428
}
14011429

14021430
func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger, cppWorkspaceEdit *lsp.WorkspaceEdit) *lsp.WorkspaceEdit {

0 commit comments

Comments
 (0)