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

Code before:
            {
                // Select the output
                const auto start = *m.commandEnd;
                auto end = *m.outputEnd;

                const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
                bufferSize.DecrementInBounds(end);

                auto lock = _terminal->LockForWriting();
                _terminal->SelectNewRegion(start, end);
                _renderer->TriggerSelection();
                return;
            }
        }
    }

    // Method Description:
    // * Don't show this if the click was on the _current_ selection
    // * Don't show this if the click wasn't on a mark with at least a command
    // * Otherwise yea, show it.
    bool ControlCore::ShouldShowSelectCommand()
    {
        // Relies on the anchor set in AnchorContextMenu

        // Don't show this if the click was on the selection
        if (_terminal->IsSelectionActive() &&
            _terminal->GetSelectionAnchor() <= _contextMenuBufferPosition &&
            _terminal->GetSelectionEnd() >= _contextMenuBufferPosition)
        {
            return false;
        }

        // DO show this if the click was on a mark with a command
        const auto& marks{ _terminal->GetScrollMarks() };
        for (auto&& m : marks)
        {
            if (!m.HasOutput())
            {
                continue;
            }
            const auto [start, end] = m.GetExtent();
            if (start <= _contextMenuBufferPosition &&
                end >= _contextMenuBufferPosition)
            {
                return true;
            }
        }

        // Didn't click on a mark with a command - don't show.
        return false;
    }

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

Code after:
                // 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::_contextMenuSelectMark(
        const til::point& pos,
        const std::function<bool(const DispatchTypes::ScrollMark&)>& filter,
        const std::function<til::point_span(const DispatchTypes::ScrollMark&)>& getSpan)
    {
        // Do nothing if the caller didn't give us a way to get the span to select for this mark.
        if (!getSpan)
        {
            return;
        }
        const auto& marks{ _terminal->GetScrollMarks() };
        for (auto&& m : marks)
        {
            // If the caller gave us a way to filter marks, check that now.
            // This can be used to filter to only marks that have a command, or output.
            if (filter && filter(m))
            {
                continue;
            }
            // If they clicked _anywhere_ in the mark...
            const auto [markStart, markEnd] = m.GetExtent();
            if (markStart <= pos &&
                markEnd >= pos)
            {
                // ... select the part of the mark the caller told us about.
                _selectSpan(getSpan(m));
                // And quick bail
                return;
            }
        }
    }

    void ControlCore::ContextMenuSelectCommand()
    {
        _contextMenuSelectMark(
            _contextMenuBufferPosition,
            [](const DispatchTypes::ScrollMark& m) -> bool { return !m.HasCommand(); },