build method

Future<TextSpan> build({
  1. required String text,
  2. CustomSpanBuilder? oldBuilder,
})

Builds a TextSpan based on definitions.

oldBuilder is used to decide whether to suppress unnecessary text parsing and building of spans by comparing old and new configurations. If not specified, every call to this method newly parses text and builds spans.

Implementation

Future<TextSpan> build({
  required String text,
  CustomSpanBuilder? oldBuilder,
}) async {
  _parsed = false;
  _built = false;
  _text = text;
  _span = oldBuilder?._span ?? const TextSpan();

  final needsParse =
      definitions.hasUpdatedMatchers(oldBuilder?.definitions) ||
          parserOptions != oldBuilder?.parserOptions ||
          text != oldBuilder?._text;

  if (needsParse) {
    _spansBuilder.elements = await _parse(text);
    return _span = await _build(
      currentSpans: _span.children ?? [],
      updatedDefinitionIndexes: [],
    );
  }
  _spansBuilder.elements = oldBuilder?._spansBuilder.elements ?? [];

  final updatedDefIndexes =
      definitions.findUpdatedDefinitions(oldBuilder?.definitions);

  final needsBuild = updatedDefIndexes.isNotEmpty ||
      style != oldBuilder?.style ||
      matchStyle != oldBuilder?.matchStyle;

  if (needsBuild) {
    return _span = await _build(
      currentSpans: _span.children ?? [],
      updatedDefinitionIndexes: updatedDefIndexes,
    );
  }

  return _span;
}