Skip to main content

Module edit

Module edit 

Source
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 whole SyntaxNode or a token span [first ..= last] of adjacent sibling tokens/nodes;
  • an EditKindInsert (before the target), Replace, or Remove;
  • a payload — replacement source text (parsed into a fresh subtree), absent for Remove;
  • 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§

EditOperation
A single non-destructive edit over a CstDocument (AD-004).
TriviaPolicy
Caller-chosen trivia handling for an edit (AD-004).

Enums§

EditError
Why an apply_edit call could not produce a tree.
EditKind
The kind of edit to perform (AD-004).
EditTarget
What an EditOperation acts on (AD-004).

Functions§

apply_edit
Apply edit to doc, returning a new CstDocument (non-destructive).