prev property

BuildBit? prev

The previous bit in the tree.

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

Implementation

BuildBit? get prev {
  BuildBit? x = this;

  while (x != null) {
    if (!x.hasParent) {
      return null;
    }
    final siblings = x.parent._children ?? const [];
    final i = siblings.indexOf(x);
    if (i == -1) {
      return null;
    }

    for (var j = i - 1; j > -1; j--) {
      final candidate = siblings[j];
      if (candidate is BuildTree) {
        final last = candidate.last;
        if (last != null) {
          return last;
        }
      } else {
        return candidate;
      }
    }

    x = x.parent;
  }

  return null;
}