Skip to content

Commit b515c35

Browse files
committed
Dramatically simplified diagnostics conversion
1 parent 1cc4c0c commit b515c35

File tree

2 files changed

+40
-66
lines changed

2 files changed

+40
-66
lines changed

Diff for: ls/ls.go

+28-8
Original file line numberDiff line numberDiff line change
@@ -926,31 +926,51 @@ func (ls *INOLanguageServer) TextDocumentDidCloseNotifFromIDE(logger jsonrpc.Fun
926926
}
927927
}
928928

929-
func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.FunctionLogger, cppParams *lsp.PublishDiagnosticsParams) {
929+
func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.FunctionLogger, clangParams *lsp.PublishDiagnosticsParams) {
930930
ls.readLock(logger, false)
931931
defer ls.readUnlock(logger)
932932

933-
logger.Logf("%s (%d diagnostics):", cppParams.URI, len(cppParams.Diagnostics))
934-
for _, diag := range cppParams.Diagnostics {
933+
logger.Logf("%s (%d diagnostics):", clangParams.URI, len(clangParams.Diagnostics))
934+
for _, diag := range clangParams.Diagnostics {
935935
logger.Logf(" > %s - %s: %s", diag.Range.Start, diag.Severity, string(diag.Code))
936936
}
937937

938938
// the diagnostics on sketch.cpp.ino once mapped into their
939939
// .ino counter parts may span over multiple .ino files...
940-
allInoParams, err := ls.clang2IdeDiagnostics(logger, cppParams)
940+
allIdeParams, err := ls.clang2IdeDiagnostics(logger, clangParams)
941941
if err != nil {
942942
logger.Logf("Error converting diagnostics to .ino: %s", err)
943943
return
944944
}
945945

946+
// If the incoming diagnostics are from sketch.cpp.ino then...
947+
if ls.clangURIRefersToIno(clangParams.URI) {
948+
// ...add all the new diagnostics...
949+
for ideInoURI := range allIdeParams {
950+
ls.ideInoDocsWithDiagnostics[ideInoURI] = true
951+
}
952+
953+
// .. and cleanup all previouse diagnostics that are no longer valid...
954+
for ideInoURI := range ls.ideInoDocsWithDiagnostics {
955+
if _, ok := allIdeParams[ideInoURI]; ok {
956+
continue
957+
}
958+
allIdeParams[ideInoURI] = &lsp.PublishDiagnosticsParams{
959+
URI: ideInoURI,
960+
Diagnostics: []lsp.Diagnostic{},
961+
}
962+
delete(ls.ideInoDocsWithDiagnostics, ideInoURI)
963+
}
964+
}
965+
946966
// Push back to IDE the converted diagnostics
947967
logger.Logf("diagnostics to IDE:")
948-
for _, inoParams := range allInoParams {
949-
logger.Logf(" - %s (%d diagnostics):", inoParams.URI, len(inoParams.Diagnostics))
950-
for _, diag := range inoParams.Diagnostics {
968+
for _, ideParams := range allIdeParams {
969+
logger.Logf(" - %s (%d diagnostics):", ideParams.URI, len(ideParams.Diagnostics))
970+
for _, diag := range ideParams.Diagnostics {
951971
logger.Logf(" > %s - %s: %s", diag.Range.Start, diag.Severity, diag.Code)
952972
}
953-
if err := ls.IDE.conn.TextDocumentPublishDiagnostics(inoParams); err != nil {
973+
if err := ls.IDE.conn.TextDocumentPublishDiagnostics(ideParams); err != nil {
954974
logger.Logf("Error sending diagnostics to IDE: %s", err)
955975
return
956976
}

Diff for: ls/ls_clang_to_ide.go

+12-58
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package ls
22

33
import (
4-
"fmt"
5-
64
"github.com/arduino/arduino-language-server/sourcemapper"
75
"go.bug.st/lsp"
86
"go.bug.st/lsp/jsonrpc"
@@ -69,79 +67,35 @@ func (ls *INOLanguageServer) clang2IdeDocumentHighlight(logger jsonrpc.FunctionL
6967
}, nil
7068
}
7169

72-
func (ls *INOLanguageServer) clang2IdeDiagnostics(logger jsonrpc.FunctionLogger, clangDiagsParams *lsp.PublishDiagnosticsParams) ([]*lsp.PublishDiagnosticsParams, error) {
73-
clangURI := clangDiagsParams.URI
74-
if !ls.clangURIRefersToIno(clangURI) {
75-
ideDiags := []lsp.Diagnostic{}
76-
ideDiagsURI := lsp.DocumentURI{}
77-
for _, clangDiag := range clangDiagsParams.Diagnostics {
78-
ideURI, ideRange, err := ls.clang2IdeRangeAndDocumentURI(logger, clangURI, clangDiag.Range)
79-
if err != nil {
80-
return nil, err
81-
}
82-
if ideURI.String() == sourcemapper.NotInoURI.String() {
83-
continue
84-
}
85-
if ideDiagsURI.String() == "" {
86-
ideDiagsURI = ideURI
87-
} else if ideDiagsURI.String() != ideURI.String() {
88-
return nil, fmt.Errorf("unexpected URI %s: it should be %s", ideURI, ideURI)
89-
}
90-
ideDiag := clangDiag
91-
ideDiag.Range = ideRange
92-
ideDiags = append(ideDiags, ideDiag)
93-
}
94-
return []*lsp.PublishDiagnosticsParams{
95-
{
96-
URI: ideDiagsURI,
97-
Diagnostics: ideDiags,
98-
},
99-
}, nil
100-
}
70+
func (ls *INOLanguageServer) clang2IdeDiagnostics(logger jsonrpc.FunctionLogger, clangDiagsParams *lsp.PublishDiagnosticsParams) (map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams, error) {
71+
// If diagnostics comes from sketch.ino.cpp they may refer to multiple .ino files,
72+
// so we collect all of the into a map.
73+
allIdeDiagsParams := map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams{}
10174

102-
// Diagnostics coming from sketch.ino.cpp refers to all .ino files, so it must update
103-
// the diagnostics list of all .ino files altogether.
104-
// XXX: maybe this logic can be moved outside of this conversion function, make it much
105-
// more straighforward.
106-
allIdeInoDiagsParams := map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams{}
107-
for ideInoURI := range ls.ideInoDocsWithDiagnostics {
108-
allIdeInoDiagsParams[ideInoURI] = &lsp.PublishDiagnosticsParams{
109-
URI: ideInoURI,
110-
Diagnostics: []lsp.Diagnostic{},
111-
}
112-
}
113-
ls.ideInoDocsWithDiagnostics = map[lsp.DocumentURI]bool{}
114-
115-
for _, clangDiag := range clangDiagsParams.Diagnostics {
116-
ideURI, ideRange, err := ls.clang2IdeRangeAndDocumentURI(logger, clangURI, clangDiag.Range)
75+
for _, clangDiagnostic := range clangDiagsParams.Diagnostics {
76+
ideURI, ideRange, err := ls.clang2IdeRangeAndDocumentURI(logger, clangDiagsParams.URI, clangDiagnostic.Range)
11777
if err != nil {
11878
return nil, err
11979
}
12080
if ideURI.String() == sourcemapper.NotInoURI.String() {
12181
continue
12282
}
12383

124-
ideInoDiagsParams, ok := allIdeInoDiagsParams[ideURI]
84+
ideDiagsParams, ok := allIdeDiagsParams[ideURI]
12585
if !ok {
126-
ideInoDiagsParams = &lsp.PublishDiagnosticsParams{
86+
ideDiagsParams = &lsp.PublishDiagnosticsParams{
12787
URI: ideURI,
12888
Diagnostics: []lsp.Diagnostic{},
12989
}
130-
allIdeInoDiagsParams[ideURI] = ideInoDiagsParams
90+
allIdeDiagsParams[ideURI] = ideDiagsParams
13191
}
13292

133-
ideInoDiag := clangDiag
93+
ideInoDiag := clangDiagnostic
13494
ideInoDiag.Range = ideRange
135-
ideInoDiagsParams.Diagnostics = append(ideInoDiagsParams.Diagnostics, ideInoDiag)
136-
137-
ls.ideInoDocsWithDiagnostics[ideURI] = true
95+
ideDiagsParams.Diagnostics = append(ideDiagsParams.Diagnostics, ideInoDiag)
13896
}
13997

140-
ideInoDiagParams := []*lsp.PublishDiagnosticsParams{}
141-
for _, v := range allIdeInoDiagsParams {
142-
ideInoDiagParams = append(ideInoDiagParams, v)
143-
}
144-
return ideInoDiagParams, nil
98+
return allIdeDiagsParams, nil
14599
}
146100

147101
func (ls *INOLanguageServer) clang2IdeSymbolInformation(clangSymbolsInformation []lsp.SymbolInformation) []lsp.SymbolInformation {

0 commit comments

Comments
 (0)