Skip to content

Commit

Permalink
Fix sync
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 25, 2024
1 parent 677d05a commit 136481b
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions pkgs/sass_language_services/lib/src/lsp/text_document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const carriageReturn = 13;
class TextDocument {
final Uri _uri;
final String _languageId;
final int _version;
int _version;
String _content;
List<int>? _lineOffsets;

Expand Down Expand Up @@ -123,21 +123,23 @@ class TextDocument {

/// Updates this text document by modifying its content.
void update(List<TextDocumentContentChangeEvent> changes, int version) {
for (var change in changes) {
if (TextDocument._isIncremental(change)) {
var range = _getWellformedRange(
(change as TextDocumentContentChangeEvent1).range);
var text = (change as TextDocumentContentChangeEvent1).text;
_version = version;
for (var c in changes) {
var change = c.map((v) => v, (v) => v);
if (change is TextDocumentContentChangeEvent1) {
// Incremental sync.
var range = _getWellformedRange(change.range);
var text = change.text;

var startOffset = offsetAt(range.start);
var endOffset = offsetAt(range.end);

// update content
// Update content.
_content = _content.substring(0, startOffset) +
text +
_content.substring(endOffset, _content.length);

// update offsets without recomputing for the whole document
// Update offsets without recomputing for the whole document.
var startLine = max(range.start.line, 0);
var endLine = max(range.end.line, 0);
var lineOffsets = _lineOffsets!;
Expand All @@ -162,21 +164,14 @@ class TextDocument {
lineOffsets[i] = lineOffsets[i] + diff;
}
}
} else if (TextDocument._isFull(change)) {
_content = (change as TextDocumentContentChangeEvent2).text;
} else if (change is TextDocumentContentChangeEvent2) {
// Full sync.
_content = change.text;
_lineOffsets = null;
}
}
}

static bool _isIncremental(TextDocumentContentChangeEvent event) {
return event is TextDocumentContentChangeEvent1;
}

static bool _isFull(TextDocumentContentChangeEvent event) {
return event is TextDocumentContentChangeEvent2;
}

List<int> _getLineOffsets() {
_lineOffsets ??= _computeLineOffsets(_content, isAtLineStart: true);
return _lineOffsets!;
Expand Down

0 comments on commit 136481b

Please sign in to comment.