findRegex method

void findRegex(
  1. RegExp regex
)

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) {
  searchHighlights.clear();

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

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

  searchHighlightsChanged = true;
  notifyListeners();
}