@@ -140,19 +140,24 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
140140 return nil , err // do not propagate to clangd
141141 }
142142 params = res
143+
144+ case * lsp.CompletionParams : // "textDocument/completion":
145+ uri = p .TextDocument .URI
146+ log .Printf ("--> completion(%s:%d:%d)\n " , p .TextDocument .URI , p .Position .Line , p .Position .Character )
147+
148+ err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
149+ log .Printf (" --> completion(%s:%d:%d)\n " , p .TextDocument .URI , p .Position .Line , p .Position .Character )
150+
143151 case * lsp.DidChangeTextDocumentParams : // "textDocument/didChange":
144152 uri = p .TextDocument .URI
145153 err = handler .ino2cppDidChangeTextDocumentParams (ctx , p )
146154 case * lsp.DidSaveTextDocumentParams : // "textDocument/didSave":
147155 uri = p .TextDocument .URI
148- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
156+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
149157 case * lsp.DidCloseTextDocumentParams : // "textDocument/didClose":
150158 uri = p .TextDocument .URI
151- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
159+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
152160 handler .deleteFileData (uri )
153- case * lsp.CompletionParams : // "textDocument/completion":
154- uri = p .TextDocument .URI
155- err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
156161 case * lsp.CodeActionParams : // "textDocument/codeAction":
157162 uri = p .TextDocument .URI
158163 err = handler .ino2cppCodeActionParams (p )
@@ -174,7 +179,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
174179 err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
175180 case * lsp.DocumentFormattingParams : // "textDocument/formatting":
176181 uri = p .TextDocument .URI
177- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
182+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
178183 case * lsp.DocumentRangeFormattingParams : // "textDocument/rangeFormatting":
179184 uri = p .TextDocument .URI
180185 err = handler .ino2cppDocumentRangeFormattingParams (p )
@@ -183,7 +188,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
183188 err = handler .ino2cppDocumentOnTypeFormattingParams (p )
184189 case * lsp.DocumentSymbolParams : // "textDocument/documentSymbol":
185190 uri = p .TextDocument .URI
186- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
191+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
187192 case * lsp.RenameParams : // "textDocument/rename":
188193 uri = p .TextDocument .URI
189194 err = handler .ino2cppRenameParams (p )
@@ -193,6 +198,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
193198 err = handler .ino2cppExecuteCommand (p )
194199 }
195200 if err != nil {
201+ log .Printf (" ~~~ %s" , err )
196202 return nil , err
197203 }
198204
@@ -441,16 +447,36 @@ func (handler *InoHandler) handleError(ctx context.Context, err error) error {
441447 return errors .New (message )
442448}
443449
444- func (handler * InoHandler ) ino2cppTextDocumentIdentifier (doc * lsp.TextDocumentIdentifier ) error {
445- if data , ok := handler .data [doc .URI ]; ok {
446- doc .URI = data .targetURI
447- return nil
450+ func (handler * InoHandler ) sketchToBuildPathTextDocumentIdentifier (doc * lsp.TextDocumentIdentifier ) error {
451+ // Sketchbook/Sketch/Sketch.ino -> build-path/sketch/Sketch.ino.cpp
452+ // Sketchbook/Sketch/AnotherTab.ino -> build-path/sketch/Sketch.ino.cpp (different section from above)
453+ // Sketchbook/Sketch/AnotherFile.cpp -> build-path/sketch/AnotherFile.cpp (1:1)
454+ // another/path/source.cpp -> unchanged
455+
456+ // Convert sketch path to build path
457+ docFile := newPathFromURI (doc .URI )
458+ newDocFile := docFile
459+
460+ if docFile .Ext () == ".ino" {
461+ newDocFile = handler .buildSketchCpp
462+ } else if inside , err := docFile .IsInsideDir (handler .sketchRoot ); err != nil {
463+ log .Printf (" could not determine if '%s' is inside '%s'" , docFile , handler .sketchRoot )
464+ return unknownURI (doc .URI )
465+ } else if ! inside {
466+ log .Printf (" passing doc identifier to '%s' as-is" , docFile )
467+ } else if rel , err := handler .sketchRoot .RelTo (docFile ); err != nil {
468+ log .Printf (" could not determine rel-path of '%s' in '%s" , docFile , handler .sketchRoot )
469+ return unknownURI (doc .URI )
470+ } else {
471+ newDocFile = handler .buildSketchRoot .JoinPath (rel )
448472 }
449- return unknownURI (doc .URI )
473+ log .Printf (" URI: '%s' -> '%s'" , docFile , newDocFile )
474+ doc .URI = pathToURI (newDocFile .String ())
475+ return nil
450476}
451477
452478func (handler * InoHandler ) ino2cppDidChangeTextDocumentParams (ctx context.Context , params * lsp.DidChangeTextDocumentParams ) error {
453- handler .ino2cppTextDocumentIdentifier (& params .TextDocument .TextDocumentIdentifier )
479+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument .TextDocumentIdentifier )
454480 if data , ok := handler .data [params .TextDocument .URI ]; ok {
455481 for index := range params .ContentChanges {
456482 err := handler .updateFileData (ctx , data , & params .ContentChanges [index ])
@@ -465,17 +491,21 @@ func (handler *InoHandler) ino2cppDidChangeTextDocumentParams(ctx context.Contex
465491}
466492
467493func (handler * InoHandler ) ino2cppTextDocumentPositionParams (params * lsp.TextDocumentPositionParams ) error {
468- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
469- if data , ok := handler .data [params .TextDocument .URI ]; ok {
470- targetLine := data .sourceMap .InoToCppLine (data .sourceURI , params .Position .Line )
471- params .Position .Line = targetLine
472- return nil
494+ sourceURI := params .TextDocument .URI
495+ if strings .HasSuffix (string (sourceURI ), ".ino" ) {
496+ line , ok := handler .sketchMapper .InoToCppLineOk (sourceURI , params .Position .Line )
497+ if ! ok {
498+ log .Printf (" invalid line requested: %s:%d" , sourceURI , params .Position .Line )
499+ return unknownURI (params .TextDocument .URI )
500+ }
501+ params .Position .Line = line
473502 }
474- return unknownURI (params .TextDocument .URI )
503+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
504+ return nil
475505}
476506
477507func (handler * InoHandler ) ino2cppCodeActionParams (params * lsp.CodeActionParams ) error {
478- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
508+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
479509 if data , ok := handler .data [params .TextDocument .URI ]; ok {
480510 params .Range = data .sourceMap .InoToCppLSPRange (data .sourceURI , params .Range )
481511 for index := range params .Context .Diagnostics {
@@ -488,7 +518,7 @@ func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams)
488518}
489519
490520func (handler * InoHandler ) ino2cppDocumentRangeFormattingParams (params * lsp.DocumentRangeFormattingParams ) error {
491- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
521+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
492522 if data , ok := handler .data [params .TextDocument .URI ]; ok {
493523 params .Range = data .sourceMap .InoToCppLSPRange (data .sourceURI , params .Range )
494524 return nil
@@ -497,7 +527,7 @@ func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(params *lsp.Docu
497527}
498528
499529func (handler * InoHandler ) ino2cppDocumentOnTypeFormattingParams (params * lsp.DocumentOnTypeFormattingParams ) error {
500- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
530+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
501531 if data , ok := handler .data [params .TextDocument .URI ]; ok {
502532 params .Position .Line = data .sourceMap .InoToCppLine (data .sourceURI , params .Position .Line )
503533 return nil
@@ -506,7 +536,7 @@ func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.Doc
506536}
507537
508538func (handler * InoHandler ) ino2cppRenameParams (params * lsp.RenameParams ) error {
509- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
539+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
510540 if data , ok := handler .data [params .TextDocument .URI ]; ok {
511541 params .Position .Line = data .sourceMap .InoToCppLine (data .sourceURI , params .Position .Line )
512542 return nil
0 commit comments