Repository: microsoft/terminal
File: src/host/cmdline.cpp (C++)

Code before:
// Routine Description:
// - Moves the cursor to the right by a glyph
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_moveCursorRight(COOKED_READ_DATA& cookedReadData) noexcept
{
    auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
    const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
    // If not at the end of the line, move cursor position right.
    if (cookedReadData.InsertionPoint() < (cookedReadData.BytesRead() / sizeof(WCHAR)))
    {
        cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
        cursorPosition.X = cursorPosition.X +
                           RetrieveNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
                                                  cookedReadData.BufferStartPtr(),
                                                  cookedReadData.InsertionPoint());
        if (CheckBisectProcessW(cookedReadData.ScreenInfo(),
                                cookedReadData.BufferStartPtr(),
                                cookedReadData.InsertionPoint() + 2,
                                sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X,
                                cookedReadData.OriginalCursorPosition().X,
                                true))
        {
            // Snap cursorPosition.X to sScreenBufferSizeX if it is off by one
            if (cursorPosition.X == (sScreenBufferSizeX - 1))
                cursorPosition.X = sScreenBufferSizeX;
        }

        cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() + 1);
        cookedReadData.InsertionPoint()++;
    }
    // if at the end of the line, copy a character from the same position in the last command
    else if (cookedReadData.HasHistory())
    {
        size_t NumSpaces;
        const auto LastCommand = cookedReadData.History().GetLastCommand();
        if (!LastCommand.empty() && LastCommand.size() > cookedReadData.InsertionPoint())
        {
            *cookedReadData.BufferCurrentPtr() = LastCommand[cookedReadData.InsertionPoint()];
            cookedReadData.BytesRead() += sizeof(WCHAR);
            cookedReadData.InsertionPoint()++;
            if (cookedReadData.IsEchoInput())
            {
                til::CoordType ScrollY = 0;
                auto CharsToWrite = sizeof(WCHAR);
                FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
                                                              cookedReadData.BufferStartPtr(),
                                                              cookedReadData.BufferCurrentPtr(),
                                                              cookedReadData.BufferCurrentPtr(),

Review comment (suggestion):
```suggestion
            // Snap cursorPosition.X to sScreenBufferSizeX if it is at the edge of the screen
```

Code after:
// Routine Description:
// - Moves the cursor to the right by a glyph
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_moveCursorRight(COOKED_READ_DATA& cookedReadData) noexcept
{
    auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
    const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
    // If not at the end of the line, move cursor position right.
    if (cookedReadData.InsertionPoint() < (cookedReadData.BytesRead() / sizeof(WCHAR)))
    {
        cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
        cursorPosition.X = cursorPosition.X +
                           RetrieveNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
                                                  cookedReadData.BufferStartPtr(),
                                                  cookedReadData.InsertionPoint());
        if (CheckBisectProcessW(cookedReadData.ScreenInfo(),
                                cookedReadData.BufferStartPtr(),
                                cookedReadData.InsertionPoint() + 2,
                                sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X,
                                cookedReadData.OriginalCursorPosition().X,
                                true))
        {
            // Snap cursorPosition.X to sScreenBufferSizeX if it is at the edge of the screen
            if (cursorPosition.X == (sScreenBufferSizeX - 1))
                cursorPosition.X = sScreenBufferSizeX;
        }

        cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() + 1);
        cookedReadData.InsertionPoint()++;
    }
    // if at the end of the line, copy a character from the same position in the last command
    else if (cookedReadData.HasHistory())
    {
        size_t NumSpaces;
        const auto LastCommand = cookedReadData.History().GetLastCommand();
        if (!LastCommand.empty() && LastCommand.size() > cookedReadData.InsertionPoint())
        {
            *cookedReadData.BufferCurrentPtr() = LastCommand[cookedReadData.InsertionPoint()];
            cookedReadData.BytesRead() += sizeof(WCHAR);
            cookedReadData.InsertionPoint()++;
            if (cookedReadData.IsEchoInput())
            {
                til::CoordType ScrollY = 0;
                auto CharsToWrite = sizeof(WCHAR);
                FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
                                                              cookedReadData.BufferStartPtr(),
                                                              cookedReadData.BufferCurrentPtr(),
                                                              cookedReadData.BufferCurrentPtr(),