Repository: microsoft/terminal
File: src/types/UiaTextRangeBase.cpp (C++)

Code before:
                resultPos = nextPos;
                (*pAmountMoved)++;
            }
            else if (allowBottomExclusive)
            {
                resultPos = documentEnd;
                (*pAmountMoved)++;
            }
            else
            {
                success = false;
            }
            break;
        }
        case MovementDirection::Backward:
        {
            if (nextPos == bufferOrigin)
            {
                success = false;
            }
            else if (const auto wordStart{ buffer.GetWordStart(nextPos, _wordDelimiters, true, documentEnd) }; allowBottomExclusive && nextPos != wordStart)
            {
                // Degenerate ranges first move to the beginning of the word
                resultPos = wordStart;
                (*pAmountMoved)--;
            }
            else if (buffer.MoveToPreviousWord(nextPos, _wordDelimiters))
            {
                resultPos = nextPos;
                (*pAmountMoved)--;
            }
            else
            {
                resultPos = bufferOrigin;
            }
            break;
        }
        default:
            return;
        }
    }

    SetEndpoint(endpoint, resultPos);
}

// Routine Description:
// - moves the UTR's endpoint by moveCount times by line.
// - if endpoints crossed, the degenerate range is created and both endpoints are moved
// - a successful movement on start entails start being at Left()
// - a successful movement on end entails end being at Left() of the NEXT line
// Arguments:

Review comment (suggestion):
Ok, this is really annoying, but I can't think of an elegant way to fix this. Right now, we're _always_ getting `wordStart`, but then if `allowBottomExclusive` is `false`, we just throw it away and never use it.

Is there a trick we can use to still short-circuit this?

I basically want something like this:
```cpp
else if (allowBottomExclusive &&
          /* NOW calculate wordStart because we're actually gonna use it */
          (const auto wordStart{ buffer.GetWordStart(nextPos, _wordDelimiters, true, documentEnd) };
           nextPos != wordStart))
{
    resultPos = wordStart;
    (*pAmountMoved)--;
}
```

Code after:
                resultPos = nextPos;
                (*pAmountMoved)++;
            }
            else if (allowBottomExclusive)
            {
                resultPos = documentEnd;
                (*pAmountMoved)++;
            }
            else
            {
                success = false;
            }
            break;
        }
        case MovementDirection::Backward:
        {
            if (nextPos == bufferOrigin)
            {
                success = false;
            }
            else if (allowBottomExclusive && _tryMoveToWordStart(buffer, documentEnd, resultPos))
            {
                // IMPORTANT: _tryMoveToWordStart modifies resultPos if successful
                // Degenerate ranges first move to the beginning of the word,
                // but if we're already at the beginning of the word, we continue
                // to the next branch and move to the previous word!
                (*pAmountMoved)--;
            }
            else if (buffer.MoveToPreviousWord(nextPos, _wordDelimiters))
            {
                resultPos = nextPos;
                (*pAmountMoved)--;
            }
            else
            {
                resultPos = bufferOrigin;
            }
            break;
        }
        default:
            return;
        }
    }

    SetEndpoint(endpoint, resultPos);
}

// Routine Description:
// - tries to move resultingPos to the beginning of the word
// Arguments:
// - buffer - the text buffer we're operating on