expandNode<T> static method

({int index, List<FlatNode<T>> list})? expandNode<T>({
  1. required List<FlatNode<T>> currentList,
  2. required String nodeId,
  3. required Set<String>? expandedNodeIds,
})

Incrementally expand a node: insert its flattened subtree right after it. Returns the new list and the index of the expanded node, or null if the node was not found (caller should fall back to full rebuild).

Implementation

static ({List<FlatNode<T>> list, int index})? expandNode<T>({
  required List<FlatNode<T>> currentList,
  required String nodeId,
  required Set<String>? expandedNodeIds,
}) {
  // Find the node's index in the flat list
  final idx = currentList.indexWhere((fn) => fn.node.id == nodeId);
  if (idx == -1) return null;

  final flatNode = currentList[idx];
  final node = flatNode.node;
  if (node.children.isEmpty) return (list: currentList, index: idx);

  // Flatten only this node's children subtree
  final childFlags = [...flatNode.ancestorIsLastFlags, flatNode.isLast];
  final subtree = flatten<T>(
    nodes: node.children,
    expandedNodeIds: expandedNodeIds,
    depth: flatNode.depth + 1,
    isRoot: false,
    ancestorIsLastFlags: childFlags,
  );

  // Insert after the node
  final result = List<FlatNode<T>>.of(currentList);
  result.insertAll(idx + 1, subtree);
  return (list: result, index: idx);
}