Skip to main content

incremental_reparse

Function incremental_reparse 

Source
pub fn incremental_reparse(old_tree: &SyntaxNode, edit: &TextEdit) -> Parse
Expand description

Reparse after applying edit to old_tree’s source.

§Implementation — smart sub-tree splice with whole-file fallback

When the incremental feature is enabled (default-on, cy-li5):

  1. Locate the smallest STATEMENT node in old_tree that fully contains edit.range via rowan::SyntaxNode::covering_element and an upward walk to the nearest STATEMENT ancestor.
  2. Reconstruct the new text for that statement’s span by stitching old_text[stmt.start..edit.start] + edit.replacement + old_text[edit.end..stmt.end].
  3. Lex the candidate statement text in isolation. If a top-level ; or UNION appears inside the new text, the edit changed the statement count — bail to whole-file.
  4. Parse the candidate text as a wrapped source-file, extract the STATEMENT green sub-tree, and splice it in via rowan::SyntaxNode::replace_with.
  5. Re-derive errors by full re-parse of the new source. This step keeps the public-API invariant (errors match what parse(new_src) would produce) honest. A future tranche may incrementally reconcile errors, but that is not a cy-li5 deliverable — correctness gates the optimization.

If any of the bail conditions trip (no enclosing STATEMENT, edit straddles a ;, top-level separator introduced/removed, candidate text fails to parse to a single STATEMENT), the implementation falls back to a whole-file parse. The whole-file path is also taken unconditionally when the incremental feature is disabled — that behaviour is preserved as the slow-but-always-correct A/B baseline (cy-zv0).

§Caveat — bench observability

The bench_incremental_edit 2k/1k ratio gate is driven by Database::edit_file, which today reduces this function’s return value to its syntax().to_string() and feeds the string back into Salsa. As a result, the green-tree splice savings inside this function are not yet observable end-to-end at the bench. Threading the precomputed Parse into Salsa as a memo is a separate follow-up tranche; the cy-li5 acceptance criterion that the bench ratio drops below 1.5× depends on that wiring landing.