Repository: microsoft/terminal
File: src/cascadia/TerminalApp/SuggestionsControl.cpp (C++)

Code before:
        // event will fire _before_ _filterTextChanged does. Updating the value
        // here will ensure that we can check this case appropriately.
        _lastFilterTextWasEmpty = _searchBox().Text().empty();

        const auto lastSelectedIndex = std::max(0, _filteredActionsView().SelectedIndex()); // SelectedIndex will return -1 for "nothing"

        _updateFilteredActions();

        if (const auto newSelectedIndex = _filteredActionsView().SelectedIndex();
            newSelectedIndex == -1)
        {
            // Make sure something stays selected
            _scrollToIndex(lastSelectedIndex);
        }
        else
        {
            // BODGY: Calling ScrollIntoView on a ListView doesn't always work
            // immediately after a change to the items. See:
            // https://stackoverflow.com/questions/16942580/why-doesnt-listview-scrollintoview-ever-work
            // The SelectionChanged thing we do (in _selectedCommandChanged),
            // but because we're also not changing the actual selected item when
            // the size of the list grows (it _stays_ selected, so it never
            // _changes_), we never get a SelectionChanged.
            //
            // To mitigate, only in the case of totally clearing out the filter
            // (like hitting `esc`), we want to breifly select the 0th item,
            // then immediately select the one we want to make visible. That
            // will make sure we get a SelectionChanged when the ListView is
            // ready, and we can use that to scroll to the right item.
            //
            // If we do this on _every_ change, then the preview text flickers
            // between the 0th item and the correct one.
            if (_lastFilterTextWasEmpty)
            {
                _filteredActionsView().SelectedIndex(0);
            }
            _scrollToIndex(newSelectedIndex);
        }

        const auto currentNeedleHasResults{ _filteredActions.Size() > 0 };
        if (!currentNeedleHasResults)
        {
            PreviewAction.raise(*this, nullptr);
        }
        _noMatchesText().Visibility(currentNeedleHasResults ? Visibility::Collapsed : Visibility::Visible);
        if (auto automationPeer{ Automation::Peers::FrameworkElementAutomationPeer::FromElement(_searchBox()) })
        {
            automationPeer.RaiseNotificationEvent(
                Automation::Peers::AutomationNotificationKind::ActionCompleted,
                Automation::Peers::AutomationNotificationProcessing::ImportantMostRecent,
                currentNeedleHasResults ?

Review comment (suggestion):
```suggestion
            // (like hitting `esc`), we want to briefly select the 0th item,
```

Code after:
        // event will fire _before_ _filterTextChanged does. Updating the value
        // here will ensure that we can check this case appropriately.
        _lastFilterTextWasEmpty = _searchBox().Text().empty();

        const auto lastSelectedIndex = std::max(0, _filteredActionsView().SelectedIndex()); // SelectedIndex will return -1 for "nothing"

        _updateFilteredActions();

        if (const auto newSelectedIndex = _filteredActionsView().SelectedIndex();
            newSelectedIndex == -1)
        {
            // Make sure something stays selected
            _scrollToIndex(lastSelectedIndex);
        }
        else
        {
            // BODGY: Calling ScrollIntoView on a ListView doesn't always work
            // immediately after a change to the items. See:
            // https://stackoverflow.com/questions/16942580/why-doesnt-listview-scrollintoview-ever-work
            // The SelectionChanged thing we do (in _selectedCommandChanged),
            // but because we're also not changing the actual selected item when
            // the size of the list grows (it _stays_ selected, so it never
            // _changes_), we never get a SelectionChanged.
            //
            // To mitigate, only in the case of totally clearing out the filter
            // (like hitting `esc`), we want to briefly select the 0th item,
            // then immediately select the one we want to make visible. That
            // will make sure we get a SelectionChanged when the ListView is
            // ready, and we can use that to scroll to the right item.
            //
            // If we do this on _every_ change, then the preview text flickers
            // between the 0th item and the correct one.
            if (_lastFilterTextWasEmpty)
            {
                _filteredActionsView().SelectedIndex(0);
            }
            _scrollToIndex(newSelectedIndex);
        }

        const auto currentNeedleHasResults{ _filteredActions.Size() > 0 };
        if (!currentNeedleHasResults)
        {
            PreviewAction.raise(*this, nullptr);
        }
        _noMatchesText().Visibility(currentNeedleHasResults ? Visibility::Collapsed : Visibility::Visible);
        if (auto automationPeer{ Automation::Peers::FrameworkElementAutomationPeer::FromElement(_searchBox()) })
        {
            automationPeer.RaiseNotificationEvent(
                Automation::Peers::AutomationNotificationKind::ActionCompleted,
                Automation::Peers::AutomationNotificationProcessing::ImportantMostRecent,
                currentNeedleHasResults ?