fatou_parser/parser/reparse.rs
1//! Incremental reparse: turn a text edit plus the previous parse into a new
2//! tree without re-lexing and re-parsing the whole file.
3//!
4//! The cheapest strategy runs first, with a full
5//! [`parse`](crate::parser::parse) as the always-correct fallback. Three tiers are
6//! available:
7//! the token tier ([`reparse_token`]) relexes an edit confined to a single
8//! `Ident`, comment, or whitespace leaf in isolation and splices it into the
9//! previous green tree ([`try_splice_plain_token`]), or — for a `StringContent`
10//! leaf, whose lexing depends on the enclosing delimiter's mode — relexes the
11//! whole enclosing string literal in isolation and requires it to reproduce its
12//! own token run ([`try_splice_string_content`]); the nested-block tier
13//! ([`reparse_block`]) reparses interior statements with the enclosing body's
14//! grammar, including module `public` declarations, docstrings, and struct
15//! fields; the top-level statement tier
16//! ([`reparse_toplevel`]) reparses the run of `ROOT` children touching the edit
17//! with the public [`parse`](crate::parser::parse) and splices the resulting
18//! children and diagnostics in place, provided the boundary guards prove the
19//! region decoupled from its neighbors.
20//!
21//! Two entry points sit above those tiers. [`reparse_edits`] takes the precise
22//! chain the language server staged from a `didChange` batch and replays it one
23//! edit at a time; [`reparse`] takes a single edit, which is what the host's
24//! `fatou::incremental` recovers with [`diff_edit`] from a pair of whole texts
25//! when no chain is available. The chain is tried first: a collapsed diff of
26//! scattered edits spans everything between them, and the region that produces
27//! is wide enough that [`region_is_too_wide`] declines it outright, where
28//! replaying the chain splices each edit on its own. The Tenet-4 oracle
29//! ([`assert_matches_full_parse`] plus `tests/incremental_reparse.rs`) checks
30//! every splice against a full parse.
31//!
32//! **Correctness invariant (Tenet 4):** a successful reparse must yield a
33//! green tree *and* diagnostics byte-identical to a full parse of the edited
34//! text. A reparse is an output-pure performance hint: the salsa layer only
35//! ever sees text changes, and a miss is a full parse, never an error.
36//!
37//! # Why the `STRING_CONTENT` splice is sound
38//!
39//! [`try_splice_string_content`] relexes the whole enclosing string literal
40//! rather than the edited leaf, and the argument that this preserves the tree
41//! is the subtlest one in the module, so it is recorded here in full.
42//!
43//! Nothing outside the lexer reads `StringContent` *text*: the parser only
44//! pushes the token, docstring eligibility tests the first token's kind, and the
45//! flag passes are kind-based — so preserving the node's token-kind sequence
46//! preserves both the tree and the diagnostics. (Nor does triple-quoted
47//! dedenting intrude: that lives in the test-only JuliaSyntax projector, which
48//! rebuilds the string at projection time.) Given that:
49//!
50//! * The bytes before the node are unchanged and the lexer is a deterministic
51//! left-to-right machine, so its mode stack at the node start is identical.
52//! That subsumes the backward join probe.
53//! * The relex proves the kinds are unchanged and that only the edited leaf's
54//! end, and the ends after it, moved by the delta. The bytes after the leaf
55//! *within* the node are therefore byte-identical — including the close
56//! delimiter and any suffix — so the mode stack is back to its starting depth
57//! at the node end and the rest of the file lexes identically, merely shifted.
58//! That subsumes the forward join probe, which could not have expressed a
59//! delimiter-plus-suffix tail with its one probe character anyway.
60//!
61//! The second bullet needs the literal to be *terminated*, which gets its own
62//! check rather than leaning on an `UnterminatedLiteral` diagnostic's anchor.
63//!
64//! The faithfulness relex (the unedited node text must reproduce the node's own
65//! tokens) is what makes that bullet an induction rather than an assumption: an
66//! isolated [`lex`] starts from an empty mode stack and no token history, while
67//! in context the node may sit under a `Str`/`Interp` frame and
68//! `prev_ends_value` consults the preceding token. Proving faithfulness for the
69//! old text, plus an identical kind sequence, carries it to the new text.
70
71use rowan::{GreenNode, GreenToken, TextRange, TextSize, TokenAtOffset};
72
73use crate::parser::diagnostics::DIAGNOSTIC_STREAMS;
74use crate::parser::{ParseDiagnostic, ParseOptions};
75
76use crate::parser::tree_builder::syntax_kind_for;
77use crate::syntax::{SyntaxKind, SyntaxNode, SyntaxToken};
78
79/// The edit currency, defined in the sibling `edit` module and re-exported
80/// here so `parser::Edit` stays the path the parser layer uses.
81pub use super::edit::{Edit, apply_edits, diff_edit, try_apply_edits};
82
83/// Structural fingerprint of a tree: one line per descendant element with
84/// `kind@range` plus the token text (empty for nodes). Two trees with equal
85/// fingerprints are byte-identical. Oracle/debug support: the Tenet-4 assert
86/// in [`reparse`] and the `tests/incremental_reparse.rs` harness share this
87/// definition so they can never diverge.
88pub fn fingerprint(node: &SyntaxNode) -> String {
89 use std::fmt::Write;
90
91 let mut out = String::new();
92 for el in node.descendants_with_tokens() {
93 let text = el
94 .as_token()
95 .map(|t| t.text().to_string())
96 .unwrap_or_default();
97 let _ = writeln!(out, "{:?}@{:?} {:?}", el.kind(), el.text_range(), text);
98 }
99 out
100}
101
102/// Which strategy produced a [`Reparsed`]. Surfaced for tests and benchmarks.
103/// Ordered cheapest-first, so a chain of reparses can report the most
104/// expensive tier it had to reach with [`Ord::max`].
105#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
106pub enum ReparseTier {
107 /// A single leaf token was relexed in isolation and spliced in place.
108 Token,
109 /// A run of statements inside a block was reparsed in its body context.
110 Block,
111 /// A run of top-level statements was reparsed and spliced under `ROOT`.
112 TopLevel,
113}
114
115/// The result of a successful incremental reparse: the new whole-file green
116/// tree and its parse diagnostics (absolute offsets in the new text).
117#[derive(Debug, Clone)]
118pub struct Reparsed {
119 pub green: GreenNode,
120 pub diagnostics: Vec<ParseDiagnostic>,
121 pub tier: ReparseTier,
122}
123
124/// Attempt an incremental reparse of the previous parse (`prev_green`, parsed
125/// from `prev_text` with `prev_diags`) under `edit`, which transforms
126/// `prev_text` into `new_text`. Returns `None` when no incremental strategy
127/// applies — the caller must then do a full parse.
128///
129/// In debug builds every successful reparse is checked against a full parse
130/// of `new_text` (Tenet 4): tree fingerprint and diagnostics vector must be
131/// identical.
132///
133/// `edit` is not trusted, for the same reason [`reparse_edits`]'s chain is
134/// not: one that does not fit `prev_text` — a stale range, a buffer that moved
135/// underneath it — is rejected here rather than applied at offsets that would
136/// panic on a slice.
137fn reparse_same_options(
138 options: ParseOptions,
139 prev_text: &str,
140 prev_green: &GreenNode,
141 prev_diags: &[ParseDiagnostic],
142 edit: &Edit,
143 new_text: &str,
144) -> Option<Reparsed> {
145 if !fits(prev_text, edit) {
146 return None;
147 }
148 let result = reparse_impl(options, prev_text, prev_green, prev_diags, edit, new_text)?;
149 assert_matches_full_parse(options, &result, new_text);
150 Some(result)
151}
152
153/// Attempt an incremental reparse across a *chain* of edits, each expressed
154/// against the text its predecessors produced — the shape an LSP `didChange`
155/// batch arrives in (see `fatou::text::apply_content_changes` host-side).
156///
157/// This is for the case [`diff_edit`] cannot express: when the edits are
158/// scattered, collapsing them into one spanning edit covers everything between
159/// the first and the last change and blows the region tiers' guards, while replaying
160/// them one at a time keeps each splice small. Try this *before* the collapsed
161/// edit: a wide span is the one shape the collapsed edit can never answer, so
162/// offering it first is the difference between a hit and a
163/// `region_is_too_wide` bail (`benches/reparse.rs`).
164///
165/// A single edit is verified against both texts without allocating an
166/// intermediate buffer, then passed directly to [`reparse`]. This avoids
167/// recovering an already-known edit with [`diff_edit`]. An empty chain returns
168/// `None`.
169///
170/// `edits` is not trusted: a slice that is not the exact transform from
171/// `prev_text` to `new_text` (a stale chain, a buffer that moved underneath
172/// it) is rejected rather than applied at meaningless offsets. Each step is
173/// proven to `fits` the text its predecessors produced *before* that text is
174/// sliced, and the composed result is proven to be `new_text` before anything
175/// is returned — so a stale chain costs at most the splices it got through,
176/// bounded by the caller's chain budget (`incremental::MAX_CHAIN_EDITS`).
177///
178/// Validating step by step rather than pre-folding the whole chain with
179/// [`try_apply_edits`] keeps the replay to one application: the fold would have
180/// to redo every `replace_range` the loop below already does, which costs the
181/// same order as the splice it is guarding (`benches/reparse.rs`,
182/// `precise_chain`).
183fn reparse_edits_same_options(
184 options: ParseOptions,
185 prev_text: &str,
186 prev_green: &GreenNode,
187 prev_diags: &[ParseDiagnostic],
188 edits: &[Edit],
189 new_text: &str,
190) -> Option<Reparsed> {
191 if edits.is_empty() {
192 return None;
193 }
194 if let [edit] = edits {
195 if !fits(prev_text, edit) {
196 return None;
197 }
198 // Prove the exact transform without rebuilding the buffer or scanning
199 // for edit boundaries the caller already supplied.
200 let suffix = new_text
201 .strip_prefix(&prev_text[..edit.range.start])?
202 .strip_prefix(&edit.insert)?;
203 if suffix != &prev_text[edit.range.end..] {
204 return None;
205 }
206 return reparse_same_options(options, prev_text, prev_green, prev_diags, edit, new_text);
207 }
208
209 let mut text = prev_text.to_string();
210 let mut green = prev_green.clone();
211 let mut diagnostics = prev_diags.to_vec();
212 let mut tier = ReparseTier::Token;
213
214 for edit in edits {
215 if !fits(&text, edit) {
216 return None;
217 }
218 let next = edit.apply(&text);
219 let step = reparse_impl(options, &text, &green, &diagnostics, edit, &next)?;
220 tier = tier.max(step.tier);
221 text = next;
222 green = step.green;
223 diagnostics = step.diagnostics;
224 }
225
226 // The chain applied cleanly, but that alone does not make it the transform
227 // the caller is asking about: a stale chain can fit and still land
228 // somewhere else entirely. This check is what the Tenet-4 assert below
229 // relies on to be comparing the same two texts.
230 if text != new_text {
231 return None;
232 }
233
234 let result = Reparsed {
235 green,
236 diagnostics,
237 tier,
238 };
239 assert_matches_full_parse(options, &result, new_text);
240 Some(result)
241}
242
243/// Tenet 4: a successful reparse yields a green tree *and* diagnostics
244/// byte-identical to a full parse of the edited text.
245///
246/// Debug builds only — this runs a whole `parse` plus two whole-tree
247/// [`fingerprint`] builds, so it is far too expensive to leave on. A chain
248/// asserts once on its composed result rather than once per step, which would
249/// otherwise make a debug-build language server unusable on a large file.
250#[cfg(debug_assertions)]
251fn assert_matches_full_parse(options: ParseOptions, result: &Reparsed, new_text: &str) {
252 let full = crate::parser::parse_with_options(new_text, options);
253 debug_assert_eq!(
254 fingerprint(&SyntaxNode::new_root(result.green.clone())),
255 fingerprint(&full.cst),
256 "Tenet 4: reparse ({:?}) tree differs from full parse",
257 result.tier,
258 );
259 debug_assert_eq!(
260 result.diagnostics, full.diagnostics,
261 "Tenet 4: reparse ({:?}) diagnostics differ from full parse",
262 result.tier,
263 );
264}
265
266#[cfg(not(debug_assertions))]
267fn assert_matches_full_parse(_options: ParseOptions, _result: &Reparsed, _new_text: &str) {}
268
269/// Whether `edit`'s range is a well-formed byte range of `text`. All tiers
270/// slice `prev_text` at the edit's offsets, so an edit that does not fit is a
271/// panic rather than a miss.
272fn fits(text: &str, edit: &Edit) -> bool {
273 edit.range.start <= edit.range.end
274 && edit.range.end <= text.len()
275 && text.is_char_boundary(edit.range.start)
276 && text.is_char_boundary(edit.range.end)
277}
278
279/// Tier dispatch: cheapest first.
280fn reparse_impl(
281 options: ParseOptions,
282 prev_text: &str,
283 prev_green: &GreenNode,
284 prev_diags: &[ParseDiagnostic],
285 edit: &Edit,
286 new_text: &str,
287) -> Option<Reparsed> {
288 reparse_token(options, prev_text, prev_green, prev_diags, edit)
289 .or_else(|| reparse_block(options, prev_text, prev_green, prev_diags, edit, new_text))
290 .or_else(|| reparse_toplevel(options, prev_text, prev_green, prev_diags, edit, new_text))
291}
292
293/// Leaf kinds proved by the isolated single-token relex plus the two join
294/// probes ([`try_splice_plain_token`]). Their text never spans a newline and
295/// their lexing is local enough for those probes to prove a splice sound.
296/// Deliberately absent: `NEWLINE` (statement structure), `CHAR` and the
297/// numeric kinds (quote-context and juxtaposition traps outweigh the win), and
298/// the string prefix/suffix kinds (glued to their delimiters).
299/// `STRING_CONTENT` is handled too, but by a different proof — its lexing
300/// depends on the enclosing delimiter's mode, so it takes the whole-literal
301/// relex in [`try_splice_string_content`] instead.
302const TOKEN_REPARSE_KINDS: &[SyntaxKind] = &[
303 SyntaxKind::IDENT,
304 SyntaxKind::COMMENT,
305 SyntaxKind::BLOCK_COMMENT,
306 SyntaxKind::WHITESPACE,
307];
308
309/// Literal node kinds a `STRING_CONTENT` leaf may sit directly under. Error
310/// recovery can leave one under an `ERROR` node instead, which
311/// [`try_splice_string_content`] rejects: the proof needs the delimiters.
312const STRING_LITERAL_KINDS: &[SyntaxKind] = &[
313 SyntaxKind::STRING_LITERAL,
314 SyntaxKind::CMD_LITERAL,
315 // `var"…"`, a non-standard *identifier*. Nothing keys on its content text
316 // either, so a content edit is as safe here as in a plain string.
317 SyntaxKind::NONSTANDARD_IDENTIFIER,
318];
319
320/// Identifier texts the parser treats structurally even though they lex as
321/// plain `Ident`, so a same-kind relex proves nothing about the tree shape.
322/// True keywords (`where`, `mutable`, …) need no entry: they lex as their own
323/// kinds, and the same-kind guard rejects them.
324///
325/// This list is the one guard here with no compile-time link to what it
326/// describes, and the Tenet-4 oracle only catches an omission if a seeded edit
327/// happens to spell the missing word — so
328/// [`every_contextual_ident_is_listed`](tests::every_contextual_ident_is_listed)
329/// scans the grammar for `Ident`-text comparisons and fails if one names a word
330/// that is not here.
331const CONTEXTUAL_IDENTS: &[&str] = &[
332 "as", // import alias (structural.rs)
333 "abstract", // `abstract type` (expr.rs)
334 "primitive", // `primitive type` (expr.rs)
335 "type", // the `type` in `abstract`/`primitive` type decls (expr.rs)
336 "typegroup", // `typegroup` declaration (expr.rs)
337 "public", // top-level `public` statement (expr.rs)
338 "var", // `var"..."` quoting prefix (expr.rs)
339 "in", // iteration spec / infix operator (expr.rs)
340 "∈", // Unicode `in` (expr.rs)
341 "isa", // infix operator (expr.rs)
342 "doc", // `doc"..."` / `@doc` marker (expr.rs)
343 "outer", // `for outer i in xs` iteration spec (expr.rs)
344];
345
346/// The token tier: when the edit is confined to a single eligible leaf, relex
347/// that leaf in isolation and splice it via [`SyntaxToken::replace_with`].
348///
349/// A pure insertion at a token boundary is ambiguous (it could extend either
350/// neighbor), so both candidates are tried, left first; whichever passes the
351/// guards yields the same new file text, and the guards plus the Tenet-4
352/// oracle arbitrate tree equality. Any guard failure returns `None`, which
353/// the caller answers with a full parse, so every bail here is safe.
354fn reparse_token(
355 options: ParseOptions,
356 prev_text: &str,
357 prev_green: &GreenNode,
358 prev_diags: &[ParseDiagnostic],
359 edit: &Edit,
360) -> Option<Reparsed> {
361 let root = SyntaxNode::new_root(prev_green.clone());
362 let (s, e) = (edit.range.start, edit.range.end);
363 let mut candidates: Vec<SyntaxToken> = Vec::with_capacity(2);
364 if s == e {
365 match root.token_at_offset(TextSize::new(s as u32)) {
366 TokenAtOffset::None => return None,
367 TokenAtOffset::Single(t) => candidates.push(t),
368 TokenAtOffset::Between(l, r) => candidates.extend([l, r]),
369 }
370 } else {
371 // A range spanning a token boundary covers a node, not a token, and
372 // bails here.
373 let range = TextRange::new(TextSize::new(s as u32), TextSize::new(e as u32));
374 candidates.push(root.covering_element(range).into_token()?);
375 }
376
377 candidates
378 .into_iter()
379 .find_map(|token| try_splice_token(options, prev_text, prev_diags, edit, &token))
380}
381
382/// Dispatch one candidate leaf to the proof its kind takes.
383fn try_splice_token(
384 options: ParseOptions,
385 prev_text: &str,
386 prev_diags: &[ParseDiagnostic],
387 edit: &Edit,
388 token: &SyntaxToken,
389) -> Option<Reparsed> {
390 match token.kind() {
391 SyntaxKind::STRING_CONTENT => {
392 try_splice_string_content(options, prev_text, prev_diags, edit, token)
393 }
394 kind if TOKEN_REPARSE_KINDS.contains(&kind) => {
395 try_splice_plain_token(options, prev_text, prev_diags, edit, token)
396 }
397 _ => None,
398 }
399}
400
401/// Run the stage-2 guards against one candidate leaf and splice on success.
402fn try_splice_plain_token(
403 options: ParseOptions,
404 prev_text: &str,
405 prev_diags: &[ParseDiagnostic],
406 edit: &Edit,
407 token: &SyntaxToken,
408) -> Option<Reparsed> {
409 // Newline ban: these kinds either cannot contain a newline or (block
410 // comments) rarely see one edited, and a newline moves statement
411 // boundaries, so bail early. The single-token relex guard below would
412 // catch most of these anyway; this is the cheap early-out. It does not
413 // apply to `STRING_CONTENT`, whose run legitimately spans newlines and
414 // whose proof does not depend on line structure.
415 if edit.insert.contains(['\n', '\r']) || prev_text[edit.range.clone()].contains(['\n', '\r']) {
416 return None;
417 }
418
419 let tr = token.text_range();
420 let (t0, t1) = (usize::from(tr.start()), usize::from(tr.end()));
421
422 // The leaf's new text, with the edit applied at its relative offset. A
423 // whole-leaf deletion leaves it empty, which the relex guard rejects.
424 let mut new_leaf = token.text().to_string();
425 new_leaf.replace_range((edit.range.start - t0)..(edit.range.end - t0), &edit.insert);
426
427 // Isolated relex: still exactly one token, same kind, spanning all of it.
428 // This alone rejects kind flips (ident ⇒ keyword, whitespace ⇒ newline)
429 // and splits (`=#` typed into a block comment).
430 let relexed = lex_for_options(options, &new_leaf);
431 let [only] = relexed.as_slice() else {
432 return None;
433 };
434 if only.start != 0 || only.end != new_leaf.len() || syntax_kind_for(only.kind) != token.kind() {
435 return None;
436 }
437
438 // Contextual identifiers parse structurally, so text changes into or out
439 // of one can reshape the tree under an unchanged token kind.
440 if token.kind() == SyntaxKind::IDENT
441 && (CONTEXTUAL_IDENTS.contains(&token.text())
442 || CONTEXTUAL_IDENTS.contains(&new_leaf.as_str()))
443 {
444 return None;
445 }
446
447 // Forward join: the next source character must not extend the token
448 // (`r` + `"…"` fusing into a prefixed string, an unterminated block
449 // comment swallowing its neighbor). At EOF probe with `\n`, which no
450 // eligible kind can absorb, so a leaf left unterminated at EOF (`#=` typed
451 // into the last block comment) still fails here.
452 let next_char = prev_text[t1..].chars().next().unwrap_or('\n');
453 let mut probe = new_leaf.clone();
454 probe.push(next_char);
455 let first = lex_for_options(options, &probe).into_iter().next()?;
456 if first.end != new_leaf.len() || syntax_kind_for(first.kind) != token.kind() {
457 return None;
458 }
459
460 // Backward join: the previous leaf plus the new text must relex to the
461 // same two tokens. Catches juxtaposition fusing (`2` + `e10` ⇒ one
462 // `Float`), and conservatively bails whenever the previous leaf's
463 // isolated lex diverges from its in-context one (e.g. a closing string
464 // delimiter reopening as a string).
465 if let Some(prev) = token.prev_token() {
466 let mut probe = prev.text().to_string();
467 let boundary = probe.len();
468 probe.push_str(&new_leaf);
469 let relexed = lex_for_options(options, &probe);
470 let [a, b] = relexed.as_slice() else {
471 return None;
472 };
473 if a.end != boundary
474 || syntax_kind_for(a.kind) != prev.kind()
475 || b.end != probe.len()
476 || syntax_kind_for(b.kind) != token.kind()
477 {
478 return None;
479 }
480 }
481
482 // Keep diagnostic remapping trivial: bail when any diagnostic touches the
483 // leaf, boundaries included. A clean same-kind relex introduces none of
484 // its own, so the rest keep their meaning and only need shifting.
485 if prev_diags.iter().any(|d| d.start <= t1 && d.end >= t0) {
486 return None;
487 }
488 let delta = edit.delta();
489 let shift = |pos: usize| (pos as isize + delta) as usize;
490 let diagnostics = prev_diags
491 .iter()
492 .map(|d| {
493 let mut d = d.clone();
494 if d.start >= t1 {
495 d.start = shift(d.start);
496 d.end = shift(d.end);
497 }
498 d
499 })
500 .collect();
501
502 let green = token.replace_with(GreenToken::new(token.kind().into(), &new_leaf));
503 Some(Reparsed {
504 green,
505 diagnostics,
506 tier: ReparseTier::Token,
507 })
508}
509
510/// `text` — the slice of the previous buffer starting at absolute offset
511/// `base` — with `edit` applied at its relative offset.
512fn edited_slice(text: &str, base: usize, edit: &Edit) -> String {
513 let mut out = text.to_string();
514 out.replace_range(
515 (edit.range.start - base)..(edit.range.end - base),
516 &edit.insert,
517 );
518 out
519}
520
521/// Every token under `node` as `(kind, end)`, with `end` relative to the node
522/// start. Tokens tile the node's text contiguously from `0` and the tree
523/// synthesizes none of its own (`tree_builder`), so this *is* the lexer's
524/// token run for that span and the ends alone pin every range.
525fn literal_token_ends(node: &SyntaxNode) -> Vec<(SyntaxKind, usize)> {
526 let base = usize::from(node.text_range().start());
527 node.descendants_with_tokens()
528 .filter_map(|el| el.into_token())
529 .map(|t| (t.kind(), usize::from(t.text_range().end()) - base))
530 .collect()
531}
532
533/// Whether an isolated [`lex`] of `text` reproduces `expected`: the same kinds
534/// in the same order, with the same ends, covering all of `text`. `grown`
535/// names the index whose end — and every end after it — is expected to have
536/// moved by that delta.
537fn relex_matches(
538 options: ParseOptions,
539 text: &str,
540 expected: &[(SyntaxKind, usize)],
541 grown: Option<(usize, isize)>,
542) -> bool {
543 let tokens = lex_for_options(options, text);
544 if tokens.len() != expected.len() {
545 return false;
546 }
547 for (i, (got, &(kind, end))) in tokens.iter().zip(expected).enumerate() {
548 let shift = match grown {
549 Some((from, delta)) if i >= from => delta,
550 _ => 0,
551 };
552 // A shift that runs an end past zero cannot describe any token, so it
553 // simply fails to match rather than wrapping into a huge `usize`.
554 let Ok(want) = usize::try_from(end as isize + shift) else {
555 return false;
556 };
557 if syntax_kind_for(got.kind) != kind || got.end != want {
558 return false;
559 }
560 }
561 tokens.last().is_some_and(|t| t.end == text.len())
562}
563
564/// The token tier's `STRING_CONTENT` path: relex the *whole enclosing string
565/// literal* in isolation and require it to reproduce its own token run with
566/// only the edited content leaf resized.
567///
568/// A content leaf cannot take [`try_splice_plain_token`]'s proof, because both
569/// of its probes assume a leaf that lexes the same standing alone: `abc` in
570/// isolation is an `Ident`, never `StringContent`, and a leaf following an
571/// interpolation (`"a$x b"`) has an `Ident` before it, which blows the
572/// two-token backward probe. Taking the delimiters along instead puts the
573/// isolated lexer into the right mode for free, so triple/raw/prefixed need no
574/// hand-written character table.
575///
576/// The soundness argument — why relexing the literal preserves the tree and the
577/// diagnostics — is in the module docs under *Why the `STRING_CONTENT` splice is
578/// sound*.
579///
580/// Newlines are allowed here, unlike in [`try_splice_plain_token`]: a content
581/// run spans them freely (a triple-quoted body, and a single-quoted one too —
582/// no newline stops the run, which is why an unterminated literal reaches EOF),
583/// and a newline *inside* one emits no `NEWLINE` token, so no statement
584/// boundary moves. Diagnostics are byte offsets and do not care about lines.
585/// That is what puts Enter in a docstring on this tier.
586fn try_splice_string_content(
587 options: ParseOptions,
588 prev_text: &str,
589 prev_diags: &[ParseDiagnostic],
590 edit: &Edit,
591 token: &SyntaxToken,
592) -> Option<Reparsed> {
593 let node = token.parent()?;
594 if !STRING_LITERAL_KINDS.contains(&node.kind()) {
595 return None;
596 }
597 let nr = node.text_range();
598 let (n0, n1) = (usize::from(nr.start()), usize::from(nr.end()));
599
600 // Bail when any diagnostic touches the literal, boundaries included. The
601 // relex proves the token sequence unchanged, so the four string-related
602 // kinds would in fact survive a shift — but "no diagnostic may touch the
603 // literal we splice" is an invariant that keeps holding as diagnostics are
604 // added, and what it gives up (`x = "abc`, `"a"b`, `var"x"y`) is the error
605 // path, not the keystroke path.
606 //
607 // This runs *first*, before either relex: an unterminated literal runs to
608 // EOF, so its node is the rest of the file, and every keystroke inside one
609 // would otherwise pay two whole-file lexes to learn what a diagnostic
610 // already says (`benches/reparse.rs`, `rejected_attempt`).
611 if prev_diags.iter().any(|d| d.start <= n1 && d.end >= n0) {
612 return None;
613 }
614
615 let expected = literal_token_ends(&node);
616 let tr = token.text_range();
617 let (t0, t1) = (usize::from(tr.start()), usize::from(tr.end()));
618 // Ends are unique (tokens tile with positive width), so this identifies the
619 // leaf by index — which the edited relex then pins as the one that resized.
620 let leaf = expected
621 .iter()
622 .position(|&(kind, end)| kind == SyntaxKind::STRING_CONTENT && end == t1 - n0)?;
623
624 // Terminated: a matching close delimiter must follow the leaf. A prefixed
625 // literal's suffix is lexed as part of the same node, so nothing but the
626 // close can end the run.
627 let close = if node.kind() == SyntaxKind::CMD_LITERAL {
628 SyntaxKind::CMD_DELIM_CLOSE
629 } else {
630 SyntaxKind::STRING_DELIM_CLOSE
631 };
632 if !expected[leaf + 1..].iter().any(|&(kind, _)| kind == close) {
633 return None;
634 }
635
636 let old_node_text = &prev_text[n0..n1];
637 if !relex_matches(options, old_node_text, &expected, None) {
638 return None;
639 }
640 let delta = edit.delta();
641 let edited = edited_slice(old_node_text, n0, edit);
642 if !relex_matches(options, &edited, &expected, Some((leaf, delta))) {
643 return None;
644 }
645
646 let new_leaf = &edited[(t0 - n0)..((t1 - n0) as isize + delta) as usize];
647
648 // Every surviving diagnostic lies wholly outside the literal, so the ones
649 // past it shift and the rest keep their positions.
650 let shift = |pos: usize| (pos as isize + delta) as usize;
651 let diagnostics = prev_diags
652 .iter()
653 .map(|d| {
654 let mut d = d.clone();
655 if d.start >= n1 {
656 d.start = shift(d.start);
657 d.end = shift(d.end);
658 }
659 d
660 })
661 .collect();
662
663 let green = token.replace_with(GreenToken::new(SyntaxKind::STRING_CONTENT.into(), new_leaf));
664 Some(Reparsed {
665 green,
666 diagnostics,
667 tier: ReparseTier::Token,
668 })
669}
670
671/// Whether a token kind is trivia. Mirrors `TokKind::is_trivia` on the CST
672/// side.
673fn is_trivia_kind(kind: SyntaxKind) -> bool {
674 matches!(
675 kind,
676 SyntaxKind::WHITESPACE
677 | SyntaxKind::NEWLINE
678 | SyntaxKind::COMMENT
679 | SyntaxKind::BLOCK_COMMENT
680 )
681}
682
683/// Whether a `ROOT` child carries statement-level content: a positive-width
684/// node, or a loose non-trivia token (the unparseable-token recovery emits
685/// bare tokens at the root, without an `ERROR` wrapper). Zero-width nodes and
686/// trivia are gap material.
687fn is_significant_child(el: &crate::syntax::SyntaxElement) -> bool {
688 match el {
689 rowan::NodeOrToken::Node(n) => !n.text_range().is_empty(),
690 rowan::NodeOrToken::Token(t) => !is_trivia_kind(t.kind()),
691 }
692}
693
694/// Below this the tier always tries, whatever fraction of the file the region
695/// is: on a small file every parse in play is small, and the fraction test
696/// alone would refuse the common case of a file with one or two statements.
697const REGION_ALWAYS_TRY_BYTES: usize = 4 * 1024;
698
699/// A region wider than `1 / REGION_MAX_FILE_DIVISOR` of the file is wide enough
700/// that answering it costs more than the full parse it is trying to avoid. A
701/// divisor rather than a fraction, so `4` here means a quarter.
702const REGION_MAX_FILE_DIVISOR: usize = 4;
703
704/// Whether a region is too wide to be worth attempting, per the two consts
705/// above. The tier answers a region with a fragment parse of it *plus* up to
706/// three neighbor-sized boundary parses, so a miss costs more than the full
707/// parse it was avoiding; bailing on the region's byte span — cheap evidence,
708/// known before any parse — caps the loss (`benches/reparse.rs`,
709/// `scattered_via_diff_edit`).
710///
711/// A performance guard only: like every other bail here it returns the caller to
712/// a full parse, so it cannot affect what a reparse yields.
713fn region_is_too_wide(region_len: usize, text_len: usize) -> bool {
714 region_len > REGION_ALWAYS_TRY_BYTES && region_len > text_len / REGION_MAX_FILE_DIVISOR
715}
716
717/// The reparse region at either statement tier: a contiguous run of container
718/// children (statement nodes and loose trivia tokens both), identified by
719/// child index and old-text byte range. The boundary child kinds
720/// disambiguate zero-width diagnostics anchored exactly on a boundary.
721struct Region {
722 first: usize,
723 last: usize,
724 start: usize,
725 end: usize,
726 first_is_node: bool,
727 last_is_node: bool,
728}
729
730/// Select the contiguous run of container children whose closed range touches
731/// the edit. Closed-interval touch makes boundary insertions extend into the
732/// neighbor they abut (blank-line typing selects the surrounding newline
733/// tokens; deleting a newline pulls in both neighbor statements). A
734/// zero-width child abutting the region start is absorbed: stray-closer
735/// recovery pairs a zero-width `ERROR` with its byte-bearing run at the same
736/// offset, and the fragment reparse regenerates both, so leaving it outside
737/// would duplicate it. `None` when the run covers every child (the fragment
738/// reparse would be the full parse) or `ROOT` is empty.
739fn select_region(root: &SyntaxNode, edit: &Edit) -> Option<Region> {
740 let (s, e) = (edit.range.start, edit.range.end);
741 let children: Vec<_> = root.children_with_tokens().collect();
742 let touches = |el: &crate::syntax::SyntaxElement| {
743 let r = el.text_range();
744 usize::from(r.start()) <= e && usize::from(r.end()) >= s
745 };
746 let mut first = children.iter().position(touches)?;
747 let last = children.len() - 1 - children.iter().rev().position(touches)?;
748
749 let start = usize::from(children[first].text_range().start());
750 while first > 0
751 && children[first - 1].text_range() == TextRange::empty(TextSize::new(start as u32))
752 {
753 first -= 1;
754 }
755
756 if first == 0 && last == children.len() - 1 {
757 return None;
758 }
759 Some(Region {
760 first,
761 last,
762 start,
763 end: usize::from(children[last].text_range().end()),
764 first_is_node: children[first].as_node().is_some(),
765 last_is_node: children[last].as_node().is_some(),
766 })
767}
768
769/// The nearest positive-width container child nodes outside the region. Trivia
770/// tokens are gap material, and a zero-width `ERROR` node contributes no text
771/// (taking one as the forward neighbor would make the boundary guard
772/// vacuous), so both are skipped.
773fn sibling_nodes(root: &SyntaxNode, region: &Region) -> (Option<SyntaxNode>, Option<SyntaxNode>) {
774 let positive_node =
775 |el: crate::syntax::SyntaxElement| el.into_node().filter(|n| !n.text_range().is_empty());
776 let prev = root
777 .children_with_tokens()
778 .take(region.first)
779 .filter_map(positive_node)
780 .last();
781 let next = root
782 .children_with_tokens()
783 .skip(region.last + 1)
784 .find_map(positive_node);
785 (prev, next)
786}
787
788/// Whether no container child (node or loose token) of `cst` straddles `seam`.
789/// Children tile the text, so no-straddle is equivalent to a boundary
790/// existing exactly at the seam: the two sides parse independently. A
791/// straddling node is a cross-boundary statement (trailing-operator
792/// continuation, a docstring fold, an unterminated block absorbing its
793/// neighbor); a straddling token is a lexical fusion (`#=` swallowing
794/// trailing trivia, an unterminated string).
795fn no_straddle(cst: &SyntaxNode, seam: usize) -> bool {
796 let seam = TextSize::new(seam as u32);
797 cst.children_with_tokens().all(|el| {
798 let r = el.text_range();
799 !(r.start() < seam && seam < r.end())
800 })
801}
802
803/// Splice the fragment's diagnostics into the previous ones, stream by
804/// stream ([`DiagnosticKind::stream`](crate::parser::diagnostics::DiagnosticKind::stream)):
805/// keep those before the region, drop those the fragment reparse
806/// regenerates, and shift those after by the edit delta. Emission order
807/// within a stream is positional across statements (the drive loop is
808/// per-line sequential, the flag passes walk in document order), so
809/// `before ++ fragment ++ after` per stream, concatenated in stream order,
810/// reproduces a full parse's vector.
811///
812/// A zero-width diagnostic exactly on a region boundary is ambiguous by
813/// position alone and is disambiguated by the boundary child's kind: a
814/// statement node owns a start-anchored marker at `start` (`const x` at the
815/// region's front) and an end-anchored one at `end`, while a trivia boundary
816/// child owns nothing, so the marker belongs to the statement just outside.
817/// This is sound only because the decoupling guards already rejected
818/// zero-gap node adjacency at the boundaries. A diagnostic genuinely
819/// straddling a boundary aborts the splice (`None`); regions are whole
820/// children and diagnostics never outgrow their statement, so this arm
821/// should be unreachable and is purely defensive.
822///
823/// A stream index outside [`DIAGNOSTIC_STREAMS`] aborts the splice too, rather
824/// than indexing out of bounds. [`DiagnosticKind::stream`] is an exhaustive
825/// match today, so this cannot fire — but a bail returns the caller to a full
826/// parse, whereas a panic here takes down an analysis query, and this module
827/// buys its whole safety argument from every failure being a bail.
828fn splice_diagnostics(
829 prev_diags: &[ParseDiagnostic],
830 frag_diags: &[ParseDiagnostic],
831 region: &Region,
832 delta: isize,
833) -> Option<Vec<ParseDiagnostic>> {
834 enum Class {
835 Before,
836 Drop,
837 After,
838 }
839 let classify = |d: &ParseDiagnostic| -> Option<Class> {
840 let (rs, re) = (region.start, region.end);
841 if d.start == d.end && d.start == rs {
842 return Some(if region.first_is_node {
843 Class::Drop
844 } else {
845 Class::Before
846 });
847 }
848 if d.start == d.end && d.start == re {
849 return Some(if region.last_is_node {
850 Class::Drop
851 } else {
852 Class::After
853 });
854 }
855 if d.end <= rs {
856 return Some(Class::Before);
857 }
858 if d.start >= re {
859 return Some(Class::After);
860 }
861 if d.start >= rs && d.end <= re {
862 return Some(Class::Drop);
863 }
864 None
865 };
866
867 let mut before: [Vec<ParseDiagnostic>; DIAGNOSTIC_STREAMS] = Default::default();
868 let mut after: [Vec<ParseDiagnostic>; DIAGNOSTIC_STREAMS] = Default::default();
869 for d in prev_diags {
870 let class = classify(d)?;
871 let stream = d.kind.stream();
872 if stream >= DIAGNOSTIC_STREAMS {
873 debug_assert!(false, "diagnostic stream {stream} is out of range");
874 return None;
875 }
876 match class {
877 Class::Before => before[stream].push(d.clone()),
878 Class::Drop => {}
879 Class::After => {
880 let mut d = d.clone();
881 d.start = (d.start as isize + delta) as usize;
882 d.end = (d.end as isize + delta) as usize;
883 after[stream].push(d);
884 }
885 }
886 }
887
888 let mut out = Vec::with_capacity(prev_diags.len() + frag_diags.len());
889 for stream in 0..DIAGNOSTIC_STREAMS {
890 out.append(&mut before[stream]);
891 out.extend(
892 frag_diags
893 .iter()
894 .filter(|d| d.kind.stream() == stream)
895 .map(|d| {
896 let mut d = d.clone();
897 d.start += region.start;
898 d.end += region.start;
899 d
900 }),
901 );
902 out.append(&mut after[stream]);
903 }
904 Some(out)
905}
906
907/// The top-level statement tier: reparse the run of `ROOT` children touching
908/// the edit with the public [`parse`](crate::parser::parse) (which reruns
909/// `fold_docstrings` and the flag passes on the fragment) and splice the
910/// resulting children and diagnostics in place.
911///
912/// The guards, in order: decoupling scans require a newline between each
913/// untouched sibling statement and the fragment's non-trivia content, because
914/// the drive loop's per-line junk recovery couples same-line siblings; the
915/// boundary parses then prove the seam survives — the neighbor plus the
916/// actual gap bytes plus the fragment must parse with a boundary exactly at
917/// the old one, which rejects trailing-operator continuation, docstring
918/// folds, forward absorption by an unterminated block, and lexical fusion.
919/// Diagnostics splice last (see [`splice_diagnostics`]).
920fn reparse_toplevel(
921 options: ParseOptions,
922 prev_text: &str,
923 prev_green: &GreenNode,
924 prev_diags: &[ParseDiagnostic],
925 edit: &Edit,
926 new_text: &str,
927) -> Option<Reparsed> {
928 let root = SyntaxNode::new_root(prev_green.clone());
929 reparse_statements(options, prev_text, prev_diags, edit, new_text, &root, None)
930}
931
932fn reparse_block(
933 options: ParseOptions,
934 prev_text: &str,
935 prev_green: &GreenNode,
936 prev_diags: &[ParseDiagnostic],
937 edit: &Edit,
938 new_text: &str,
939) -> Option<Reparsed> {
940 let root = SyntaxNode::new_root(prev_green.clone());
941 let range = TextRange::new(
942 TextSize::try_from(edit.range.start).ok()?,
943 TextSize::try_from(edit.range.end).ok()?,
944 );
945 let covering = root.covering_element(range);
946 let node = match covering {
947 rowan::NodeOrToken::Node(node) => node,
948 rowan::NodeOrToken::Token(token) => token.parent()?,
949 };
950 // Recovery in an enclosing construct can emit a diagnostic anchored
951 // before this region *after* its children. The positional stream splice
952 // cannot reproduce that ordering, so leave damaged constructs to the
953 // enclosing-statement tier.
954 let statement = node
955 .ancestors()
956 .find(|n| n.parent().is_some_and(|p| p.kind() == SyntaxKind::ROOT))?;
957 let extent = statement.text_range();
958 if prev_diags.iter().any(|d| {
959 d.kind.stream() == 0
960 && d.start >= usize::from(extent.start())
961 && d.start <= usize::from(extent.end())
962 }) {
963 return None;
964 }
965 for block in node.ancestors().filter(|n| n.kind() == SyntaxKind::BLOCK) {
966 let owner = block.parent()?.kind();
967 if let Some(result) = reparse_statements(
968 options,
969 prev_text,
970 prev_diags,
971 edit,
972 new_text,
973 &block,
974 Some(owner),
975 ) {
976 return Some(result);
977 }
978 }
979 None
980}
981
982/// Share seam and diagnostic checks between root statements and nested bodies.
983/// A nested run needs untouched statements on both sides: this keeps the owner
984/// header, clause terminators, and body-dependent signature flags unchanged.
985#[allow(clippy::too_many_arguments)]
986fn reparse_statements(
987 options: ParseOptions,
988 prev_text: &str,
989 prev_diags: &[ParseDiagnostic],
990 edit: &Edit,
991 new_text: &str,
992 root: &SyntaxNode,
993 owner: Option<SyntaxKind>,
994) -> Option<Reparsed> {
995 let parse_fragment = |text: &str| match owner {
996 Some(kind) => super::core::parse_block_fragment(text, kind, options),
997 None => Some(crate::parser::parse_with_options(text, options)),
998 };
999 let selection;
1000 let edit_region = if owner.is_some() {
1001 // A semicolon separates block statements without wrapping them in a
1002 // top-level semicolon node. Include their whole physical line so the
1003 // existing newline decoupling guards can bound the run.
1004 let start = usize::from(root.text_range().start());
1005 let end = usize::from(root.text_range().end());
1006 let line_start = prev_text[start..edit.range.start]
1007 .rfind('\n')
1008 .map_or(start, |at| start + at + 1);
1009 let line_end = prev_text[edit.range.end..end]
1010 .find('\n')
1011 .map_or(end, |at| edit.range.end + at);
1012 selection = Edit {
1013 range: line_start..line_end,
1014 insert: String::new(),
1015 };
1016 &selection
1017 } else {
1018 edit
1019 };
1020 let region = select_region(root, edit_region)?;
1021 if region_is_too_wide(region.end - region.start, prev_text.len()) {
1022 return None;
1023 }
1024 let delta = edit.delta();
1025 let frag_end = (region.end as isize + delta) as usize;
1026 let fragment = &new_text[region.start..frag_end];
1027
1028 let (prev_node, next_node) = sibling_nodes(root, ®ion);
1029 if owner.is_some() {
1030 // Interior neighbors bound both seams without reparsing the owner's
1031 // header or closing clause. Edge edits climb to an enclosing tier.
1032 prev_node.as_ref()?;
1033 next_node.as_ref()?;
1034 let old = parse_fragment(&prev_text[region.start..region.end])?;
1035 let expected: Vec<_> = root
1036 .green()
1037 .children()
1038 .skip(region.first)
1039 .take(region.last + 1 - region.first)
1040 .map(|child| child.to_owned())
1041 .collect();
1042 let actual: Vec<_> = old
1043 .cst
1044 .green()
1045 .children()
1046 .map(|child| child.to_owned())
1047 .collect();
1048 if expected != actual {
1049 return None;
1050 }
1051 }
1052 let frag = parse_fragment(fragment)?;
1053
1054 let positive_nodes = |node: &SyntaxNode| {
1055 node.children_with_tokens()
1056 .filter_map(|el| el.into_node())
1057 .filter(|n| !n.text_range().is_empty())
1058 };
1059
1060 // Backward decoupling: a newline must separate the last significant
1061 // content before the region (a statement node or a loose junk token, not
1062 // just `prev_node`) from the fragment's first significant content (or
1063 // all of a trivia-only fragment). The drive loop's per-line junk
1064 // recovery couples same-line siblings, and the newline often lives in
1065 // the unchanged gap outside the region, so scan `new_text`.
1066 let prev_significant_end = root
1067 .children_with_tokens()
1068 .take(region.first)
1069 .filter(is_significant_child)
1070 .last()
1071 .map(|el| usize::from(el.text_range().end()));
1072 if let Some(from) = prev_significant_end {
1073 let anchor = frag
1074 .cst
1075 .children_with_tokens()
1076 .find(is_significant_child)
1077 .map(|el| usize::from(el.text_range().start()))
1078 .unwrap_or(fragment.len());
1079 if !new_text[from..region.start + anchor].contains('\n') {
1080 return None;
1081 }
1082 }
1083
1084 // Forward decoupling, in both texts: the next statement is kept, not
1085 // reparsed, so its line must have been decoupled from the old region
1086 // content too (deleting `x` from `x y` puts the kept junk `y` at line
1087 // start, where a full parse reads it as a plain statement). The new-text
1088 // scan anchors at the fragment's last statement *node* end, not its last
1089 // token: an unterminated bracket consumes the separating newline into
1090 // the statement, and a consumed newline decouples nothing.
1091 if let Some(next) = &next_node {
1092 let anchor = frag
1093 .cst
1094 .children_with_tokens()
1095 .filter(is_significant_child)
1096 .last()
1097 .map(|el| usize::from(el.text_range().end()))
1098 .unwrap_or(0);
1099 let to = (usize::from(next.text_range().start()) as isize + delta) as usize;
1100 if !new_text[region.start + anchor..to].contains('\n') {
1101 return None;
1102 }
1103
1104 let old_anchor = root
1105 .children_with_tokens()
1106 .skip(region.first)
1107 .take(region.last + 1 - region.first)
1108 .filter(is_significant_child)
1109 .last()
1110 .map(|el| usize::from(el.text_range().end()))
1111 .or(prev_significant_end)
1112 .unwrap_or(0);
1113 if !prev_text[old_anchor..usize::from(next.text_range().start())].contains('\n') {
1114 return None;
1115 }
1116 }
1117
1118 // Cross-region coupling: when the fragment carries no significant content,
1119 // the two neighbors sit next to each other across it and can couple in a
1120 // way neither boundary parse can see, because each concatenation contains
1121 // only one of them. Deleting the `]` from `"doc" ]\nf() = 1` is the shape:
1122 // the stray closer was all that kept the string from being the statement
1123 // below it, and once it goes `fold_docstrings` folds the pair into a `DOC`
1124 // spanning both seams. Parse the neighbors together, through the real gap
1125 // bytes, and require both seams to survive. Only reachable when the region
1126 // is reduced to trivia, so the extra parse is rare and neighbor-sized.
1127 if !frag
1128 .cst
1129 .children_with_tokens()
1130 .any(|el| is_significant_child(&el))
1131 && let (Some(prev), Some(next)) = (&prev_node, &next_node)
1132 {
1133 let back_gap = &prev_text[usize::from(prev.text_range().end())..region.start];
1134 let fwd_gap = &prev_text[region.end..usize::from(next.text_range().start())];
1135 let guard = format!(
1136 "{}{}{}{}{}",
1137 prev.text(),
1138 back_gap,
1139 fragment,
1140 fwd_gap,
1141 next.text()
1142 );
1143 let back_seam = usize::from(prev.text_range().len()) + back_gap.len();
1144 let fwd_seam = back_seam + fragment.len();
1145 let parsed = parse_fragment(&guard)?;
1146 if !no_straddle(&parsed.cst, back_seam) || !no_straddle(&parsed.cst, fwd_seam) {
1147 return None;
1148 }
1149 }
1150
1151 // A zero-width diagnostic exactly on a guard seam is disambiguated by
1152 // the fragment's own boundary child, mirroring `splice_diagnostics`: a
1153 // statement node owns a start-anchored marker at its start and an
1154 // end-anchored one at its end; a trivia boundary child owns nothing.
1155 let frag_starts_with_node = frag
1156 .cst
1157 .children_with_tokens()
1158 .next()
1159 .is_some_and(|el| el.as_node().is_some());
1160 let frag_ends_with_node = frag
1161 .cst
1162 .children_with_tokens()
1163 .last()
1164 .is_some_and(|el| el.as_node().is_some());
1165
1166 // Backward boundary parse: previous statement + gap trivia + fragment
1167 // must keep a boundary exactly at the old one, and the fragment must
1168 // parse in context with exactly the diagnostics it produced in
1169 // isolation.
1170 if let Some(prev) = &prev_node {
1171 let back_gap = &prev_text[usize::from(prev.text_range().end())..region.start];
1172 let guard = format!("{}{}{}", prev.text(), back_gap, fragment);
1173 let seam = guard.len() - fragment.len();
1174 let parsed = parse_fragment(&guard)?;
1175 if !no_straddle(&parsed.cst, seam) {
1176 return None;
1177 }
1178 let guard_frag: Vec<ParseDiagnostic> = parsed
1179 .diagnostics
1180 .iter()
1181 .filter(|d| {
1182 d.start > seam
1183 || (d.start == seam && d.end > seam)
1184 || (d.start == seam && d.end == seam && frag_starts_with_node)
1185 })
1186 .map(|d| {
1187 let mut d = d.clone();
1188 d.start -= seam;
1189 d.end -= seam;
1190 d
1191 })
1192 .collect();
1193 if guard_frag != frag.diagnostics {
1194 return None;
1195 }
1196 }
1197
1198 // Forward boundary parse: fragment + gap trivia + next statement (or the
1199 // trailing trivia to EOF when no statement follows). Three checks
1200 // against one parse of the concatenation: no element straddles the seam;
1201 // the fragment reparses exactly as it did in isolation (an EOF-adjacent
1202 // junk run wraps differently once text follows it); and the kept next
1203 // statement reappears untouched — same start, kind, and length — since a
1204 // fragment ending in an unterminated bracket can consume the separating
1205 // newline and demote the next line to flat junk without anything
1206 // straddling the seam.
1207 let tail = match &next_node {
1208 Some(next) => {
1209 let gap = &prev_text[region.end..usize::from(next.text_range().start())];
1210 format!("{}{}", gap, next.text())
1211 }
1212 None => prev_text[region.end..usize::from(root.text_range().end())].to_string(),
1213 };
1214 if !tail.is_empty() {
1215 let guard = format!("{fragment}{tail}");
1216 let seam = fragment.len();
1217 let parsed = parse_fragment(&guard)?;
1218 if !no_straddle(&parsed.cst, seam) {
1219 return None;
1220 }
1221 let signature = |el: crate::syntax::SyntaxElement| {
1222 (
1223 usize::from(el.text_range().start()),
1224 el.as_node().is_some(),
1225 el.kind(),
1226 usize::from(el.text_range().len()),
1227 )
1228 };
1229 let frag_sig: Vec<_> = frag.cst.children_with_tokens().map(signature).collect();
1230 let pre_seam: Vec<_> = parsed
1231 .cst
1232 .children_with_tokens()
1233 .filter(|el| {
1234 let r = el.text_range();
1235 // A zero-width child exactly at the seam pairs with a junk
1236 // run on the far side; it belongs to the tail.
1237 usize::from(r.end()) <= seam && !(r.is_empty() && usize::from(r.start()) == seam)
1238 })
1239 .map(signature)
1240 .collect();
1241 if pre_seam != frag_sig {
1242 return None;
1243 }
1244 // The fragment's in-context diagnostics must also match: a trailing
1245 // token at the fragment's EOF can emit differently once text follows
1246 // it (`:` with a newline after it flags `QuoteColonWhitespace`; at
1247 // EOF it does not).
1248 let guard_frag: Vec<ParseDiagnostic> = parsed
1249 .diagnostics
1250 .iter()
1251 .filter(|d| {
1252 d.end < seam
1253 || (d.end == seam && d.start < seam)
1254 || (d.start == seam && d.end == seam && frag_ends_with_node)
1255 })
1256 .cloned()
1257 .collect();
1258 if guard_frag != frag.diagnostics {
1259 return None;
1260 }
1261 if let Some(next) = &next_node {
1262 let gap_len = usize::from(next.text_range().start()) - region.end;
1263 let reparsed_next =
1264 positive_nodes(&parsed.cst).find(|n| usize::from(n.text_range().start()) >= seam);
1265 let intact = reparsed_next.is_some_and(|n| {
1266 usize::from(n.text_range().start()) == seam + gap_len
1267 && n.kind() == next.kind()
1268 && n.text_range().len() == next.text_range().len()
1269 });
1270 if !intact {
1271 return None;
1272 }
1273 }
1274 }
1275
1276 let diagnostics = splice_diagnostics(prev_diags, &frag.diagnostics, ®ion, delta)?;
1277
1278 let frag_green = frag.cst.green();
1279 let green = root.green().splice_children(
1280 region.first..=region.last,
1281 frag_green.children().map(|c| c.to_owned()),
1282 );
1283 Some(Reparsed {
1284 green: root.replace_with(green),
1285 diagnostics,
1286 tier: if owner.is_some() {
1287 ReparseTier::Block
1288 } else {
1289 ReparseTier::TopLevel
1290 },
1291 })
1292}
1293
1294/// The parse options and source from which an incremental reparse starts.
1295/// `green` and `diagnostics` must come from parsing `text` with `options`.
1296#[derive(Clone, Copy)]
1297pub struct ReparseBase<'a> {
1298 pub text: &'a str,
1299 pub green: &'a GreenNode,
1300 pub diagnostics: &'a [ParseDiagnostic],
1301 pub options: ParseOptions,
1302}
1303
1304/// Incrementally reparse a tree produced with the default parsing options.
1305pub fn reparse(
1306 prev_text: &str,
1307 prev_green: &GreenNode,
1308 prev_diags: &[ParseDiagnostic],
1309 edit: &Edit,
1310 new_text: &str,
1311) -> Option<Reparsed> {
1312 let options = ParseOptions::default();
1313 reparse_with_options(
1314 ReparseBase {
1315 text: prev_text,
1316 green: prev_green,
1317 diagnostics: prev_diags,
1318 options,
1319 },
1320 edit,
1321 new_text,
1322 options,
1323 )
1324}
1325
1326/// Reparse under the selected edition, declining a base from another edition.
1327pub fn reparse_with_options(
1328 base: ReparseBase<'_>,
1329 edit: &Edit,
1330 new_text: &str,
1331 options: ParseOptions,
1332) -> Option<Reparsed> {
1333 if base.options != options {
1334 return None;
1335 }
1336 reparse_same_options(
1337 options,
1338 base.text,
1339 base.green,
1340 base.diagnostics,
1341 edit,
1342 new_text,
1343 )
1344}
1345
1346/// Replay edits against a tree produced with the default parsing options.
1347pub fn reparse_edits(
1348 prev_text: &str,
1349 prev_green: &GreenNode,
1350 prev_diags: &[ParseDiagnostic],
1351 edits: &[Edit],
1352 new_text: &str,
1353) -> Option<Reparsed> {
1354 let options = ParseOptions::default();
1355 reparse_edits_with_options(
1356 ReparseBase {
1357 text: prev_text,
1358 green: prev_green,
1359 diagnostics: prev_diags,
1360 options,
1361 },
1362 edits,
1363 new_text,
1364 options,
1365 )
1366}
1367
1368/// Replay an edit chain under the selected edition. Edition changes require a
1369/// full parse, even when the source text has not changed.
1370pub fn reparse_edits_with_options(
1371 base: ReparseBase<'_>,
1372 edits: &[Edit],
1373 new_text: &str,
1374 options: ParseOptions,
1375) -> Option<Reparsed> {
1376 if base.options != options {
1377 return None;
1378 }
1379 reparse_edits_same_options(
1380 options,
1381 base.text,
1382 base.green,
1383 base.diagnostics,
1384 edits,
1385 new_text,
1386 )
1387}
1388
1389fn lex_for_options(options: ParseOptions, text: &str) -> Vec<super::lexer::Token<'_>> {
1390 super::core::lex_for_options(text, options)
1391}
1392
1393#[cfg(test)]
1394mod tests {
1395 use super::*;
1396
1397 fn edit(range: std::ops::Range<usize>, insert: &str) -> Edit {
1398 Edit {
1399 range,
1400 insert: insert.to_string(),
1401 }
1402 }
1403
1404 /// The grammar decides some tree shapes by an identifier's *text*, and the
1405 /// token tier's same-kind relex cannot see such a change — so every such
1406 /// word has to be in [`CONTEXTUAL_IDENTS`]. Nothing in the type system says
1407 /// so, and the oracle only surfaces an omission by luck, so scan the
1408 /// grammar for the two spellings those comparisons take and check the words
1409 /// they name.
1410 ///
1411 /// The known words are asserted to have been *found*, not just listed: if a
1412 /// comparison is ever rewritten into a shape this scan does not recognize,
1413 /// the test fails loudly rather than quietly covering nothing.
1414 #[test]
1415 fn every_contextual_ident_is_listed() {
1416 // `t.kind == TokKind::Ident && t.text == "word"` and any form that
1417 // passes `t.text` along to a comparison, `matches!(t.text, "word" |
1418 // "other")` above all. Both are keyed on `t.text`, which is what makes
1419 // them text-structural.
1420 fn words_after(haystack: &str, marker: &str) -> Vec<String> {
1421 let mut out = Vec::new();
1422 for tail in haystack.split(marker).skip(1) {
1423 // Take the literals in the comparison that follows, stopping at
1424 // the first token that ends it.
1425 let stop = tail.find([';', '{', '}']).unwrap_or(tail.len());
1426 let mut rest = &tail[..stop];
1427 while let Some(open) = rest.find('"') {
1428 let Some(close) = rest[open + 1..].find('"') else {
1429 break;
1430 };
1431 out.push(rest[open + 1..open + 1 + close].to_string());
1432 rest = &rest[open + 1 + close + 1..];
1433 }
1434 }
1435 out
1436 }
1437
1438 // The grammar files, read from source: this is a lint over the code,
1439 // not over anything the crate exposes at runtime.
1440 let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/parser");
1441 let mut found: Vec<String> = Vec::new();
1442 for name in ["expr.rs", "structural.rs", "core.rs", "recovery.rs"] {
1443 let src = std::fs::read_to_string(dir.join(name))
1444 .unwrap_or_else(|e| panic!("read {name}: {e}"));
1445 found.extend(words_after(&src, "t.text =="));
1446 found.extend(words_after(&src, "t.text,"));
1447 found.extend(words_after(&src, "next.text =="));
1448 }
1449 assert!(
1450 !found.is_empty(),
1451 "the scan matched nothing — the grammar's `Ident`-text comparisons \
1452 have been rewritten, and this guard is no longer guarding anything"
1453 );
1454
1455 for word in &found {
1456 assert!(
1457 CONTEXTUAL_IDENTS.contains(&word.as_str()),
1458 "the grammar branches on the identifier text {word:?}, so an \
1459 edit into or out of it can reshape the tree under an unchanged \
1460 token kind. Add it to CONTEXTUAL_IDENTS.",
1461 );
1462 }
1463
1464 // Every word the scan is known to reach today. A rewrite that hides one
1465 // from the scan trips this rather than silently narrowing the guard.
1466 for expected in ["as", "abstract", "primitive", "type", "typegroup", "public"] {
1467 assert!(
1468 found.iter().any(|w| w == expected),
1469 "the scan no longer finds the {expected:?} comparison",
1470 );
1471 }
1472 }
1473
1474 #[test]
1475 fn fingerprint_is_stable_and_discriminating() {
1476 let a1 = fingerprint(&crate::parser::parse("x = 1\n").cst);
1477 let a2 = fingerprint(&crate::parser::parse("x = 1\n").cst);
1478 let b = fingerprint(&crate::parser::parse("x = 2\n").cst);
1479 assert_eq!(a1, a2);
1480 assert_ne!(a1, b);
1481 }
1482
1483 /// A numeric literal is deliberately outside `TOKEN_REPARSE_KINDS`, and a
1484 /// lone `x = 1` statement is the whole `ROOT`, so the top-level tier finds
1485 /// no decoupled region either.
1486 #[test]
1487 fn a_number_edit_in_a_lone_statement_falls_back() {
1488 let parsed = crate::parser::parse("x = 1\n");
1489 let green = parsed.cst.green().to_owned();
1490 let e = diff_edit("x = 1\n", "x = 2\n");
1491 assert!(reparse("x = 1\n", &green, &parsed.diagnostics, &e, "x = 2\n").is_none());
1492 }
1493
1494 /// The point of the `STRING_CONTENT` path: a docstring body edit stays at
1495 /// the token tier. `fold_docstrings` folds a docstring with the definition
1496 /// it documents into one `ROOT` child, so the statement tier would answer
1497 /// this by reparsing the docstring *and* the whole function under it.
1498 #[test]
1499 fn a_docstring_body_edit_stays_at_the_token_tier() {
1500 let old = "\"\"\"\ndoc\n\"\"\"\nfunction f(x)\n x + 1\nend\n";
1501 let parsed = crate::parser::parse(old);
1502 let green = parsed.cst.green().to_owned();
1503 let e = edit(5..5, "s");
1504 let new = e.apply(old);
1505
1506 let out = reparse(old, &green, &parsed.diagnostics, &e, &new)
1507 .expect("a docstring body edit is spliceable");
1508 assert_eq!(out.tier, ReparseTier::Token);
1509 }
1510
1511 /// An unterminated literal runs to EOF, so its node is the rest of the
1512 /// file. The diagnostic bail must reject a content edit inside one — and
1513 /// must do so before either relex, which is why it is hoisted to the top
1514 /// of `try_splice_string_content`.
1515 #[test]
1516 fn a_content_edit_in_an_unterminated_literal_falls_back() {
1517 let old = "x = 1\ny = \"abc\nz = 2\n";
1518 let parsed = crate::parser::parse(old);
1519 let green = parsed.cst.green().to_owned();
1520 // Offset 12 is inside the content run, which here swallows the rest of
1521 // the file; `UnterminatedLiteral` is anchored at the literal's start.
1522 let token = SyntaxNode::new_root(green.clone())
1523 .token_at_offset(TextSize::new(12))
1524 .right_biased()
1525 .expect("a leaf at offset 12");
1526 assert_eq!(token.kind(), SyntaxKind::STRING_CONTENT);
1527
1528 let e = edit(12..13, "X");
1529 let new = e.apply(old);
1530 assert!(
1531 try_splice_string_content(
1532 ParseOptions::default(),
1533 old,
1534 &parsed.diagnostics,
1535 &e,
1536 &token
1537 )
1538 .is_none()
1539 );
1540 // The statement tier may still answer it (the in-crate Tenet-4 assert
1541 // validates that splice); what matters is that the token tier declined.
1542 let tier = reparse(old, &green, &parsed.diagnostics, &e, &new).map(|r| r.tier);
1543 assert_ne!(tier, Some(ReparseTier::Token));
1544 }
1545
1546 /// An edit that does not fit `prev_text` is rejected rather than applied at
1547 /// offsets that would panic on a slice. `reparse_edits` checks its edits
1548 /// before slicing; the single-edit entry point has to check too.
1549 #[test]
1550 fn reparse_rejects_an_edit_that_does_not_fit() {
1551 let old = "α = 1\n";
1552 let parsed = crate::parser::parse(old);
1553 let green = parsed.cst.green().to_owned();
1554 // `new_text` is deliberately not the edit's result: a misfitting edit
1555 // has none, which is the whole point.
1556 let run = |e: Edit| reparse(old, &green, &parsed.diagnostics, &e, old);
1557
1558 // Past the end of the text.
1559 assert!(run(edit(99..99, "x")).is_none());
1560 // Off a char boundary (α is two bytes).
1561 assert!(run(edit(1..1, "x")).is_none());
1562 // Inverted range (built by hand: a literal `4..2` is a clippy error).
1563 let inverted = Edit {
1564 range: std::ops::Range { start: 4, end: 2 },
1565 insert: "x".to_string(),
1566 };
1567 assert!(run(inverted).is_none());
1568 }
1569
1570 /// A region wide enough that answering it costs more than the full parse it
1571 /// avoids is declined on the cheap evidence, before any fragment parse. The
1572 /// shape is a collapsed `diff_edit` of scattered edits, which spans
1573 /// everything between the first and the last.
1574 #[test]
1575 fn a_region_covering_most_of_a_large_file_is_declined() {
1576 let mut old = String::new();
1577 for i in 0..2000 {
1578 old.push_str(&format!("var_{i} = {i}\n"));
1579 }
1580 let parsed = crate::parser::parse(&old);
1581 let green = parsed.cst.green().to_owned();
1582
1583 // A narrow edit near the end still splices: the guard keys on the
1584 // region, not on the file size.
1585 let narrow = edit(old.len()..old.len(), "tail = 1\n");
1586 assert!(
1587 reparse(
1588 &old,
1589 &green,
1590 &parsed.diagnostics,
1591 &narrow,
1592 &narrow.apply(&old)
1593 )
1594 .is_some()
1595 );
1596
1597 // The same net change seen as one collapsed edit spanning most of the
1598 // file: `select_region` returns nearly every child, and the tier bails.
1599 // Both sites are inside an identifier, and the second is expressed
1600 // against the text the first produced (one byte longer), so the chain
1601 // below stays at the token tier. The last line is `var_1999 = 1999\n`,
1602 // 16 bytes, whose name runs from its start.
1603 let scattered = vec![edit(4..4, "X"), edit(old.len() - 11..old.len() - 11, "X")];
1604 let new = apply_edits(&old, &scattered);
1605 let collapsed = diff_edit(&old, &new);
1606 assert!(collapsed.range.len() > old.len() / 2, "{collapsed:?}");
1607 assert!(reparse(&old, &green, &parsed.diagnostics, &collapsed, &new).is_none());
1608
1609 // Replaying the chain still answers it at the token tier — the guard
1610 // costs the precise-edit path nothing.
1611 let out = reparse_edits(&old, &green, &parsed.diagnostics, &scattered, &new)
1612 .expect("the precise chain is unaffected");
1613 assert_eq!(out.tier, ReparseTier::Token);
1614 }
1615
1616 /// The width guard is a fraction *and* a floor: a small file's region is
1617 /// always worth trying, however much of the file it covers.
1618 #[test]
1619 fn the_region_width_guard_leaves_small_files_alone() {
1620 assert!(!region_is_too_wide(4 * 1024, 4 * 1024));
1621 assert!(!region_is_too_wide(64 * 1024, 1024 * 1024));
1622 assert!(region_is_too_wide(64 * 1024, 128 * 1024));
1623 }
1624
1625 /// The shape `reparse_edits` exists for: two identifier edits far apart,
1626 /// which `diff_edit` would collapse into one span covering both statements.
1627 #[test]
1628 fn reparse_edits_chains_scattered_edits() {
1629 let old = "alpha = 1\nfiller = 2\nomega = 3\n";
1630 let parsed = crate::parser::parse(old);
1631 let green = parsed.cst.green().to_owned();
1632 // `alpha` -> `alphaX`, then `omega` -> `omegaX` at its post-first-edit
1633 // offset (one byte further along).
1634 let edits = vec![edit(5..5, "X"), edit(27..27, "X")];
1635 let new = apply_edits(old, &edits);
1636 assert_eq!(new, "alphaX = 1\nfiller = 2\nomegaX = 3\n");
1637
1638 // The collapsed diff spans from `alpha` all the way to `omega`, so the
1639 // cheap token tier cannot touch it and the whole run of statements has
1640 // to be reparsed. Replaying the chain keeps both splices at the token
1641 // tier. On three statements that is a wash; on a real file the
1642 // difference is two orders of magnitude (`benches/reparse.rs`), which
1643 // is why the chain is tried first.
1644 let collapsed = diff_edit(old, &new);
1645 assert!(collapsed.range.len() > 20, "{collapsed:?} should be coarse");
1646 let via_diff = reparse(old, &green, &parsed.diagnostics, &collapsed, &new)
1647 .expect("the whole-file region is still spliceable at this size");
1648 assert_eq!(via_diff.tier, ReparseTier::TopLevel);
1649
1650 let out = reparse_edits(old, &green, &parsed.diagnostics, &edits, &new)
1651 .expect("two identifier edits should chain");
1652 assert_eq!(out.tier, ReparseTier::Token);
1653 assert_eq!(
1654 fingerprint(&SyntaxNode::new_root(out.green)),
1655 fingerprint(&crate::parser::parse(&new).cst),
1656 );
1657 }
1658
1659 /// The chain reports the most expensive tier it had to reach.
1660 #[test]
1661 fn reparse_edits_reports_the_max_tier() {
1662 let old = "alpha = 1\nfiller = 2\nomega = 3\n";
1663 let parsed = crate::parser::parse(old);
1664 let green = parsed.cst.green().to_owned();
1665 // An ident edit (token tier) plus a whole added statement (top level),
1666 // appended at the post-first-edit end of the buffer.
1667 let edits = vec![edit(5..5, "X"), edit(32..32, "beta = 4\n")];
1668 let new = apply_edits(old, &edits);
1669
1670 let out = reparse_edits(old, &green, &parsed.diagnostics, &edits, &new)
1671 .expect("ident edit plus a new statement should chain");
1672 assert_eq!(out.tier, ReparseTier::TopLevel);
1673 }
1674
1675 #[test]
1676 fn reparse_edits_declines_an_empty_chain() {
1677 let old = "alpha = 1\nomega = 3\n";
1678 let parsed = crate::parser::parse(old);
1679 let green = parsed.cst.green().to_owned();
1680
1681 assert!(reparse_edits(old, &green, &parsed.diagnostics, &[], old).is_none());
1682 }
1683
1684 #[test]
1685 fn reparse_edits_accepts_a_single_verified_edit() {
1686 let old = "alpha = 1\nfiller = 2\nomega = 3\n";
1687 let parsed = crate::parser::parse(old);
1688 let green = parsed.cst.green().to_owned();
1689 for change in [
1690 edit(5..5, "β"),
1691 edit(0..5, "β"),
1692 edit(4..5, ""),
1693 edit(0..0, "beta = 4\n"),
1694 edit(old.len()..old.len(), "beta = 4\n"),
1695 ] {
1696 let new = change.apply(old);
1697 let out = reparse_edits(old, &green, &parsed.diagnostics, &[change], &new)
1698 .expect("a single verified edit should reparse");
1699 let full = crate::parser::parse(&new);
1700 let root = SyntaxNode::new_root(out.green);
1701 assert_eq!(root.to_string(), new);
1702 assert_eq!(fingerprint(&root), fingerprint(&full.cst));
1703 assert_eq!(out.diagnostics, full.diagnostics);
1704 }
1705 }
1706
1707 #[test]
1708 fn reparse_edits_rejects_a_stale_or_invalid_single_edit() {
1709 let old = "α = 1\nomega = 3\n";
1710 let parsed = crate::parser::parse(old);
1711 let green = parsed.cst.green().to_owned();
1712 for (change, actual) in [
1713 (edit(2..2, "X"), "βX = 1\nomega = 3\n"),
1714 (edit(2..2, "X"), "αY = 1\nomega = 3\n"),
1715 (edit(2..2, "X"), "αX = 1\nomega = 4\n"),
1716 (edit(2..2, "X"), ""),
1717 (edit(2..2, "X"), "α"),
1718 (edit(2..2, "X"), "αX"),
1719 (edit(500..500, "X"), old),
1720 (edit(std::ops::Range { start: 2, end: 1 }, "X"), old),
1721 (edit(1..2, "X"), old),
1722 (edit(0..1, "X"), old),
1723 ] {
1724 assert!(
1725 reparse_edits(
1726 old,
1727 &green,
1728 &parsed.diagnostics,
1729 std::slice::from_ref(&change),
1730 actual,
1731 )
1732 .is_none(),
1733 "must reject {change:?} targeting {actual:?}"
1734 );
1735 }
1736 }
1737
1738 /// A chain that is not the exact transform between the two texts is
1739 /// rejected before any offset is trusted — both when it simply does not
1740 /// reproduce the target, and when it does not fit the source at all.
1741 #[test]
1742 fn reparse_edits_rejects_a_stale_chain() {
1743 let old = "alpha = 1\nfiller = 2\nomega = 3\n";
1744 let parsed = crate::parser::parse(old);
1745 let green = parsed.cst.green().to_owned();
1746
1747 // Applies cleanly, but the buffer moved on underneath it.
1748 let edits = vec![edit(5..5, "X"), edit(26..26, "X")];
1749 let elsewhere = "alphaX = 1\nfiller = 2\nomegaX = 3\nextra = 4\n";
1750 assert!(reparse_edits(old, &green, &parsed.diagnostics, &edits, elsewhere).is_none());
1751
1752 // Does not even fit the source text.
1753 let past_end = vec![edit(500..500, "X"), edit(501..501, "X")];
1754 assert!(reparse_edits(old, &green, &parsed.diagnostics, &past_end, old).is_none());
1755 }
1756
1757 /// One unhandleable step aborts the whole chain — the caller full-parses.
1758 #[test]
1759 fn reparse_edits_aborts_on_an_unhandleable_step() {
1760 let old = "alpha = 1\nfiller = 2\nomega = 3\n";
1761 let parsed = crate::parser::parse(old);
1762 let green = parsed.cst.green().to_owned();
1763 // The second edit opens an unterminated string, which no tier splices.
1764 let edits = vec![edit(5..5, "X"), edit(26..26, "\"")];
1765 let new = apply_edits(old, &edits);
1766 assert!(reparse_edits(old, &green, &parsed.diagnostics, &edits, &new).is_none());
1767 }
1768}