find<T extends OrgNode> method

({T node, OrgPath path})? find<T extends OrgNode>(
  1. bool predicate(
    1. T
    ), [
  2. OrgPath path = const []
])

Find the first node in the AST that satisfies predicate. Specify a type T to only visit nodes of that type. Returns a tuple of the node and its path from the root of the tree, or null if no node is found.

Implementation

({T node, OrgPath path})? find<T extends OrgNode>(
  bool Function(T) predicate, [
  OrgPath path = const [],
]) {
  final self = this;
  if (path.isEmpty) {
    path = [self];
  }
  if (self is T && predicate(self)) {
    return (node: self, path: path);
  }
  if (children != null) {
    for (final child in children!) {
      final result = child.find<T>(predicate, [...path, child]);
      if (result != null) {
        return result;
      }
    }
  }
  return null;
}