@@ -58,8 +58,8 @@ type InoHandler struct {
58
58
sketchName string
59
59
sketchMapper * sourcemapper.InoMapper
60
60
sketchTrackedFilesCount int
61
- docs map [lsp. DocumentURI ]* lsp.TextDocumentItem
62
- inoDocsWithDiagnostics map [lsp. DocumentURI ]bool
61
+ docs map [string ]* lsp.TextDocumentItem
62
+ inoDocsWithDiagnostics map [string ]bool
63
63
64
64
config lsp.BoardConfig
65
65
synchronizer Synchronizer
@@ -68,8 +68,8 @@ type InoHandler struct {
68
68
// NewInoHandler creates and configures an InoHandler.
69
69
func NewInoHandler (stdio io.ReadWriteCloser , board lsp.Board ) * InoHandler {
70
70
handler := & InoHandler {
71
- docs : map [lsp. DocumentURI ]* lsp.TextDocumentItem {},
72
- inoDocsWithDiagnostics : map [lsp. DocumentURI ]bool {},
71
+ docs : map [string ]* lsp.TextDocumentItem {},
72
+ inoDocsWithDiagnostics : map [string ]bool {},
73
73
config : lsp.BoardConfig {
74
74
SelectedBoard : board ,
75
75
},
@@ -542,7 +542,7 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.
542
542
func (handler * InoHandler ) didOpen (inoDidOpen * lsp.DidOpenTextDocumentParams ) (* lsp.DidOpenTextDocumentParams , error ) {
543
543
// Add the TextDocumentItem in the tracked files list
544
544
inoItem := inoDidOpen .TextDocument
545
- handler .docs [inoItem .URI ] = & inoItem
545
+ handler .docs [inoItem .URI . Canonical () ] = & inoItem
546
546
547
547
// If we are tracking a .ino...
548
548
if inoItem .URI .Ext () == ".ino" {
@@ -566,8 +566,8 @@ func (handler *InoHandler) didOpen(inoDidOpen *lsp.DidOpenTextDocumentParams) (*
566
566
567
567
func (handler * InoHandler ) didClose (inoDidClose * lsp.DidCloseTextDocumentParams ) (* lsp.DidCloseTextDocumentParams , error ) {
568
568
inoIdentifier := inoDidClose .TextDocument
569
- if _ , exist := handler .docs [inoIdentifier .URI ]; exist {
570
- delete (handler .docs , inoIdentifier .URI )
569
+ if _ , exist := handler .docs [inoIdentifier .URI . Canonical () ]; exist {
570
+ delete (handler .docs , inoIdentifier .URI . Canonical () )
571
571
} else {
572
572
log .Printf (" didClose of untracked document: %s" , inoIdentifier .URI )
573
573
return nil , unknownURI (inoIdentifier .URI )
@@ -602,8 +602,8 @@ func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem)
602
602
cppItem .Text = handler .sketchMapper .CppText .Text
603
603
cppItem .Version = handler .sketchMapper .CppText .Version
604
604
} else {
605
- cppItem .Text = handler .docs [inoItem .URI ].Text
606
- cppItem .Version = handler .docs [inoItem .URI ].Version
605
+ cppItem .Text = handler .docs [inoItem .URI . Canonical () ].Text
606
+ cppItem .Version = handler .docs [inoItem .URI . Canonical () ].Version
607
607
}
608
608
609
609
return cppItem , nil
@@ -612,7 +612,7 @@ func (handler *InoHandler) ino2cppTextDocumentItem(inoItem lsp.TextDocumentItem)
612
612
func (handler * InoHandler ) didChange (ctx context.Context , req * lsp.DidChangeTextDocumentParams ) (* lsp.DidChangeTextDocumentParams , error ) {
613
613
doc := req .TextDocument
614
614
615
- trackedDoc , ok := handler .docs [doc .URI ]
615
+ trackedDoc , ok := handler .docs [doc .URI . Canonical () ]
616
616
if ! ok {
617
617
return nil , unknownURI (doc .URI )
618
618
}
@@ -766,6 +766,23 @@ func (handler *InoHandler) ino2cppDocumentURI(inoURI lsp.DocumentURI) (lsp.Docum
766
766
return lsp .NilURI , err
767
767
}
768
768
769
+ func (handler * InoHandler ) inoDocumentURIFromInoPath (inoPath string ) (lsp.DocumentURI , error ) {
770
+ if inoPath == sourcemapper .NotIno .File {
771
+ return sourcemapper .NotInoURI , nil
772
+ }
773
+ doc , ok := handler .docs [inoPath ]
774
+ if ! ok {
775
+ log .Printf (" !!! Unresolved .ino path: %s" , inoPath )
776
+ log .Printf (" !!! Known doc paths are:" )
777
+ for p := range handler .docs {
778
+ log .Printf (" !!! > %s" , p )
779
+ }
780
+ uri := lsp .NewDocumentURI (inoPath )
781
+ return uri , unknownURI (uri )
782
+ }
783
+ return doc .URI , nil
784
+ }
785
+
769
786
func (handler * InoHandler ) cpp2inoDocumentURI (cppURI lsp.DocumentURI , cppRange lsp.Range ) (lsp.DocumentURI , lsp.Range , error ) {
770
787
// Sketchbook/Sketch/Sketch.ino <- build-path/sketch/Sketch.ino.cpp
771
788
// Sketchbook/Sketch/AnotherTab.ino <- build-path/sketch/Sketch.ino.cpp (different section from above)
@@ -784,16 +801,19 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
784
801
} else {
785
802
log .Printf (" URI: ERROR: %s" , err )
786
803
handler .sketchMapper .DebugLogAll ()
804
+ return lsp .NilURI , lsp .NilRange , err
787
805
}
788
- return lsp .NewDocumentURI (inoPath ), inoRange , err
806
+ inoURI , err := handler .inoDocumentURIFromInoPath (inoPath )
807
+ return inoURI , inoRange , err
789
808
}
790
809
791
810
inside , err := cppPath .IsInsideDir (handler .buildSketchRoot )
792
811
if err != nil {
793
812
log .Printf (" could not determine if '%s' is inside '%s'" , cppPath , handler .buildSketchRoot )
794
- return lsp .NilURI , lsp.Range {} , err
813
+ return lsp .NilURI , lsp .NilRange , err
795
814
}
796
815
if ! inside {
816
+ log .Printf (" '%s' is not inside '%s'" , cppPath , handler .buildSketchRoot )
797
817
log .Printf (" keep doc identifier to '%s' as-is" , cppPath )
798
818
return cppURI , cppRange , nil
799
819
}
@@ -806,7 +826,7 @@ func (handler *InoHandler) cpp2inoDocumentURI(cppURI lsp.DocumentURI, cppRange l
806
826
}
807
827
808
828
log .Printf (" could not determine rel-path of '%s' in '%s': %s" , cppPath , handler .buildSketchRoot , err )
809
- return lsp .NilURI , lsp.Range {} , err
829
+ return lsp .NilURI , lsp .NilRange , err
810
830
}
811
831
812
832
func (handler * InoHandler ) ino2cppTextDocumentPositionParams (inoParams * lsp.TextDocumentPositionParams ) (* lsp.TextDocumentPositionParams , error ) {
@@ -1111,36 +1131,41 @@ func (handler *InoHandler) Cpp2InoCommand(command *lsp.Command) *lsp.Command {
1111
1131
return inoCommand
1112
1132
}
1113
1133
1114
- func (handler * InoHandler ) cpp2inoWorkspaceEdit (origWorkspaceEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
1115
- if origWorkspaceEdit == nil {
1134
+ func (handler * InoHandler ) cpp2inoWorkspaceEdit (cppWorkspaceEdit * lsp.WorkspaceEdit ) * lsp.WorkspaceEdit {
1135
+ if cppWorkspaceEdit == nil {
1116
1136
return nil
1117
1137
}
1118
- resWorkspaceEdit := & lsp.WorkspaceEdit {
1138
+ inoWorkspaceEdit := & lsp.WorkspaceEdit {
1119
1139
Changes : map [lsp.DocumentURI ][]lsp.TextEdit {},
1120
1140
}
1121
- for editURI , edits := range origWorkspaceEdit .Changes {
1141
+ for editURI , edits := range cppWorkspaceEdit .Changes {
1122
1142
// if the edits are not relative to sketch file...
1123
1143
if ! editURI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
1124
1144
// ...pass them through...
1125
- resWorkspaceEdit .Changes [editURI ] = edits
1145
+ inoWorkspaceEdit .Changes [editURI ] = edits
1126
1146
continue
1127
1147
}
1128
1148
1129
1149
// ...otherwise convert edits to the sketch.ino.cpp into multilpe .ino edits
1130
1150
for _ , edit := range edits {
1131
- cppRange := edit .Range
1132
- inoFile , inoRange := handler .sketchMapper .CppToInoRange (cppRange )
1133
- inoURI := lsp .NewDocumentURI (inoFile )
1134
- if _ , have := resWorkspaceEdit .Changes [inoURI ]; ! have {
1135
- resWorkspaceEdit .Changes [inoURI ] = []lsp.TextEdit {}
1151
+ inoURI , inoRange , err := handler .cpp2inoDocumentURI (editURI , edit .Range )
1152
+ if err != nil {
1153
+ log .Printf (" error converting edit %s:%s: %s" , editURI , edit .Range , err )
1154
+ continue
1155
+ }
1156
+ //inoFile, inoRange := handler.sketchMapper.CppToInoRange(edit.Range)
1157
+ //inoURI := lsp.NewDocumentURI(inoFile)
1158
+ if _ , have := inoWorkspaceEdit .Changes [inoURI ]; ! have {
1159
+ inoWorkspaceEdit .Changes [inoURI ] = []lsp.TextEdit {}
1136
1160
}
1137
- resWorkspaceEdit .Changes [inoURI ] = append (resWorkspaceEdit .Changes [inoURI ], lsp.TextEdit {
1161
+ inoWorkspaceEdit .Changes [inoURI ] = append (inoWorkspaceEdit .Changes [inoURI ], lsp.TextEdit {
1138
1162
NewText : edit .NewText ,
1139
1163
Range : inoRange ,
1140
1164
})
1141
1165
}
1142
1166
}
1143
- return resWorkspaceEdit
1167
+ log .Printf (" done converting workspaceEdit" )
1168
+ return inoWorkspaceEdit
1144
1169
}
1145
1170
1146
1171
func (handler * InoHandler ) cpp2inoLocation (cppLocation lsp.Location ) (lsp.Location , error ) {
@@ -1260,7 +1285,7 @@ func (handler *InoHandler) cpp2inoDiagnostics(cppDiags *lsp.PublishDiagnosticsPa
1260
1285
return []* lsp.PublishDiagnosticsParams {}, nil
1261
1286
}
1262
1287
1263
- inoURI , _ , err := handler .cpp2inoDocumentURI (cppDiags .URI , lsp.Range {} )
1288
+ inoURI , _ , err := handler .cpp2inoDocumentURI (cppDiags .URI , lsp .NilRange )
1264
1289
return []* lsp.PublishDiagnosticsParams {
1265
1290
{
1266
1291
URI : inoURI ,
@@ -1332,9 +1357,9 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
1332
1357
}
1333
1358
1334
1359
// Push back to IDE the converted diagnostics
1335
- inoDocsWithDiagnostics := map [lsp. DocumentURI ]bool {}
1360
+ inoDocsWithDiagnostics := map [string ]bool {}
1336
1361
for _ , inoDiag := range inoDiagnostics {
1337
- if inoDiag .URI == lsp . NewDocumentURI ( sourcemapper .NotIno . File ) {
1362
+ if inoDiag .URI . String () == sourcemapper .NotInoURI . String ( ) {
1338
1363
cleanUpInoDiagnostics = true
1339
1364
continue
1340
1365
}
@@ -1350,7 +1375,7 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
1350
1375
// check for newly created symbols (that in turn may trigger a
1351
1376
// new arduino-preprocessing of the sketch).
1352
1377
if inoDiag .URI .Ext () == ".ino" {
1353
- inoDocsWithDiagnostics [inoDiag .URI ] = true
1378
+ inoDocsWithDiagnostics [inoDiag .URI . Canonical () ] = true
1354
1379
cleanUpInoDiagnostics = true
1355
1380
for _ , diag := range inoDiag .Diagnostics {
1356
1381
if diag .Code == "undeclared_var_use_suggest" || diag .Code == "undeclared_var_use" {
@@ -1366,14 +1391,14 @@ func (handler *InoHandler) FromClangd(ctx context.Context, connection *jsonrpc2.
1366
1391
1367
1392
if cleanUpInoDiagnostics {
1368
1393
// Remove diagnostics from all .ino where there are no errors coming from clang
1369
- for sourceURI := range handler .inoDocsWithDiagnostics {
1370
- if inoDocsWithDiagnostics [sourceURI ] {
1394
+ for sourcePath := range handler .inoDocsWithDiagnostics {
1395
+ if inoDocsWithDiagnostics [sourcePath ] {
1371
1396
// skip if we already sent updated diagnostics
1372
1397
continue
1373
1398
}
1374
1399
// otherwise clear previous diagnostics
1375
1400
msg := lsp.PublishDiagnosticsParams {
1376
- URI : sourceURI ,
1401
+ URI : lsp . NewDocumentURI ( sourcePath ) ,
1377
1402
Diagnostics : []lsp.Diagnostic {},
1378
1403
}
1379
1404
if enableLogging {
0 commit comments