@@ -2,6 +2,7 @@ package handler
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"io"
7
8
"log"
@@ -757,16 +758,16 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
757
758
// method "textDocument/codeAction"
758
759
log .Printf (" <-- codeAction(%d elements)" , len (* r ))
759
760
for i , item := range * r {
760
- (* r )[i ] = lsp.CommandOrCodeAction {
761
- Command : handler .cpp2inoCommand (item .Command ),
762
- CodeAction : handler .cpp2inoCodeAction (item .CodeAction , uri ),
763
- }
764
761
if item .Command != nil {
765
762
log .Printf (" > Command: %s" , item .Command .Title )
766
763
}
767
764
if item .CodeAction != nil {
768
765
log .Printf (" > CodeAction: %s" , item .CodeAction .Title )
769
766
}
767
+ (* r )[i ] = lsp.CommandOrCodeAction {
768
+ Command : handler .Cpp2InoCommand (item .Command ),
769
+ CodeAction : handler .cpp2inoCodeAction (item .CodeAction , uri ),
770
+ }
770
771
}
771
772
log .Printf ("<-- codeAction(%d elements)" , len (* r ))
772
773
@@ -811,7 +812,7 @@ func (handler *InoHandler) cpp2inoCodeAction(codeAction *lsp.CodeAction, uri lsp
811
812
Kind : codeAction .Kind ,
812
813
Edit : handler .cpp2inoWorkspaceEdit (codeAction .Edit ),
813
814
Diagnostics : codeAction .Diagnostics ,
814
- Command : handler .cpp2inoCommand (codeAction .Command ),
815
+ Command : handler .Cpp2InoCommand (codeAction .Command ),
815
816
}
816
817
if uri .AsPath ().Ext () == ".ino" {
817
818
for i , diag := range inoCodeAction .Diagnostics {
@@ -821,7 +822,7 @@ func (handler *InoHandler) cpp2inoCodeAction(codeAction *lsp.CodeAction, uri lsp
821
822
return inoCodeAction
822
823
}
823
824
824
- func (handler * InoHandler ) cpp2inoCommand (command * lsp.Command ) * lsp.Command {
825
+ func (handler * InoHandler ) Cpp2InoCommand (command * lsp.Command ) * lsp.Command {
825
826
if command == nil {
826
827
return nil
827
828
}
@@ -830,10 +831,29 @@ func (handler *InoHandler) cpp2inoCommand(command *lsp.Command) *lsp.Command {
830
831
Command : command .Command ,
831
832
Arguments : command .Arguments ,
832
833
}
833
- if len (command .Arguments ) == 1 {
834
- arg := handler .parseCommandArgument (inoCommand .Arguments [0 ])
835
- if workspaceEdit , ok := arg .(* lsp.WorkspaceEdit ); ok {
836
- inoCommand .Arguments [0 ] = handler .cpp2inoWorkspaceEdit (workspaceEdit )
834
+ if command .Command == "clangd.applyTweak" {
835
+ for i := range command .Arguments {
836
+ v := struct {
837
+ TweakID string `json:"tweakID"`
838
+ File lsp.DocumentURI `json:"file"`
839
+ Selection lsp.Range `json:"selection"`
840
+ }{}
841
+ if err := json .Unmarshal (command .Arguments [0 ], & v ); err == nil {
842
+ if v .TweakID == "ExtractVariable" {
843
+ log .Println (" > converted clangd ExtractVariable" )
844
+ if v .File .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
845
+ inoFile , inoSelection := handler .sketchMapper .CppToInoRange (v .Selection )
846
+ v .File = lsp .NewDocumentURI (inoFile )
847
+ v .Selection = inoSelection
848
+ }
849
+ }
850
+ }
851
+
852
+ converted , err := json .Marshal (v )
853
+ if err != nil {
854
+ panic ("Internal Error: json conversion of codeAcion command arguments" )
855
+ }
856
+ inoCommand .Arguments [i ] = converted
837
857
}
838
858
}
839
859
return inoCommand
@@ -1056,37 +1076,6 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
1056
1076
return result , err
1057
1077
}
1058
1078
1059
- func (handler * InoHandler ) parseCommandArgument (rawArg interface {}) interface {} {
1060
- log .Printf (" TRY TO PARSE: %+v" , rawArg )
1061
- panic ("not implemented" )
1062
- return nil
1063
- // if m1, ok := rawArg.(map[string]interface{}); ok && len(m1) == 1 && m1["changes"] != nil {
1064
- // m2 := m1["changes"].(map[string]interface{})
1065
- // workspaceEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}
1066
- // for uri, rawValue := range m2 {
1067
- // rawTextEdits := rawValue.([]interface{})
1068
- // textEdits := make([]lsp.TextEdit, len(rawTextEdits))
1069
- // for index := range rawTextEdits {
1070
- // m3 := rawTextEdits[index].(map[string]interface{})
1071
- // rawRange := m3["range"]
1072
- // m4 := rawRange.(map[string]interface{})
1073
- // rawStart := m4["start"]
1074
- // m5 := rawStart.(map[string]interface{})
1075
- // textEdits[index].Range.Start.Line = int(m5["line"].(float64))
1076
- // textEdits[index].Range.Start.Character = int(m5["character"].(float64))
1077
- // rawEnd := m4["end"]
1078
- // m6 := rawEnd.(map[string]interface{})
1079
- // textEdits[index].Range.End.Line = int(m6["line"].(float64))
1080
- // textEdits[index].Range.End.Character = int(m6["character"].(float64))
1081
- // textEdits[index].NewText = m3["newText"].(string)
1082
- // }
1083
- // workspaceEdit.Changes[uri] = textEdits
1084
- // }
1085
- // return &workspaceEdit
1086
- // }
1087
- // return nil
1088
- }
1089
-
1090
1079
func (handler * InoHandler ) showMessage (ctx context.Context , msgType lsp.MessageType , message string ) {
1091
1080
params := lsp.ShowMessageParams {
1092
1081
Type : msgType ,
0 commit comments