applyChanges method

String? applyChanges(
  1. String uri,
  2. int? version,
  3. List<Object?> contentChanges
)

Applies contentChanges (full or incremental) to the open document and returns the updated text, or null if the document is not open.

Implementation

String? applyChanges(String uri, int? version, List<Object?> contentChanges) {
  final doc = _docs[uri];
  if (doc == null) return null;

  for (final change in contentChanges) {
    if (change is! Map<String, Object?>) continue;
    final rangeJson = change['range'];
    final newText = change['text'] as String? ?? '';
    if (rangeJson is Map<String, Object?>) {
      final range = Range.fromJson(rangeJson);
      final index = LineIndex(doc.text);
      final start = index.offsetAt(range.start);
      final end = index.offsetAt(range.end);
      doc.text = doc.text.replaceRange(start, end, newText);
    } else {
      // Full-document sync.
      doc.text = newText;
    }
  }
  if (version != null) doc.version = version;
  return doc.text;
}