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

Code before:
                // 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();
                while (attr.IsHyperlink() && attr.GetHyperlinkId() == hyperlinkId)
                {
                    hyperlinkEndIter++;
                    attr = hyperlinkEndIter->TextAttr();
                }

                result = { hyperlinkStartIter.Pos(), hyperlinkEndIter.Pos() };
                break;
            }
        }
        iter += dir == SearchDirection::Forward ? 1 : -1;
    }

    // 3. Select the hyperlink, if one exists
    if (!result.has_value())
    {
        return;
    }
    else
    {
        auto selection{ _selection.write() };
        wil::hide_name _selection;
        selection->start = result->first;
        selection->pivot = result->first;
        selection->end = result->second;
        bufferSize.DecrementInBounds(selection->end);
        _selectionIsTargetingUrl = true;
        _selectionEndpoint = SelectionEndpoint::End;
    }

    // 4. Scroll to the selected area (if necessary)
    _ScrollToPoint(_selection->end);
}

Terminal::UpdateSelectionParams Terminal::ConvertKeyEventToUpdateSelectionParams(const ControlKeyStates mods, const WORD vkey) const noexcept
{
    if ((_selectionMode == SelectionInteractionMode::Mark || mods.IsShiftPressed()) && !mods.IsAltPressed())
    {
        if (mods.IsCtrlPressed())
        {
            // Ctrl + Shift + _
            // (Mark Mode) Ctrl + _
            switch (vkey)
            {

Review comment (suggestion):
the entire rest of the function can  be outdented and live outside the `else` block since you added an early return!

Code after:
            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)
        {
            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 (hyperlinkStartIter.Pos() > searchStart && attr.IsHyperlink() && attr.GetHyperlinkId() == hyperlinkId)
                {
                    --hyperlinkStartIter;
                    attr = hyperlinkStartIter->TextAttr();