find method

void find(
  1. String query, {
  2. bool scrollToMatch = true,
})

Performs a text search.

query is the text to search for. scrollToMatch determines if the editor should scroll to the selected match.

Implementation

void find(String query, {bool scrollToMatch = true}) {
  _lastQuery = query;

  if (query.isEmpty) {
    _clearMatches();
    return;
  }

  final text = _codeController.text;
  String pattern = query;

  if (!_isRegex) {
    pattern = RegExp.escape(pattern);
  }

  if (_matchWholeWord) {
    pattern = r'\b' + pattern + r'\b';
  }

  try {
    final regExp = RegExp(
      pattern,
      caseSensitive: _caseSensitive,
      multiLine: true,
    );

    _matches = regExp.allMatches(text).toList();
  } catch (e) {
    _matches = [];
    _currentMatchIndex = -1;
    _updateHighlights();
    notifyListeners();
    return;
  }
  if (_matches.isEmpty) {
    _currentMatchIndex = -1;
    _updateHighlights();
    notifyListeners();
    return;
  }

  final cursor = _codeController.selection.start;
  int index = 0;
  bool found = false;

  for (int i = 0; i < _matches.length; i++) {
    if (_matches[i].start >= cursor) {
      index = i;
      found = true;
      break;
    }
  }

  _currentMatchIndex = found ? index : 0;

  _updateHighlights();

  if (scrollToMatch) {
    _scrollToCurrentMatch();
  }
  notifyListeners();
}