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

Code before:
        {
            // If this mark doesn't know anything about the position of its
            // command, OR it does but thinks that it was empty, then just skip
            // it.
            if (!m.commandEnd.has_value() || *m.commandEnd == m.end)
            {
                continue;
            }
            // If this mark is before/after the start of our search in the
            // buffer, ...

            auto inTheRightDirection = (goUp && (m.commandEnd < start)) || // prev
                                       (!goUp && (m.end > start)); // next
            // (If we're going down, we need to compare the end, not the
            // commandEnd, 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/before this mark in the buffer
                if (!nearest.has_value() ||
                    ((goUp && (*m.commandEnd > *nearest->commandEnd)) || // prev
                     (!goUp && (m.end < *nearest->commandEnd)))) // next
                {
                    // stash this as the new match
                    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)

Review comment (suggestion):
I don't entirely get why we use a reverse-iteration if there's no early-exit condition anyways. Can't this just forward-iterate then?

Also, instead of using `inTheRightDirection` can't we just calculate a signed distance value for `start - m.commandEnd` or `m.end - start`? If we track the best distance and don't throw it away we can drop the check here and merge it with `inTheRightDirection`. It'd be just this as far as I can see:
```cpp
// Early return so we don't have to check for the validity of `nearest` below after the loop exits.
if (marks.empty() {
    return;
}

// `CoordTypeMax` doesn't exist yet. See here:
//   https://github.com/microsoft/terminal/blob/db90593275091edd6a7ce07a37be89fc15fa5a45/src/inc/til/point.h#L9-L10
static constexpr til::point worst{ til::CoordTypeMax, til::CoordTypeMax };

DispatchTypes::ScrollMark nearest;
auto bestDistance = worst;

for (const auto& m : marks) {
    const auto distance = goUp
        ? start - m.commandEnd.value_or(worst)
        : m.end - start;
    if (dinstance && distance < bestDistance) {
        nearest = m;
    }
}
```

Same for the other function below.

Code after:
            for (const auto& mark : marks)
            {
                const auto newY = mark.start.y;
                if (newY > highest)
                {
                    tgt = mark;
                    highest = newY;
                }
            }
            break;
        }
        case ScrollToMarkDirection::First:
        {
            int lowest = currentOffset;
            for (const auto& mark : marks)
            {
                const auto newY = mark.start.y;
                if (newY < lowest)
                {
                    tgt = mark;
                    lowest = newY;
                }
            }
            break;
        }
        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;
                }