@@ -12,6 +12,8 @@ import (
12
12
"strings"
13
13
"time"
14
14
15
+ "github.com/bcmi-labs/arduino-language-server/handler/sourcemapper"
16
+ "github.com/bcmi-labs/arduino-language-server/handler/textutils"
15
17
"github.com/pkg/errors"
16
18
lsp "github.com/sourcegraph/go-lsp"
17
19
"github.com/sourcegraph/jsonrpc2"
@@ -70,12 +72,11 @@ type InoHandler struct {
70
72
71
73
// FileData gathers information on a .ino source file.
72
74
type FileData struct {
73
- sourceText string
74
- sourceURI lsp.DocumentURI
75
- targetURI lsp.DocumentURI
76
- sourceLineMap map [int ]int
77
- targetLineMap map [int ]int
78
- version int
75
+ sourceText string
76
+ sourceURI lsp.DocumentURI
77
+ targetURI lsp.DocumentURI
78
+ sourceMap * sourcemapper.InoMapper
79
+ version int
79
80
}
80
81
81
82
// StopClangd closes the connection to the clangd process.
@@ -240,13 +241,11 @@ func (handler *InoHandler) createFileData(ctx context.Context, sourceURI lsp.Doc
240
241
}
241
242
242
243
targetURI := pathToURI (targetPath )
243
- sourceLineMap , targetLineMap := createSourceMaps (bytes .NewReader (targetBytes ))
244
244
data := & FileData {
245
245
sourceText ,
246
246
sourceURI ,
247
247
targetURI ,
248
- sourceLineMap ,
249
- targetLineMap ,
248
+ sourcemapper .CreateInoMapper (bytes .NewReader (targetBytes )),
250
249
version ,
251
250
}
252
251
handler .data [sourceURI ] = data
@@ -262,7 +261,7 @@ func (handler *InoHandler) updateFileData(ctx context.Context, data *FileData, c
262
261
if rang == nil {
263
262
newSourceText = change .Text
264
263
} else {
265
- newSourceText , err = applyTextChange (data .sourceText , * rang , change .Text )
264
+ newSourceText , err = textutils . ApplyTextChange (data .sourceText , * rang , change .Text )
266
265
if err != nil {
267
266
return err
268
267
}
@@ -277,35 +276,28 @@ func (handler *InoHandler) updateFileData(ctx context.Context, data *FileData, c
277
276
}
278
277
} else {
279
278
// Fallback: try to apply a multi-line update
280
- targetStartLine := data .targetLineMap [rang .Start .Line ]
281
- targetEndLine := data .targetLineMap [rang .End .Line ]
282
279
data .sourceText = newSourceText
283
- updateSourceMaps (data .sourceLineMap , data .targetLineMap , rang .End .Line - rang .Start .Line , rang .Start .Line , change .Text )
284
- rang .Start .Line = targetStartLine
285
- rang .End .Line = targetEndLine
280
+ data .sourceMap .Update (rang .End .Line - rang .Start .Line , rang .Start .Line , change .Text )
281
+ * rang = data .sourceMap .InoToCppRange (* rang )
286
282
return nil
287
283
}
288
284
}
289
285
290
- sourceLineMap , targetLineMap := createSourceMaps (bytes .NewReader (targetBytes ))
291
286
data .sourceText = newSourceText
292
- data .sourceLineMap = sourceLineMap
293
- data .targetLineMap = targetLineMap
287
+ data .sourceMap = sourcemapper .CreateInoMapper (bytes .NewReader (targetBytes ))
294
288
295
289
change .Text = string (targetBytes )
296
290
change .Range = nil
297
291
change .RangeLength = 0
298
292
} else {
299
293
// Apply an update to a single line both to the source and the target text
300
- targetLine := data .targetLineMap [rang .Start .Line ]
301
- data .sourceText , err = applyTextChange (data .sourceText , * rang , change .Text )
294
+ data .sourceText , err = textutils .ApplyTextChange (data .sourceText , * rang , change .Text )
302
295
if err != nil {
303
296
return err
304
297
}
305
- updateSourceMaps ( data .sourceLineMap , data . targetLineMap , 0 , rang .Start .Line , change .Text )
298
+ data .sourceMap . Update ( 0 , rang .Start .Line , change .Text )
306
299
307
- rang .Start .Line = targetLine
308
- rang .End .Line = targetLine
300
+ * rang = data .sourceMap .InoToCppRange (* rang )
309
301
}
310
302
return nil
311
303
}
@@ -391,7 +383,7 @@ func (handler *InoHandler) ino2cppDidChangeTextDocumentParams(ctx context.Contex
391
383
func (handler * InoHandler ) ino2cppTextDocumentPositionParams (params * lsp.TextDocumentPositionParams ) error {
392
384
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
393
385
if data , ok := handler .data [params .TextDocument .URI ]; ok {
394
- targetLine := data .targetLineMap [ params .Position .Line ]
386
+ targetLine := data .sourceMap . InoToCppLine ( params .Position .Line )
395
387
params .Position .Line = targetLine
396
388
return nil
397
389
}
@@ -401,12 +393,10 @@ func (handler *InoHandler) ino2cppTextDocumentPositionParams(params *lsp.TextDoc
401
393
func (handler * InoHandler ) ino2cppCodeActionParams (params * lsp.CodeActionParams ) error {
402
394
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
403
395
if data , ok := handler .data [params .TextDocument .URI ]; ok {
404
- params .Range .Start .Line = data .targetLineMap [params .Range .Start .Line ]
405
- params .Range .End .Line = data .targetLineMap [params .Range .End .Line ]
396
+ params .Range = data .sourceMap .InoToCppRange (params .Range )
406
397
for index := range params .Context .Diagnostics {
407
398
r := & params .Context .Diagnostics [index ].Range
408
- r .Start .Line = data .targetLineMap [r .Start .Line ]
409
- r .End .Line = data .targetLineMap [r .End .Line ]
399
+ * r = data .sourceMap .InoToCppRange (* r )
410
400
}
411
401
return nil
412
402
}
@@ -416,8 +406,7 @@ func (handler *InoHandler) ino2cppCodeActionParams(params *lsp.CodeActionParams)
416
406
func (handler * InoHandler ) ino2cppDocumentRangeFormattingParams (params * lsp.DocumentRangeFormattingParams ) error {
417
407
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
418
408
if data , ok := handler .data [params .TextDocument .URI ]; ok {
419
- params .Range .Start .Line = data .targetLineMap [params .Range .Start .Line ]
420
- params .Range .End .Line = data .targetLineMap [params .Range .End .Line ]
409
+ params .Range = data .sourceMap .InoToCppRange (params .Range )
421
410
return nil
422
411
}
423
412
return unknownURI (params .TextDocument .URI )
@@ -426,7 +415,7 @@ func (handler *InoHandler) ino2cppDocumentRangeFormattingParams(params *lsp.Docu
426
415
func (handler * InoHandler ) ino2cppDocumentOnTypeFormattingParams (params * lsp.DocumentOnTypeFormattingParams ) error {
427
416
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
428
417
if data , ok := handler .data [params .TextDocument .URI ]; ok {
429
- params .Position .Line = data .targetLineMap [ params .Position .Line ]
418
+ params .Position .Line = data .sourceMap . InoToCppLine ( params .Position .Line )
430
419
return nil
431
420
}
432
421
return unknownURI (params .TextDocument .URI )
@@ -435,7 +424,7 @@ func (handler *InoHandler) ino2cppDocumentOnTypeFormattingParams(params *lsp.Doc
435
424
func (handler * InoHandler ) ino2cppRenameParams (params * lsp.RenameParams ) error {
436
425
handler .ino2cppTextDocumentIdentifier (& params .TextDocument )
437
426
if data , ok := handler .data [params .TextDocument .URI ]; ok {
438
- params .Position .Line = data .targetLineMap [ params .Position .Line ]
427
+ params .Position .Line = data .sourceMap . InoToCppLine ( params .Position .Line )
439
428
return nil
440
429
}
441
430
return unknownURI (params .TextDocument .URI )
@@ -467,13 +456,9 @@ func (handler *InoHandler) ino2cppWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *ls
467
456
if data , ok := handler .data [lsp .DocumentURI (uri )]; ok {
468
457
newValue := make ([]lsp.TextEdit , len (edit ))
469
458
for index := range edit {
470
- r := edit [index ].Range
471
459
newValue [index ] = lsp.TextEdit {
472
460
NewText : edit [index ].NewText ,
473
- Range : lsp.Range {
474
- Start : lsp.Position {Line : data .targetLineMap [r .Start .Line ], Character : r .Start .Character },
475
- End : lsp.Position {Line : data .targetLineMap [r .End .Line ], Character : r .End .Character },
476
- },
461
+ Range : data .sourceMap .InoToCppRange (edit [index ].Range ),
477
462
}
478
463
}
479
464
newEdit .Changes [string (data .targetURI )] = newValue
@@ -577,9 +562,7 @@ func (handler *InoHandler) cpp2inoCompletionList(list *lsp.CompletionList, uri l
577
562
for _ , item := range list .Items {
578
563
if ! strings .HasPrefix (item .InsertText , "_" ) {
579
564
if item .TextEdit != nil {
580
- r := & item .TextEdit .Range
581
- r .Start .Line = data .sourceLineMap [r .Start .Line ]
582
- r .End .Line = data .sourceLineMap [r .End .Line ]
565
+ item .TextEdit .Range = data .sourceMap .CppToInoRange (item .TextEdit .Range )
583
566
}
584
567
newItems = append (newItems , item )
585
568
}
@@ -592,9 +575,7 @@ func (handler *InoHandler) cpp2inoCodeAction(codeAction *CodeAction, uri lsp.Doc
592
575
codeAction .Edit = handler .cpp2inoWorkspaceEdit (codeAction .Edit )
593
576
if data , ok := handler .data [uri ]; ok {
594
577
for index := range codeAction .Diagnostics {
595
- r := & codeAction .Diagnostics [index ].Range
596
- r .Start .Line = data .sourceLineMap [r .Start .Line ]
597
- r .End .Line = data .sourceLineMap [r .End .Line ]
578
+ codeAction .Diagnostics [index ].Range = data .sourceMap .CppToInoRange (codeAction .Diagnostics [index ].Range )
598
579
}
599
580
}
600
581
}
@@ -614,13 +595,9 @@ func (handler *InoHandler) cpp2inoWorkspaceEdit(origEdit *lsp.WorkspaceEdit) *ls
614
595
if data , ok := handler .data [lsp .DocumentURI (uri )]; ok {
615
596
newValue := make ([]lsp.TextEdit , len (edit ))
616
597
for index := range edit {
617
- r := edit [index ].Range
618
598
newValue [index ] = lsp.TextEdit {
619
599
NewText : edit [index ].NewText ,
620
- Range : lsp.Range {
621
- Start : lsp.Position {Line : data .sourceLineMap [r .Start .Line ], Character : r .Start .Character },
622
- End : lsp.Position {Line : data .sourceLineMap [r .End .Line ], Character : r .End .Character },
623
- },
600
+ Range : data .sourceMap .CppToInoRange (edit [index ].Range ),
624
601
}
625
602
}
626
603
newEdit .Changes [string (data .sourceURI )] = newValue
@@ -635,31 +612,27 @@ func (handler *InoHandler) cpp2inoHover(hover *Hover, uri lsp.DocumentURI) {
635
612
if data , ok := handler .data [uri ]; ok {
636
613
r := hover .Range
637
614
if r != nil {
638
- r .Start .Line = data .sourceLineMap [r .Start .Line ]
639
- r .End .Line = data .sourceLineMap [r .End .Line ]
615
+ * r = data .sourceMap .CppToInoRange (* r )
640
616
}
641
617
}
642
618
}
643
619
644
620
func (handler * InoHandler ) cpp2inoLocation (location * lsp.Location ) {
645
621
if data , ok := handler .data [location .URI ]; ok {
646
622
location .URI = data .sourceURI
647
- location .Range .Start .Line = data .sourceLineMap [location .Range .Start .Line ]
648
- location .Range .End .Line = data .sourceLineMap [location .Range .End .Line ]
623
+ location .Range = data .sourceMap .CppToInoRange (location .Range )
649
624
}
650
625
}
651
626
652
627
func (handler * InoHandler ) cpp2inoDocumentHighlight (highlight * lsp.DocumentHighlight , uri lsp.DocumentURI ) {
653
628
if data , ok := handler .data [uri ]; ok {
654
- highlight .Range .Start .Line = data .sourceLineMap [highlight .Range .Start .Line ]
655
- highlight .Range .End .Line = data .sourceLineMap [highlight .Range .End .Line ]
629
+ highlight .Range = data .sourceMap .CppToInoRange (highlight .Range )
656
630
}
657
631
}
658
632
659
633
func (handler * InoHandler ) cpp2inoTextEdit (edit * lsp.TextEdit , uri lsp.DocumentURI ) {
660
634
if data , ok := handler .data [uri ]; ok {
661
- edit .Range .Start .Line = data .sourceLineMap [edit .Range .Start .Line ]
662
- edit .Range .End .Line = data .sourceLineMap [edit .Range .End .Line ]
635
+ edit .Range = data .sourceMap .CppToInoRange (edit .Range )
663
636
}
664
637
}
665
638
@@ -672,8 +645,7 @@ func (handler *InoHandler) cpp2inoDocumentSymbols(origSymbols []DocumentSymbol,
672
645
symbolIdx := make (map [string ]* DocumentSymbol )
673
646
for i := 0 ; i < len (origSymbols ); i ++ {
674
647
symbol := & origSymbols [i ]
675
- symbol .Range .Start .Line = data .sourceLineMap [symbol .Range .Start .Line ]
676
- symbol .Range .End .Line = data .sourceLineMap [symbol .Range .End .Line ]
648
+ symbol .Range = data .sourceMap .CppToInoRange (symbol .Range )
677
649
678
650
duplicate := false
679
651
other , duplicate := symbolIdx [symbol .Name ]
@@ -686,8 +658,7 @@ func (handler *InoHandler) cpp2inoDocumentSymbols(origSymbols []DocumentSymbol,
686
658
}
687
659
}
688
660
689
- symbol .SelectionRange .Start .Line = data .sourceLineMap [symbol .SelectionRange .Start .Line ]
690
- symbol .SelectionRange .End .Line = data .sourceLineMap [symbol .SelectionRange .End .Line ]
661
+ symbol .SelectionRange = data .sourceMap .CppToInoRange (symbol .SelectionRange )
691
662
symbol .Children = handler .cpp2inoDocumentSymbols (symbol .Children , uri )
692
663
symbolIdx [symbol .Name ] = symbol
693
664
}
@@ -776,9 +747,9 @@ func (handler *InoHandler) cpp2inoPublishDiagnosticsParams(params *lsp.PublishDi
776
747
newDiagnostics := make ([]lsp.Diagnostic , 0 , len (params .Diagnostics ))
777
748
for index := range params .Diagnostics {
778
749
r := & params .Diagnostics [index ].Range
779
- if startLine , ok := data .sourceLineMap [ r .Start .Line ] ; ok {
750
+ if startLine , ok := data .sourceMap . CppToInoLineOk ( r .Start .Line ) ; ok {
780
751
r .Start .Line = startLine
781
- r .End .Line = data .sourceLineMap [ r .End .Line ]
752
+ r .End .Line = data .sourceMap . CppToInoLine ( r .End .Line )
782
753
newDiagnostics = append (newDiagnostics , params .Diagnostics [index ])
783
754
}
784
755
}
0 commit comments