Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
            attr.SetForeground(foregroundAsTextColor);
            attr.SetBackground(backgroundAsTextColor);

            _terminal->ColorSelection(attr, matchMode);
            _terminal->ClearSelection();
            if (matchMode != Core::MatchMode::None)
            {
                // ClearSelection will invalidate the selection area... but if we are
                // coloring other matches, then we need to make sure those get redrawn,
                // too.
                _renderer->TriggerRedrawAll();
            }
        }
    }

    void ControlCore::AnchorContextMenu(const til::point viewportRelativeCharacterPosition)
    {
        // viewportRelativeCharacterPosition is relative to the current
        // viewport, so adjust for that:
        _contextMenuBufferPosition = _terminal->GetViewport().Origin() + viewportRelativeCharacterPosition;
    }

    void ControlCore::ContextMenuSelectCommand()
    {
        const auto& marks{ _terminal->GetScrollMarks() };
        for (auto&& m : marks)
        {
            if (!m.HasCommand())
            {
                continue;
            }
            // If they clicked _anywhere_ in the mark...
            const auto [markStart, markEnd] = m.GetExtent();
            if (markStart <= _contextMenuBufferPosition &&
                markEnd >= _contextMenuBufferPosition)
            {
                // Select the command
                const auto start = m.end;
                auto end = *m.commandEnd;

                const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
                bufferSize.DecrementInBounds(end);

                auto lock = _terminal->LockForWriting();
                _terminal->SelectNewRegion(start, end);
                _renderer->TriggerSelection();
                return;
            }
        }
    }
    void ControlCore::ContextMenuSelectOutput()

Review comment (suggestion):
```suggestion
        for (const auto&& m : marks)
```

Code after:
    void ControlCore::SelectOutput(const bool goUp)
    {
        const til::point start = HasSelection() ? (goUp ? _terminal->GetSelectionAnchor() : _terminal->GetSelectionEnd()) :
                                                  _terminal->GetTextBuffer().GetCursor().GetPosition();

        std::optional<DispatchTypes::ScrollMark> nearest{ std::nullopt };
        const auto& marks{ _terminal->GetScrollMarks() };

        static constexpr til::point worst{ til::CoordTypeMax, til::CoordTypeMax };
        til::point bestDistance{ worst };

        for (const auto& m : marks)
        {
            if (!m.HasOutput())
            {
                continue;
            }

            const auto distance = goUp ? start - *m.commandEnd : *m.commandEnd - start;
            if ((distance > til::point{ 0, 0 }) && distance < bestDistance)
            {
                nearest = m;
                bestDistance = distance;
            }
        }

        if (nearest.has_value())
        {
            const auto start = *nearest->commandEnd;
            auto end = *nearest->outputEnd;
            _selectSpan(til::point_span{ start, end });
        }
    }

    void ControlCore::ColorSelection(const Control::SelectionColor& fg, const Control::SelectionColor& bg, Core::MatchMode matchMode)
    {
        if (HasSelection())
        {
            const auto pForeground = winrt::get_self<implementation::SelectionColor>(fg);
            const auto pBackground = winrt::get_self<implementation::SelectionColor>(bg);

            TextColor foregroundAsTextColor;
            TextColor backgroundAsTextColor;

            if (pForeground)
            {
                foregroundAsTextColor = pForeground->AsTextColor();
            }

            if (pBackground)
            {