@@ -306,7 +306,7 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
306
306
defer ls .readUnlock (logger )
307
307
308
308
logger .Logf ("--> completion(%s)\n " , inoParams .TextDocument )
309
- cppTextDocPositionParams , err := ls .ino2cppTextDocumentPositionParams (logger , inoParams .TextDocumentPositionParams )
309
+ cppTextDocPositionParams , err := ls .ide2clangTextDocumentPositionParams (logger , inoParams .TextDocumentPositionParams )
310
310
if err != nil {
311
311
logger .Logf ("Error: %s" , err )
312
312
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -350,25 +350,20 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
350
350
return & inoResp , nil
351
351
}
352
352
353
- func (ls * INOLanguageServer ) TextDocumentHoverReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , inoParams * lsp.HoverParams ) (* lsp.Hover , * jsonrpc.ResponseError ) {
353
+ func (ls * INOLanguageServer ) TextDocumentHoverReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , ideParams * lsp.HoverParams ) (* lsp.Hover , * jsonrpc.ResponseError ) {
354
354
ls .readLock (logger , true )
355
355
defer ls .readUnlock (logger )
356
356
357
- inoURI := inoParams .TextDocument .URI
358
- inoTextDocPosition := inoParams .TextDocumentPositionParams
359
- logger .Logf ("--> hover(%s)\n " , inoTextDocPosition )
360
-
361
- cppTextDocPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocPosition )
357
+ clangTextDocPosition , err := ls .ide2clangTextDocumentPositionParams (logger , ideParams .TextDocumentPositionParams )
362
358
if err != nil {
363
359
logger .Logf ("Error: %s" , err )
364
360
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
365
361
}
366
362
367
- logger .Logf (" --> hover(%s)\n " , cppTextDocPosition )
368
- cppParams := & lsp.HoverParams {
369
- TextDocumentPositionParams : cppTextDocPosition ,
363
+ clangParams := & lsp.HoverParams {
364
+ TextDocumentPositionParams : clangTextDocPosition ,
370
365
}
371
- clangResp , clangErr , err := ls .Clangd .conn .TextDocumentHover (ctx , cppParams )
366
+ clangResp , clangErr , err := ls .Clangd .conn .TextDocumentHover (ctx , clangParams )
372
367
if err != nil {
373
368
logger .Logf ("clangd connectiono error: %v" , err )
374
369
ls .Close ()
@@ -380,21 +375,26 @@ func (ls *INOLanguageServer) TextDocumentHoverReqFromIDE(ctx context.Context, lo
380
375
}
381
376
382
377
if clangResp == nil {
378
+ logger .Logf ("response: nil" )
383
379
return nil , nil
384
380
}
385
381
386
- inoResp := * clangResp
387
- // TODO: ????
388
- // if len(clangResp.Contents.Value) == 0 {
389
- // return nil
390
- // }
391
- cppToIno := inoURI != lsp .NilURI && inoURI .AsPath ().EquivalentTo (ls .buildSketchCpp )
392
- if cppToIno {
393
- _ , inoRange := ls .sketchMapper .CppToInoRange (* clangResp .Range )
394
- inoResp .Range = & inoRange
382
+ _ , r , err := ls .clang2IdeRangeAndDocumentURI (logger , clangParams .TextDocument .URI , * clangResp .Range )
383
+ if err != nil {
384
+ logger .Logf ("error during range conversion: %v" , err )
385
+ ls .Close ()
386
+ return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
395
387
}
396
- logger .Logf ("<-- hover(%s)" , strconv .Quote (inoResp .Contents .Value ))
397
- return & inoResp , nil
388
+ ideResp := lsp.Hover {
389
+ Contents : clangResp .Contents ,
390
+ Range : & r ,
391
+ }
392
+ logger .Logf ("Hover content: %s" , strconv .Quote (ideResp .Contents .Value ))
393
+ return & ideResp , nil
394
+ }
395
+
396
+ func (ls * INOLanguageServer ) clangURIRefersToIno (uri lsp.DocumentURI ) bool {
397
+ return uri .AsPath ().EquivalentTo (ls .buildSketchCpp )
398
398
}
399
399
400
400
func (ls * INOLanguageServer ) TextDocumentSignatureHelpReqFromIDE (ctx context.Context , logger jsonrpc.FunctionLogger , inoParams * lsp.SignatureHelpParams ) (* lsp.SignatureHelp , * jsonrpc.ResponseError ) {
@@ -404,7 +404,7 @@ func (ls *INOLanguageServer) TextDocumentSignatureHelpReqFromIDE(ctx context.Con
404
404
inoTextDocumentPosition := inoParams .TextDocumentPositionParams
405
405
406
406
logger .Logf ("%s" , inoTextDocumentPosition )
407
- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
407
+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
408
408
if err != nil {
409
409
logger .Logf ("Error: %s" , err )
410
410
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -436,7 +436,7 @@ func (ls *INOLanguageServer) TextDocumentDefinitionReqFromIDE(ctx context.Contex
436
436
inoTextDocPosition := p .TextDocumentPositionParams
437
437
438
438
logger .Logf ("%s" , inoTextDocPosition )
439
- cppTextDocPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocPosition )
439
+ cppTextDocPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocPosition )
440
440
if err != nil {
441
441
logger .Logf ("Error: %s" , err )
442
442
return nil , nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -481,7 +481,7 @@ func (ls *INOLanguageServer) TextDocumentTypeDefinitionReqFromIDE(ctx context.Co
481
481
482
482
logger .Logf ("%s" , inoTextDocumentPosition )
483
483
// inoURI := inoTextDocumentPosition.TextDocument.URI
484
- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
484
+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
485
485
if err != nil {
486
486
logger .Logf ("Error: %s" , err )
487
487
return nil , nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -527,7 +527,7 @@ func (ls *INOLanguageServer) TextDocumentImplementationReqFromIDE(ctx context.Co
527
527
inoTextDocumentPosition := inoParams .TextDocumentPositionParams
528
528
logger .Logf ("%s" , inoTextDocumentPosition )
529
529
530
- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
530
+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
531
531
if err != nil {
532
532
logger .Logf ("Error: %s" , err )
533
533
return nil , nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -569,7 +569,7 @@ func (ls *INOLanguageServer) TextDocumentDocumentHighlightReqFromIDE(ctx context
569
569
defer ls .readUnlock (logger )
570
570
571
571
inoTextDocumentPosition := inoParams .TextDocumentPositionParams
572
- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
572
+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
573
573
if err != nil {
574
574
logger .Logf ("Error: %s" , err )
575
575
return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -1235,7 +1235,7 @@ func (ls *INOLanguageServer) ino2cppDocumentURI(logger jsonrpc.FunctionLogger, i
1235
1235
return lsp .NilURI , err
1236
1236
}
1237
1237
1238
- func (ls * INOLanguageServer ) inoDocumentURIFromInoPath (logger jsonrpc.FunctionLogger , inoPath string ) (lsp.DocumentURI , error ) {
1238
+ func (ls * INOLanguageServer ) idePathToIdeURI (logger jsonrpc.FunctionLogger , inoPath string ) (lsp.DocumentURI , error ) {
1239
1239
if inoPath == sourcemapper .NotIno .File {
1240
1240
return sourcemapper .NotInoURI , nil
1241
1241
}
@@ -1252,65 +1252,57 @@ func (ls *INOLanguageServer) inoDocumentURIFromInoPath(logger jsonrpc.FunctionLo
1252
1252
return doc .URI , nil
1253
1253
}
1254
1254
1255
- func (ls * INOLanguageServer ) cpp2inoDocumentURI (logger jsonrpc.FunctionLogger , cppURI lsp.DocumentURI , cppRange lsp.Range ) (lsp.DocumentURI , lsp.Range , error ) {
1256
- // TODO: Split this function into 2
1257
- // - Cpp2inoSketchDocumentURI: converts sketch (cppURI, cppRange) -> (inoURI, inoRange)
1258
- // - Cpp2inoDocumentURI : converts non-sketch (cppURI) -> (inoURI) [range is the same]
1259
-
1260
- // Sketchbook/Sketch/Sketch.ino <- build-path/sketch/Sketch.ino.cpp
1261
- // Sketchbook/Sketch/AnotherTab.ino <- build-path/sketch/Sketch.ino.cpp (different section from above)
1262
- // Sketchbook/Sketch/AnotherFile.cpp <- build-path/sketch/AnotherFile.cpp (1:1)
1263
- // another/path/source.cpp <- unchanged
1264
-
1265
- // Convert build path to sketch path
1266
- cppPath := cppURI .AsPath ()
1267
- if cppPath .EquivalentTo (ls .buildSketchCpp ) {
1268
- inoPath , inoRange , err := ls .sketchMapper .CppToInoRangeOk (cppRange )
1255
+ func (ls * INOLanguageServer ) clang2IdeRangeAndDocumentURI (logger jsonrpc.FunctionLogger , clangURI lsp.DocumentURI , clangRange lsp.Range ) (lsp.DocumentURI , lsp.Range , error ) {
1256
+ // Sketchbook/Sketch/Sketch.ino <-> build-path/sketch/Sketch.ino.cpp
1257
+ // Sketchbook/Sketch/AnotherTab.ino <-> build-path/sketch/Sketch.ino.cpp (different section from above)
1258
+ if ls .clangURIRefersToIno (clangURI ) {
1259
+ // We are converting from preprocessed sketch.ino.cpp back to a sketch.ino file
1260
+ idePath , ideRange , err := ls .sketchMapper .CppToInoRangeOk (clangRange )
1269
1261
if err == nil {
1270
- if ls .sketchMapper .IsPreprocessedCppLine (cppRange .Start .Line ) {
1271
- inoPath = sourcemapper .NotIno .File
1272
- logger .Logf (" URI: is in preprocessed section" )
1273
- logger .Logf (" converted %s to %s:%s" , cppRange , inoPath , inoRange )
1274
- } else {
1275
- logger .Logf (" URI: converted %s to %s:%s" , cppRange , inoPath , inoRange )
1262
+ if ls .sketchMapper .IsPreprocessedCppLine (clangRange .Start .Line ) {
1263
+ idePath = sourcemapper .NotIno .File
1264
+ logger .Logf (" Range is in PREPROCESSED section of the sketch" )
1276
1265
}
1277
1266
} else if _ , ok := err .(sourcemapper.AdjustedRangeErr ); ok {
1278
- logger .Logf (" URI: converted %s to %s:%s ( END LINE ADJUSTED)" , cppRange , inoPath , inoRange )
1267
+ logger .Logf (" Range has been END LINE ADJSUTED" )
1279
1268
err = nil
1280
1269
} else {
1281
- logger .Logf (" URI: ERROR: %s" , err )
1270
+ logger .Logf (" Range conversion ERROR: %s" , err )
1282
1271
ls .sketchMapper .DebugLogAll ()
1283
1272
return lsp .NilURI , lsp .NilRange , err
1284
1273
}
1285
- inoURI , err := ls .inoDocumentURIFromInoPath (logger , inoPath )
1286
- return inoURI , inoRange , err
1274
+ ideURI , err := ls .idePathToIdeURI (logger , idePath )
1275
+ logger .Logf (" Range: %s:%s -> %s:%s" , clangURI , clangRange , ideURI , ideRange )
1276
+ return ideURI , ideRange , err
1287
1277
}
1288
1278
1289
- inside , err := cppPath .IsInsideDir (ls .buildSketchRoot )
1279
+ // /another/global/path/to/source.cpp <-> /another/global/path/to/source.cpp (same range)
1280
+ ideRange := clangRange
1281
+ clangPath := clangURI .AsPath ()
1282
+ inside , err := clangPath .IsInsideDir (ls .buildSketchRoot )
1290
1283
if err != nil {
1291
- logger .Logf (" could not determine if '%s' is inside '%s'" , cppPath , ls .buildSketchRoot )
1284
+ logger .Logf ("ERROR: could not determine if '%s' is inside '%s'" , clangURI , ls .buildSketchRoot )
1292
1285
return lsp .NilURI , lsp .NilRange , err
1293
1286
}
1294
1287
if ! inside {
1295
- logger . Logf ( " '%s' is not inside '%s'" , cppPath , ls . buildSketchRoot )
1296
- logger .Logf (" keep doc identifier to '%s' as-is " , cppPath )
1297
- return cppURI , cppRange , nil
1288
+ ideURI := clangURI
1289
+ logger .Logf (" Range: %s:%s -> %s:%s " , clangURI , clangRange , ideURI , ideRange )
1290
+ return clangURI , clangRange , nil
1298
1291
}
1299
1292
1300
- rel , err := ls .buildSketchRoot .RelTo (cppPath )
1301
- if err == nil {
1302
- inoPath := ls .sketchRoot .JoinPath (rel ).String ()
1303
- logger .Logf (" URI: '%s' -> '%s'" , cppPath , inoPath )
1304
- inoURI , err := ls .inoDocumentURIFromInoPath (logger , inoPath )
1305
- logger .Logf (" as URI: '%s'" , inoURI )
1306
- return inoURI , cppRange , err
1293
+ // Sketchbook/Sketch/AnotherFile.cpp <-> build-path/sketch/AnotherFile.cpp (same range)
1294
+ rel , err := ls .buildSketchRoot .RelTo (clangPath )
1295
+ if err != nil {
1296
+ logger .Logf ("ERROR: could not transform '%s' into a relative path on '%s': %s" , clangURI , ls .buildSketchRoot , err )
1297
+ return lsp .NilURI , lsp .NilRange , err
1307
1298
}
1308
-
1309
- logger .Logf (" could not determine rel-path of '%s' in '%s': %s" , cppPath , ls .buildSketchRoot , err )
1310
- return lsp .NilURI , lsp .NilRange , err
1299
+ idePath := ls .sketchRoot .JoinPath (rel ).String ()
1300
+ ideURI , err := ls .idePathToIdeURI (logger , idePath )
1301
+ logger .Logf (" Range: %s:%s -> %s:%s" , clangURI , clangRange , ideURI , ideRange )
1302
+ return ideURI , clangRange , err
1311
1303
}
1312
1304
1313
- func (ls * INOLanguageServer ) ino2cppTextDocumentPositionParams (logger jsonrpc.FunctionLogger , inoParams lsp.TextDocumentPositionParams ) (lsp.TextDocumentPositionParams , error ) {
1305
+ func (ls * INOLanguageServer ) ide2clangTextDocumentPositionParams (logger jsonrpc.FunctionLogger , inoParams lsp.TextDocumentPositionParams ) (lsp.TextDocumentPositionParams , error ) {
1314
1306
inoTextDocument := inoParams .TextDocument
1315
1307
inoPosition := inoParams .Position
1316
1308
inoURI := inoTextDocument .URI
@@ -1447,7 +1439,7 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
1447
1439
1448
1440
// ...otherwise convert edits to the sketch.ino.cpp into multilpe .ino edits
1449
1441
for _ , edit := range edits {
1450
- inoURI , inoRange , err := ls .cpp2inoDocumentURI (logger , editURI , edit .Range )
1442
+ inoURI , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , editURI , edit .Range )
1451
1443
if err != nil {
1452
1444
logger .Logf (" error converting edit %s:%s: %s" , editURI , edit .Range , err )
1453
1445
continue
@@ -1468,15 +1460,15 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
1468
1460
}
1469
1461
1470
1462
func (ls * INOLanguageServer ) cpp2inoLocation (logger jsonrpc.FunctionLogger , cppLocation lsp.Location ) (lsp.Location , error ) {
1471
- inoURI , inoRange , err := ls .cpp2inoDocumentURI (logger , cppLocation .URI , cppLocation .Range )
1463
+ inoURI , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , cppLocation .URI , cppLocation .Range )
1472
1464
return lsp.Location {
1473
1465
URI : inoURI ,
1474
1466
Range : inoRange ,
1475
1467
}, err
1476
1468
}
1477
1469
1478
1470
func (ls * INOLanguageServer ) cpp2inoDocumentHighlight (logger jsonrpc.FunctionLogger , cppHighlight lsp.DocumentHighlight , cppURI lsp.DocumentURI ) (lsp.DocumentHighlight , error ) {
1479
- _ , inoRange , err := ls .cpp2inoDocumentURI (logger , cppURI , cppHighlight .Range )
1471
+ _ , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , cppHighlight .Range )
1480
1472
if err != nil {
1481
1473
return lsp.DocumentHighlight {}, err
1482
1474
}
@@ -1510,7 +1502,7 @@ func (ls *INOLanguageServer) cpp2inoTextEdits(logger jsonrpc.FunctionLogger, cpp
1510
1502
}
1511
1503
1512
1504
func (ls * INOLanguageServer ) cpp2inoTextEdit (logger jsonrpc.FunctionLogger , cppURI lsp.DocumentURI , cppEdit lsp.TextEdit ) (lsp.DocumentURI , lsp.TextEdit , error ) {
1513
- inoURI , inoRange , err := ls .cpp2inoDocumentURI (logger , cppURI , cppEdit .Range )
1505
+ inoURI , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , cppEdit .Range )
1514
1506
inoEdit := cppEdit
1515
1507
inoEdit .Range = inoRange
1516
1508
return inoURI , inoEdit , err
@@ -1570,13 +1562,13 @@ func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, c
1570
1562
isSketch := cppURI .AsPath ().EquivalentTo (ls .buildSketchCpp )
1571
1563
1572
1564
if ! isSketch {
1573
- inoURI , _ , err := ls .cpp2inoDocumentURI (logger , cppURI , lsp .NilRange )
1565
+ inoURI , _ , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , lsp .NilRange )
1574
1566
if err != nil {
1575
1567
return nil , err
1576
1568
}
1577
1569
inoDiags := []lsp.Diagnostic {}
1578
1570
for _ , cppDiag := range cppDiagsParams .Diagnostics {
1579
- inoURIofConvertedRange , inoRange , err := ls .cpp2inoDocumentURI (logger , cppURI , cppDiag .Range )
1571
+ inoURIofConvertedRange , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , cppDiag .Range )
1580
1572
if err != nil {
1581
1573
return nil , err
1582
1574
}
@@ -1608,7 +1600,7 @@ func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, c
1608
1600
ls .inoDocsWithDiagnostics = map [lsp.DocumentURI ]bool {}
1609
1601
1610
1602
for _ , cppDiag := range cppDiagsParams .Diagnostics {
1611
- inoURI , inoRange , err := ls .cpp2inoDocumentURI (logger , cppURI , cppDiag .Range )
1603
+ inoURI , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , cppDiag .Range )
1612
1604
if err != nil {
1613
1605
return nil , err
1614
1606
}
0 commit comments