@@ -140,19 +140,24 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
140
140
return nil , err // do not propagate to clangd
141
141
}
142
142
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
+
143
151
case * lsp.DidChangeTextDocumentParams : // "textDocument/didChange":
144
152
uri = p .TextDocument .URI
145
153
err = handler .ino2cppDidChangeTextDocumentParams (ctx , p )
146
154
case * lsp.DidSaveTextDocumentParams : // "textDocument/didSave":
147
155
uri = p .TextDocument .URI
148
- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
156
+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
149
157
case * lsp.DidCloseTextDocumentParams : // "textDocument/didClose":
150
158
uri = p .TextDocument .URI
151
- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
159
+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
152
160
handler .deleteFileData (uri )
153
- case * lsp.CompletionParams : // "textDocument/completion":
154
- uri = p .TextDocument .URI
155
- err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
156
161
case * lsp.CodeActionParams : // "textDocument/codeAction":
157
162
uri = p .TextDocument .URI
158
163
err = handler .ino2cppCodeActionParams (p )
@@ -174,7 +179,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
174
179
err = handler .ino2cppTextDocumentPositionParams (& p .TextDocumentPositionParams )
175
180
case * lsp.DocumentFormattingParams : // "textDocument/formatting":
176
181
uri = p .TextDocument .URI
177
- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
182
+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
178
183
case * lsp.DocumentRangeFormattingParams : // "textDocument/rangeFormatting":
179
184
uri = p .TextDocument .URI
180
185
err = handler .ino2cppDocumentRangeFormattingParams (p )
@@ -183,7 +188,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
183
188
err = handler .ino2cppDocumentOnTypeFormattingParams (p )
184
189
case * lsp.DocumentSymbolParams : // "textDocument/documentSymbol":
185
190
uri = p .TextDocument .URI
186
- err = handler .ino2cppTextDocumentIdentifier (& p .TextDocument )
191
+ err = handler .sketchToBuildPathTextDocumentIdentifier (& p .TextDocument )
187
192
case * lsp.RenameParams : // "textDocument/rename":
188
193
uri = p .TextDocument .URI
189
194
err = handler .ino2cppRenameParams (p )
@@ -193,6 +198,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
193
198
err = handler .ino2cppExecuteCommand (p )
194
199
}
195
200
if err != nil {
201
+ log .Printf (" ~~~ %s" , err )
196
202
return nil , err
197
203
}
198
204
@@ -441,16 +447,36 @@ func (handler *InoHandler) handleError(ctx context.Context, err error) error {
441
447
return errors .New (message )
442
448
}
443
449
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 )
448
472
}
449
- return unknownURI (doc .URI )
473
+ log .Printf (" URI: '%s' -> '%s'" , docFile , newDocFile )
474
+ doc .URI = pathToURI (newDocFile .String ())
475
+ return nil
450
476
}
451
477
452
478
func (handler * InoHandler ) ino2cppDidChangeTextDocumentParams (ctx context.Context , params * lsp.DidChangeTextDocumentParams ) error {
453
- handler .ino2cppTextDocumentIdentifier (& params .TextDocument .TextDocumentIdentifier )
479
+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument .TextDocumentIdentifier )
454
480
if data , ok := handler .data [params .TextDocument .URI ]; ok {
455
481
for index := range params .ContentChanges {
456
482
err := handler .updateFileData (ctx , data , & params .ContentChanges [index ])
@@ -465,17 +491,21 @@ func (handler *InoHandler) ino2cppDidChangeTextDocumentParams(ctx context.Contex
465
491
}
466
492
467
493
func (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
473
502
}
474
- return unknownURI (params .TextDocument .URI )
503
+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
504
+ return nil
475
505
}
476
506
477
507
func (handler * InoHandler ) ino2cppCodeActionParams (params * lsp.CodeActionParams ) error {
478
- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
508
+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
479
509
if data , ok := handler .data [params .TextDocument .URI ]; ok {
480
510
params .Range = data .sourceMap .InoToCppLSPRange (data .sourceURI , params .Range )
481
511
for index := range params .Context .Diagnostics {
@@ -488,7 +518,7 @@ func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams)
488
518
}
489
519
490
520
func (handler * InoHandler ) ino2cppDocumentRangeFormattingParams (params * lsp.DocumentRangeFormattingParams ) error {
491
- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
521
+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
492
522
if data , ok := handler .data [params .TextDocument .URI ]; ok {
493
523
params .Range = data .sourceMap .InoToCppLSPRange (data .sourceURI , params .Range )
494
524
return nil
@@ -497,7 +527,7 @@ func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(params *lsp.Docu
497
527
}
498
528
499
529
func (handler * InoHandler ) ino2cppDocumentOnTypeFormattingParams (params * lsp.DocumentOnTypeFormattingParams ) error {
500
- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
530
+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
501
531
if data , ok := handler .data [params .TextDocument .URI ]; ok {
502
532
params .Position .Line = data .sourceMap .InoToCppLine (data .sourceURI , params .Position .Line )
503
533
return nil
@@ -506,7 +536,7 @@ func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.Doc
506
536
}
507
537
508
538
func (handler * InoHandler ) ino2cppRenameParams (params * lsp.RenameParams ) error {
509
- handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
539
+ handler .sketchToBuildPathTextDocumentIdentifier (& params .TextDocument )
510
540
if data , ok := handler .data [params .TextDocument .URI ]; ok {
511
541
params .Position .Line = data .sourceMap .InoToCppLine (data .sourceURI , params .Position .Line )
512
542
return nil
0 commit comments