previousWordRange function

({int end, int start})? previousWordRange(
  1. List<String> graphemes,
  2. int offset, {
  3. required GraphemePredicate isWord,
})

Implementation

({int start, int end})? previousWordRange(
  List<String> graphemes,
  int offset, {
  required GraphemePredicate isWord,
}) {
  if (offset <= 0 || graphemes.isEmpty) {
    return null;
  }

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

  final end = position + 1;
  while (position >= 0 && isWord(graphemes[position])) {
    position--;
  }
  return (start: position + 1, end: end);
}