format method

TextSpan format(
  1. String src
)
override

Implementation

TextSpan format(String src) {
  _src = src;
  _scanner = StringScanner(_src);

  if (_generateSpans()) {
    /// Successfully parsed the code
    final List<TextSpan> formattedText = <TextSpan>[];
    int currentPosition = 0;

    for (HighlightSpan span in _spans) {
      if (currentPosition > span.start) continue;
      if (currentPosition != span.start) {
        formattedText.add(
          TextSpan(
            text: _src.substring(currentPosition, span.start),
          ),
        );
      }

      formattedText.add(TextSpan(
        style: span.textStyle(syntaxTheme),
        text: span.textForSpan(_src),
      ));

      currentPosition = span.end;
    }

    if (currentPosition != _src.length)
      formattedText.add(TextSpan(
        text: _src.substring(currentPosition, _src.length),
      ));

    return TextSpan(style: syntaxTheme!.baseStyle, children: formattedText);
  } else {
    /// Parsing failed, return with only basic formatting
    return TextSpan(style: syntaxTheme!.baseStyle, text: src);
  }
}