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

Code before:
                {
                    _openTooltip(cmd);
                }
                else
                {
                    // If there's no description, then just close the tooltip.
                    _descriptionsView().Visibility(Visibility::Collapsed);
                    _descriptionsBackdrop().Visibility(Visibility::Collapsed);
                    _recalculateTopMargin();
                }
            }
        }
    }

    winrt::fire_and_forget SuggestionsControl::_openTooltip(Command cmd)
    {
        const auto description{ cmd.Description() };
        if (description.empty())
        {
            co_return;
        }

        // If it's already open, then just re-target it and update
        // the content immediately. Otherwise:
        // wait a bit before opening it.
        if (_descriptionsView().Visibility() != Visibility::Visible)
        {
            co_await winrt::resume_after(200ms);
            co_await wil::resume_foreground(Dispatcher());
        }

        // Build the contents of the "tooltip" based on the description
        //
        // First, the title. This is just the name of the command.
        _descriptionTitle().Inlines().Clear();
        Documents::Run titleRun;
        titleRun.Text(cmd.Name());
        _descriptionTitle().Inlines().Append(titleRun);

        // Now fill up the "subtitle" part of the "tooltip" with the actual
        // description itself.
        _descriptionComment().Inlines().Clear();

        // First, replace all "\r\n" with "\n"
        std::wstring filtered = description.c_str();
        std::wstring::size_type pos = 0;
        while ((pos = filtered.find(L"\r\n", pos)) != std::wstring::npos)
        {
            filtered.erase(pos, 1);
        }


Review comment (bug):
This code references `_descriptionTitle().Inlines()` again and again. Should we just assign it to a variable? That would make stepping over the code with a debugger potentially a bit easier.

Code after:
                {
                    _openTooltip(cmd);
                }
                else
                {
                    // If there's no description, then just close the tooltip.
                    _descriptionsView().Visibility(Visibility::Collapsed);
                    _descriptionsBackdrop().Visibility(Visibility::Collapsed);
                    _recalculateTopMargin();
                }
            }
        }
    }

    void SuggestionsControl::_openTooltip(Command cmd)
    {
        const auto description{ cmd.Description() };
        if (description.empty())
        {
            return;
        }

        // Build the contents of the "tooltip" based on the description
        //
        // First, the title. This is just the name of the command.
        _descriptionTitle().Inlines().Clear();
        Documents::Run titleRun;
        titleRun.Text(cmd.Name());
        _descriptionTitle().Inlines().Append(titleRun);

        // Now fill up the "subtitle" part of the "tooltip" with the actual
        // description itself.
        const auto& inlines{ _descriptionComment().Inlines() };
        inlines.Clear();

        // Split the filtered description on '\n`
        const auto lines = ::Microsoft::Console::Utils::SplitString(description, L'\n');
        // build a Run + LineBreak, and add them to the text block
        for (const auto& line : lines)
        {
            // Trim off any `\r`'s in the string. Pwsh completions will
            // frequently have these embedded.
            std::wstring trimmed{ line };
            trimmed.erase(std::remove(trimmed.begin(), trimmed.end(), L'\r'), trimmed.end());
            if (trimmed.empty())
            {
                continue;
            }

            Documents::Run textRun;
            textRun.Text(trimmed);