recalculateListIndices function

void recalculateListIndices(
  1. Root root
)

Recalculates the indices of all lists in the document. Updates indexList for each ListItem based on hierarchical position.

Implementation

void recalculateListIndices(Root root) {
  // Early exit: skip O(n) walk when no lists exist.
  // Ceiling: O(n) is-type scan; upgrade path: maintain a hasLists flag.
  if (!root.nodes.any((n) => n is FluentList)) return;

  void recalculateList(FluentList list, List<int> parentIndices) {
    for (var i = 0; i < list.items.length; i++) {
      final item = list.items[i];
      final newIndexList = [...parentIndices, i + 1];
      item.indexList = newIndexList;

      for (final child in item.children) {
        if (child is FluentList) {
          recalculateList(child, newIndexList);
        }
      }
    }
  }

  for (final node in root.nodes) {
    if (node is FluentList) {
      recalculateList(node, []);
    }
  }
}