Repository: microsoft/terminal
File: src/cascadia/TerminalCore/TerminalSelection.cpp (C++)

Code before:
            if (!isAlreadySelected(list.front()))
            {
                resultFromList = std::make_pair(list.front().start, list.front().stop);
            }
        }
        else if (list.size() > 1)
        {
            // multiple results, find the first one in the direction we're going
            if (forward)
            {
                for (const auto& range : list)
                {
                    if (isAlreadySelected(range))
                    {
                        continue;
                    }
                    else if (!resultFromList || range.start < resultFromList->first)
                    {
                        resultFromList = std::make_pair(range.start, range.stop);
                    }
                }
            }
            else
            {
                for (const auto& range : list)
                {
                    if (isAlreadySelected(range))
                    {
                        continue;
                    }
                    else if (!resultFromList || range.start > resultFromList->first)
                    {
                        resultFromList = std::make_pair(range.start, range.stop);
                    }
                }
            }
        }

        // useViewportCoords --> pattern tree stores everything as viewport coords,
        // so we need to convert them on the way out
        if (useViewportCoords && resultFromList)
        {
            resultFromList->first = _ConvertToBufferCell(resultFromList->first);
            resultFromList->second = _ConvertToBufferCell(resultFromList->second);
        }
        return resultFromList;
    };

    // 1. Look for the hyperlink
    til::point searchStart = searchForward ? _selection->start : til::point{ bufferSize.Left(), _VisibleStartIndex() };
    til::point searchEnd = searchForward ? til::point{ bufferSize.RightInclusive(), _VisibleEndIndex() } : _selection->start;

Review comment (suggestion):
You could combine these loops by doing something like `(range.start < resultFromList->first) == forward`. This would work as long as you make sure that calls to `findContained` are properly excluding the current link's coordinates.

Code after:

        std::optional<std::pair<til::point, til::point>> resultFromList;
        if (!list.empty())
        {
            if (dir == SearchDirection::Forward)
            {
                // pattern tree includes the currently selected range when going forward,
                // so we need to check if we're pointing to that one before returning it.
                auto range = list.front();
                if (_isTargetingUrl && range.start == selectionStartInSearchArea)
                {
                    if (list.size() > 1)
                    {
                        // if we're pointing to the currently selected URL,
                        // pick the next one.
                        range = til::at(list, 1);
                        resultFromList = { range.start, range.stop };
                    }
                    else
                    {
                        // LOAD-BEARING: the only range here is the one that's currently selected.
                        // Make sure this is set to nullopt so that we keep searching through the buffer.
                        resultFromList = std::nullopt;
                    }
                }
                else
                {
                    // not on currently selected range, return the first one
                    resultFromList = { range.start, range.stop };
                }
            }
            else if (dir == SearchDirection::Backward)
            {
                // moving backwards excludes the currently selected range,
                // simply return the last one in the list as it's ordered
                const auto range = list.back();
                resultFromList = { range.start, range.stop };
            }
        }

        // pattern tree stores everything as viewport coords,
        // so we need to convert them on the way out
        if (resultFromList)
        {
            searchArea.ConvertFromOrigin(&resultFromList->first);
            searchArea.ConvertFromOrigin(&resultFromList->second);
        }
        return resultFromList;
    };

    // 1. Look for the hyperlink