pub fn compact(changes: &[Change]) -> Vec<Change>Expand description
Coalesce redundant changes while preserving the apply
result. Only safe reductions are performed:
- repeated writes to the same node field (
SetText/SetMarks, orSetAttr/RemoveAttr/SetExtra/RemoveExtraon the same key) collapse to the last one (each write fully determines that field’s final value); - an
Insertimmediately followed by aRemoveof that same just-inserted child cancels out.
Structural ops (Insert/Remove/Move/Replace) act as barriers:
because [apply] resolves indices against the live tree, field ops are never
coalesced across them. compact(c) is never longer than c.
Precondition: this preserves the apply result for change lists that apply
cleanly to their intended base (as produced by diff /
Transform). For a deliberately invalid list it can
differ — e.g. an out-of-bounds Insert immediately followed by its matching
Remove cancels here, whereas applying the original would error; validity
can’t be checked from a change list alone.