@@ -56,7 +56,6 @@ type INOLanguageServer struct {
56
56
buildPath * paths.Path
57
57
buildSketchRoot * paths.Path
58
58
buildSketchCpp * paths.Path
59
- buildSketchCppVersion int
60
59
buildSketchSymbols []lsp.DocumentSymbol
61
60
buildSketchIncludesCanary string
62
61
buildSketchSymbolsCanary string
@@ -68,7 +67,7 @@ type INOLanguageServer struct {
68
67
sketchName string
69
68
sketchMapper * sourcemapper.InoMapper
70
69
sketchTrackedFilesCount int
71
- docs map [string ]lsp.TextDocumentItem
70
+ trackedInoDocs map [string ]lsp.TextDocumentItem
72
71
inoDocsWithDiagnostics map [lsp.DocumentURI ]bool
73
72
74
73
config BoardConfig
@@ -147,7 +146,7 @@ func (ls *INOLanguageServer) waitClangdStart(logger jsonrpc.FunctionLogger) erro
147
146
func NewINOLanguageServer (stdin io.Reader , stdout io.Writer , board Board ) * INOLanguageServer {
148
147
logger := NewLSPFunctionLogger (color .HiWhiteString , "LS: " )
149
148
ls := & INOLanguageServer {
150
- docs : map [string ]lsp.TextDocumentItem {},
149
+ trackedInoDocs : map [string ]lsp.TextDocumentItem {},
151
150
inoDocsWithDiagnostics : map [lsp.DocumentURI ]bool {},
152
151
closing : make (chan bool ),
153
152
buildSketchSymbolsLoad : make (chan bool , 1 ),
@@ -833,7 +832,7 @@ func (ls *INOLanguageServer) TextDocumentDidOpenNotifFromIDE(logger jsonrpc.Func
833
832
834
833
// Add the TextDocumentItem in the tracked files list
835
834
inoTextDocItem := inoParam .TextDocument
836
- ls .docs [inoTextDocItem .URI .AsPath ().String ()] = inoTextDocItem
835
+ ls .trackedInoDocs [inoTextDocItem .URI .AsPath ().String ()] = inoTextDocItem
837
836
838
837
// If we are tracking a .ino...
839
838
if inoTextDocItem .URI .Ext () == ".ino" {
@@ -1044,7 +1043,6 @@ func (ls *INOLanguageServer) initializeWorkbench(logger jsonrpc.FunctionLogger,
1044
1043
return err
1045
1044
}
1046
1045
ls .buildSketchCpp = ls .buildSketchRoot .Join (ls .sketchName + ".ino.cpp" )
1047
- ls .buildSketchCppVersion = 1
1048
1046
ls .lspInitializeParams .RootPath = ls .buildSketchRoot .String ()
1049
1047
ls .lspInitializeParams .RootURI = lsp .NewDocumentURIFromPath (ls .buildSketchRoot )
1050
1048
@@ -1255,8 +1253,8 @@ func canonicalizeCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]
1255
1253
1256
1254
func (ls * INOLanguageServer ) didClose (logger jsonrpc.FunctionLogger , inoDidClose * lsp.DidCloseTextDocumentParams ) (* lsp.DidCloseTextDocumentParams , error ) {
1257
1255
inoIdentifier := inoDidClose .TextDocument
1258
- if _ , exist := ls .docs [inoIdentifier .URI .AsPath ().String ()]; exist {
1259
- delete (ls .docs , inoIdentifier .URI .AsPath ().String ())
1256
+ if _ , exist := ls .trackedInoDocs [inoIdentifier .URI .AsPath ().String ()]; exist {
1257
+ delete (ls .trackedInoDocs , inoIdentifier .URI .AsPath ().String ())
1260
1258
} else {
1261
1259
logger .Logf (" didClose of untracked document: %s" , inoIdentifier .URI )
1262
1260
return nil , unknownURI (inoIdentifier .URI )
@@ -1293,89 +1291,90 @@ func (ls *INOLanguageServer) ino2cppTextDocumentItem(logger jsonrpc.FunctionLogg
1293
1291
} else {
1294
1292
cppItem .LanguageID = inoItem .LanguageID
1295
1293
inoPath := inoItem .URI .AsPath ().String ()
1296
- cppItem .Text = ls .docs [inoPath ].Text
1297
- cppItem .Version = ls .docs [inoPath ].Version
1294
+ cppItem .Text = ls .trackedInoDocs [inoPath ].Text
1295
+ cppItem .Version = ls .trackedInoDocs [inoPath ].Version
1298
1296
}
1299
1297
1300
1298
return cppItem , nil
1301
1299
}
1302
1300
1303
- func (ls * INOLanguageServer ) didChange (logger jsonrpc.FunctionLogger , req * lsp.DidChangeTextDocumentParams ) (* lsp.DidChangeTextDocumentParams , error ) {
1304
- doc := req .TextDocument
1301
+ func (ls * INOLanguageServer ) didChange (logger jsonrpc.FunctionLogger , inoDidChangeParams * lsp.DidChangeTextDocumentParams ) (* lsp.DidChangeTextDocumentParams , error ) {
1302
+ inoDoc := inoDidChangeParams .TextDocument
1305
1303
1306
- trackedDoc , ok := ls .docs [doc .URI .AsPath ().String ()]
1304
+ // Apply the change to the tracked sketch file.
1305
+ trackedInoID := inoDoc .URI .AsPath ().String ()
1306
+ trackedInoDoc , ok := ls .trackedInoDocs [trackedInoID ]
1307
1307
if ! ok {
1308
- return nil , unknownURI (doc .URI )
1308
+ return nil , unknownURI (inoDoc .URI )
1309
+ }
1310
+ if updatedTrackedInoDoc , err := textutils .ApplyLSPTextDocumentContentChangeEvent (trackedInoDoc , inoDidChangeParams .ContentChanges , inoDoc .Version ); err != nil {
1311
+ return nil , err
1312
+ } else {
1313
+ ls .trackedInoDocs [trackedInoID ] = updatedTrackedInoDoc
1314
+ }
1315
+
1316
+ logger .Logf ("Tracked SKETCH file:----------+\n " + ls .trackedInoDocs [trackedInoID ].Text + "\n ----------------------" )
1317
+
1318
+ // If the file is not part of a .ino flie forward the change as-is to clangd
1319
+ if inoDoc .URI .Ext () != ".ino" {
1320
+ if cppDoc , err := ls .ino2cppVersionedTextDocumentIdentifier (logger , inoDidChangeParams .TextDocument ); err != nil {
1321
+ return nil , err
1322
+ } else {
1323
+ cppDidChangeParams := * inoDidChangeParams
1324
+ cppDidChangeParams .TextDocument = cppDoc
1325
+ return & cppDidChangeParams , nil
1326
+ }
1309
1327
}
1310
- textutils .ApplyLSPTextDocumentContentChangeEvent (& trackedDoc , req .ContentChanges , doc .Version )
1311
1328
1312
1329
// If changes are applied to a .ino file we increment the global .ino.cpp versioning
1313
1330
// for each increment of the single .ino file.
1314
- if doc .URI .Ext () == ".ino" {
1315
1331
1316
- cppChanges := []lsp.TextDocumentContentChangeEvent {}
1317
- for _ , inoChange := range req .ContentChanges {
1318
- cppRange , ok := ls .sketchMapper .InoToCppLSPRangeOk (doc .URI , inoChange .Range )
1319
- if ! ok {
1320
- return nil , errors .Errorf ("invalid change range %s:%s" , doc .URI , inoChange .Range )
1321
- }
1332
+ cppChanges := []lsp.TextDocumentContentChangeEvent {}
1333
+ for _ , inoChange := range inoDidChangeParams .ContentChanges {
1334
+ cppChangeRange , ok := ls .sketchMapper .InoToCppLSPRangeOk (inoDoc .URI , inoChange .Range )
1335
+ if ! ok {
1336
+ return nil , errors .Errorf ("invalid change range %s:%s" , inoDoc .URI , inoChange .Range )
1337
+ }
1322
1338
1323
- // Detect changes in critical lines (for example function definitions)
1324
- // and trigger arduino-preprocessing + clangd restart.
1325
- dirty := false
1326
- for _ , sym := range ls .buildSketchSymbols {
1327
- if sym .SelectionRange .Overlaps (cppRange ) {
1328
- dirty = true
1329
- logger .Logf ("--! DIRTY CHANGE detected using symbol tables, force sketch rebuild!" )
1330
- break
1331
- }
1332
- }
1333
- if ls .sketchMapper .ApplyTextChange (doc .URI , inoChange ) {
1339
+ // Detect changes in critical lines (for example function definitions)
1340
+ // and trigger arduino-preprocessing + clangd restart.
1341
+ dirty := false
1342
+ for _ , sym := range ls .buildSketchSymbols {
1343
+ if sym .SelectionRange .Overlaps (cppChangeRange ) {
1334
1344
dirty = true
1335
- logger .Logf ("--! DIRTY CHANGE detected with sketch mapper, force sketch rebuild!" )
1336
- }
1337
- if dirty {
1338
- ls .scheduleRebuildEnvironment ()
1339
- }
1340
-
1341
- // logger.Logf("New version:----------")
1342
- // logger.Logf(ls.sketchMapper.CppText.Text)
1343
- // logger.Logf("----------------------")
1344
-
1345
- cppChange := lsp.TextDocumentContentChangeEvent {
1346
- Range : cppRange ,
1347
- RangeLength : inoChange .RangeLength ,
1348
- Text : inoChange .Text ,
1345
+ logger .Logf ("--! DIRTY CHANGE detected using symbol tables, force sketch rebuild!" )
1346
+ break
1349
1347
}
1350
- cppChanges = append (cppChanges , cppChange )
1351
1348
}
1352
-
1353
- ls .CheckCppIncludesChanges ()
1354
-
1355
- // build a cpp equivalent didChange request
1356
- cppReq := & lsp.DidChangeTextDocumentParams {
1357
- ContentChanges : cppChanges ,
1358
- TextDocument : lsp.VersionedTextDocumentIdentifier {
1359
- TextDocumentIdentifier : lsp.TextDocumentIdentifier {
1360
- URI : lsp .NewDocumentURIFromPath (ls .buildSketchCpp ),
1361
- },
1362
- Version : ls .sketchMapper .CppText .Version ,
1363
- },
1349
+ if ls .sketchMapper .ApplyTextChange (inoDoc .URI , inoChange ) {
1350
+ dirty = true
1351
+ logger .Logf ("--! DIRTY CHANGE detected with sketch mapper, force sketch rebuild!" )
1352
+ }
1353
+ if dirty {
1354
+ ls .scheduleRebuildEnvironment ()
1364
1355
}
1365
1356
1366
- return cppReq , nil
1367
- }
1357
+ logger .Logf ("New version:----------+\n " + ls .sketchMapper .CppText .Text + "\n ----------------------" )
1368
1358
1369
- // If changes are applied to other files pass them by converting just the URI
1370
- cppDoc , err := ls .ino2cppVersionedTextDocumentIdentifier (logger , req .TextDocument )
1371
- if err != nil {
1372
- return nil , err
1373
- }
1374
- cppReq := & lsp.DidChangeTextDocumentParams {
1375
- TextDocument : cppDoc ,
1376
- ContentChanges : req .ContentChanges ,
1359
+ cppChanges = append (cppChanges , lsp.TextDocumentContentChangeEvent {
1360
+ Range : cppChangeRange ,
1361
+ RangeLength : inoChange .RangeLength ,
1362
+ Text : inoChange .Text ,
1363
+ })
1377
1364
}
1378
- return cppReq , err
1365
+
1366
+ ls .CheckCppIncludesChanges ()
1367
+
1368
+ // build a cpp equivalent didChange request
1369
+ return & lsp.DidChangeTextDocumentParams {
1370
+ ContentChanges : cppChanges ,
1371
+ TextDocument : lsp.VersionedTextDocumentIdentifier {
1372
+ TextDocumentIdentifier : lsp.TextDocumentIdentifier {
1373
+ URI : lsp .NewDocumentURIFromPath (ls .buildSketchCpp ),
1374
+ },
1375
+ Version : ls .sketchMapper .CppText .Version ,
1376
+ },
1377
+ }, nil
1379
1378
}
1380
1379
1381
1380
func (ls * INOLanguageServer ) ino2cppVersionedTextDocumentIdentifier (logger jsonrpc.FunctionLogger , doc lsp.VersionedTextDocumentIdentifier ) (lsp.VersionedTextDocumentIdentifier , error ) {
@@ -1429,11 +1428,11 @@ func (ls *INOLanguageServer) inoDocumentURIFromInoPath(logger jsonrpc.FunctionLo
1429
1428
if inoPath == sourcemapper .NotIno .File {
1430
1429
return sourcemapper .NotInoURI , nil
1431
1430
}
1432
- doc , ok := ls .docs [inoPath ]
1431
+ doc , ok := ls .trackedInoDocs [inoPath ]
1433
1432
if ! ok {
1434
1433
logger .Logf (" !!! Unresolved .ino path: %s" , inoPath )
1435
1434
logger .Logf (" !!! Known doc paths are:" )
1436
- for p := range ls .docs {
1435
+ for p := range ls .trackedInoDocs {
1437
1436
logger .Logf (" !!! > %s" , p )
1438
1437
}
1439
1438
uri := lsp .NewDocumentURI (inoPath )
0 commit comments