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

Code before:
        }
    }
    return std::nullopt;
}

// Method Description:
// - updates the selection endpoints based on a direction and expansion mode. Primarily used for keyboard selection.
// Arguments:
// - direction: the direction to move the selection endpoint in
// - mode: the type of movement to be performed (i.e. move by word)
// - (optional) moveSelectionEnd: used by mark mode. Can be used to explicitly move a specific endpoint. If true, move the "end" endpoint. Otherwise, move the start endpoint.
void Terminal::UpdateSelection(SelectionDirection direction, SelectionExpansion mode, std::optional<bool> moveSelectionEnd)
{
    // 1. Figure out which endpoint to update
    // One of the endpoints is the pivot, signifying that the other endpoint is the one we want to move.
    const auto movingEnd{ (_markMode && moveSelectionEnd.has_value()) ? *moveSelectionEnd : _selection->start == _selection->pivot };
    auto targetPos{ movingEnd ? _selection->end : _selection->start };

    // 2. Perform the movement
    switch (mode)
    {
    case SelectionExpansion::Char:
        _MoveByChar(direction, targetPos);
        break;
    case SelectionExpansion::Word:
        _MoveByWord(direction, targetPos);
        break;
    case SelectionExpansion::Viewport:
        _MoveByViewport(direction, targetPos);
        break;
    case SelectionExpansion::Buffer:
        _MoveByBuffer(direction, targetPos);
        break;
    }

    // 3. Actually modify the selection
    // NOTE: targetStart doesn't matter here
    if (_markMode)
    {
        // [Mark Mode]
        // - moveSelectionEnd  --> just move end (i.e. shift + arrow keys)
        // - !moveSelectionEnd --> move all three (i.e. just use arrow keys)
        _selection->end = targetPos;
        if (!*moveSelectionEnd)
        {
            _selection->start = targetPos;
            _selection->pivot = targetPos;
        }
    }
    else
    {

Review comment (suggestion):
For some reason I'm having a hard time parsing this `moveSelectionEnd` logic here. So, there's three cases:
* `nullopt`: not in mark mode. use `_selection->start == _selection->pivot`. We'll later just move the end anchor of the selection.
* `true`: in mark mode. Shift was pressed. we're just going to move the end anchor?
* `false`: in mark mode. We're going to move the start. But then later we're in mark mode, so we're just going to move the end to where the start is?

idk something about this just felt weird.

Code after:
                return UpdateSelectionParams{ std::in_place, SelectionDirection::Down, SelectionExpansion::Viewport };
            case VK_LEFT:
                return UpdateSelectionParams{ std::in_place, SelectionDirection::Left, SelectionExpansion::Char };
            case VK_RIGHT:
                return UpdateSelectionParams{ std::in_place, SelectionDirection::Right, SelectionExpansion::Char };
            case VK_UP:
                return UpdateSelectionParams{ std::in_place, SelectionDirection::Up, SelectionExpansion::Char };
            case VK_DOWN:
                return UpdateSelectionParams{ std::in_place, SelectionDirection::Down, SelectionExpansion::Char };
            }
        }
    }
    return std::nullopt;
}

// Method Description:
// - updates the selection endpoints based on a direction and expansion mode. Primarily used for keyboard selection.
// Arguments:
// - direction: the direction to move the selection endpoint in
// - mode: the type of movement to be performed (i.e. move by word)
// - mods: the key modifiers pressed when performing this update
void Terminal::UpdateSelection(SelectionDirection direction, SelectionExpansion mode, ControlKeyStates mods)
{
    // 1. Figure out which endpoint to update
    // If we're in mark mode, shift dictates whether you are moving the end or not.
    // Otherwise, we're updating an existing selection, so one of the endpoints is the pivot,
    //   signifying that the other endpoint is the one we want to move.
    const auto movingEnd{ _markMode ? mods.IsShiftPressed() : _selection->start == _selection->pivot };
    auto targetPos{ movingEnd ? _selection->end : _selection->start };

    // 2. Perform the movement
    switch (mode)
    {
    case SelectionExpansion::Char:
        _MoveByChar(direction, targetPos);
        break;
    case SelectionExpansion::Word:
        _MoveByWord(direction, targetPos);
        break;
    case SelectionExpansion::Viewport:
        _MoveByViewport(direction, targetPos);
        break;
    case SelectionExpansion::Buffer:
        _MoveByBuffer(direction, targetPos);
        break;
    }

    // 3. Actually modify the selection
    // NOTE: targetStart doesn't matter here
    if (_markMode)
    {