@@ -672,7 +672,7 @@ func (handler *InoHandler) ino2cppExecuteCommand(executeCommand *lsp.ExecuteComm
672
672
673
673
func (handler * InoHandler ) ino2cppWorkspaceEdit (origEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
674
674
panic ("not implemented" )
675
- newEdit := lsp.WorkspaceEdit {Changes : make (map [string ][]lsp.TextEdit )}
675
+ newEdit := lsp.WorkspaceEdit {Changes : make (map [lsp. DocumentURI ][]lsp.TextEdit )}
676
676
// for uri, edit := range origEdit.Changes {
677
677
// if data, ok := handler.data[lsp.DocumentURI(uri)]; ok {
678
678
// newValue := make([]lsp.TextEdit, len(edit))
@@ -737,22 +737,22 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
737
737
// Treat the input as null
738
738
}
739
739
740
- case * []* lsp.CommandOrCodeAction :
740
+ case * []lsp.CommandOrCodeAction :
741
741
// method "textDocument/codeAction"
742
- // TODO: implement response
743
- r = & [] * lsp. CommandOrCodeAction {}
744
- log . Printf ( "<-- codeAction(empty)" )
745
- break
746
- for index := range * r {
747
- command := ( * r )[ index ]. Command
748
- if command != nil {
749
- handler . cpp2inoCommand ( command )
742
+ log . Printf ( " <-- codeAction(%d elements)" , len ( * r ))
743
+ for i , item := range * r {
744
+ ( * r )[ i ] = lsp. CommandOrCodeAction {
745
+ Command : handler . cpp2inoCommand ( item . Command ),
746
+ CodeAction : handler . cpp2inoCodeAction ( item . CodeAction , uri ),
747
+ }
748
+ if item . Command != nil {
749
+ log . Printf ( " > Command: %s" , item . Command . Title )
750
750
}
751
- codeAction := (* r )[index ].CodeAction
752
- if codeAction != nil {
753
- handler .cpp2inoCodeAction (codeAction , uri )
751
+ if item .CodeAction != nil {
752
+ log .Printf (" > CodeAction: %s" , item .CodeAction .Title )
754
753
}
755
754
}
755
+ log .Printf ("<-- codeAction(%d elements)" , len (* r ))
756
756
757
757
// case "textDocument/definition":
758
758
// fallthrough
@@ -786,45 +786,73 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
786
786
return result
787
787
}
788
788
789
- func (handler * InoHandler ) cpp2inoCodeAction (codeAction * lsp.CodeAction , uri lsp.DocumentURI ) {
790
- panic ("not implemented" )
791
- // codeAction.Edit = handler.cpp2inoWorkspaceEdit(codeAction.Edit)
792
- // if data, ok := handler.data[uri]; ok {
793
- // for index := range codeAction.Diagnostics {
794
- // _, codeAction.Diagnostics[index].Range = data.sourceMap.CppToInoRange(codeAction.Diagnostics[index].Range)
795
- // }
796
- // }
789
+ func (handler * InoHandler ) cpp2inoCodeAction (codeAction * lsp.CodeAction , uri lsp.DocumentURI ) * lsp.CodeAction {
790
+ if codeAction == nil {
791
+ return nil
792
+ }
793
+ inoCodeAction := & lsp.CodeAction {
794
+ Title : codeAction .Title ,
795
+ Kind : codeAction .Kind ,
796
+ Edit : handler .cpp2inoWorkspaceEdit (codeAction .Edit ),
797
+ Diagnostics : codeAction .Diagnostics ,
798
+ Command : handler .cpp2inoCommand (codeAction .Command ),
799
+ }
800
+ if uri .AsPath ().Ext () == ".ino" {
801
+ for i , diag := range inoCodeAction .Diagnostics {
802
+ _ , inoCodeAction .Diagnostics [i ].Range = handler .sketchMapper .CppToInoRange (diag .Range )
803
+ }
804
+ }
805
+ return inoCodeAction
797
806
}
798
807
799
- func (handler * InoHandler ) cpp2inoCommand (command * lsp.Command ) {
800
- panic ("not implemented" )
801
- // if len(command.Arguments) == 1 {
802
- // arg := handler.parseCommandArgument(command.Arguments[0])
803
- // if workspaceEdit, ok := arg.(*lsp.WorkspaceEdit); ok {
804
- // command.Arguments[0] = handler.cpp2inoWorkspaceEdit(workspaceEdit)
805
- // }
806
- // }
808
+ func (handler * InoHandler ) cpp2inoCommand (command * lsp.Command ) * lsp.Command {
809
+ if command == nil {
810
+ return nil
811
+ }
812
+ inoCommand := & lsp.Command {
813
+ Title : command .Title ,
814
+ Command : command .Command ,
815
+ Arguments : command .Arguments ,
816
+ }
817
+ if len (command .Arguments ) == 1 {
818
+ arg := handler .parseCommandArgument (inoCommand .Arguments [0 ])
819
+ if workspaceEdit , ok := arg .(* lsp.WorkspaceEdit ); ok {
820
+ inoCommand .Arguments [0 ] = handler .cpp2inoWorkspaceEdit (workspaceEdit )
821
+ }
822
+ }
823
+ return inoCommand
807
824
}
808
825
809
- func (handler * InoHandler ) cpp2inoWorkspaceEdit (origEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
810
- panic ("not implemented" )
811
- // newEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}
812
- // for uri, edit := range origEdit.Changes {
813
- // if data, ok := handler.data[lsp.DocumentURI(uri)]; ok {
814
- // newValue := make([]lsp.TextEdit, len(edit))
815
- // for index := range edit {
816
- // _, newRange := data.sourceMap.CppToInoRange(edit[index].Range)
817
- // newValue[index] = lsp.TextEdit{
818
- // NewText: edit[index].NewText,
819
- // Range: newRange,
820
- // }
821
- // }
822
- // newEdit.Changes[string(data.sourceURI)] = newValue
823
- // } else {
824
- // newEdit.Changes[uri] = edit
825
- // }
826
- // }
827
- // return &newEdit
826
+ func (handler * InoHandler ) cpp2inoWorkspaceEdit (origWorkspaceEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
827
+ if origWorkspaceEdit == nil {
828
+ return nil
829
+ }
830
+ resWorkspaceEdit := & lsp.WorkspaceEdit {
831
+ Changes : map [lsp.DocumentURI ][]lsp.TextEdit {},
832
+ }
833
+ for editURI , edits := range origWorkspaceEdit .Changes {
834
+ // if the edits are not relative to sketch file...
835
+ if ! editURI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
836
+ // ...pass them through...
837
+ resWorkspaceEdit .Changes [editURI ] = edits
838
+ continue
839
+ }
840
+
841
+ // ...otherwise convert edits to the sketch.ino.cpp into multilpe .ino edits
842
+ for _ , edit := range edits {
843
+ cppRange := edit .Range
844
+ inoFile , inoRange := handler .sketchMapper .CppToInoRange (cppRange )
845
+ inoURI := lsp .NewDocumentURI (inoFile )
846
+ if _ , have := resWorkspaceEdit .Changes [inoURI ]; ! have {
847
+ resWorkspaceEdit .Changes [inoURI ] = []lsp.TextEdit {}
848
+ }
849
+ resWorkspaceEdit .Changes [inoURI ] = append (resWorkspaceEdit .Changes [inoURI ], lsp.TextEdit {
850
+ NewText : edit .NewText ,
851
+ Range : inoRange ,
852
+ })
853
+ }
854
+ }
855
+ return resWorkspaceEdit
828
856
}
829
857
830
858
func (handler * InoHandler ) cpp2inoLocation (location * lsp.Location ) {
@@ -1013,7 +1041,9 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
1013
1041
}
1014
1042
1015
1043
func (handler * InoHandler ) parseCommandArgument (rawArg interface {}) interface {} {
1044
+ log .Printf (" TRY TO PARSE: %+v" , rawArg )
1016
1045
panic ("not implemented" )
1046
+ return nil
1017
1047
// if m1, ok := rawArg.(map[string]interface{}); ok && len(m1) == 1 && m1["changes"] != nil {
1018
1048
// m2 := m1["changes"].(map[string]interface{})
1019
1049
// workspaceEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}
0 commit comments