@@ -195,7 +195,8 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
195195 uri = p .TextDocument .URI
196196 log .Printf ("--> codeAction(%s:%s)" , p .TextDocument .URI , p .Range .Start )
197197
198- if err := handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument ); err != nil {
198+ p .TextDocument , err = handler .ino2cppTextDocumentIdentifier (p .TextDocument )
199+ if err != nil {
199200 break
200201 }
201202 if p .TextDocument .URI .AsPath ().EquivalentTo (handler .buildSketchCpp ) {
@@ -221,14 +222,14 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
221222 uri = p .TextDocument .URI
222223 log .Printf ("--> documentSymbol(%s)" , p .TextDocument .URI )
223224
224- err = handler .sketchToBuildPathTextDocumentIdentifier ( & p .TextDocument )
225+ p . TextDocument , err = handler .ino2cppTextDocumentIdentifier ( p .TextDocument )
225226 log .Printf (" --> documentSymbol(%s)" , p .TextDocument .URI )
226227
227228 case * lsp.DidSaveTextDocumentParams : // "textDocument/didSave":
228229 log .Printf ("--X " + req .Method )
229230 return nil , nil
230231 uri = p .TextDocument .URI
231- err = handler .sketchToBuildPathTextDocumentIdentifier ( & p .TextDocument )
232+ p . TextDocument , err = handler .ino2cppTextDocumentIdentifier ( p .TextDocument )
232233 case * lsp.DidCloseTextDocumentParams : // "textDocument/didClose":
233234 log .Printf ("--X " + req .Method )
234235 return nil , nil
@@ -257,7 +258,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
257258 log .Printf ("--X " + req .Method )
258259 return nil , nil
259260 uri = p .TextDocument .URI
260- err = handler .sketchToBuildPathTextDocumentIdentifier ( & p .TextDocument )
261+ p . TextDocument , err = handler .ino2cppTextDocumentIdentifier ( p .TextDocument )
261262 case * lsp.DocumentRangeFormattingParams : // "textDocument/rangeFormatting":
262263 log .Printf ("--X " + req .Method )
263264 return nil , nil
@@ -503,34 +504,48 @@ func startClangd(compileCommandsDir, sketchCpp *paths.Path) (io.WriteCloser, io.
503504 }
504505}
505506
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 ) {
507508 // 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
510511
511512 // If we are tracking a .ino...
512- if doc .URI .Ext () == ".ino" {
513+ if inoItem .URI .Ext () == ".ino" {
513514 handler .sketchTrackedFilesCount ++
514515 log .Printf (" increasing .ino tracked files count: %d" , handler .sketchTrackedFilesCount )
515516
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
531520 }
521+
522+ // trigger a documentSymbol load
523+ handler .buildSketchSymbolsLoad = true
532524 }
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
534549}
535550
536551func (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
592607 }
593608
594609 // 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+ }
595614 cppReq := & lsp.DidChangeTextDocumentParams {
596- TextDocument : req . TextDocument ,
615+ TextDocument : cppDoc ,
597616 ContentChanges : req .ContentChanges ,
598617 }
599- err := handler .sketchToBuildPathTextDocumentIdentifier (& cppReq .TextDocument .TextDocumentIdentifier )
600618 return cppReq , err
601619}
602620
@@ -635,32 +653,45 @@ func (handler *InoHandler) handleError(ctx context.Context, err error) error {
635653 return errors .New (message )
636654}
637655
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 ) {
639671 // Sketchbook/Sketch/Sketch.ino -> build-path/sketch/Sketch.ino.cpp
640672 // Sketchbook/Sketch/AnotherTab.ino -> build-path/sketch/Sketch.ino.cpp (different section from above)
641673 // Sketchbook/Sketch/AnotherFile.cpp -> build-path/sketch/AnotherFile.cpp (1:1)
642674 // another/path/source.cpp -> unchanged
643675
644676 // 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 )
653685 } 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 )
658690 } else {
659- newDocFile = handler .buildSketchRoot .JoinPath (rel )
691+ cppPath = handler .buildSketchRoot .JoinPath (rel )
660692 }
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
664695}
665696
666697func (handler * InoHandler ) ino2cppTextDocumentPositionParams (params * lsp.TextDocumentPositionParams ) error {
@@ -673,7 +704,11 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(params *lsp.TextDoc
673704 }
674705 params .Position .Line = line
675706 }
676- handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
707+ cppDoc , err := handler .ino2cppTextDocumentIdentifier (params .TextDocument )
708+ if err != nil {
709+ return err
710+ }
711+ params .TextDocument = cppDoc
677712 return nil
678713}
679714
0 commit comments