prev property

TextBit prev

The previous bit in the text tree.

Note: the previous bit may not have the same parent or grandparent, it's only guaranteed to be within the same tree.

Implementation

TextBit get prev {
  TextBit x = this;

  while (x != null) {
    final siblings = x.parent?._children;
    final i = siblings?.indexOf(x) ?? -1;
    if (i != -1) {
      for (var j = i - 1; j > -1; j--) {
        final candidate = siblings[j];
        if (candidate is TextBits) {
          final last = candidate.last;
          if (last != null) return last;
        } else {
          return candidate;
        }
      }
    }

    x = x.parent;
  }

  return null;
}