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

Code before:
        }

        return {
            .TotalMatches = totalMatches,
            .CurrentMatch = currentMatch,
            .SearchInvalidated = searchInvalidated,
            .SearchRegexInvalid = !_searcher.IsOk(),
        };
    }

    const std::vector<til::point_span>& ControlCore::SearchResultRows() const noexcept
    {
        return _searcher.Results();
    }

    void ControlCore::ClearSearch()
    {
        const auto lock = _terminal->LockForWriting();
        _terminal->SetSearchHighlights({});
        _terminal->SetSearchHighlightFocused(0);
        _renderer->TriggerSearchHighlight(_searcher.Results());
        _searcher = {};
    }

    void ControlCore::Close()
    {
        if (!_IsClosing())
        {
            _closing = true;

            // Ensure Close() doesn't hang, waiting for MidiAudio to finish playing an hour long song.
            _midiAudio.BeginSkip();
        }

        _closeConnection();
    }

    void ControlCore::PersistToPath(const wchar_t* path) const
    {
        const auto lock = _terminal->LockForReading();
        _terminal->SerializeMainBuffer(path);
    }

    void ControlCore::RestoreFromPath(const wchar_t* path) const
    {
        const wil::unique_handle file{ CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr) };
        if (!file)
        {
            return;
        }


Review comment (suggestion):
Personally, I think leaving behind selected text after performing a search feels _a little_ weird, but I think it's necessary. This enables the user to begin a selection at the focused search result using _only_ the keyboard. If we get rid of this, the user would need to manually click on the terminal or transfer focus over somehow.

Code after:
        }

        return {
            .TotalMatches = totalMatches,
            .CurrentMatch = currentMatch,
            .SearchInvalidated = searchInvalidated,
            .SearchRegexInvalid = !_searcher.IsOk(),
        };
    }

    const std::vector<til::point_span>& ControlCore::SearchResultRows() const noexcept
    {
        return _searcher.Results();
    }

    void ControlCore::ClearSearch()
    {
        const auto lock = _terminal->LockForWriting();

        // GH #19358: select the focused search result before clearing search
        if (const auto focusedSearchResult = _terminal->GetSearchHighlightFocused())
        {
            _terminal->SetSelectionAnchor(focusedSearchResult->start);
            _terminal->SetSelectionEnd(focusedSearchResult->end);
            _renderer->TriggerSelection();
        }

        _terminal->SetSearchHighlights({});
        _terminal->SetSearchHighlightFocused(0);
        _renderer->TriggerSearchHighlight(_searcher.Results());
        _searcher = {};
    }

    void ControlCore::Close()
    {
        if (!_IsClosing())
        {
            _closing = true;

            // Ensure Close() doesn't hang, waiting for MidiAudio to finish playing an hour long song.
            _midiAudio.BeginSkip();
        }

        _closeConnection();
    }

    void ControlCore::PersistToPath(const wchar_t* path) const
    {
        const auto lock = _terminal->LockForReading();
        _terminal->SerializeMainBuffer(path);
    }