insert_node_at_index

Function insert_node_at_index 

Source
pub fn insert_node_at_index(
    root: &mut LayoutNode,
    node: LayoutNode,
    index_arr: &[usize],
) -> Result<(), LayoutError>
Expand description

Insert node at the location identified by index_arr — an ordered sequence of child indices (e.g. [0, 2] = root.children[0].children at index 3). The node is inserted AFTER the position identified by the last index.

Mirrors findInsertLocationFromIndexArr in frontend/layout/lib/layoutNode.ts:

  • Each segment is clamped to [0, children.len() - 1]. Out-of-range indices do NOT error — they resolve to the last child slot.
  • Descent stops at a leaf. Any remaining segments after we hit a node with no children are ignored, and the leaf becomes the insert target (where ensure_group_node then promotes it). This matches the TS oracle’s if (indexArr.length == 0 || !node.children) return ....

This tolerance is required for “identical state transitions” with the frontend reducer under concurrent edits — the frontend may issue a stale or over-deep indexArr against a tree that has since shrunk.