Skip to content

Commit 7943ae1

Browse files
committed
partial implementation of documentSymbol
1 parent aac7cdc commit 7943ae1

File tree

4 files changed

+80
-51
lines changed

4 files changed

+80
-51
lines changed

Diff for: handler/handler.go

+78-48
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ func (handler *InoHandler) ino2cppExecuteCommand(executeCommand *lsp.ExecuteComm
672672

673673
func (handler *InoHandler) ino2cppWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *lsp.WorkspaceEdit {
674674
panic("not implemented")
675-
newEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}
675+
newEdit := lsp.WorkspaceEdit{Changes: make(map[lsp.DocumentURI][]lsp.TextEdit)}
676676
// for uri, edit := range origEdit.Changes {
677677
// if data, ok := handler.data[lsp.DocumentURI(uri)]; ok {
678678
// newValue := make([]lsp.TextEdit, len(edit))
@@ -737,22 +737,22 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
737737
// Treat the input as null
738738
}
739739

740-
case *[]*lsp.CommandOrCodeAction:
740+
case *[]lsp.CommandOrCodeAction:
741741
// 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)
750750
}
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)
754753
}
755754
}
755+
log.Printf("<-- codeAction(%d elements)", len(*r))
756756

757757
// case "textDocument/definition":
758758
// fallthrough
@@ -786,45 +786,73 @@ func (handler *InoHandler) transformClangdResult(method string, uri lsp.Document
786786
return result
787787
}
788788

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
797806
}
798807

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
807824
}
808825

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
828856
}
829857

830858
func (handler *InoHandler) cpp2inoLocation(location *lsp.Location) {
@@ -1013,7 +1041,9 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
10131041
}
10141042

10151043
func (handler *InoHandler) parseCommandArgument(rawArg interface{}) interface{} {
1044+
log.Printf(" TRY TO PARSE: %+v", rawArg)
10161045
panic("not implemented")
1046+
return nil
10171047
// if m1, ok := rawArg.(map[string]interface{}); ok && len(m1) == 1 && m1["changes"] != nil {
10181048
// m2 := m1["changes"].(map[string]interface{})
10191049
// workspaceEdit := lsp.WorkspaceEdit{Changes: make(map[string][]lsp.TextEdit)}

Diff for: handler/sourcemapper/ino.go

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
type InoMapper struct {
1717
InoText map[lsp.DocumentURI]*SourceRevision
1818
CppText *SourceRevision
19-
CppFile lsp.DocumentURI
2019
toCpp map[InoLine]int // Converts File.ino:line -> line
2120
toIno map[int]InoLine // Convers line -> File.ino:line
2221
inoPreprocessed map[InoLine]int // map of the lines taken by the preprocessor: File.ino:line -> preprocessed line

Diff for: lsp/protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func SendRequest(ctx context.Context, conn *jsonrpc2.Conn, method string, params
116116
err := conn.Call(ctx, method, params, result)
117117
return result, err
118118
case "textDocument/codeAction":
119-
result := new([]*CommandOrCodeAction)
119+
result := new([]CommandOrCodeAction)
120120
err := conn.Call(ctx, method, params, result)
121121
return result, err
122122
case "completionItem/resolve":

Diff for: lsp/structures.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ type WorkspaceEdit struct {
111111
/**
112112
* Holds changes to existing resources.
113113
*/
114-
Changes map[string][]TextEdit `json:"changes"`
114+
Changes map[DocumentURI][]TextEdit `json:"changes"`
115115
}
116116

117117
type TextDocumentIdentifier struct {

0 commit comments

Comments
 (0)