Skip to content

Commit 00590aa

Browse files
committed
Refactored and rationalized in-preprocessed section updates
1 parent b515c35 commit 00590aa

File tree

2 files changed

+96
-45
lines changed

2 files changed

+96
-45
lines changed

Diff for: ls/ls.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,15 @@ func (ls *INOLanguageServer) TextDocumentHoverReqFromIDE(ctx context.Context, lo
379379
return nil, nil
380380
}
381381

382-
_, r, err := ls.clang2IdeRangeAndDocumentURI(logger, clangParams.TextDocument.URI, *clangResp.Range)
382+
_, r, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, clangParams.TextDocument.URI, *clangResp.Range)
383383
if err != nil {
384384
logger.Logf("error during range conversion: %v", err)
385385
ls.Close()
386386
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: err.Error()}
387387
}
388+
if inPreprocessed {
389+
return nil, nil
390+
}
388391
ideResp := lsp.Hover{
389392
Contents: clangResp.Contents,
390393
Range: &r,
@@ -595,7 +598,10 @@ func (ls *INOLanguageServer) TextDocumentDocumentHighlightReqFromIDE(ctx context
595598

596599
ideHighlights := []lsp.DocumentHighlight{}
597600
for _, clangHighlight := range clangHighlights {
598-
ideHighlight, err := ls.clang2IdeDocumentHighlight(logger, clangHighlight, clangURI)
601+
ideHighlight, inPreprocessed, err := ls.clang2IdeDocumentHighlight(logger, clangHighlight, clangURI)
602+
if inPreprocessed {
603+
continue
604+
}
599605
if err != nil {
600606
logger.Logf("ERROR converting highlight %s:%s: %s", clangURI, clangHighlight.Range, err)
601607
return nil, &jsonrpc.ResponseError{Code: jsonrpc.ErrorCodesInternalError, Message: clangErr.AsError().Error()}
@@ -1230,11 +1236,15 @@ func (ls *INOLanguageServer) ide2ClangRange(logger jsonrpc.FunctionLogger, ideUR
12301236
func (ls *INOLanguageServer) cpp2inoLocationArray(logger jsonrpc.FunctionLogger, cppLocations []lsp.Location) ([]lsp.Location, error) {
12311237
inoLocations := []lsp.Location{}
12321238
for _, cppLocation := range cppLocations {
1233-
inoLocation, err := ls.cpp2inoLocation(logger, cppLocation)
1239+
inoLocation, inPreprocessed, err := ls.cpp2inoLocation(logger, cppLocation)
12341240
if err != nil {
12351241
logger.Logf("ERROR converting location %s: %s", cppLocation, err)
12361242
return nil, err
12371243
}
1244+
if inPreprocessed {
1245+
logger.Logf("ignored in-preprocessed-section location")
1246+
continue
1247+
}
12381248
inoLocations = append(inoLocations, inoLocation)
12391249
}
12401250
return inoLocations, nil
@@ -1325,11 +1335,16 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
13251335

13261336
// ...otherwise convert edits to the sketch.ino.cpp into multilpe .ino edits
13271337
for _, edit := range edits {
1328-
inoURI, inoRange, err := ls.clang2IdeRangeAndDocumentURI(logger, editURI, edit.Range)
1338+
inoURI, inoRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, editURI, edit.Range)
13291339
if err != nil {
13301340
logger.Logf(" error converting edit %s:%s: %s", editURI, edit.Range, err)
13311341
continue
13321342
}
1343+
if inPreprocessed {
1344+
// XXX: ignore
1345+
logger.Logf(" ignored in-preprocessed-section change")
1346+
continue
1347+
}
13331348
//inoFile, inoRange := ls.sketchMapper.CppToInoRange(edit.Range)
13341349
//inoURI := lsp.NewDocumentURI(inoFile)
13351350
if _, have := inoWorkspaceEdit.Changes[inoURI]; !have {
@@ -1345,23 +1360,27 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
13451360
return inoWorkspaceEdit
13461361
}
13471362

1348-
func (ls *INOLanguageServer) cpp2inoLocation(logger jsonrpc.FunctionLogger, cppLocation lsp.Location) (lsp.Location, error) {
1349-
inoURI, inoRange, err := ls.clang2IdeRangeAndDocumentURI(logger, cppLocation.URI, cppLocation.Range)
1363+
func (ls *INOLanguageServer) cpp2inoLocation(logger jsonrpc.FunctionLogger, cppLocation lsp.Location) (lsp.Location, bool, error) {
1364+
inoURI, inoRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, cppLocation.URI, cppLocation.Range)
13501365
return lsp.Location{
13511366
URI: inoURI,
13521367
Range: inoRange,
1353-
}, err
1368+
}, inPreprocessed, err
13541369
}
13551370

13561371
func (ls *INOLanguageServer) cpp2inoTextEdits(logger jsonrpc.FunctionLogger, cppURI lsp.DocumentURI, cppEdits []lsp.TextEdit) (map[lsp.DocumentURI][]lsp.TextEdit, error) {
13571372
logger.Logf("%s cpp/textEdit (%d elements)", cppURI, len(cppEdits))
13581373
allInoEdits := map[lsp.DocumentURI][]lsp.TextEdit{}
13591374
for _, cppEdit := range cppEdits {
13601375
logger.Logf(" > %s -> %s", cppEdit.Range, strconv.Quote(cppEdit.NewText))
1361-
inoURI, inoEdit, err := ls.cpp2inoTextEdit(logger, cppURI, cppEdit)
1376+
inoURI, inoEdit, inPreprocessed, err := ls.cpp2inoTextEdit(logger, cppURI, cppEdit)
13621377
if err != nil {
13631378
return nil, err
13641379
}
1380+
if inPreprocessed {
1381+
logger.Logf(("ignoring in-preprocessed-section edit"))
1382+
continue
1383+
}
13651384
allInoEdits[inoURI] = append(allInoEdits[inoURI], inoEdit)
13661385
}
13671386

@@ -1376,11 +1395,11 @@ func (ls *INOLanguageServer) cpp2inoTextEdits(logger jsonrpc.FunctionLogger, cpp
13761395
return allInoEdits, nil
13771396
}
13781397

1379-
func (ls *INOLanguageServer) cpp2inoTextEdit(logger jsonrpc.FunctionLogger, cppURI lsp.DocumentURI, cppEdit lsp.TextEdit) (lsp.DocumentURI, lsp.TextEdit, error) {
1380-
inoURI, inoRange, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, cppEdit.Range)
1398+
func (ls *INOLanguageServer) cpp2inoTextEdit(logger jsonrpc.FunctionLogger, cppURI lsp.DocumentURI, cppEdit lsp.TextEdit) (lsp.DocumentURI, lsp.TextEdit, bool, error) {
1399+
inoURI, inoRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, cppEdit.Range)
13811400
inoEdit := cppEdit
13821401
inoEdit.Range = inoRange
1383-
return inoURI, inoEdit, err
1402+
return inoURI, inoEdit, inPreprocessed, err
13841403
}
13851404

13861405
func (ls *INOLanguageServer) cpp2inoDocumentSymbols(logger jsonrpc.FunctionLogger, cppSymbols []lsp.DocumentSymbol, inoRequestedURI lsp.DocumentURI) []lsp.DocumentSymbol {

Diff for: ls/ls_clang_to_ide.go

+66-34
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,31 @@ import (
66
"go.bug.st/lsp/jsonrpc"
77
)
88

9-
func (ls *INOLanguageServer) clang2IdeRangeAndDocumentURI(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangRange lsp.Range) (lsp.DocumentURI, lsp.Range, error) {
9+
func (ls *INOLanguageServer) clang2IdeRangeAndDocumentURI(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangRange lsp.Range) (lsp.DocumentURI, lsp.Range, bool, error) {
1010
// Sketchbook/Sketch/Sketch.ino <-> build-path/sketch/Sketch.ino.cpp
1111
// Sketchbook/Sketch/AnotherTab.ino <-> build-path/sketch/Sketch.ino.cpp (different section from above)
1212
if ls.clangURIRefersToIno(clangURI) {
1313
// We are converting from preprocessed sketch.ino.cpp back to a sketch.ino file
1414
idePath, ideRange, err := ls.sketchMapper.CppToInoRangeOk(clangRange)
15-
if err == nil {
16-
if ls.sketchMapper.IsPreprocessedCppLine(clangRange.Start.Line) {
17-
idePath = sourcemapper.NotIno.File
18-
logger.Logf("Range is in PREPROCESSED section of the sketch")
19-
}
20-
} else if _, ok := err.(sourcemapper.AdjustedRangeErr); ok {
15+
if _, ok := err.(sourcemapper.AdjustedRangeErr); ok {
2116
logger.Logf("Range has been END LINE ADJSUTED")
22-
err = nil
23-
} else {
17+
} else if err != nil {
2418
logger.Logf("Range conversion ERROR: %s", err)
2519
ls.sketchMapper.DebugLogAll()
26-
return lsp.NilURI, lsp.NilRange, err
20+
return lsp.NilURI, lsp.NilRange, false, err
2721
}
2822
ideURI, err := ls.idePathToIdeURI(logger, idePath)
23+
if err != nil {
24+
logger.Logf("Range conversion ERROR: %s", err)
25+
ls.sketchMapper.DebugLogAll()
26+
return lsp.NilURI, lsp.NilRange, false, err
27+
}
28+
inPreprocessed := ls.sketchMapper.IsPreprocessedCppLine(clangRange.Start.Line)
29+
if inPreprocessed {
30+
logger.Logf("Range is in PREPROCESSED section of the sketch")
31+
}
2932
logger.Logf("Range: %s:%s -> %s:%s", clangURI, clangRange, ideURI, ideRange)
30-
return ideURI, ideRange, err
33+
return ideURI, ideRange, inPreprocessed, err
3134
}
3235

3336
// /another/global/path/to/source.cpp <-> /another/global/path/to/source.cpp (same range)
@@ -36,35 +39,35 @@ func (ls *INOLanguageServer) clang2IdeRangeAndDocumentURI(logger jsonrpc.Functio
3639
inside, err := clangPath.IsInsideDir(ls.buildSketchRoot)
3740
if err != nil {
3841
logger.Logf("ERROR: could not determine if '%s' is inside '%s'", clangURI, ls.buildSketchRoot)
39-
return lsp.NilURI, lsp.NilRange, err
42+
return lsp.NilURI, lsp.NilRange, false, err
4043
}
4144
if !inside {
4245
ideURI := clangURI
4346
logger.Logf("Range: %s:%s -> %s:%s", clangURI, clangRange, ideURI, ideRange)
44-
return clangURI, clangRange, nil
47+
return clangURI, clangRange, false, nil
4548
}
4649

4750
// Sketchbook/Sketch/AnotherFile.cpp <-> build-path/sketch/AnotherFile.cpp (same range)
4851
rel, err := ls.buildSketchRoot.RelTo(clangPath)
4952
if err != nil {
5053
logger.Logf("ERROR: could not transform '%s' into a relative path on '%s': %s", clangURI, ls.buildSketchRoot, err)
51-
return lsp.NilURI, lsp.NilRange, err
54+
return lsp.NilURI, lsp.NilRange, false, err
5255
}
5356
idePath := ls.sketchRoot.JoinPath(rel).String()
5457
ideURI, err := ls.idePathToIdeURI(logger, idePath)
5558
logger.Logf("Range: %s:%s -> %s:%s", clangURI, clangRange, ideURI, ideRange)
56-
return ideURI, clangRange, err
59+
return ideURI, clangRange, false, err
5760
}
5861

59-
func (ls *INOLanguageServer) clang2IdeDocumentHighlight(logger jsonrpc.FunctionLogger, clangHighlight lsp.DocumentHighlight, cppURI lsp.DocumentURI) (lsp.DocumentHighlight, error) {
60-
_, ideRange, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, clangHighlight.Range)
61-
if err != nil {
62-
return lsp.DocumentHighlight{}, err
62+
func (ls *INOLanguageServer) clang2IdeDocumentHighlight(logger jsonrpc.FunctionLogger, clangHighlight lsp.DocumentHighlight, cppURI lsp.DocumentURI) (lsp.DocumentHighlight, bool, error) {
63+
_, ideRange, inPreprocessed, err := ls.clang2IdeRangeAndDocumentURI(logger, cppURI, clangHighlight.Range)
64+
if err != nil || inPreprocessed {
65+
return lsp.DocumentHighlight{}, inPreprocessed, err
6366
}
6467
return lsp.DocumentHighlight{
6568
Kind: clangHighlight.Kind,
6669
Range: ideRange,
67-
}, nil
70+
}, false, nil
6871
}
6972

7073
func (ls *INOLanguageServer) clang2IdeDiagnostics(logger jsonrpc.FunctionLogger, clangDiagsParams *lsp.PublishDiagnosticsParams) (map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams, error) {
@@ -73,31 +76,60 @@ func (ls *INOLanguageServer) clang2IdeDiagnostics(logger jsonrpc.FunctionLogger,
7376
allIdeDiagsParams := map[lsp.DocumentURI]*lsp.PublishDiagnosticsParams{}
7477

7578
for _, clangDiagnostic := range clangDiagsParams.Diagnostics {
76-
ideURI, ideRange, err := ls.clang2IdeRangeAndDocumentURI(logger, clangDiagsParams.URI, clangDiagnostic.Range)
79+
ideURI, ideDiagnostic, inPreprocessed, err := ls.clang2IdeDiagnostic(logger, clangDiagsParams.URI, clangDiagnostic)
7780
if err != nil {
7881
return nil, err
7982
}
80-
if ideURI.String() == sourcemapper.NotInoURI.String() {
83+
if inPreprocessed {
8184
continue
8285
}
83-
84-
ideDiagsParams, ok := allIdeDiagsParams[ideURI]
85-
if !ok {
86-
ideDiagsParams = &lsp.PublishDiagnosticsParams{
87-
URI: ideURI,
88-
Diagnostics: []lsp.Diagnostic{},
89-
}
90-
allIdeDiagsParams[ideURI] = ideDiagsParams
86+
if _, ok := allIdeDiagsParams[ideURI]; !ok {
87+
allIdeDiagsParams[ideURI] = &lsp.PublishDiagnosticsParams{URI: ideURI}
9188
}
92-
93-
ideInoDiag := clangDiagnostic
94-
ideInoDiag.Range = ideRange
95-
ideDiagsParams.Diagnostics = append(ideDiagsParams.Diagnostics, ideInoDiag)
89+
allIdeDiagsParams[ideURI].Diagnostics = append(allIdeDiagsParams[ideURI].Diagnostics, ideDiagnostic)
9690
}
9791

9892
return allIdeDiagsParams, nil
9993
}
10094

95+
func (ls *INOLanguageServer) clang2IdeDiagnostic(logger jsonrpc.FunctionLogger, clangURI lsp.DocumentURI, clangDiagnostic lsp.Diagnostic) (lsp.DocumentURI, lsp.Diagnostic, bool, error) {
96+
ideURI, ideRange, inPreproccesed, err := ls.clang2IdeRangeAndDocumentURI(logger, clangURI, clangDiagnostic.Range)
97+
if err != nil || inPreproccesed {
98+
return lsp.DocumentURI{}, lsp.Diagnostic{}, inPreproccesed, err
99+
}
100+
101+
ideDiagnostic := clangDiagnostic
102+
ideDiagnostic.Range = ideRange
103+
104+
if len(clangDiagnostic.RelatedInformation) > 0 {
105+
ideInfos, err := ls.clang2IdeDiagnosticRelatedInformationArray(logger, clangDiagnostic.RelatedInformation)
106+
if err != nil {
107+
return lsp.DocumentURI{}, lsp.Diagnostic{}, false, err
108+
}
109+
ideDiagnostic.RelatedInformation = ideInfos
110+
}
111+
return ideURI, ideDiagnostic, false, nil
112+
}
113+
114+
func (ls *INOLanguageServer) clang2IdeDiagnosticRelatedInformationArray(logger jsonrpc.FunctionLogger, clangInfos []lsp.DiagnosticRelatedInformation) ([]lsp.DiagnosticRelatedInformation, error) {
115+
ideInfos := []lsp.DiagnosticRelatedInformation{}
116+
for _, clangInfo := range clangInfos {
117+
ideLocation, inPreprocessed, err := ls.cpp2inoLocation(logger, clangInfo.Location)
118+
if err != nil {
119+
return nil, err
120+
}
121+
if inPreprocessed {
122+
logger.Logf("Ignoring in-preprocessed-section diagnostic related information")
123+
continue
124+
}
125+
ideInfos = append(ideInfos, lsp.DiagnosticRelatedInformation{
126+
Message: clangInfo.Message,
127+
Location: ideLocation,
128+
})
129+
}
130+
return ideInfos, nil
131+
}
132+
101133
func (ls *INOLanguageServer) clang2IdeSymbolInformation(clangSymbolsInformation []lsp.SymbolInformation) []lsp.SymbolInformation {
102134
panic("not implemented")
103135
}

0 commit comments

Comments
 (0)