moveByChar function

SelectionRange moveByChar(
  1. EditorState state,
  2. SelectionRange start,
  3. bool forward, {
  4. bool Function(String) by(
    1. String
    )?,
})

Move the cursor by one character.

This is the base character movement function. The by parameter can be used to extend movement to word boundaries.

Implementation

SelectionRange moveByChar(
  EditorState state,
  SelectionRange start,
  bool forward, {
  bool Function(String) Function(String)? by,
}) {
  var line = state.doc.lineAt(start.head);

  SelectionRange cur = start;
  bool Function(String)? check;

  while (true) {
    final next = _moveVisually(state, line, cur, forward);
    final char = _lastMovedOver;

    if (next == null) {
      // At document boundary - try to move to next/previous line
      if (line.number == (forward ? state.doc.lines : 1)) {
        return cur;
      }

      line = state.doc.line(line.number + (forward ? 1 : -1));
      final nextPos = forward ? line.from : line.to;
      cur = EditorSelection.cursor(nextPos);

      // If we're checking for word boundaries, reset on newline
      if (check != null && !check('\n')) {
        return cur;
      }
      continue;
    }

    if (check == null) {
      if (by == null) return next;
      check = by(char);
    } else if (!check(char)) {
      return cur;
    }
    cur = next;
  }
}