insertAfter<T> static method

List<TreeNode<T>> insertAfter<T>(
  1. List<TreeNode<T>> nodes,
  2. TreeNodeId afterId,
  3. TreeNode<T> toAdd
)

Rebuild nodes, inserting toAdd immediately after the node afterId at whatever depth it lives.

Implementation

static List<TreeNode<T>> insertAfter<T>(List<TreeNode<T>> nodes, TreeNodeId afterId, TreeNode<T> toAdd) {
  final out = <TreeNode<T>>[];
  for (final n in nodes) {
    out.add(n.children.isEmpty ? n : n.copyWith(children: insertAfter<T>(n.children, afterId, toAdd)));
    if (n.id == afterId) out.add(toAdd);
  }
  return out;
}