Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:

    // Method Description:
    // - Search text in text buffer. This is triggered if the user click
    //   search button or press enter.
    // Arguments:
    // - text: the text to search
    // - goForward: boolean that represents if the current search direction is forward
    // - caseSensitive: boolean that represents if the current search is case sensitive
    // Return Value:
    // - <none>
    SearchResults ControlCore::Search(const std::wstring_view& text, const bool goForward, const bool caseSensitive, const bool reset)
    {
        const auto lock = _terminal->LockForWriting();

        bool searchInvalidated = false;
        std::vector<til::point_span> oldResults;
        if (_searcher.ResetIfStale(*GetRenderData(), text, !goForward, !caseSensitive, &oldResults))
        {
            searchInvalidated = true;

            _cachedSearchResultRows = {};
            if (SnapSearchResultToSelection())
            {
                _searcher.MoveToCurrentSelection();
                SnapSearchResultToSelection(false);
            }

            _terminal->SetSearchHighlights(_searcher.Results());
            _renderer->TriggerSearchHighlight(oldResults);
        }
        else if (!reset)
        {
            _searcher.FindNext();
        }

        int32_t totalMatches = 0;
        int32_t currentMatch = 0;
        if (const auto idx = _searcher.CurrentMatch(); idx >= 0)
        {
            totalMatches = gsl::narrow<int32_t>(_searcher.Results().size());
            currentMatch = gsl::narrow<int32_t>(idx);
            _terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(idx));
        }

        return {
            .TotalMatches = totalMatches,
            .CurrentMatch = currentMatch,
            .SearchInvalidated = searchInvalidated,
        };
    }


Review comment (suggestion):
This allows us to ensure that we don't accidentally call `FindNext()` just because the `OutputIdle` event got triggered, which results in a `Search()` call.

Code after:
    // Arguments:
    // - text: the text to search
    // - goForward: boolean that represents if the current search direction is forward
    // - caseSensitive: boolean that represents if the current search is case sensitive
    // Return Value:
    // - <none>
    SearchResults ControlCore::Search(const std::wstring_view& text, const bool goForward, const bool caseSensitive, const bool reset)
    {
        const auto lock = _terminal->LockForWriting();

        bool searchInvalidated = false;
        std::vector<til::point_span> oldResults;
        if (_searcher.ResetIfStale(*GetRenderData(), text, !goForward, !caseSensitive, &oldResults))
        {
            searchInvalidated = true;

            _cachedSearchResultRows = {};
            if (SnapSearchResultToSelection())
            {
                _searcher.MoveToCurrentSelection();
                SnapSearchResultToSelection(false);
            }

            _terminal->SetSearchHighlights(_searcher.Results());
        }
        else if (!reset)
        {
            _searcher.FindNext();
        }

        int32_t totalMatches = 0;
        int32_t currentMatch = 0;
        if (const auto idx = _searcher.CurrentMatch(); idx >= 0)
        {
            totalMatches = gsl::narrow<int32_t>(_searcher.Results().size());
            currentMatch = gsl::narrow<int32_t>(idx);
            _terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(idx));
        }

        _renderer->TriggerSearchHighlight(oldResults);

        return {
            .TotalMatches = totalMatches,
            .CurrentMatch = currentMatch,
            .SearchInvalidated = searchInvalidated,
        };
    }

    Windows::Foundation::Collections::IVector<int32_t> ControlCore::SearchResultRows()
    {
        if (!_cachedSearchResultRows)