findRegex method

void findRegex(
  1. RegExp regex,
  2. TextStyle? highlightStyle
)

Search the document using a regular expression and add highlight ranges for each match.

  • regex: The regular expression used to find matches in the current document text. All matches returned by regex.allMatches are added as highlights.
  • highlightStyle: Optional TextStyle applied to each match. If null, a default amber background style is used.

Behavior: Clears existing searchHighlights, applies regex to the document text, appends a SearchHighlight for every match and then sets searchHighlightsChanged = true and calls notifyListeners().

Implementation

void findRegex(RegExp regex, TextStyle? highlightStyle) {
  final style =
      highlightStyle ?? const TextStyle(backgroundColor: Colors.amberAccent);

  searchHighlights.clear();

  final searchText = text;
  final matches = regex.allMatches(searchText);

  for (final match in matches) {
    searchHighlights.add(
      SearchHighlight(start: match.start, end: match.end, style: style),
    );
  }

  searchHighlightsChanged = true;
  notifyListeners();
}