Expand description
Non-destructive CST edit primitives (TR-011, OBJ4).
rowan green trees are immutable / persistent: an edit never mutates a
shared node in place, it produces a new green tree that shares all
untouched subtrees with the original (structural sharing). apply_edit
exploits this to satisfy the edit-locality invariant (INV-8 / SC-007): every
region the edit does not touch prints byte-identically, because those
subtrees are the very same green nodes as before.
§Model (AD-004)
An EditOperation names:
- an
EditTarget— a wholeSyntaxNodeor a token span[first ..= last]of adjacent sibling tokens/nodes; - an
EditKind—Insert(before the target),Replace, orRemove; - a
payload— replacement source text (parsed into a fresh subtree), absent forRemove; - a
TriviaPolicy— whether the adjacent leading / trailing trivia of the target is kept or discarded (AD-004).
§How a new tree is built (green-node splicing)
Every edit reduces to rebuilding one parent node’s child list and then
re-rooting the tree with rowan::SyntaxNode::replace_with, which rebuilds
only the spine from that parent up to the root (cost ∝ tree depth) and reuses
every other subtree verbatim. The payload text is lexed into raw green tokens
(not re-parsed structurally) so the spliced bytes are preserved exactly and
the surrounding tree keeps printing byte-for-byte.
The result is wrapped back into a fresh CstDocument. Diagnostics from the
original parse are not carried over — the edited tree is a new document
whose diagnostics (if any) would come from re-parsing; for OBJ4 we expose the
spliced tree with an empty diagnostics set (the tree is still fully
printable, INV-8). Re-validation is a later-epic concern.
Structs§
- Edit
Operation - A single non-destructive edit over a
CstDocument(AD-004). - Trivia
Policy - Caller-chosen trivia handling for an edit (AD-004).
Enums§
- Edit
Error - Why an
apply_editcall could not produce a tree. - Edit
Kind - The kind of edit to perform (AD-004).
- Edit
Target - What an
EditOperationacts on (AD-004).
Functions§
- apply_
edit - Apply
edittodoc, returning a newCstDocument(non-destructive).