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

Code before:
                if (_selection->start.y > 0)
                {
                    _selection->start.y -= 1;
                    _selection->end.y -= 1;
                }
                else
                {
                    // The start of the selection is at 0, if the end is greater than 0, then only reduce the end
                    if (_selection->end.y > 0)
                    {
                        _selection->start.x = 0;
                        _selection->end.y -= 1;
                    }
                    else
                    {
                        // Both the start and end of the selection are at 0, clear the selection
                        _selection.reset();
                    }
                }
            }
        }

        // manually erase our pattern intervals since the locations have changed now
        _patternIntervalTree = {};
    }

    // Update Cursor Position
    cursor.SetPosition(proposedCursorPosition);

    // Move the viewport down if the cursor moved below the viewport.
    // Obviously, don't need to do this in the alt buffer.
    if (!_inAltBuffer())
    {
        auto updatedViewport = false;
        const auto scrollAmount = std::max(0, proposedCursorPosition.y - _mutableViewport.BottomInclusive());
        if (scrollAmount > 0)
        {
            const auto newViewTop = std::max(0, proposedCursorPosition.y - (_mutableViewport.Height() - 1));
            // In the alt buffer, we never need to adjust _mutableViewport, which is the viewport of the main buffer.
            if (newViewTop != _mutableViewport.Top())
            {
                _mutableViewport = Viewport::FromDimensions({ 0, newViewTop },
                                                            _mutableViewport.Dimensions());
                updatedViewport = true;
            }
        }

        // If the viewport moved, or we circled the buffer, we might need to update
        // our _scrollOffset
        if (updatedViewport || newRows != 0)
        {

Review comment (bug):
```suggestion
```

I respect that this is true, but also... `git blame` could probably tell you that, and I don't know that every time we make a change we need to decorate the code with signposts that say "this was a bug back when xyz" :)

Code after:
                const auto pivotWasStart = _selection->start == _selection->pivot;
                // If the start of the selection is above 0, we can reduce both the start and end by 1
                if (_selection->start.y > 0)
                {
                    _selection->start.y -= 1;
                    _selection->end.y -= 1;
                }
                else
                {
                    // The start of the selection is at 0, if the end is greater than 0, then only reduce the end
                    if (_selection->end.y > 0)
                    {
                        _selection->start.x = 0;
                        _selection->end.y -= 1;
                    }
                    else
                    {
                        // Both the start and end of the selection are at 0, clear the selection
                        _selection.reset();
                    }
                }

                // If we still have a selection, make sure to sync the pivot
                // with whichever value is the right one.
                //
                // Failure to do this might lead to GH #14462
                if (_selection.has_value())
                {
                    _selection->pivot = pivotWasStart ? _selection->start : _selection->end;
                }
            }
        }

        // manually erase our pattern intervals since the locations have changed now
        _patternIntervalTree = {};
    }

    // Update Cursor Position
    cursor.SetPosition(proposedCursorPosition);

    // Move the viewport down if the cursor moved below the viewport.
    // Obviously, don't need to do this in the alt buffer.
    if (!_inAltBuffer())
    {
        auto updatedViewport = false;
        const auto scrollAmount = std::max(0, proposedCursorPosition.y - _mutableViewport.BottomInclusive());
        if (scrollAmount > 0)
        {
            const auto newViewTop = std::max(0, proposedCursorPosition.y - (_mutableViewport.Height() - 1));
            // In the alt buffer, we never need to adjust _mutableViewport, which is the viewport of the main buffer.
            if (newViewTop != _mutableViewport.Top())