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

Code before:
                                                      const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
    {
        _SetUpProviderInSettingsRequestedHandlers(nullptr, nullptr);
        _close();
    }

    // Method Description:
    // - Dismiss the query palette. This will:
    //   * clear all the current text in the input box
    //   * set our visibility to Collapsed
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void ExtensionPalette::_close()
    {
        Visibility(Visibility::Collapsed);

        // Clear the text box each time we close the dialog. This is consistent with VsCode.
        _queryBox().Text(winrt::hstring{});
    }

    ChatMessage::ChatMessage(winrt::hstring content, bool isQuery, bool isCode) :
        _messageContent{ content },
        _isQuery{ isQuery },
        _isCode{ isCode }
    {
        _richBlock = Microsoft::Terminal::UI::Markdown::Builder::Convert(_messageContent, L"");
        const auto resources = Application::Current().Resources();
        if (_isQuery)
        {
            if (const auto textBrush = resources.Lookup(box_value(L"TextOnAccentFillColorPrimaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
            {
                _richBlock.Foreground(textBrush);
            }
        }
        else
        {
            for (const auto& b : _richBlock.Blocks())
            {
                if (const auto& p{ b.try_as<Windows::UI::Xaml::Documents::Paragraph>() })
                {
                    for (const auto& line : p.Inlines())
                    {
                        if (const auto& otherContent{ line.try_as<Windows::UI::Xaml::Documents::InlineUIContainer>() })
                        {
                            if (const auto& codeBlock{ otherContent.Child().try_as<Microsoft::Terminal::UI::Markdown::CodeBlock>() })
                            {
                                codeBlock.Margin({ 0, 8, 0, 8 });
                                codeBlock.PlayButtonVisibility(Windows::UI::Xaml::Visibility::Visible);
                                if (const auto backgroundBrush = resources.Lookup(box_value(L"ControlAltFillColorSecondaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())

Review comment (bug):
you'll need a `_richBlock{ nullptr }` here, otherwise we will construct one on line 460 and then overwrite it on line 465.

Code after:
    }

    ChatMessage::ChatMessage(winrt::hstring content, bool isQuery) :
        _messageContent{ content },
        _isQuery{ isQuery },
        _richBlock{ nullptr }
    {
        _richBlock = Microsoft::Terminal::UI::Markdown::Builder::Convert(_messageContent, L"");
        const auto resources = Application::Current().Resources();
        const auto textBrushObj = _isQuery ? resources.Lookup(box_value(L"TextOnAccentFillColorPrimaryBrush")) : resources.Lookup(box_value(L"TextFillColorPrimaryBrush"));
        if (const auto textBrush = textBrushObj.try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
        {
            _richBlock.Foreground(textBrush);
        }
        if (!_isQuery)
        {
            for (const auto& b : _richBlock.Blocks())
            {
                if (const auto& p{ b.try_as<Windows::UI::Xaml::Documents::Paragraph>() })
                {
                    for (const auto& line : p.Inlines())
                    {
                        if (const auto& otherContent{ line.try_as<Windows::UI::Xaml::Documents::InlineUIContainer>() })
                        {
                            if (const auto& codeBlock{ otherContent.Child().try_as<Microsoft::Terminal::UI::Markdown::CodeBlock>() })
                            {
                                codeBlock.Margin({ 0, 8, 0, 8 });
                                codeBlock.PlayButtonVisibility(Windows::UI::Xaml::Visibility::Visible);
                                if (const auto backgroundBrush = resources.Lookup(box_value(L"ControlAltFillColorSecondaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
                                {
                                    codeBlock.Background(backgroundBrush);
                                }
                                if (const auto foregroundBrush = resources.Lookup(box_value(L"AccentTextFillColorPrimaryBrush")).try_as<Windows::UI::Xaml::Media::SolidColorBrush>())
                                {
                                    codeBlock.Foreground(foregroundBrush);
                                }
                                codeBlock.RequestRunCommands([this, commandlines = codeBlock.Commandlines()](auto&&, auto&&) {
                                    _RunCommandClickedHandlers(*this, commandlines);
                                });
                            }
                        }
                    }
                }
            }
        }
    }
}