searchResult property

TerminalSearchResult get searchResult

returns the current search result or triggers a new search if it has to the result is a up to date search result either way

Implementation

TerminalSearchResult get searchResult {
  if (_pattern == null || !_isActive) {
    return TerminalSearchResult.empty();
  }
  if (_lastSearchResult != null && !_isDirty) {
    return _lastSearchResult!;
  }

  final terminalWidth = _terminal.terminalWidth;

  if (_searchRegexp == null) {
    var pattern = _pattern!;
    if (!_terminalSearchOptions.useRegex) {
      pattern = RegExp.escape(_pattern!);
    }
    _searchRegexp = RegExp(_createRegexPattern(pattern),
        caseSensitive: _terminalSearchOptions.caseSensitive,
        multiLine: false);
  }

  final hits = List<TerminalSearchHit>.empty(growable: true);

  for (final match
      in _searchRegexp!.allMatches(_search.terminalSearchString)) {
    final start = match.start;
    final end = match.end;
    final startLineIndex = (start / terminalWidth).floor();
    final endLineIndex = (end / terminalWidth).floor();

    // subtract the lines that got added in order to get the index inside the line
    final startIndex = start - startLineIndex * terminalWidth;
    final endIndex = end - endLineIndex * terminalWidth;

    if (_terminalSearchOptions.matchWholeWord) {
      // we match a whole word when the hit fulfills:
      // 1) starts at a line beginning or has a word-separator before it
      final startIsOK =
          startIndex == 0 || kWordSeparators.contains(match.input[start - 1]);
      // 2) ends with a line or has a word-separator after it
      final endIsOK = endIndex == terminalWidth ||
          kWordSeparators.contains(match.input[end]);

      if (!startIsOK || !endIsOK) {
        continue;
      }
    }

    hits.add(
      TerminalSearchHit(
        startLineIndex,
        startIndex,
        endLineIndex,
        endIndex,
      ),
    );
  }

  _markLinesForSearchDone();

  _isPatternDirty = false;
  _lastSearchResult = TerminalSearchResult.fromHits(hits);
  _hasBeenUsingAltBuffer = _terminal.isUsingAltBuffer();
  return _lastSearchResult!;
}