updateSemanticTokens method

void updateSemanticTokens(
  1. List<LspSemanticToken> tokens,
  2. String getLineText(
    1. int
    ),
  3. int lineCount
)

Implementation

void updateSemanticTokens(
  List<LspSemanticToken> tokens,
  String Function(int) getLineText,
  int lineCount,
) {
  final updatedLineSemanticSpans = <int, List<SemanticWordSpan>>{};
  final lineCache = <int, String>{};

  for (final token in tokens) {
    if (token.line < 0 || token.line >= lineCount) continue;
    final lineText = lineCache.putIfAbsent(
      token.line,
      () => getLineText(token.line),
    );
    final start = token.start.clamp(0, lineText.length);
    final end = (token.start + token.length).clamp(0, lineText.length);

    if (start < end) {
      final word = lineText.substring(start, end);
      final style = _resolveSemanticStyle(token.tokenTypeName);

      if (style != null && word.isNotEmpty) {
        final lineSpans = updatedLineSemanticSpans.putIfAbsent(
          token.line,
          () => [],
        );
        lineSpans.add(
          SemanticWordSpan(
            startChar: start,
            endChar: end,
            word: word,
            style: style,
          ),
        );
      }
    }
  }

  for (final spans in updatedLineSemanticSpans.values) {
    spans.sort((a, b) => a.startChar.compareTo(b.startChar));
  }

  for (final entry in updatedLineSemanticSpans.entries) {
    _lineSemanticSpans[entry.key] = entry.value;
  }

  _isEditing = false;
  _lineSpanCache.clear();
  _mergedCache.clear();
  _grammarCache.clear();
  _version++;
}