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

Code before:
                    nearest = m;
                }
            }
        }

        if (nearest.has_value())
        {
            auto start = nearest->end;
            auto end = *nearest->commandEnd;

            const auto bufferSize{ _terminal->GetTextBuffer().GetSize() };
            bufferSize.DecrementInBounds(end);

            auto lock = _terminal->LockForWriting();
            _terminal->SelectNewRegion(start, end);
            _renderer->TriggerSelection();
        }
    }

    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{ std::ranges::reverse_view(_terminal->GetScrollMarks()) };
        for (auto&& m : marks)
        {
            // If this mark doesn't know anything about the position of its
            // output, OR it does but thinks that it was empty, then just skip
            // it.
            if (!m.outputEnd.has_value() || *m.outputEnd == *m.commandEnd)
            {
                continue;
            }
            // If this mark is before/after the start of our search in the buffer, ...
            auto inTheRightDirection = (goUp && (m.outputEnd < start)) || // prev
                                       (!goUp && (m.commandEnd > start)); // next
            // (If we're going down, we need to compare the commandEnd, not the
            // outputEnd, to actually find the next one. Otherwise we'll just
            // find the mark of the current selection again.)
            if (inTheRightDirection)
            {
                // .. and we either haven't found a match, or the current
                // nearest is after this mark in the buffer
                if (!nearest.has_value() ||
                    ((goUp && (*m.outputEnd > *nearest->outputEnd)) || // prev
                     (!goUp && (*m.commandEnd < *nearest->outputEnd)))) // next
                {
                    // stash this as the new match
                    nearest = m;
                }

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

Code after:
        case ScrollToMarkDirection::Next:
        {
            int minDistance = INT_MAX;
            for (const auto& mark : marks)
            {
                const auto delta = mark.start.y - currentOffset;
                if (delta > 0 && delta < minDistance)
                {
                    tgt = mark;
                    minDistance = delta;
                }
            }
            break;
        }
        case ScrollToMarkDirection::Previous:
        default:
        {
            int minDistance = INT_MAX;
            for (const auto& mark : marks)
            {
                const auto delta = currentOffset - mark.start.y;
                if (delta > 0 && delta < minDistance)
                {
                    tgt = mark;
                    minDistance = delta;
                }
            }
            break;
        }
        }

        const auto viewHeight = ViewHeight();
        const auto bufferSize = BufferHeight();

        // UserScrollViewport, to update the Terminal about where the viewport should be
        // then raise a _terminalScrollPositionChanged to inform the control to update the scrollbar.
        if (tgt.has_value())
        {
            UserScrollViewport(tgt->start.y);
            _terminalScrollPositionChanged(tgt->start.y, viewHeight, bufferSize);
        }
        else
        {
            if (direction == ScrollToMarkDirection::Last || direction == ScrollToMarkDirection::Next)
            {
                UserScrollViewport(BufferHeight());
                _terminalScrollPositionChanged(BufferHeight(), viewHeight, bufferSize);
            }
            else if (direction == ScrollToMarkDirection::First || direction == ScrollToMarkDirection::Previous)
            {
                UserScrollViewport(0);