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

Code before:
    // - 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())
        {
            _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<winrt::TerminalApp::FilteredCommand> CommandPalette::_buildCommandLineCommand(std::wstring const& commandLine)  const
    {
        if (commandLine.empty())
        {
            return std::nullopt;
        }

        winrt::hstring cl{ commandLine };
        auto commandLinePaletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(cl) };
        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

Review comment (bug):
You can prevent the unneeded, costly string copy with `.c_str()` by replacing this function with:
```cpp
std::optional<winrt::TerminalApp::FilteredCommand> CommandPalette::_buildCommandLineCommand(const winrt::hstring& commandLine) const
{
    if (commandLine.empty())
    {
        return std::nullopt;
    }

    auto commandLinePaletteItem{ winrt::make<winrt::TerminalApp::implementation::CommandLinePaletteItem>(commandLine) };
    return winrt::make<FilteredCommand>(commandLinePaletteItem);
}
```

This will fail compilation though which is why you need to put this into `CommandPalette::_dispatchCommandline`:
```cpp
const auto filteredCommand = command ? command : _buildCommandLineCommand(winrt::hstring{ _getTrimmedInput() });
```


Code after:
    // - filteredCommand - Selected filtered command - might be null
    // Return Value:
    // - <none>
    void CommandPalette::_dispatchCommandline(winrt::TerminalApp::FilteredCommand const& command)
    {
        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",