@@ -195,7 +195,8 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
195
195
uri = p .TextDocument .URI
196
196
log .Printf ("--> codeAction(%s:%s)" , p .TextDocument .URI , p .Range .Start )
197
197
198
- if err := handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument ); err != nil {
198
+ p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
199
+ if err != nil {
199
200
break
200
201
}
201
202
if p .TextDocument .URI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
@@ -221,14 +222,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
221
222
uri = p .TextDocument .URI
222
223
log .Printf ("--> documentSymbol(%s)" , p .TextDocument .URI )
223
224
224
- err = handler .sketchToBuildPathTextDocumentIdentifier ( & p .TextDocument )
225
+ p . TextDocument , err = handler .ino2cppTextDocumentIdentifier ( p .TextDocument )
225
226
log .Printf (" --> documentSymbol(%s)" , p .TextDocument .URI )
226
227
227
228
case * lsp.DidSaveTextDocumentParams : // "textDocument/didSave":
228
229
log .Printf ("--X " + req .Method )
229
230
return nil , nil
230
231
uri = p .TextDocument .URI
231
- err = handler .sketchToBuildPathTextDocumentIdentifier ( & p .TextDocument )
232
+ p . TextDocument , err = handler .ino2cppTextDocumentIdentifier ( p .TextDocument )
232
233
case * lsp.DidCloseTextDocumentParams : // "textDocument/didClose":
233
234
log .Printf ("--X " + req .Method )
234
235
return nil , nil
@@ -257,7 +258,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
257
258
log .Printf ("--X " + req .Method )
258
259
return nil , nil
259
260
uri = p .TextDocument .URI
260
- err = handler .sketchToBuildPathTextDocumentIdentifier ( & p .TextDocument )
261
+ p . TextDocument , err = handler .ino2cppTextDocumentIdentifier ( p .TextDocument )
261
262
case * lsp.DocumentRangeFormattingParams : // "textDocument/rangeFormatting":
262
263
log .Printf ("--X " + req .Method )
263
264
return nil , nil
@@ -503,34 +504,48 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.
503
504
}
504
505
}
505
506
506
- func (handler * InoHandler ) didOpen (ctx context.Context , params * lsp.DidOpenTextDocumentParams ) (* lsp.DidOpenTextDocumentParams , error ) {
507
+ func (handler * InoHandler ) didOpen (ctx context.Context , inoDidOpen * lsp.DidOpenTextDocumentParams ) (* lsp.DidOpenTextDocumentParams , error ) {
507
508
// Add the TextDocumentItem in the tracked files list
508
- doc := params .TextDocument
509
- handler .docs [doc .URI ] = & doc
509
+ inoItem := inoDidOpen .TextDocument
510
+ handler .docs [inoItem .URI ] = & inoItem
510
511
511
512
// If we are tracking a .ino...
512
- if doc .URI .Ext () == ".ino" {
513
+ if inoItem .URI .Ext () == ".ino" {
513
514
handler .sketchTrackedFilesCount ++
514
515
log .Printf (" increasing .ino tracked files count: %d" , handler .sketchTrackedFilesCount )
515
516
516
- // ...notify clang that sketchCpp is no longer valid on disk
517
- if handler .sketchTrackedFilesCount == 1 {
518
- sketchCpp , err := handler .buildSketchCpp .ReadFile ()
519
- newParam := & lsp.DidOpenTextDocumentParams {
520
- TextDocument : lsp.TextDocumentItem {
521
- URI : lsp .NewDocumentURIFromPath (handler .buildSketchCpp ),
522
- Text : string (sketchCpp ),
523
- LanguageID : "cpp" ,
524
- Version : handler .buildSketchCppVersion ,
525
- },
526
- }
527
-
528
- // Trigger a documentSymbol load
529
- handler .buildSketchSymbolsLoad = true
530
- return newParam , err
517
+ // notify clang that sketchCpp has been opened only once
518
+ if handler .sketchTrackedFilesCount != 1 {
519
+ return nil , nil
531
520
}
521
+
522
+ // trigger a documentSymbol load
523
+ handler .buildSketchSymbolsLoad = true
532
524
}
533
- return nil , nil
525
+
526
+ cppItem , err := handler .ino2cppTextDocumentItem (inoItem )
527
+ return & lsp.DidOpenTextDocumentParams {
528
+ TextDocument : cppItem ,
529
+ }, err
530
+ }
531
+
532
+ func (handler * InoHandler ) ino2cppTextDocumentItem (inoItem lsp.TextDocumentItem ) (cppItem lsp.TextDocumentItem , err error ) {
533
+ cppURI , err := handler .ino2cppDocumentURI (inoItem .URI )
534
+ if err != nil {
535
+ return cppItem , err
536
+ }
537
+ cppItem .URI = cppURI
538
+
539
+ if cppURI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
540
+ cppItem .LanguageID = "cpp"
541
+ cppItem .Text = handler .sketchMapper .CppText .Text
542
+ cppItem .Version = handler .sketchMapper .CppText .Version
543
+ } else {
544
+ cppItem .Text = handler .docs [inoItem .URI ].Text
545
+ cppItem .Version = handler .docs [inoItem .URI ].Version
546
+ }
547
+
548
+ return cppItem , nil
534
549
}
535
550
536
551
func (handler * InoHandler ) didChange (ctx context.Context , req * lsp.DidChangeTextDocumentParams ) (* lsp.DidChangeTextDocumentParams , error ) {
@@ -592,11 +607,14 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
592
607
}
593
608
594
609
// If changes are applied to other files pass them by converting just the URI
610
+ cppDoc , err := handler .ino2cppVersionedTextDocumentIdentifier (req .TextDocument )
611
+ if err != nil {
612
+ return nil , err
613
+ }
595
614
cppReq := & lsp.DidChangeTextDocumentParams {
596
- TextDocument : req . TextDocument ,
615
+ TextDocument : cppDoc ,
597
616
ContentChanges : req .ContentChanges ,
598
617
}
599
- err := handler .sketchToBuildPathTextDocumentIdentifier (& cppReq .TextDocument .TextDocumentIdentifier )
600
618
return cppReq , err
601
619
}
602
620
@@ -635,32 +653,45 @@ func (handler *InoHandler) handleError(ctx context.Context, err error) error {
635
653
return errors .New (message )
636
654
}
637
655
638
- func (handler * InoHandler ) sketchToBuildPathTextDocumentIdentifier (doc * lsp.TextDocumentIdentifier ) error {
656
+ func (handler * InoHandler ) ino2cppVersionedTextDocumentIdentifier (doc lsp.VersionedTextDocumentIdentifier ) (lsp.VersionedTextDocumentIdentifier , error ) {
657
+ cppURI , err := handler .ino2cppDocumentURI (doc .URI )
658
+ res := doc
659
+ res .URI = cppURI
660
+ return res , err
661
+ }
662
+
663
+ func (handler * InoHandler ) ino2cppTextDocumentIdentifier (doc lsp.TextDocumentIdentifier ) (lsp.TextDocumentIdentifier , error ) {
664
+ cppURI , err := handler .ino2cppDocumentURI (doc .URI )
665
+ res := doc
666
+ res .URI = cppURI
667
+ return res , err
668
+ }
669
+
670
+ func (handler * InoHandler ) ino2cppDocumentURI (uri lsp.DocumentURI ) (lsp.DocumentURI , error ) {
639
671
// Sketchbook/Sketch/Sketch.ino -> build-path/sketch/Sketch.ino.cpp
640
672
// Sketchbook/Sketch/AnotherTab.ino -> build-path/sketch/Sketch.ino.cpp (different section from above)
641
673
// Sketchbook/Sketch/AnotherFile.cpp -> build-path/sketch/AnotherFile.cpp (1:1)
642
674
// another/path/source.cpp -> unchanged
643
675
644
676
// Convert sketch path to build path
645
- docFile := doc . URI .AsPath ()
646
- newDocFile := docFile
647
-
648
- if docFile .Ext () == ".ino" {
649
- newDocFile = handler .buildSketchCpp
650
- } else if inside , err := docFile .IsInsideDir (handler .sketchRoot ); err != nil {
651
- log .Printf (" could not determine if '%s' is inside '%s'" , docFile , handler .sketchRoot )
652
- return unknownURI (doc . URI )
677
+ inoPath := uri .AsPath ()
678
+ cppPath := inoPath
679
+
680
+ if inoPath .Ext () == ".ino" {
681
+ cppPath = handler .buildSketchCpp
682
+ } else if inside , err := inoPath .IsInsideDir (handler .sketchRoot ); err != nil {
683
+ log .Printf (" could not determine if '%s' is inside '%s'" , inoPath , handler .sketchRoot )
684
+ return "" , unknownURI (uri )
653
685
} else if ! inside {
654
- log .Printf (" passing doc identifier to '%s' as-is" , docFile )
655
- } else if rel , err := handler .sketchRoot .RelTo (docFile ); err != nil {
656
- log .Printf (" could not determine rel-path of '%s' in '%s" , docFile , handler .sketchRoot )
657
- return unknownURI (doc . URI )
686
+ log .Printf (" passing doc identifier to '%s' as-is" , inoPath )
687
+ } else if rel , err := handler .sketchRoot .RelTo (inoPath ); err != nil {
688
+ log .Printf (" could not determine rel-path of '%s' in '%s" , inoPath , handler .sketchRoot )
689
+ return "" , unknownURI (uri )
658
690
} else {
659
- newDocFile = handler .buildSketchRoot .JoinPath (rel )
691
+ cppPath = handler .buildSketchRoot .JoinPath (rel )
660
692
}
661
- log .Printf (" URI: '%s' -> '%s'" , docFile , newDocFile )
662
- doc .URI = lsp .NewDocumentURIFromPath (newDocFile )
663
- return nil
693
+ log .Printf (" URI: '%s' -> '%s'" , inoPath , cppPath )
694
+ return lsp .NewDocumentURIFromPath (cppPath ), nil
664
695
}
665
696
666
697
func (handler * InoHandler ) ino2cppTextDocumentPositionParams (params * lsp.TextDocumentPositionParams ) error {
@@ -673,7 +704,11 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(params *lsp.TextDoc
673
704
}
674
705
params .Position .Line = line
675
706
}
676
- handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
707
+ cppDoc , err := handler .ino2cppTextDocumentIdentifier (params .TextDocument )
708
+ if err != nil {
709
+ return err
710
+ }
711
+ params .TextDocument = cppDoc
677
712
return nil
678
713
}
679
714
0 commit comments