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

Code before:
    }

    // Method Description:
    // - Dispatch the current search text as a ExecuteCommandline action.
    // Arguments:
    // - filteredCommand - Selected filtered command - might be null
    // Return Value:
    // - <none>
    void CommandPalette::_dispatchCommandline(winrt::TerminalApp::FilteredCommand const& command)
    {
        const auto filteredCommand = command ? command : _buildCommandLineCommand(_getTrimmedInput());
        if (filteredCommand.has_value())
        {
            if (_commandLineHistory.Size() == CommandLineHistoryLength)
            {
                _commandLineHistory.RemoveAtEnd();
            }
            _commandLineHistory.InsertAt(0, filteredCommand.value());

            auto recentCommands = winrt::single_threaded_vector<hstring>();
            for (const auto& recentFilteredCommand : _commandLineHistory)
            {
                recentCommands.Append(recentFilteredCommand.Item().Name());
            }

            ApplicationState::SharedInstance().RecentCommands(recentCommands);

            TraceLoggingWrite(
                g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider
                "CommandPaletteDispatchedCommandline",
                TraceLoggingDescription("Event emitted when the user runs a commandline in the Command Palette"),
                TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
                TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance));

            if (const auto commandLinePaletteItem{ filteredCommand.value().Item().try_as<winrt::TerminalApp::CommandLinePaletteItem>() })
            {
                _CommandLineExecutionRequestedHandlers(*this, commandLinePaletteItem.CommandLine());
                _close();
            }
        }
    }

    std::optional<winrt::TerminalApp::FilteredCommand> CommandPalette::_buildCommandLineCommand(std::wstring const& commandLine)
    {
        if (commandLine.empty())
        {
            return std::nullopt;
        }

        winrt::hstring cl{ commandLine };
        auto commandLinePaletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(cl) };

Review comment (suggestion):
Will this cause problems if you have multiple windows? Seems like this would overwrite changes if you run a command in another window.

Code after:
        const auto filteredCommand = command ? command : _buildCommandLineCommand(winrt::hstring(_getTrimmedInput()));
        if (filteredCommand.has_value())
        {
            _updateRecentCommands(filteredCommand.value().Item().Name());

            TraceLoggingWrite(
                g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider
                "CommandPaletteDispatchedCommandline",
                TraceLoggingDescription("Event emitted when the user runs a commandline in the Command Palette"),
                TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
                TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance));

            if (const auto commandLinePaletteItem{ filteredCommand.value().Item().try_as<winrt::TerminalApp::CommandLinePaletteItem>() })
            {
                _CommandLineExecutionRequestedHandlers(*this, commandLinePaletteItem.CommandLine());
                _close();
            }
        }
    }

    std::optional<TerminalApp::FilteredCommand> CommandPalette::_buildCommandLineCommand(const hstring& commandLine)
    {
        if (commandLine.empty())
        {
            return std::nullopt;
        }

        auto commandLinePaletteItem{ winrt::make<CommandLinePaletteItem>(commandLine) };
        return winrt::make<FilteredCommand>(commandLinePaletteItem);
    }

    // Method Description:
    // - Helper method for closing the command palette, when the user has _not_
    //   selected an action. Also fires a tracelogging event indicating that the
    //   user closed the palette without running a command.
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void CommandPalette::_dismissPalette()
    {
        _close();

        TraceLoggingWrite(
            g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider
            "CommandPaletteDismissed",
            TraceLoggingDescription("Event emitted when the user dismisses the Command Palette without selecting an action"),
            TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
            TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance));
    }