@@ -2,6 +2,7 @@ package handler
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
67 "io"
78 "log"
@@ -757,16 +758,16 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
757758 // method "textDocument/codeAction"
758759 log .Printf (" <-- codeAction(%d elements)" , len (* r ))
759760 for i , item := range * r {
760- (* r )[i ] = lsp.CommandOrCodeAction {
761- Command : handler .cpp2inoCommand (item .Command ),
762- CodeAction : handler .cpp2inoCodeAction (item .CodeAction , uri ),
763- }
764761 if item .Command != nil {
765762 log .Printf (" > Command: %s" , item .Command .Title )
766763 }
767764 if item .CodeAction != nil {
768765 log .Printf (" > CodeAction: %s" , item .CodeAction .Title )
769766 }
767+ (* r )[i ] = lsp.CommandOrCodeAction {
768+ Command : handler .Cpp2InoCommand (item .Command ),
769+ CodeAction : handler .cpp2inoCodeAction (item .CodeAction , uri ),
770+ }
770771 }
771772 log .Printf ("<-- codeAction(%d elements)" , len (* r ))
772773
@@ -811,7 +812,7 @@ func (handler *InoHandler) cpp2inoCodeAction(codeAction *lsp.CodeAction, uri lsp
811812 Kind : codeAction .Kind ,
812813 Edit : handler .cpp2inoWorkspaceEdit (codeAction .Edit ),
813814 Diagnostics : codeAction .Diagnostics ,
814- Command : handler .cpp2inoCommand (codeAction .Command ),
815+ Command : handler .Cpp2InoCommand (codeAction .Command ),
815816 }
816817 if uri .AsPath ().Ext () == ".ino" {
817818 for i , diag := range inoCodeAction .Diagnostics {
@@ -821,7 +822,7 @@ func (handler *InoHandler) cpp2inoCodeAction(codeAction *lsp.CodeAction, uri lsp
821822 return inoCodeAction
822823}
823824
824- func (handler * InoHandler ) cpp2inoCommand (command * lsp.Command ) * lsp.Command {
825+ func (handler * InoHandler ) Cpp2InoCommand (command * lsp.Command ) * lsp.Command {
825826 if command == nil {
826827 return nil
827828 }
@@ -830,10 +831,29 @@ func (handler *InoHandler) cpp2inoCommand(command *lsp.Command) *lsp.Command {
830831 Command : command .Command ,
831832 Arguments : command .Arguments ,
832833 }
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
837857 }
838858 }
839859 return inoCommand
@@ -1056,37 +1076,6 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
10561076 return result , err
10571077}
10581078
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-
10901079func (handler * InoHandler ) showMessage (ctx context.Context , msgType lsp.MessageType , message string ) {
10911080 params := lsp.ShowMessageParams {
10921081 Type : msgType ,
0 commit comments