findContainingTree<T extends OrgNode> method

OrgTree? findContainingTree<T extends OrgNode>(
  1. T node, {
  2. bool where(
    1. OrgTree
    )?,
})

Find the immediate parent OrgSection or OrgDocument of the specified node.

Implementation

OrgTree? findContainingTree<T extends OrgNode>(T node,
    {bool Function(OrgTree)? where}) {
  where ??= (_) => true;
  final found = find<T>((n) => identical(node, n));
  if (found == null) return null;
  final (node: _, :path) = found;
  for (final node in path.reversed) {
    if (node is OrgTree && where(node)) return node;
  }
  return null;
}