Skip to content

Commit 1cc4c0c

Browse files
committed
Some more moving/renaming of functions
1 parent e2637a2 commit 1cc4c0c

File tree

2 files changed

+117
-114
lines changed

2 files changed

+117
-114
lines changed

Diff for: ls/ls.go

+36-114
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ type INOLanguageServer struct {
3232
IDE *IDELSPServer
3333
Clangd *ClangdLSPClient
3434

35-
progressHandler *ProgressProxyHandler
36-
closing chan bool
37-
clangdStarted *sync.Cond
38-
dataMux sync.RWMutex
39-
compileCommandsDir *paths.Path
40-
buildPath *paths.Path
41-
buildSketchRoot *paths.Path
42-
buildSketchCpp *paths.Path
43-
sketchRoot *paths.Path
44-
sketchName string
45-
sketchMapper *sourcemapper.SketchMapper
46-
sketchTrackedFilesCount int
47-
trackedIDEDocs map[string]lsp.TextDocumentItem
48-
inoDocsWithDiagnostics map[lsp.DocumentURI]bool
49-
sketchRebuilder *SketchRebuilder
35+
progressHandler *ProgressProxyHandler
36+
closing chan bool
37+
clangdStarted *sync.Cond
38+
dataMux sync.RWMutex
39+
compileCommandsDir *paths.Path
40+
buildPath *paths.Path
41+
buildSketchRoot *paths.Path
42+
buildSketchCpp *paths.Path
43+
sketchRoot *paths.Path
44+
sketchName string
45+
sketchMapper *sourcemapper.SketchMapper
46+
sketchTrackedFilesCount int
47+
trackedIDEDocs map[string]lsp.TextDocumentItem
48+
ideInoDocsWithDiagnostics map[lsp.DocumentURI]bool
49+
sketchRebuilder *SketchRebuilder
5050
}
5151

