Repository: microsoft/terminal
File: src/cascadia/QueryExtension/ExtensionPalette.cpp (C++)

Code before:
    }

    // Method Description:
    // - This event is called when the user clicks on a Chat Message. We will
    //   dispatch the contents of the message to the app to input into the active control.
    // Arguments:
    // - e: an ItemClickEventArgs who's ClickedItem() will be the message that was clicked on.
    // Return Value:
    // - <none>
    void ExtensionPalette::_listItemClicked(const Windows::Foundation::IInspectable& /*sender*/,
                                            const Windows::UI::Xaml::Controls::ItemClickEventArgs& e)
    {
        const auto selectedSuggestionItem = e.ClickedItem();
        const auto selectedItemAsChatMessage = selectedSuggestionItem.as<winrt::Microsoft::Terminal::Query::Extension::ChatMessage>();
        if (selectedItemAsChatMessage.IsCode())
        {
            auto suggestion = winrt::to_string(selectedItemAsChatMessage.MessageContent());

            // the AI sometimes sends multiline code blocks
            // we don't want to run any of those commands when the chat item is clicked,
            // so we replace newlines with the appropriate delimiter
            size_t pos = 0;
            while ((pos = suggestion.find("\n", pos)) != std::string::npos)
            {
                const auto delimiter = _ActiveCommandline == cmdExe ? cmdCommandDelimiter : commandDelimiter;
                suggestion.replace(pos, 1, delimiter);
                pos += 1; // Move past the replaced character
            }
            _InputSuggestionRequestedHandlers(*this, winrt::to_hstring(suggestion));
            _close();

            TraceLoggingWrite(
                g_hQueryExtensionProvider,
                "AICodeResponseInputted",
                TraceLoggingDescription("Event emitted when the user clicks on a suggestion to have it be input into their active shell"),
                TraceLoggingKeyword(MICROSOFT_KEYWORD_CRITICAL_DATA),
                TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
        }
    }

    // Method Description:
    // - This event is triggered when someone clicks anywhere in the bounds of
    //   the window that's _not_ the query palette UI. When that happens,
    //   we'll want to dismiss the palette.
    // Arguments:
    // - <unused>
    // Return Value:
    // - <none>
    void ExtensionPalette::_rootPointerPressed(const Windows::Foundation::IInspectable& /*sender*/,
                                               const Windows::UI::Xaml::Input::PointerRoutedEventArgs& /*e*/)
    {

Review comment (suggestion):
If you intention is to replace a single character with a single character, I believe the most straight forward approach would be to just index into the string:
```cpp
const auto delimiter = _ActiveCommandline == L"cmd.exe" ? L'&' : L';';
suggestion.at(pos) = delimiter;
```

Code after:
        }
    }

    // Method Description:
    // - This event is called when the user clicks on a Chat Message. We will
    //   dispatch the contents of the message to the app to input into the active control.
    // Arguments:
    // - e: an ItemClickEventArgs who's ClickedItem() will be the message that was clicked on.
    // Return Value:
    // - <none>
    void ExtensionPalette::_listItemClicked(const Windows::Foundation::IInspectable& /*sender*/,
                                            const Windows::UI::Xaml::Controls::ItemClickEventArgs& e)
    {
        const auto selectedSuggestionItem = e.ClickedItem();
        const auto selectedItemAsChatMessage = selectedSuggestionItem.as<winrt::Microsoft::Terminal::Query::Extension::ChatMessage>();
        if (selectedItemAsChatMessage.IsCode())
        {
            auto suggestion = winrt::to_string(selectedItemAsChatMessage.MessageContent());

            // the AI sometimes sends multiline code blocks
            // we don't want to run any of those commands when the chat item is clicked,
            // so we replace newlines with the appropriate delimiter
            size_t pos = 0;
            while ((pos = suggestion.find("\n", pos)) != std::string::npos)
            {
                const auto delimiter = (_ActiveCommandline == cmdExe || _ActiveCommandline == cmd) ? cmdCommandDelimiter : commandDelimiter;
                suggestion.at(pos) = delimiter;
                pos += 1; // Move past the replaced character
            }
            _InputSuggestionRequestedHandlers(*this, winrt::to_hstring(suggestion));
            _close();

            TraceLoggingWrite(
                g_hQueryExtensionProvider,
                "AICodeResponseInputted",
                TraceLoggingDescription("Event emitted when the user clicks on a suggestion to have it be input into their active shell"),
                TraceLoggingKeyword(MICROSOFT_KEYWORD_CRITICAL_DATA),
                TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
        }
    }

    // Method Description:
    // - This event is triggered when someone clicks anywhere in the bounds of
    //   the window that's _not_ the query palette UI. When that happens,
    //   we'll want to dismiss the palette.
    // Arguments:
    // - <unused>
    // Return Value:
    // - <none>
    void ExtensionPalette::_rootPointerPressed(const Windows::Foundation::IInspectable& /*sender*/,
                                               const Windows::UI::Xaml::Input::PointerRoutedEventArgs& /*e*/)