Skip to main content

map_path

Function map_path 

Source
pub fn map_path(path: &[usize], changes: &[Change]) -> Option<Vec<usize>>
Expand description

Carry an index-path through a change list: where does the node originally at path end up after applying changes? Returns None if it was removed or replaced away.

Field changes never move nodes. Structural changes shift the index components of path at the level they act on (an Insert at or before the index shifts it right; a Remove/Move shifts accordingly; removing or replacing a node on the path drops it). This mirrors apply’s live-index semantics, so the mapped path addresses the same node in the applied tree.

use tiptap_rusty_parser::{map_path, Change};
// A sibling inserted before index 1 shifts it to 2.
let changes = vec![Change::Insert { path: vec![], index: 0, node: Default::default() }];
assert_eq!(map_path(&[1, 0], &changes), Some(vec![2, 0]));
// Removing the node on the path drops it.
let removed = vec![Change::Remove { path: vec![], index: 1 }];
assert_eq!(map_path(&[1, 0], &removed), None);