5252
// Config describes the language server configuration.
@@ -115,10 +115,10 @@ func (ls *INOLanguageServer) readUnlock(logger jsonrpc.FunctionLogger) {
115115
func NewINOLanguageServer(stdin io.Reader, stdout io.Writer, config *Config) *INOLanguageServer {
116116
logger := NewLSPFunctionLogger(color.HiWhiteString, "LS: ")
117117
ls := &INOLanguageServer{
118-
trackedIDEDocs: map[string]lsp.TextDocumentItem{},
119-
inoDocsWithDiagnostics: map[lsp.DocumentURI]bool{},
120-
closing: make(chan bool),
121-
config: config,
118+
trackedIDEDocs: map[string]lsp.TextDocumentItem{},
119+
ideInoDocsWithDiagnostics: map[lsp.DocumentURI]bool{},
120+
closing: make(chan bool),
121+
config: config,
122122
}
123123
ls.clangdStarted = sync.NewCond(&ls.dataMux)
124124
ls.sketchRebuilder = NewSketchBuilder(ls)
@@ -641,7 +641,7 @@ func (ls *INOLanguageServer) TextDocumentDocumentSymbolReqFromIDE(ctx context.Co
641641
var inoSymbolInformation []lsp.SymbolInformation
642642
if cppSymbolInformation != nil {
643643
logger.Logf(" <-- documentSymbol(%d symbol information)", len(cppSymbolInformation))
644-
inoSymbolInformation = ls.cpp2inoSymbolInformation(cppSymbolInformation)
644+
inoSymbolInformation = ls.clang2IdeSymbolInformation(cppSymbolInformation)
645645
}
646646
return inoDocSymbols, inoSymbolInformation, nil
647647
}
@@ -762,7 +762,7 @@ func (ls *INOLanguageServer) TextDocumentRangeFormattingReqFromIDE(ctx context.C
762762
// Method: "textDocument/rangeFormatting"
763763
logger.Logf("%s", inoParams.TextDocument)
764764
inoURI := inoParams.TextDocument.URI
765-
cppParams, err := ls.ino2cppDocumentRangeFormattingParams(logger, inoParams)
765+
cppParams, err := ls.ide2ClangDocumentRangeFormattingParams(logger, inoParams)
766766
if err != nil {
767767
logger.Logf("Error: %s", err)
768768
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
@@ -937,7 +937,7 @@ func (ls *INOLanguageServer) PublishDiagnosticsNotifFromClangd(logger jsonrpc.Fu
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.cpp2inoDiagnostics(logger, cppParams)
940+
allInoParams, err := ls.clang2IdeDiagnostics(logger, cppParams)
941941
if err != nil {
942942
logger.Logf("Error converting diagnostics to .ino: %s", err)
943943
return
@@ -1195,16 +1195,16 @@ func (ls *INOLanguageServer) ino2cppVersionedTextDocumentIdentifier(logger jsonr
11951195
return res, err
11961196
}
11971197

1198-
func (ls *INOLanguageServer) ino2cppRange(logger jsonrpc.FunctionLogger, inoURI lsp.DocumentURI, inoRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
1199-
cppURI, err := ls.ide2ClangDocumentURI(logger, inoURI)
1198+
func (ls *INOLanguageServer) ide2ClangRange(logger jsonrpc.FunctionLogger, ideURI lsp.DocumentURI, ideRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
1199+
clangURI, err := ls.ide2ClangDocumentURI(logger, ideURI)
12001200
if err != nil {
1201-
return lsp.NilURI, lsp.Range{}, err
1201+
return lsp.DocumentURI{}, lsp.Range{}, err
12021202
}
1203-
if cppURI.AsPath().EquivalentTo(ls.buildSketchCpp) {
1204-
cppRange := ls.sketchMapper.InoToCppLSPRange(inoURI, inoRange)
1205-
return cppURI, cppRange, nil
1203+
if clangURI.AsPath().EquivalentTo(ls.buildSketchCpp) {
1204+
cppRange := ls.sketchMapper.InoToCppLSPRange(ideURI, ideRange)
1205+
return clangURI, cppRange, nil
12061206
}
1207-
return cppURI, inoRange, nil
1207+
return clangURI, ideRange, nil
12081208
}
12091209

12101210
func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger, cppLocations []lsp.Location) ([]lsp.Location, error) {
@@ -1220,17 +1220,18 @@ func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger,
12201220
return inoLocations, nil
12211221
}
12221222

1223-
func (ls *INOLanguageServer) ino2cppDocumentRangeFormattingParams(logger jsonrpc.FunctionLogger, inoParams *lsp.DocumentRangeFormattingParams) (*lsp.DocumentRangeFormattingParams, error) {
1224-
cppTextDocument, err := ls.ide2ClangTextDocumentIdentifier(logger, inoParams.TextDocument)
1223+
func (ls *INOLanguageServer) ide2ClangDocumentRangeFormattingParams(logger jsonrpc.FunctionLogger, ideParams *lsp.DocumentRangeFormattingParams) (*lsp.DocumentRangeFormattingParams, error) {
1224+
clangTextDocumentIdentifier, err := ls.ide2ClangTextDocumentIdentifier(logger, ideParams.TextDocument)
12251225
if err != nil {
12261226
return nil, err
12271227
}
12281228

1229-
_, cppRange, err := ls.ino2cppRange(logger, inoParams.TextDocument.URI, inoParams.Range)
1229+
_, clangRange, err := ls.ide2ClangRange(logger, ideParams.TextDocument.URI, ideParams.Range)
12301230
return &lsp.DocumentRangeFormattingParams{
1231-
TextDocument: cppTextDocument,
1232-
Range: cppRange,
1233-
Options: inoParams.Options,
1231+
WorkDoneProgressParams: ideParams.WorkDoneProgressParams,
1232+
Options: ideParams.Options,
1233+
TextDocument: clangTextDocumentIdentifier,
1234+
Range: clangRange,
12341235
}, err
12351236
}
12361237

@@ -1406,85 +1407,6 @@ func (ls *INOLanguageServer) cpp2inoDocumentSymbols(logger jsonrpc.FunctionLogge
14061407
return inoSymbols
14071408
}
14081409

1409-
func (ls *INOLanguageServer) cpp2inoSymbolInformation(syms []lsp.SymbolInformation) []lsp.SymbolInformation {
1410-
panic("not implemented")
1411-
}
1412-
1413-
func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, cppDiagsParams *lsp.PublishDiagnosticsParams) ([]*lsp.PublishDiagnosticsParams, error) {
1414-
1415-
cppURI := cppDiagsParams.URI
1416-
isSketch := cppURI.AsPath().EquivalentTo(ls.buildSketchCpp)
1417-
1418-
if !isSketch {
1419-
inoURI, _, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, lsp.NilRange)
1420-
if err != nil {
1421-
return nil, err
1422-
}
1423-
inoDiags := []lsp.Diagnostic{}
1424-
for _, cppDiag := range cppDiagsParams.Diagnostics {
1425-
inoURIofConvertedRange, inoRange, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, cppDiag.Range)
1426-
if err != nil {
1427-
return nil, err
1428-
}
1429-
if inoURIofConvertedRange.String() == sourcemapper.NotInoURI.String() {
1430-
continue
1431-
}
1432-
if inoURIofConvertedRange.String() != inoURI.String() {
1433-
return nil, fmt.Errorf("unexpected inoURI %s: it should be %s", inoURIofConvertedRange, inoURI)
1434-
}
1435-
inoDiag := cppDiag
1436-
inoDiag.Range = inoRange
1437-
inoDiags = append(inoDiags, inoDiag)
1438-
}
1439-
return []*lsp.PublishDiagnosticsParams{
1440-
{
1441-
URI: inoURI,
1442-
Diagnostics: inoDiags,
1443-
},
1444-
}, nil
1445-
}
1446-
1447-
allInoDiagsParams := map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams{}
1448-
for inoURI := range ls.inoDocsWithDiagnostics {
1449-
allInoDiagsParams[inoURI] = &lsp.PublishDiagnosticsParams{
1450-
URI: inoURI,
1451-
Diagnostics: []lsp.Diagnostic{},
1452-
}
1453-
}
1454-
ls.inoDocsWithDiagnostics = map[lsp.DocumentURI]bool{}
1455-
1456-
for _, cppDiag := range cppDiagsParams.Diagnostics {
1457-
inoURI, inoRange, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, cppDiag.Range)
1458-
if err != nil {
1459-
return nil, err
1460-
}
1461-
if inoURI.String() == sourcemapper.NotInoURI.String() {
1462-
continue
1463-
}
1464-
1465-
inoDiagsParams, ok := allInoDiagsParams[inoURI]
1466-
if !ok {
1467-
inoDiagsParams = &lsp.PublishDiagnosticsParams{
1468-
URI: inoURI,
1469-
Diagnostics: []lsp.Diagnostic{},
1470-
}
1471-
allInoDiagsParams[inoURI] = inoDiagsParams
1472-
}
1473-
1474-
inoDiag := cppDiag
1475-
inoDiag.Range = inoRange
1476-
inoDiagsParams.Diagnostics = append(inoDiagsParams.Diagnostics, inoDiag)
1477-
1478-
ls.inoDocsWithDiagnostics[inoURI] = true
1479-
}
1480-
1481-
inoDiagParams := []*lsp.PublishDiagnosticsParams{}
1482-
for _, v := range allInoDiagsParams {
1483-
inoDiagParams = append(inoDiagParams, v)
1484-
}
1485-
return inoDiagParams, nil
1486-
}
1487-
14881410
func unknownURI(uri lsp.DocumentURI) error {
14891411
return errors.New("Document is not available: " + uri.String())
14901412
}

Diff for: ls/ls_clang_to_ide.go

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

33
import (
4+
"fmt"
5+
46
"github.com/arduino/arduino-language-server/sourcemapper"
57
"go.bug.st/lsp"
68
"go.bug.st/lsp/jsonrpc"
@@ -66,3 +68,82 @@ func (ls *INOLanguageServer) clang2IdeDocumentHighlight(logger jsonrpc.FunctionL
6668
Range: ideRange,
6769
}, nil
6870
}
71+
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+
}
101+
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)
117+
if err != nil {
118+
return nil, err
119+
}
120+
if ideURI.String() == sourcemapper.NotInoURI.String() {
121+
continue
122+
}
123+
124+
ideInoDiagsParams, ok := allIdeInoDiagsParams[ideURI]
125+
if !ok {
126+
ideInoDiagsParams = &lsp.PublishDiagnosticsParams{
127+
URI: ideURI,
128+
Diagnostics: []lsp.Diagnostic{},
129+
}
130+
allIdeInoDiagsParams[ideURI] = ideInoDiagsParams
131+
}
132+
133+
ideInoDiag := clangDiag
134+
ideInoDiag.Range = ideRange
135+
ideInoDiagsParams.Diagnostics = append(ideInoDiagsParams.Diagnostics, ideInoDiag)
136+
137+
ls.ideInoDocsWithDiagnostics[ideURI] = true
138+
}
139+
140+
ideInoDiagParams := []*lsp.PublishDiagnosticsParams{}
141+
for _, v := range allIdeInoDiagsParams {
142+
ideInoDiagParams = append(ideInoDiagParams, v)
143+
}
144+
return ideInoDiagParams, nil
145+
}
146+
147+
func (ls *INOLanguageServer) clang2IdeSymbolInformation(clangSymbolsInformation []lsp.SymbolInformation) []lsp.SymbolInformation {
148+
panic("not implemented")
149+
}

0 commit comments

Comments
 (0)