moveWordRight function

NavigationResult moveWordRight(
  1. Root root,
  2. CaretStop current, {
  3. List<CaretStop>? stops,
  4. List<LogicalLine>? cachedLines,
})

Implementation

NavigationResult moveWordRight(Root root, CaretStop current, {
  List<CaretStop>? stops,
  List<LogicalLine>? cachedLines,
}) {
  final stops_ = stops ?? buildAllStops(root);
  int idx = findStopIndex(stops_, current.fragmentId, current.offset);
  if (idx < 0 || idx >= stops_.length - 1) return NavigationResult.none;

  _ensureWordCaches(root, stops_, cachedLines: cachedLines);
  final cache = _wordFragCache!;
  final lineIdx = _wordLineIdx!;
  String? ch(int i) => _charRight(stops_, i, cache, lineIdx);

  final startChar = ch(idx);

  if (startChar == null) return NavigationResult.none; // end of document

  if (_isSpaceChar(startChar)) {
    // On space or line boundary: skip separators, then skip the word
    while (idx < stops_.length - 1 && _isSpaceChar(ch(idx) ?? '')) {
      idx++;
    }
    while (idx < stops_.length - 1) {
      final c = ch(idx);
      if (c == null || !_isWordChar(c)) break;
      idx++;
    }
  } else if (_isWordChar(startChar)) {
    // Inside a word: go to the end
    while (idx < stops_.length - 1) {
      final c = ch(idx);
      if (c == null || !_isWordChar(c)) break;
      idx++;
    }
  } else {
    // Punctuation: skip the sequence
    while (idx < stops_.length - 1) {
      final c = ch(idx);
      if (c == null || _isWordChar(c) || _isSpaceChar(c)) break;
      idx++;
    }
  }

  return NavigationResult(position: stops_[idx], preferredX: 0.0);
}