Skip to content

Commit 796a463

Browse files
committed
Fixed text synchronization issues
Text versioning is not linear with the number of changes: we just use the version number passed by the client and hope for the best.
1 parent e5c82d6 commit 796a463

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

Diff for: handler/handler.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
162162
uri = p.TextDocument.URI
163163
log.Printf("--> didChange(%s@%d)", p.TextDocument.URI, p.TextDocument.Version)
164164
for _, change := range p.ContentChanges {
165-
log.Printf(" > %s -> '%s'", change.Range, strconv.Quote(change.Text))
165+
log.Printf(" > %s -> %s", change.Range, strconv.Quote(change.Text))
166166
}
167167

168168
if res, err := handler.didChange(ctx, p); err != nil {
@@ -177,7 +177,7 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
177177

178178
log.Printf(" --> didChange(%s@%d)", p.TextDocument.URI, p.TextDocument.Version)
179179
for _, change := range p.ContentChanges {
180-
log.Printf(" > %s -> '%s'", change.Range, strconv.Quote(change.Text))
180+
log.Printf(" > %s -> %s", change.Range, strconv.Quote(change.Text))
181181
}
182182
err = handler.ClangdConn.Notify(ctx, req.Method, p)
183183
return nil, err
@@ -291,16 +291,12 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
291291
var result interface{}
292292
if req.Notif {
293293
err = handler.ClangdConn.Notify(ctx, req.Method, params)
294-
if enableLogging {
295-
log.Println(" sent", req.Method, "notification to clangd")
296-
}
294+
// log.Println(" sent", req.Method, "notification to clangd")
297295
} else {
298296
ctx, cancel := context.WithTimeout(ctx, 800*time.Millisecond)
299297
defer cancel()
300298
result, err = lsp.SendRequest(ctx, handler.ClangdConn, req.Method, params)
301-
if enableLogging {
302-
log.Println(" sent", req.Method, "request id", req.ID, " to clangd")
303-
}
299+
// log.Println(" sent", req.Method, "request id", req.ID, " to clangd")
304300
}
305301
if err == nil && handler.buildSketchSymbolsLoad {
306302
handler.buildSketchSymbolsLoad = false
@@ -555,12 +551,7 @@ func (handler *InoHandler) didChange(ctx context.Context, req *lsp.DidChangeText
555551
if !ok {
556552
return nil, unknownURI(doc.URI)
557553
}
558-
if trackedDoc.Version+len(req.ContentChanges) != doc.Version {
559-
return nil, errors.Errorf("document out-of-sync: expected version %d but got %d", trackedDoc.Version+1, doc.Version)
560-
}
561-
for _, change := range req.ContentChanges {
562-
textutils.ApplyLSPTextDocumentContentChangeEvent(trackedDoc, &change)
563-
}
554+
textutils.ApplyLSPTextDocumentContentChangeEvent(trackedDoc, req.ContentChanges, doc.Version)
564555

565556
// If changes are applied to a .ino file we increment the global .ino.cpp versioning
566557
// for each increment of the single .ino file.

Diff for: handler/textutils/textutils.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import (
77
)
88

99
// 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
10+
func ApplyLSPTextDocumentContentChangeEvent(textDoc *lsp.TextDocumentItem, changes []lsp.TextDocumentContentChangeEvent, version int) error {
11+
newText := textDoc.Text
12+
for _, change := range changes {
13+
if t, err := ApplyTextChange(newText, *change.Range, change.Text); err == nil {
14+
newText = t
15+
} else {
16+
return err
17+
}
1418
}
1519
textDoc.Text = newText
16-
textDoc.Version++
20+
textDoc.Version = version
1721
return nil
1822
}
1923

0 commit comments

Comments
 (0)