moveWordBackward function

int moveWordBackward(
  1. List<String> graphemes,
  2. int offset, {
  3. required GraphemePredicate isWord,
})

Implementation

int moveWordBackward(
  List<String> graphemes,
  int offset, {
  required GraphemePredicate isWord,
}) {
  if (offset <= 0 || graphemes.isEmpty) {
    return 0;
  }

  var position = offset - 1;
  while (position > 0 && !isWord(graphemes[position])) {
    position--;
  }
  while (position > 0 && isWord(graphemes[position - 1])) {
    position--;
  }
  return position.clamp(0, graphemes.length);
}