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

Code before:
        if (!_cachedSearchResultRows)
        {
            auto results = std::vector<int32_t>();
            auto lastRow = til::CoordTypeMin;

            for (const auto& match : _searcher.Results())
            {
                const auto row{ match.start.y };
                if (row != lastRow)
                {
                    results.push_back(row);
                    lastRow = row;
                }
            }

            _cachedSearchResultRows = winrt::single_threaded_vector<int32_t>(std::move(results));
        }

        return _cachedSearchResultRows;
    }

    void ControlCore::ClearSearch()
    {
        const auto lock = _terminal->LockForWriting();
        _terminal->SetSearchHighlights({});
        _terminal->SetSearchHighlightFocused({});
        _renderer->TriggerSearchHighlight(_searcher.Results());
        _searcher = {};
        _cachedSearchResultRows = {};
    }

    // Method Description:
    // - Tells ControlCore to snap the current search result index to currently
    //   selected text if the search was started using it.
    void ControlCore::SnapSearchResultToSelection(bool shouldSnap) noexcept
    {
        _snapSearchResultToSelection = shouldSnap;
    }

    // Method Description:
    // - Returns true, if we should snap the current search result index to
    //   the currently selected text after a new search is started, else false.
    bool ControlCore::SnapSearchResultToSelection() const noexcept
    {
        return _snapSearchResultToSelection;
    }

    void ControlCore::Close()
    {
        if (!_IsClosing())
        {

Review comment (suggestion):
We can't check `_searcher.GetCurrent()` here because otherwise we don't reset the `_lastMutationId` of a `Searcher` that found nothing. This is relevant in case you open the search box (the Ctrl+Shift+F one), enter something that doesn't exist, close it, and reopen it. If it gets closed and doesn't get reset here, then reopening it won't trigger a search otherwise.

Code after:
            auto lastRow = til::CoordTypeMin;

            for (const auto& match : _searcher.Results())
            {
                const auto row{ match.start.y };
                if (row != lastRow)
                {
                    results.push_back(row);
                    lastRow = row;
                }
            }

            _cachedSearchResultRows = winrt::single_threaded_vector<int32_t>(std::move(results));
        }

        return _cachedSearchResultRows;
    }

    void ControlCore::ClearSearch()
    {
        const auto lock = _terminal->LockForWriting();
        _terminal->SetSearchHighlights({});
        _terminal->SetSearchHighlightFocused({});
        _renderer->TriggerSearchHighlight(_searcher.Results());
        _searcher = {};
        _cachedSearchResultRows = {};
    }

    // Method Description:
    // - Tells ControlCore to snap the current search result index to currently
    //   selected text if the search was started using it.
    void ControlCore::SnapSearchResultToSelection(bool shouldSnap) noexcept
    {
        _snapSearchResultToSelection = shouldSnap;
    }

    // Method Description:
    // - Returns true, if we should snap the current search result index to
    //   the currently selected text after a new search is started, else false.
    bool ControlCore::SnapSearchResultToSelection() const noexcept
    {
        return _snapSearchResultToSelection;
    }

    void ControlCore::Close()
    {
        if (!_IsClosing())
        {
            _closing = true;

            // Ensure Close() doesn't hang, waiting for MidiAudio to finish playing an hour long song.