highlight method

  1. @override
List<InlineSpan> highlight(
  1. String code,
  2. String? language,
  3. TextStyle baseStyle
)
override

Returns the spans for code, tagged for language. Implementations must preserve text exactly: the concatenation of the returned spans' text must equal code, so selection and copy stay aligned. baseStyle is the code block's base (monospace) style, already applied to the enclosing span.

Implementation

@override
List<InlineSpan> highlight(
  String code,
  String? language,
  TextStyle baseStyle,
) {
  final grammar =
      language == null ? null : _languages[language.toLowerCase()];
  if (grammar == null) return <InlineSpan>[TextSpan(text: code)];
  final key = (language!.toLowerCase(), code, baseStyle);
  final cached = _cache.remove(key);
  if (cached != null) return _cache[key] = cached;
  final spans = _spansFor(_tokenize(code, grammar), theme, baseStyle);
  if (code.length > _maxCachedLength) return spans;
  _cache[key] = spans;
  _cachedLength += code.length;
  while (_cachedLength > _maxCachedLength) {
    final oldest = _cache.keys.first;
    _cachedLength -= oldest.$2.length;
    _cache.remove(oldest);
  }
  return spans;
}