@@ -306,7 +306,7 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
306306 defer ls .readUnlock (logger )
307307
308308 logger .Logf ("--> completion(%s)\n " , inoParams .TextDocument )
309- cppTextDocPositionParams , err := ls .ino2cppTextDocumentPositionParams (logger , inoParams .TextDocumentPositionParams )
309+ cppTextDocPositionParams , err := ls .ide2clangTextDocumentPositionParams (logger , inoParams .TextDocumentPositionParams )
310310 if err != nil {
311311 logger .Logf ("Error: %s" , err )
312312 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -350,25 +350,20 @@ func (ls *INOLanguageServer) TextDocumentCompletionReqFromIDE(ctx context.Contex
350350 return & inoResp , nil
351351}
352352
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 ) {
354354 ls .readLock (logger , true )
355355 defer ls .readUnlock (logger )
356356
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 )
362358 if err != nil {
363359 logger .Logf ("Error: %s" , err )
364360 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
365361 }
366362
367- logger .Logf (" --> hover(%s)\n " , cppTextDocPosition )
368- cppParams := & lsp.HoverParams {
369- TextDocumentPositionParams : cppTextDocPosition ,
363+ clangParams := & lsp.HoverParams {
364+ TextDocumentPositionParams : clangTextDocPosition ,
370365 }
371- clangResp , clangErr , err := ls .Clangd .conn .TextDocumentHover (ctx , cppParams )
366+ clangResp , clangErr , err := ls .Clangd .conn .TextDocumentHover (ctx , clangParams )
372367 if err != nil {
373368 logger .Logf ("clangd connectiono error: %v" , err )
374369 ls .Close ()
@@ -380,21 +375,26 @@ func (ls *INOLanguageServer) TextDocumentHoverReqFromIDE(ctx context.Context, lo
380375 }
381376
382377 if clangResp == nil {
378+ logger .Logf ("response: nil" )
383379 return nil , nil
384380 }
385381
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 ()}
395387 }
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 )
398398}
399399
400400func (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
404404 inoTextDocumentPosition := inoParams .TextDocumentPositionParams
405405
406406 logger .Logf ("%s" , inoTextDocumentPosition )
407- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
407+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
408408 if err != nil {
409409 logger .Logf ("Error: %s" , err )
410410 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -436,7 +436,7 @@ func (ls *INOLanguageServer) TextDocumentDefinitionReqFromIDE(ctx context.Contex
436436 inoTextDocPosition := p .TextDocumentPositionParams
437437
438438 logger .Logf ("%s" , inoTextDocPosition )
439- cppTextDocPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocPosition )
439+ cppTextDocPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocPosition )
440440 if err != nil {
441441 logger .Logf ("Error: %s" , err )
442442 return nil , nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -481,7 +481,7 @@ func (ls *INOLanguageServer) TextDocumentTypeDefinitionReqFromIDE(ctx context.Co
481481
482482 logger .Logf ("%s" , inoTextDocumentPosition )
483483 // inoURI := inoTextDocumentPosition.TextDocument.URI
484- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
484+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
485485 if err != nil {
486486 logger .Logf ("Error: %s" , err )
487487 return nil , nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -527,7 +527,7 @@ func (ls *INOLanguageServer) TextDocumentImplementationReqFromIDE(ctx context.Co
527527 inoTextDocumentPosition := inoParams .TextDocumentPositionParams
528528 logger .Logf ("%s" , inoTextDocumentPosition )
529529
530- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
530+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
531531 if err != nil {
532532 logger .Logf ("Error: %s" , err )
533533 return nil , nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -569,7 +569,7 @@ func (ls *INOLanguageServer) TextDocumentDocumentHighlightReqFromIDE(ctx context
569569 defer ls .readUnlock (logger )
570570
571571 inoTextDocumentPosition := inoParams .TextDocumentPositionParams
572- cppTextDocumentPosition , err := ls .ino2cppTextDocumentPositionParams (logger , inoTextDocumentPosition )
572+ cppTextDocumentPosition , err := ls .ide2clangTextDocumentPositionParams (logger , inoTextDocumentPosition )
573573 if err != nil {
574574 logger .Logf ("Error: %s" , err )
575575 return nil , & jsonrpc.ResponseError {Code : jsonrpc .ErrorCodesInternalError , Message : err .Error ()}
@@ -1235,7 +1235,7 @@ func (ls *INOLanguageServer) ino2cppDocumentURI(logger jsonrpc.FunctionLogger, i
12351235 return lsp .NilURI , err
12361236}
12371237
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 ) {
12391239 if inoPath == sourcemapper .NotIno .File {
12401240 return sourcemapper .NotInoURI , nil
12411241 }
@@ -1252,65 +1252,57 @@ func (ls *INOLanguageServer) inoDocumentURIFromInoPath(logger jsonrpc.FunctionLo
12521252 return doc .URI , nil
12531253}
12541254
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 )
12691261 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" )
12761265 }
12771266 } 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" )
12791268 err = nil
12801269 } else {
1281- logger .Logf (" URI: ERROR: %s" , err )
1270+ logger .Logf (" Range conversion ERROR: %s" , err )
12821271 ls .sketchMapper .DebugLogAll ()
12831272 return lsp .NilURI , lsp .NilRange , err
12841273 }
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
12871277 }
12881278
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 )
12901283 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 )
12921285 return lsp .NilURI , lsp .NilRange , err
12931286 }
12941287 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
12981291 }
12991292
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
13071298 }
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
13111303}
13121304
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 ) {
13141306 inoTextDocument := inoParams .TextDocument
13151307 inoPosition := inoParams .Position
13161308 inoURI := inoTextDocument .URI
@@ -1447,7 +1439,7 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
14471439
14481440 // ...otherwise convert edits to the sketch.ino.cpp into multilpe .ino edits
14491441 for _ , edit := range edits {
1450- inoURI , inoRange , err := ls .cpp2inoDocumentURI (logger , editURI , edit .Range )
1442+ inoURI , inoRange , err := ls .clang2IdeRangeAndDocumentURI (logger , editURI , edit .Range )
14511443 if err != nil {
14521444 logger .Logf (" error converting edit %s:%s: %s" , editURI , edit .Range , err )
14531445 continue
@@ -1468,15 +1460,15 @@ func (ls *INOLanguageServer) cpp2inoWorkspaceEdit(logger jsonrpc.FunctionLogger,
14681460}
14691461
14701462func (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 )
14721464 return lsp.Location {
14731465 URI : inoURI ,
14741466 Range : inoRange ,
14751467 }, err
14761468}
14771469
14781470func (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 )
14801472 if err != nil {
14811473 return lsp.DocumentHighlight {}, err
14821474 }
@@ -1510,7 +1502,7 @@ func (ls *INOLanguageServer) cpp2inoTextEdits(logger jsonrpc.FunctionLogger, cpp
15101502}
15111503
15121504func (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 )
15141506 inoEdit := cppEdit
15151507 inoEdit .Range = inoRange
15161508 return inoURI , inoEdit , err
@@ -1570,13 +1562,13 @@ func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, c
15701562 isSketch := cppURI .AsPath ().EquivalentTo (ls .buildSketchCpp )
15711563
15721564 if ! isSketch {
1573- inoURI , _ , err := ls .cpp2inoDocumentURI (logger , cppURI , lsp .NilRange )
1565+ inoURI , _ , err := ls .clang2IdeRangeAndDocumentURI (logger , cppURI , lsp .NilRange )
15741566 if err != nil {
15751567 return nil , err
15761568 }
15771569 inoDiags := []lsp.Diagnostic {}
15781570 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 )
15801572 if err != nil {
15811573 return nil , err
15821574 }
@@ -1608,7 +1600,7 @@ func (ls *INOLanguageServer) cpp2inoDiagnostics(logger jsonrpc.FunctionLogger, c
16081600 ls .inoDocsWithDiagnostics = map [lsp.DocumentURI ]bool {}
16091601
16101602 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 )
16121604 if err != nil {
16131605 return nil , err
16141606 }
0 commit comments