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

Code before:
            result = extractResultFromList(resultList);
            if (!result)
            {
                if (dir == SearchDirection::Forward)
                {
                    searchStart.y += 1;
                    searchEnd.y = std::min(searchStart.y + viewportHeight, ViewEndIndex());
                }
                else
                {
                    searchEnd.y -= 1;
                    searchStart.y = std::max(searchEnd.y - viewportHeight, bufferSize.Top());
                }
                searchArea = Viewport::FromDimensions(searchStart, { searchEnd.x + 1, searchEnd.y + 1 });
            }
        }
    }

    // 2. We found a hyperlink from the pattern tree. Look for embedded hyperlinks too!
    // Use the result (if one was found) to narrow down the search.
    searchStart = dir == SearchDirection::Forward ?
                      _selection->start :
                      (result ? result->second : bufferSize.Origin());
    searchEnd = dir == SearchDirection::Forward ?
                    (result ? result->first : buffer.GetLastNonSpaceCharacter()) :
                    _selection->start;
    auto iter = buffer.GetCellDataAt(dir == SearchDirection::Forward ? searchStart : searchEnd);
    while (dir == SearchDirection::Forward ? iter.Pos() < searchEnd : iter.Pos() > searchStart)
    {
        // Don't let us select the same hyperlink again
        if (iter.Pos() < _selection->start || iter.Pos() > _selection->end)
        {
            if (auto attr = iter->TextAttr(); attr.IsHyperlink())
            {
                // Found an embedded hyperlink!
                const auto hyperlinkId = attr.GetHyperlinkId();

                // Expand the start to include the entire hyperlink
                TextBufferCellIterator hyperlinkStartIter{ buffer, iter.Pos() };
                while (attr.IsHyperlink() && attr.GetHyperlinkId() == hyperlinkId)
                {
                    hyperlinkStartIter--;
                    attr = hyperlinkStartIter->TextAttr();
                }
                // undo a move to be inclusive
                hyperlinkStartIter++;

                // Expand the end to include the entire hyperlink
                // No need to undo a move! We'll decrement in the next step anyways.
                TextBufferCellIterator hyperlinkEndIter{ buffer, iter.Pos() };
                attr = hyperlinkEndIter->TextAttr();

Review comment (suggestion):
This is kind of obtuse IMO, but even after thinking about it for a while I can't think of a better way to write this. One option may to explicitly split it up into
```cpp
if (dir == SearchDirection::Forward) {
} else {
}
```

Code after:
            searchStart = { bufferSize.Left(), std::max(searchStart.y - viewportHeight, bufferSize.Top()) };
        }
        searchArea = Viewport::FromDimensions(searchStart, { searchEnd.x + 1, searchEnd.y + 1 });

        const til::point bufferStart{ bufferSize.Origin() };
        const til::point bufferEnd{ bufferSize.RightInclusive(), ViewEndIndex() };
        while (!result && bufferSize.IsInBounds(searchStart) && bufferSize.IsInBounds(searchEnd) && searchStart <= searchEnd && bufferStart <= searchStart && searchEnd <= bufferEnd)
        {
            auto patterns = _getPatterns(searchStart.y, searchEnd.y);
            resultList = patterns.findContained(convertToSearchArea(searchStart), convertToSearchArea(searchEnd));
            result = extractResultFromList(resultList);
            if (!result)
            {
                if (dir == SearchDirection::Forward)
                {
                    searchStart.y += 1;
                    searchEnd.y = std::min(searchStart.y + viewportHeight, ViewEndIndex());
                }
                else
                {
                    searchEnd.y -= 1;
                    searchStart.y = std::max(searchEnd.y - viewportHeight, bufferSize.Top());
                }
                searchArea = Viewport::FromDimensions(searchStart, { searchEnd.x + 1, searchEnd.y + 1 });
            }
        }
    }

    // 2. We found a hyperlink from the pattern tree. Look for embedded hyperlinks too!
    // Use the result (if one was found) to narrow down the search.
    if (dir == SearchDirection::Forward)
    {
        searchStart = _selection->start;
        searchEnd = (result ? result->first : buffer.GetLastNonSpaceCharacter());
    }
    else
    {
        searchStart = (result ? result->second : bufferSize.Origin());
        searchEnd = _selection->start;
    }

    // Careful! Selection can point to RightExclusive(), which doesn't contain data!
    // Clamp to be safe.
    auto initialPos = dir == SearchDirection::Forward ? searchStart : searchEnd;
    bufferSize.Clamp(initialPos);
    auto iter = buffer.GetCellDataAt(initialPos);
    while (dir == SearchDirection::Forward ? iter.Pos() < searchEnd : iter.Pos() > searchStart)
    {
        // Don't let us select the same hyperlink again
        if (iter.Pos() < _selection->start || iter.Pos() > _selection->end)
        {