Skip to content

Commit 8a67a18

Browse files
committed
didChange now keeps track of changes in the text
1 parent 7943ae1 commit 8a67a18

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

Diff for: handler/handler.go

+16-14
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,16 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
150150
log.Printf(" > %s -> '%s'", change.Range, strconv.Quote(change.Text))
151151
}
152152

153-
res, err := handler.didChange(ctx, p)
154-
if err != nil {
153+
if res, err := handler.didChange(ctx, p); err != nil {
155154
log.Printf(" --E error: %s", err)
156155
return nil, err
157-
}
158-
if res == nil {
156+
} else if res == nil {
159157
log.Println(" --X notification is not propagated to clangd")
160158
return nil, err // do not propagate to clangd
159+
} else {
160+
p = res
161161
}
162162

163-
p = res
164163
log.Printf(" --> didChange(%s@%d)", p.TextDocument.URI, p.TextDocument.Version)
165164
for _, change := range p.ContentChanges {
166165
log.Printf(" > %s -> '%s'", change.Range, strconv.Quote(change.Text))
@@ -432,11 +431,13 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
432431
if trackedDoc.Version+1 != doc.Version {
433432
return nil, errors.Errorf("document out-of-sync: expected version %d but got %d", trackedDoc.Version+1, doc.Version)
434433
}
435-
trackedDoc.Version++
434+
for _, change := range req.ContentChanges {
435+
textutils.ApplyLSPTextDocumentContentChangeEvent(trackedDoc, &change)
436+
}
436437

438+
// If changes are applied to a .ino file we increment the global .ino.cpp versioning
439+
// for each increment of the single .ino file.
437440
if doc.URI.AsPath().Ext() == ".ino" {
438-
// If changes are applied to a .ino file we increment the global .ino.cpp versioning
439-
// for each increment of the single .ino file.
440441

441442
cppChanges := []lsp.TextDocumentContentChangeEvent{}
442443
for _, inoChange := range req.ContentChanges {
@@ -475,14 +476,15 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
475476
},
476477
}
477478
return cppReq, nil
478-
} else {
479-
480-
// TODO
481-
return nil, unknownURI(doc.URI)
482-
483479
}
484480

485-
return nil, unknownURI(doc.URI)
481+
// If changes are applied to other files pass them by converting just the URI
482+
cppReq := &lsp.DidChangeTextDocumentParams{
483+
TextDocument: req.TextDocument,
484+
ContentChanges: req.ContentChanges,
485+
}
486+
err := handler.sketchToBuildPathTextDocumentIdentifier(&cppReq.TextDocument.TextDocumentIdentifier)
487+
return cppReq, err
486488
}
487489

488490
func (handler *InoHandler) updateFileData(ctx context.Context, data *FileData, change *lsp.TextDocumentContentChangeEvent) (err error) {

Diff for: handler/textutils/textutils.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import (
66
"github.com/bcmi-labs/arduino-language-server/lsp"
77
)
88

9+
// ApplyLSPTextDocumentContentChangeEvent applies the LSP change in the given text
10+
func ApplyLSPTextDocumentContentChangeEvent(textDoc *lsp.TextDocumentItem, change *lsp.TextDocumentContentChangeEvent) error {
11+
newText, err := ApplyTextChange(textDoc.Text, *change.Range, change.Text)
12+
if err != nil {
13+
return err
14+
}
15+
textDoc.Text = newText
16+
textDoc.Version++
17+
return nil
18+
}
19+
920
// ApplyTextChange replaces startingText substring specified by replaceRange with insertText
1021
func ApplyTextChange(startingText string, replaceRange lsp.Range, insertText string) (res string, err error) {
1122
start, err := getOffset(startingText, replaceRange.Start)

0 commit comments

Comments
 (0)