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

Code before:
            }
        }

        if (newMode != _currentMode)
        {
            //_switchToMode will remove the '>' character from the input.
            _switchToMode(newMode);
        }
    }

    Collections::IObservableVector<winrt::TerminalApp::FilteredCommand> CommandPalette::FilteredActions()
    {
        return _filteredActions;
    }

    void CommandPalette::SetActionMap(const Microsoft::Terminal::Settings::Model::IActionMapView& actionMap)
    {
        _actionMap = actionMap;
        _setCommands();
    }

    void CommandPalette::_setCommands()
    {
        if (_actionMap)
        {
            _allCommands.Clear();
            const auto expandedCommands{ _actionMap.ExpandedCommands() };
            for (const auto& action : expandedCommands)
            {
                const auto keyChordText{ KeyChordSerialization::ToString(_actionMap.GetKeyBindingForAction(action.ID())) };
                auto actionPaletteItem{ winrt::make<winrt::TerminalApp::implementation::ActionPaletteItem>(action, keyChordText) };
                auto filteredCommand{ winrt::make<FilteredCommand>(actionPaletteItem) };
                _allCommands.Append(filteredCommand);
            }

            if (Visibility() == Visibility::Visible && _currentMode == CommandPaletteMode::ActionMode)
            {
                _updateFilteredActions();
            }
        }
    }

    // Method Description:
    // - Replaces a list of filtered commands in the target collection with new
    //   commands based on the tabs in the source collection.
    // Although the source observable we still don't register on it,
    // so the palette user will need to reset the binding manually every time
    // the source collection changes
    // Arguments:
    // - source: the tabs to use for creation filtered commands
    // - target: the collection to store newly created filtered commands

Review comment (bug):
this has a behavior difference - if you call `SetCommands(nullptr)` it clears `allCommands`. Now if you call `SetActionMap(nullptr)` it does _not_ clear `allCommands`

Is this important?

Code after:
            }
        }

        if (newMode != _currentMode)
        {
            //_switchToMode will remove the '>' character from the input.
            _switchToMode(newMode);
        }
    }

    Collections::IObservableVector<winrt::TerminalApp::FilteredCommand> CommandPalette::FilteredActions()
    {
        return _filteredActions;
    }

    void CommandPalette::SetActionMap(const Microsoft::Terminal::Settings::Model::IActionMapView& actionMap)
    {
        _actionMap = actionMap;
        _populateCommands();
    }

    void CommandPalette::_populateCommands()
    {
        _allCommands.Clear();
        if (_actionMap)
        {
            const auto expandedCommands{ _actionMap.ExpandedCommands() };
            for (const auto& action : expandedCommands)
            {
                const auto keyChordText{ KeyChordSerialization::ToString(_actionMap.GetKeyBindingForAction(action.ID())) };
                auto actionPaletteItem{ winrt::make<winrt::TerminalApp::implementation::ActionPaletteItem>(action, keyChordText) };
                auto filteredCommand{ winrt::make<FilteredCommand>(actionPaletteItem) };
                _allCommands.Append(filteredCommand);
            }

            if (Visibility() == Visibility::Visible && _currentMode == CommandPaletteMode::ActionMode)
            {
                _updateFilteredActions();
            }
        }
    }

    // Method Description:
    // - Replaces a list of filtered commands in the target collection with new
    //   commands based on the tabs in the source collection.
    // Although the source observable we still don't register on it,
    // so the palette user will need to reset the binding manually every time
    // the source collection changes
    // Arguments:
    // - source: the tabs to use for creation filtered commands
    // - target: the collection to store newly created filtered commands