Skip to content

Commit 4590458

Browse files
committed
Added some Commands.Arguments
1 parent 611bf6b commit 4590458

File tree

2 files changed

+35
-43
lines changed

2 files changed

+35
-43
lines changed

Diff for: handler/handler.go

+30-41
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
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-
10901079
func (handler *InoHandler) showMessage(ctx context.Context, msgType lsp.MessageType, message string) {
10911080
params := lsp.ShowMessageParams{
10921081
Type: msgType,

Diff for: lsp/structures.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package lsp
22

3-
import "fmt"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
47

58
type Position struct {
69
/**
@@ -90,7 +93,7 @@ type Command struct {
9093
* Arguments that the command handler should be
9194
* invoked with.
9295
*/
93-
Arguments []interface{} `json:"arguments"`
96+
Arguments []json.RawMessage `json:"arguments"`
9497
}
9598

9699
type TextEdit struct {

0 commit comments

Comments
 (0)