fatou-parser 0.10.0

Lossless CST parser, typed AST wrappers, and incremental reparser for the Julia language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
//! Incremental reparse: turn a text edit plus the previous parse into a new
//! tree without re-lexing and re-parsing the whole file.
//!
//! The cheapest strategy runs first, with a full
//! [`parse`](crate::parser::parse) as the always-correct fallback. Three tiers are
//! available:
//! the token tier ([`reparse_token`]) relexes an edit confined to a single
//! `Ident`, comment, or whitespace leaf in isolation and splices it into the
//! previous green tree ([`try_splice_plain_token`]), or — for a `StringContent`
//! leaf, whose lexing depends on the enclosing delimiter's mode — relexes the
//! whole enclosing string literal in isolation and requires it to reproduce its
//! own token run ([`try_splice_string_content`]); the nested-block tier
//! ([`reparse_block`]) reparses interior statements with the enclosing body's
//! grammar, including module `public` declarations, docstrings, and struct
//! fields; the top-level statement tier
//! ([`reparse_toplevel`]) reparses the run of `ROOT` children touching the edit
//! with the public [`parse`](crate::parser::parse) and splices the resulting
//! children and diagnostics in place, provided the boundary guards prove the
//! region decoupled from its neighbors.
//!
//! Two entry points sit above those tiers. [`reparse_edits`] takes the precise
//! chain the language server staged from a `didChange` batch and replays it one
//! edit at a time; [`reparse`] takes a single edit, which is what the host's
//! `fatou::incremental` recovers with [`diff_edit`] from a pair of whole texts
//! when no chain is available. The chain is tried first: a collapsed diff of
//! scattered edits spans everything between them, and the region that produces
//! is wide enough that [`region_is_too_wide`] declines it outright, where
//! replaying the chain splices each edit on its own. The Tenet-4 oracle
//! ([`assert_matches_full_parse`] plus `tests/incremental_reparse.rs`) checks
//! every splice against a full parse.
//!
//! **Correctness invariant (Tenet 4):** a successful reparse must yield a
//! green tree *and* diagnostics byte-identical to a full parse of the edited
//! text. A reparse is an output-pure performance hint: the salsa layer only
//! ever sees text changes, and a miss is a full parse, never an error.
//!
//! # Why the `STRING_CONTENT` splice is sound
//!
//! [`try_splice_string_content`] relexes the whole enclosing string literal
//! rather than the edited leaf, and the argument that this preserves the tree
//! is the subtlest one in the module, so it is recorded here in full.
//!
//! Nothing outside the lexer reads `StringContent` *text*: the parser only
//! pushes the token, docstring eligibility tests the first token's kind, and the
//! flag passes are kind-based — so preserving the node's token-kind sequence
//! preserves both the tree and the diagnostics. (Nor does triple-quoted
//! dedenting intrude: that lives in the test-only JuliaSyntax projector, which
//! rebuilds the string at projection time.) Given that:
//!
//! * The bytes before the node are unchanged and the lexer is a deterministic
//!   left-to-right machine, so its mode stack at the node start is identical.
//!   That subsumes the backward join probe.
//! * The relex proves the kinds are unchanged and that only the edited leaf's
//!   end, and the ends after it, moved by the delta. The bytes after the leaf
//!   *within* the node are therefore byte-identical — including the close
//!   delimiter and any suffix — so the mode stack is back to its starting depth
//!   at the node end and the rest of the file lexes identically, merely shifted.
//!   That subsumes the forward join probe, which could not have expressed a
//!   delimiter-plus-suffix tail with its one probe character anyway.
//!
//! The second bullet needs the literal to be *terminated*, which gets its own
//! check rather than leaning on an `UnterminatedLiteral` diagnostic's anchor.
//!
//! The faithfulness relex (the unedited node text must reproduce the node's own
//! tokens) is what makes that bullet an induction rather than an assumption: an
//! isolated [`lex`] starts from an empty mode stack and no token history, while
//! in context the node may sit under a `Str`/`Interp` frame and
//! `prev_ends_value` consults the preceding token. Proving faithfulness for the
//! old text, plus an identical kind sequence, carries it to the new text.

use rowan::{GreenNode, GreenToken, TextRange, TextSize, TokenAtOffset};

use crate::parser::diagnostics::DIAGNOSTIC_STREAMS;
use crate::parser::{ParseDiagnostic, ParseOptions};

use crate::parser::tree_builder::syntax_kind_for;
use crate::syntax::{SyntaxKind, SyntaxNode, SyntaxToken};

/// The edit currency, defined in the sibling `edit` module and re-exported
/// here so `parser::Edit` stays the path the parser layer uses.
pub use super::edit::{Edit, apply_edits, diff_edit, try_apply_edits};

/// Structural fingerprint of a tree: one line per descendant element with
/// `kind@range` plus the token text (empty for nodes). Two trees with equal
/// fingerprints are byte-identical. Oracle/debug support: the Tenet-4 assert
/// in [`reparse`] and the `tests/incremental_reparse.rs` harness share this
/// definition so they can never diverge.
pub fn fingerprint(node: &SyntaxNode) -> String {
    use std::fmt::Write;

    let mut out = String::new();
    for el in node.descendants_with_tokens() {
        let text = el
            .as_token()
            .map(|t| t.text().to_string())
            .unwrap_or_default();
        let _ = writeln!(out, "{:?}@{:?} {:?}", el.kind(), el.text_range(), text);
    }
    out
}

/// Which strategy produced a [`Reparsed`]. Surfaced for tests and benchmarks.
/// Ordered cheapest-first, so a chain of reparses can report the most
/// expensive tier it had to reach with [`Ord::max`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ReparseTier {
    /// A single leaf token was relexed in isolation and spliced in place.
    Token,
    /// A run of statements inside a block was reparsed in its body context.
    Block,
    /// A run of top-level statements was reparsed and spliced under `ROOT`.
    TopLevel,
}

/// The result of a successful incremental reparse: the new whole-file green
/// tree and its parse diagnostics (absolute offsets in the new text).
#[derive(Debug, Clone)]
pub struct Reparsed {
    pub green: GreenNode,
    pub diagnostics: Vec<ParseDiagnostic>,
    pub tier: ReparseTier,
}

/// Attempt an incremental reparse of the previous parse (`prev_green`, parsed
/// from `prev_text` with `prev_diags`) under `edit`, which transforms
/// `prev_text` into `new_text`. Returns `None` when no incremental strategy
/// applies — the caller must then do a full parse.
///
/// In debug builds every successful reparse is checked against a full parse
/// of `new_text` (Tenet 4): tree fingerprint and diagnostics vector must be
/// identical.
///
/// `edit` is not trusted, for the same reason [`reparse_edits`]'s chain is
/// not: one that does not fit `prev_text` — a stale range, a buffer that moved
/// underneath it — is rejected here rather than applied at offsets that would
/// panic on a slice.
fn reparse_same_options(
    options: ParseOptions,
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    new_text: &str,
) -> Option<Reparsed> {
    if !fits(prev_text, edit) {
        return None;
    }
    let result = reparse_impl(options, prev_text, prev_green, prev_diags, edit, new_text)?;
    assert_matches_full_parse(options, &result, new_text);
    Some(result)
}

/// Attempt an incremental reparse across a *chain* of edits, each expressed
/// against the text its predecessors produced — the shape an LSP `didChange`
/// batch arrives in (see `fatou::text::apply_content_changes` host-side).
///
/// This is for the case [`diff_edit`] cannot express: when the edits are
/// scattered, collapsing them into one spanning edit covers everything between
/// the first and the last change and blows the region tiers' guards, while replaying
/// them one at a time keeps each splice small. Try this *before* the collapsed
/// edit: a wide span is the one shape the collapsed edit can never answer, so
/// offering it first is the difference between a hit and a
/// `region_is_too_wide` bail (`benches/reparse.rs`).
///
/// A single edit is verified against both texts without allocating an
/// intermediate buffer, then passed directly to [`reparse`]. This avoids
/// recovering an already-known edit with [`diff_edit`]. An empty chain returns
/// `None`.
///
/// `edits` is not trusted: a slice that is not the exact transform from
/// `prev_text` to `new_text` (a stale chain, a buffer that moved underneath
/// it) is rejected rather than applied at meaningless offsets. Each step is
/// proven to `fits` the text its predecessors produced *before* that text is
/// sliced, and the composed result is proven to be `new_text` before anything
/// is returned — so a stale chain costs at most the splices it got through,
/// bounded by the caller's chain budget (`incremental::MAX_CHAIN_EDITS`).
///
/// Validating step by step rather than pre-folding the whole chain with
/// [`try_apply_edits`] keeps the replay to one application: the fold would have
/// to redo every `replace_range` the loop below already does, which costs the
/// same order as the splice it is guarding (`benches/reparse.rs`,
/// `precise_chain`).
fn reparse_edits_same_options(
    options: ParseOptions,
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edits: &[Edit],
    new_text: &str,
) -> Option<Reparsed> {
    if edits.is_empty() {
        return None;
    }
    if let [edit] = edits {
        if !fits(prev_text, edit) {
            return None;
        }
        // Prove the exact transform without rebuilding the buffer or scanning
        // for edit boundaries the caller already supplied.
        let suffix = new_text
            .strip_prefix(&prev_text[..edit.range.start])?
            .strip_prefix(&edit.insert)?;
        if suffix != &prev_text[edit.range.end..] {
            return None;
        }
        return reparse_same_options(options, prev_text, prev_green, prev_diags, edit, new_text);
    }

    let mut text = prev_text.to_string();
    let mut green = prev_green.clone();
    let mut diagnostics = prev_diags.to_vec();
    let mut tier = ReparseTier::Token;

    for edit in edits {
        if !fits(&text, edit) {
            return None;
        }
        let next = edit.apply(&text);
        let step = reparse_impl(options, &text, &green, &diagnostics, edit, &next)?;
        tier = tier.max(step.tier);
        text = next;
        green = step.green;
        diagnostics = step.diagnostics;
    }

    // The chain applied cleanly, but that alone does not make it the transform
    // the caller is asking about: a stale chain can fit and still land
    // somewhere else entirely. This check is what the Tenet-4 assert below
    // relies on to be comparing the same two texts.
    if text != new_text {
        return None;
    }

    let result = Reparsed {
        green,
        diagnostics,
        tier,
    };
    assert_matches_full_parse(options, &result, new_text);
    Some(result)
}

/// Tenet 4: a successful reparse yields a green tree *and* diagnostics
/// byte-identical to a full parse of the edited text.
///
/// Debug builds only — this runs a whole `parse` plus two whole-tree
/// [`fingerprint`] builds, so it is far too expensive to leave on. A chain
/// asserts once on its composed result rather than once per step, which would
/// otherwise make a debug-build language server unusable on a large file.
#[cfg(debug_assertions)]
fn assert_matches_full_parse(options: ParseOptions, result: &Reparsed, new_text: &str) {
    let full = crate::parser::parse_with_options(new_text, options);
    debug_assert_eq!(
        fingerprint(&SyntaxNode::new_root(result.green.clone())),
        fingerprint(&full.cst),
        "Tenet 4: reparse ({:?}) tree differs from full parse",
        result.tier,
    );
    debug_assert_eq!(
        result.diagnostics, full.diagnostics,
        "Tenet 4: reparse ({:?}) diagnostics differ from full parse",
        result.tier,
    );
}

#[cfg(not(debug_assertions))]
fn assert_matches_full_parse(_options: ParseOptions, _result: &Reparsed, _new_text: &str) {}

/// Whether `edit`'s range is a well-formed byte range of `text`. All tiers
/// slice `prev_text` at the edit's offsets, so an edit that does not fit is a
/// panic rather than a miss.
fn fits(text: &str, edit: &Edit) -> bool {
    edit.range.start <= edit.range.end
        && edit.range.end <= text.len()
        && text.is_char_boundary(edit.range.start)
        && text.is_char_boundary(edit.range.end)
}

/// Tier dispatch: cheapest first.
fn reparse_impl(
    options: ParseOptions,
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    new_text: &str,
) -> Option<Reparsed> {
    reparse_token(options, prev_text, prev_green, prev_diags, edit)
        .or_else(|| reparse_block(options, prev_text, prev_green, prev_diags, edit, new_text))
        .or_else(|| reparse_toplevel(options, prev_text, prev_green, prev_diags, edit, new_text))
}

/// Leaf kinds proved by the isolated single-token relex plus the two join
/// probes ([`try_splice_plain_token`]). Their text never spans a newline and
/// their lexing is local enough for those probes to prove a splice sound.
/// Deliberately absent: `NEWLINE` (statement structure), `CHAR` and the
/// numeric kinds (quote-context and juxtaposition traps outweigh the win), and
/// the string prefix/suffix kinds (glued to their delimiters).
/// `STRING_CONTENT` is handled too, but by a different proof — its lexing
/// depends on the enclosing delimiter's mode, so it takes the whole-literal
/// relex in [`try_splice_string_content`] instead.
const TOKEN_REPARSE_KINDS: &[SyntaxKind] = &[
    SyntaxKind::IDENT,
    SyntaxKind::COMMENT,
    SyntaxKind::BLOCK_COMMENT,
    SyntaxKind::WHITESPACE,
];

/// Literal node kinds a `STRING_CONTENT` leaf may sit directly under. Error
/// recovery can leave one under an `ERROR` node instead, which
/// [`try_splice_string_content`] rejects: the proof needs the delimiters.
const STRING_LITERAL_KINDS: &[SyntaxKind] = &[
    SyntaxKind::STRING_LITERAL,
    SyntaxKind::CMD_LITERAL,
    // `var"…"`, a non-standard *identifier*. Nothing keys on its content text
    // either, so a content edit is as safe here as in a plain string.
    SyntaxKind::NONSTANDARD_IDENTIFIER,
];

/// Identifier texts the parser treats structurally even though they lex as
/// plain `Ident`, so a same-kind relex proves nothing about the tree shape.
/// True keywords (`where`, `mutable`, …) need no entry: they lex as their own
/// kinds, and the same-kind guard rejects them.
///
/// This list is the one guard here with no compile-time link to what it
/// describes, and the Tenet-4 oracle only catches an omission if a seeded edit
/// happens to spell the missing word — so
/// [`every_contextual_ident_is_listed`](tests::every_contextual_ident_is_listed)
/// scans the grammar for `Ident`-text comparisons and fails if one names a word
/// that is not here.
const CONTEXTUAL_IDENTS: &[&str] = &[
    "as",        // import alias (structural.rs)
    "abstract",  // `abstract type` (expr.rs)
    "primitive", // `primitive type` (expr.rs)
    "type",      // the `type` in `abstract`/`primitive` type decls (expr.rs)
    "typegroup", // `typegroup` declaration (expr.rs)
    "public",    // top-level `public` statement (expr.rs)
    "var",       // `var"..."` quoting prefix (expr.rs)
    "in",        // iteration spec / infix operator (expr.rs)
    "∈",         // Unicode `in` (expr.rs)
    "isa",       // infix operator (expr.rs)
    "doc",       // `doc"..."` / `@doc` marker (expr.rs)
    "outer",     // `for outer i in xs` iteration spec (expr.rs)
];

/// The token tier: when the edit is confined to a single eligible leaf, relex
/// that leaf in isolation and splice it via [`SyntaxToken::replace_with`].
///
/// A pure insertion at a token boundary is ambiguous (it could extend either
/// neighbor), so both candidates are tried, left first; whichever passes the
/// guards yields the same new file text, and the guards plus the Tenet-4
/// oracle arbitrate tree equality. Any guard failure returns `None`, which
/// the caller answers with a full parse, so every bail here is safe.
fn reparse_token(
    options: ParseOptions,
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
) -> Option<Reparsed> {
    let root = SyntaxNode::new_root(prev_green.clone());
    let (s, e) = (edit.range.start, edit.range.end);
    let mut candidates: Vec<SyntaxToken> = Vec::with_capacity(2);
    if s == e {
        match root.token_at_offset(TextSize::new(s as u32)) {
            TokenAtOffset::None => return None,
            TokenAtOffset::Single(t) => candidates.push(t),
            TokenAtOffset::Between(l, r) => candidates.extend([l, r]),
        }
    } else {
        // A range spanning a token boundary covers a node, not a token, and
        // bails here.
        let range = TextRange::new(TextSize::new(s as u32), TextSize::new(e as u32));
        candidates.push(root.covering_element(range).into_token()?);
    }

    candidates
        .into_iter()
        .find_map(|token| try_splice_token(options, prev_text, prev_diags, edit, &token))
}

/// Dispatch one candidate leaf to the proof its kind takes.
fn try_splice_token(
    options: ParseOptions,
    prev_text: &str,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    token: &SyntaxToken,
) -> Option<Reparsed> {
    match token.kind() {
        SyntaxKind::STRING_CONTENT => {
            try_splice_string_content(options, prev_text, prev_diags, edit, token)
        }
        kind if TOKEN_REPARSE_KINDS.contains(&kind) => {
            try_splice_plain_token(options, prev_text, prev_diags, edit, token)
        }
        _ => None,
    }
}

/// Run the stage-2 guards against one candidate leaf and splice on success.
fn try_splice_plain_token(
    options: ParseOptions,
    prev_text: &str,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    token: &SyntaxToken,
) -> Option<Reparsed> {
    // Newline ban: these kinds either cannot contain a newline or (block
    // comments) rarely see one edited, and a newline moves statement
    // boundaries, so bail early. The single-token relex guard below would
    // catch most of these anyway; this is the cheap early-out. It does not
    // apply to `STRING_CONTENT`, whose run legitimately spans newlines and
    // whose proof does not depend on line structure.
    if edit.insert.contains(['\n', '\r']) || prev_text[edit.range.clone()].contains(['\n', '\r']) {
        return None;
    }

    let tr = token.text_range();
    let (t0, t1) = (usize::from(tr.start()), usize::from(tr.end()));

    // The leaf's new text, with the edit applied at its relative offset. A
    // whole-leaf deletion leaves it empty, which the relex guard rejects.
    let mut new_leaf = token.text().to_string();
    new_leaf.replace_range((edit.range.start - t0)..(edit.range.end - t0), &edit.insert);

    // Isolated relex: still exactly one token, same kind, spanning all of it.
    // This alone rejects kind flips (ident ⇒ keyword, whitespace ⇒ newline)
    // and splits (`=#` typed into a block comment).
    let relexed = lex_for_options(options, &new_leaf);
    let [only] = relexed.as_slice() else {
        return None;
    };
    if only.start != 0 || only.end != new_leaf.len() || syntax_kind_for(only.kind) != token.kind() {
        return None;
    }

    // Contextual identifiers parse structurally, so text changes into or out
    // of one can reshape the tree under an unchanged token kind.
    if token.kind() == SyntaxKind::IDENT
        && (CONTEXTUAL_IDENTS.contains(&token.text())
            || CONTEXTUAL_IDENTS.contains(&new_leaf.as_str()))
    {
        return None;
    }

    // Forward join: the next source character must not extend the token
    // (`r` + `"…"` fusing into a prefixed string, an unterminated block
    // comment swallowing its neighbor). At EOF probe with `\n`, which no
    // eligible kind can absorb, so a leaf left unterminated at EOF (`#=` typed
    // into the last block comment) still fails here.
    let next_char = prev_text[t1..].chars().next().unwrap_or('\n');
    let mut probe = new_leaf.clone();
    probe.push(next_char);
    let first = lex_for_options(options, &probe).into_iter().next()?;
    if first.end != new_leaf.len() || syntax_kind_for(first.kind) != token.kind() {
        return None;
    }

    // Backward join: the previous leaf plus the new text must relex to the
    // same two tokens. Catches juxtaposition fusing (`2` + `e10` ⇒ one
    // `Float`), and conservatively bails whenever the previous leaf's
    // isolated lex diverges from its in-context one (e.g. a closing string
    // delimiter reopening as a string).
    if let Some(prev) = token.prev_token() {
        let mut probe = prev.text().to_string();
        let boundary = probe.len();
        probe.push_str(&new_leaf);
        let relexed = lex_for_options(options, &probe);
        let [a, b] = relexed.as_slice() else {
            return None;
        };
        if a.end != boundary
            || syntax_kind_for(a.kind) != prev.kind()
            || b.end != probe.len()
            || syntax_kind_for(b.kind) != token.kind()
        {
            return None;
        }
    }

    // Keep diagnostic remapping trivial: bail when any diagnostic touches the
    // leaf, boundaries included. A clean same-kind relex introduces none of
    // its own, so the rest keep their meaning and only need shifting.
    if prev_diags.iter().any(|d| d.start <= t1 && d.end >= t0) {
        return None;
    }
    let delta = edit.delta();
    let shift = |pos: usize| (pos as isize + delta) as usize;
    let diagnostics = prev_diags
        .iter()
        .map(|d| {
            let mut d = d.clone();
            if d.start >= t1 {
                d.start = shift(d.start);
                d.end = shift(d.end);
            }
            d
        })
        .collect();

    let green = token.replace_with(GreenToken::new(token.kind().into(), &new_leaf));
    Some(Reparsed {
        green,
        diagnostics,
        tier: ReparseTier::Token,
    })
}

/// `text` — the slice of the previous buffer starting at absolute offset
/// `base` — with `edit` applied at its relative offset.
fn edited_slice(text: &str, base: usize, edit: &Edit) -> String {
    let mut out = text.to_string();
    out.replace_range(
        (edit.range.start - base)..(edit.range.end - base),
        &edit.insert,
    );
    out
}

/// Every token under `node` as `(kind, end)`, with `end` relative to the node
/// start. Tokens tile the node's text contiguously from `0` and the tree
/// synthesizes none of its own (`tree_builder`), so this *is* the lexer's
/// token run for that span and the ends alone pin every range.
fn literal_token_ends(node: &SyntaxNode) -> Vec<(SyntaxKind, usize)> {
    let base = usize::from(node.text_range().start());
    node.descendants_with_tokens()
        .filter_map(|el| el.into_token())
        .map(|t| (t.kind(), usize::from(t.text_range().end()) - base))
        .collect()
}

/// Whether an isolated [`lex`] of `text` reproduces `expected`: the same kinds
/// in the same order, with the same ends, covering all of `text`. `grown`
/// names the index whose end — and every end after it — is expected to have
/// moved by that delta.
fn relex_matches(
    options: ParseOptions,
    text: &str,
    expected: &[(SyntaxKind, usize)],
    grown: Option<(usize, isize)>,
) -> bool {
    let tokens = lex_for_options(options, text);
    if tokens.len() != expected.len() {
        return false;
    }
    for (i, (got, &(kind, end))) in tokens.iter().zip(expected).enumerate() {
        let shift = match grown {
            Some((from, delta)) if i >= from => delta,
            _ => 0,
        };
        // A shift that runs an end past zero cannot describe any token, so it
        // simply fails to match rather than wrapping into a huge `usize`.
        let Ok(want) = usize::try_from(end as isize + shift) else {
            return false;
        };
        if syntax_kind_for(got.kind) != kind || got.end != want {
            return false;
        }
    }
    tokens.last().is_some_and(|t| t.end == text.len())
}

/// The token tier's `STRING_CONTENT` path: relex the *whole enclosing string
/// literal* in isolation and require it to reproduce its own token run with
/// only the edited content leaf resized.
///
/// A content leaf cannot take [`try_splice_plain_token`]'s proof, because both
/// of its probes assume a leaf that lexes the same standing alone: `abc` in
/// isolation is an `Ident`, never `StringContent`, and a leaf following an
/// interpolation (`"a$x b"`) has an `Ident` before it, which blows the
/// two-token backward probe. Taking the delimiters along instead puts the
/// isolated lexer into the right mode for free, so triple/raw/prefixed need no
/// hand-written character table.
///
/// The soundness argument — why relexing the literal preserves the tree and the
/// diagnostics — is in the module docs under *Why the `STRING_CONTENT` splice is
/// sound*.
///
/// Newlines are allowed here, unlike in [`try_splice_plain_token`]: a content
/// run spans them freely (a triple-quoted body, and a single-quoted one too —
/// no newline stops the run, which is why an unterminated literal reaches EOF),
/// and a newline *inside* one emits no `NEWLINE` token, so no statement
/// boundary moves. Diagnostics are byte offsets and do not care about lines.
/// That is what puts Enter in a docstring on this tier.
fn try_splice_string_content(
    options: ParseOptions,
    prev_text: &str,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    token: &SyntaxToken,
) -> Option<Reparsed> {
    let node = token.parent()?;
    if !STRING_LITERAL_KINDS.contains(&node.kind()) {
        return None;
    }
    let nr = node.text_range();
    let (n0, n1) = (usize::from(nr.start()), usize::from(nr.end()));

    // Bail when any diagnostic touches the literal, boundaries included. The
    // relex proves the token sequence unchanged, so the four string-related
    // kinds would in fact survive a shift — but "no diagnostic may touch the
    // literal we splice" is an invariant that keeps holding as diagnostics are
    // added, and what it gives up (`x = "abc`, `"a"b`, `var"x"y`) is the error
    // path, not the keystroke path.
    //
    // This runs *first*, before either relex: an unterminated literal runs to
    // EOF, so its node is the rest of the file, and every keystroke inside one
    // would otherwise pay two whole-file lexes to learn what a diagnostic
    // already says (`benches/reparse.rs`, `rejected_attempt`).
    if prev_diags.iter().any(|d| d.start <= n1 && d.end >= n0) {
        return None;
    }

    let expected = literal_token_ends(&node);
    let tr = token.text_range();
    let (t0, t1) = (usize::from(tr.start()), usize::from(tr.end()));
    // Ends are unique (tokens tile with positive width), so this identifies the
    // leaf by index — which the edited relex then pins as the one that resized.
    let leaf = expected
        .iter()
        .position(|&(kind, end)| kind == SyntaxKind::STRING_CONTENT && end == t1 - n0)?;

    // Terminated: a matching close delimiter must follow the leaf. A prefixed
    // literal's suffix is lexed as part of the same node, so nothing but the
    // close can end the run.
    let close = if node.kind() == SyntaxKind::CMD_LITERAL {
        SyntaxKind::CMD_DELIM_CLOSE
    } else {
        SyntaxKind::STRING_DELIM_CLOSE
    };
    if !expected[leaf + 1..].iter().any(|&(kind, _)| kind == close) {
        return None;
    }

    let old_node_text = &prev_text[n0..n1];
    if !relex_matches(options, old_node_text, &expected, None) {
        return None;
    }
    let delta = edit.delta();
    let edited = edited_slice(old_node_text, n0, edit);
    if !relex_matches(options, &edited, &expected, Some((leaf, delta))) {
        return None;
    }

    let new_leaf = &edited[(t0 - n0)..((t1 - n0) as isize + delta) as usize];

    // Every surviving diagnostic lies wholly outside the literal, so the ones
    // past it shift and the rest keep their positions.
    let shift = |pos: usize| (pos as isize + delta) as usize;
    let diagnostics = prev_diags
        .iter()
        .map(|d| {
            let mut d = d.clone();
            if d.start >= n1 {
                d.start = shift(d.start);
                d.end = shift(d.end);
            }
            d
        })
        .collect();

    let green = token.replace_with(GreenToken::new(SyntaxKind::STRING_CONTENT.into(), new_leaf));
    Some(Reparsed {
        green,
        diagnostics,
        tier: ReparseTier::Token,
    })
}

/// Whether a token kind is trivia. Mirrors `TokKind::is_trivia` on the CST
/// side.
fn is_trivia_kind(kind: SyntaxKind) -> bool {
    matches!(
        kind,
        SyntaxKind::WHITESPACE
            | SyntaxKind::NEWLINE
            | SyntaxKind::COMMENT
            | SyntaxKind::BLOCK_COMMENT
    )
}

/// Whether a `ROOT` child carries statement-level content: a positive-width
/// node, or a loose non-trivia token (the unparseable-token recovery emits
/// bare tokens at the root, without an `ERROR` wrapper). Zero-width nodes and
/// trivia are gap material.
fn is_significant_child(el: &crate::syntax::SyntaxElement) -> bool {
    match el {
        rowan::NodeOrToken::Node(n) => !n.text_range().is_empty(),
        rowan::NodeOrToken::Token(t) => !is_trivia_kind(t.kind()),
    }
}

/// Below this the tier always tries, whatever fraction of the file the region
/// is: on a small file every parse in play is small, and the fraction test
/// alone would refuse the common case of a file with one or two statements.
const REGION_ALWAYS_TRY_BYTES: usize = 4 * 1024;

/// A region wider than `1 / REGION_MAX_FILE_DIVISOR` of the file is wide enough
/// that answering it costs more than the full parse it is trying to avoid. A
/// divisor rather than a fraction, so `4` here means a quarter.
const REGION_MAX_FILE_DIVISOR: usize = 4;

/// Whether a region is too wide to be worth attempting, per the two consts
/// above. The tier answers a region with a fragment parse of it *plus* up to
/// three neighbor-sized boundary parses, so a miss costs more than the full
/// parse it was avoiding; bailing on the region's byte span — cheap evidence,
/// known before any parse — caps the loss (`benches/reparse.rs`,
/// `scattered_via_diff_edit`).
///
/// A performance guard only: like every other bail here it returns the caller to
/// a full parse, so it cannot affect what a reparse yields.
fn region_is_too_wide(region_len: usize, text_len: usize) -> bool {
    region_len > REGION_ALWAYS_TRY_BYTES && region_len > text_len / REGION_MAX_FILE_DIVISOR
}

/// The reparse region at either statement tier: a contiguous run of container
/// children (statement nodes and loose trivia tokens both), identified by
/// child index and old-text byte range. The boundary child kinds
/// disambiguate zero-width diagnostics anchored exactly on a boundary.
struct Region {
    first: usize,
    last: usize,
    start: usize,
    end: usize,
    first_is_node: bool,
    last_is_node: bool,
}

/// Select the contiguous run of container children whose closed range touches
/// the edit. Closed-interval touch makes boundary insertions extend into the
/// neighbor they abut (blank-line typing selects the surrounding newline
/// tokens; deleting a newline pulls in both neighbor statements). A
/// zero-width child abutting the region start is absorbed: stray-closer
/// recovery pairs a zero-width `ERROR` with its byte-bearing run at the same
/// offset, and the fragment reparse regenerates both, so leaving it outside
/// would duplicate it. `None` when the run covers every child (the fragment
/// reparse would be the full parse) or `ROOT` is empty.
fn select_region(root: &SyntaxNode, edit: &Edit) -> Option<Region> {
    let (s, e) = (edit.range.start, edit.range.end);
    let children: Vec<_> = root.children_with_tokens().collect();
    let touches = |el: &crate::syntax::SyntaxElement| {
        let r = el.text_range();
        usize::from(r.start()) <= e && usize::from(r.end()) >= s
    };
    let mut first = children.iter().position(touches)?;
    let last = children.len() - 1 - children.iter().rev().position(touches)?;

    let start = usize::from(children[first].text_range().start());
    while first > 0
        && children[first - 1].text_range() == TextRange::empty(TextSize::new(start as u32))
    {
        first -= 1;
    }

    if first == 0 && last == children.len() - 1 {
        return None;
    }
    Some(Region {
        first,
        last,
        start,
        end: usize::from(children[last].text_range().end()),
        first_is_node: children[first].as_node().is_some(),
        last_is_node: children[last].as_node().is_some(),
    })
}

/// The nearest positive-width container child nodes outside the region. Trivia
/// tokens are gap material, and a zero-width `ERROR` node contributes no text
/// (taking one as the forward neighbor would make the boundary guard
/// vacuous), so both are skipped.
fn sibling_nodes(root: &SyntaxNode, region: &Region) -> (Option<SyntaxNode>, Option<SyntaxNode>) {
    let positive_node =
        |el: crate::syntax::SyntaxElement| el.into_node().filter(|n| !n.text_range().is_empty());
    let prev = root
        .children_with_tokens()
        .take(region.first)
        .filter_map(positive_node)
        .last();
    let next = root
        .children_with_tokens()
        .skip(region.last + 1)
        .find_map(positive_node);
    (prev, next)
}

/// Whether no container child (node or loose token) of `cst` straddles `seam`.
/// Children tile the text, so no-straddle is equivalent to a boundary
/// existing exactly at the seam: the two sides parse independently. A
/// straddling node is a cross-boundary statement (trailing-operator
/// continuation, a docstring fold, an unterminated block absorbing its
/// neighbor); a straddling token is a lexical fusion (`#=` swallowing
/// trailing trivia, an unterminated string).
fn no_straddle(cst: &SyntaxNode, seam: usize) -> bool {
    let seam = TextSize::new(seam as u32);
    cst.children_with_tokens().all(|el| {
        let r = el.text_range();
        !(r.start() < seam && seam < r.end())
    })
}

/// Splice the fragment's diagnostics into the previous ones, stream by
/// stream ([`DiagnosticKind::stream`](crate::parser::diagnostics::DiagnosticKind::stream)):
/// keep those before the region, drop those the fragment reparse
/// regenerates, and shift those after by the edit delta. Emission order
/// within a stream is positional across statements (the drive loop is
/// per-line sequential, the flag passes walk in document order), so
/// `before ++ fragment ++ after` per stream, concatenated in stream order,
/// reproduces a full parse's vector.
///
/// A zero-width diagnostic exactly on a region boundary is ambiguous by
/// position alone and is disambiguated by the boundary child's kind: a
/// statement node owns a start-anchored marker at `start` (`const x` at the
/// region's front) and an end-anchored one at `end`, while a trivia boundary
/// child owns nothing, so the marker belongs to the statement just outside.
/// This is sound only because the decoupling guards already rejected
/// zero-gap node adjacency at the boundaries. A diagnostic genuinely
/// straddling a boundary aborts the splice (`None`); regions are whole
/// children and diagnostics never outgrow their statement, so this arm
/// should be unreachable and is purely defensive.
///
/// A stream index outside [`DIAGNOSTIC_STREAMS`] aborts the splice too, rather
/// than indexing out of bounds. [`DiagnosticKind::stream`] is an exhaustive
/// match today, so this cannot fire — but a bail returns the caller to a full
/// parse, whereas a panic here takes down an analysis query, and this module
/// buys its whole safety argument from every failure being a bail.
fn splice_diagnostics(
    prev_diags: &[ParseDiagnostic],
    frag_diags: &[ParseDiagnostic],
    region: &Region,
    delta: isize,
) -> Option<Vec<ParseDiagnostic>> {
    enum Class {
        Before,
        Drop,
        After,
    }
    let classify = |d: &ParseDiagnostic| -> Option<Class> {
        let (rs, re) = (region.start, region.end);
        if d.start == d.end && d.start == rs {
            return Some(if region.first_is_node {
                Class::Drop
            } else {
                Class::Before
            });
        }
        if d.start == d.end && d.start == re {
            return Some(if region.last_is_node {
                Class::Drop
            } else {
                Class::After
            });
        }
        if d.end <= rs {
            return Some(Class::Before);
        }
        if d.start >= re {
            return Some(Class::After);
        }
        if d.start >= rs && d.end <= re {
            return Some(Class::Drop);
        }
        None
    };

    let mut before: [Vec<ParseDiagnostic>; DIAGNOSTIC_STREAMS] = Default::default();
    let mut after: [Vec<ParseDiagnostic>; DIAGNOSTIC_STREAMS] = Default::default();
    for d in prev_diags {
        let class = classify(d)?;
        let stream = d.kind.stream();
        if stream >= DIAGNOSTIC_STREAMS {
            debug_assert!(false, "diagnostic stream {stream} is out of range");
            return None;
        }
        match class {
            Class::Before => before[stream].push(d.clone()),
            Class::Drop => {}
            Class::After => {
                let mut d = d.clone();
                d.start = (d.start as isize + delta) as usize;
                d.end = (d.end as isize + delta) as usize;
                after[stream].push(d);
            }
        }
    }

    let mut out = Vec::with_capacity(prev_diags.len() + frag_diags.len());
    for stream in 0..DIAGNOSTIC_STREAMS {
        out.append(&mut before[stream]);
        out.extend(
            frag_diags
                .iter()
                .filter(|d| d.kind.stream() == stream)
                .map(|d| {
                    let mut d = d.clone();
                    d.start += region.start;
                    d.end += region.start;
                    d
                }),
        );
        out.append(&mut after[stream]);
    }
    Some(out)
}

/// The top-level statement tier: reparse the run of `ROOT` children touching
/// the edit with the public [`parse`](crate::parser::parse) (which reruns
/// `fold_docstrings` and the flag passes on the fragment) and splice the
/// resulting children and diagnostics in place.
///
/// The guards, in order: decoupling scans require a newline between each
/// untouched sibling statement and the fragment's non-trivia content, because
/// the drive loop's per-line junk recovery couples same-line siblings; the
/// boundary parses then prove the seam survives — the neighbor plus the
/// actual gap bytes plus the fragment must parse with a boundary exactly at
/// the old one, which rejects trailing-operator continuation, docstring
/// folds, forward absorption by an unterminated block, and lexical fusion.
/// Diagnostics splice last (see [`splice_diagnostics`]).
fn reparse_toplevel(
    options: ParseOptions,
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    new_text: &str,
) -> Option<Reparsed> {
    let root = SyntaxNode::new_root(prev_green.clone());
    reparse_statements(options, prev_text, prev_diags, edit, new_text, &root, None)
}

fn reparse_block(
    options: ParseOptions,
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    new_text: &str,
) -> Option<Reparsed> {
    let root = SyntaxNode::new_root(prev_green.clone());
    let range = TextRange::new(
        TextSize::try_from(edit.range.start).ok()?,
        TextSize::try_from(edit.range.end).ok()?,
    );
    let covering = root.covering_element(range);
    let node = match covering {
        rowan::NodeOrToken::Node(node) => node,
        rowan::NodeOrToken::Token(token) => token.parent()?,
    };
    // Recovery in an enclosing construct can emit a diagnostic anchored
    // before this region *after* its children. The positional stream splice
    // cannot reproduce that ordering, so leave damaged constructs to the
    // enclosing-statement tier.
    let statement = node
        .ancestors()
        .find(|n| n.parent().is_some_and(|p| p.kind() == SyntaxKind::ROOT))?;
    let extent = statement.text_range();
    if prev_diags.iter().any(|d| {
        d.kind.stream() == 0
            && d.start >= usize::from(extent.start())
            && d.start <= usize::from(extent.end())
    }) {
        return None;
    }
    for block in node.ancestors().filter(|n| n.kind() == SyntaxKind::BLOCK) {
        let owner = block.parent()?.kind();
        if let Some(result) = reparse_statements(
            options,
            prev_text,
            prev_diags,
            edit,
            new_text,
            &block,
            Some(owner),
        ) {
            return Some(result);
        }
    }
    None
}

/// Share seam and diagnostic checks between root statements and nested bodies.
/// A nested run needs untouched statements on both sides: this keeps the owner
/// header, clause terminators, and body-dependent signature flags unchanged.
#[allow(clippy::too_many_arguments)]
fn reparse_statements(
    options: ParseOptions,
    prev_text: &str,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    new_text: &str,
    root: &SyntaxNode,
    owner: Option<SyntaxKind>,
) -> Option<Reparsed> {
    let parse_fragment = |text: &str| match owner {
        Some(kind) => super::core::parse_block_fragment(text, kind, options),
        None => Some(crate::parser::parse_with_options(text, options)),
    };
    let selection;
    let edit_region = if owner.is_some() {
        // A semicolon separates block statements without wrapping them in a
        // top-level semicolon node. Include their whole physical line so the
        // existing newline decoupling guards can bound the run.
        let start = usize::from(root.text_range().start());
        let end = usize::from(root.text_range().end());
        let line_start = prev_text[start..edit.range.start]
            .rfind('\n')
            .map_or(start, |at| start + at + 1);
        let line_end = prev_text[edit.range.end..end]
            .find('\n')
            .map_or(end, |at| edit.range.end + at);
        selection = Edit {
            range: line_start..line_end,
            insert: String::new(),
        };
        &selection
    } else {
        edit
    };
    let region = select_region(root, edit_region)?;
    if region_is_too_wide(region.end - region.start, prev_text.len()) {
        return None;
    }
    let delta = edit.delta();
    let frag_end = (region.end as isize + delta) as usize;
    let fragment = &new_text[region.start..frag_end];

    let (prev_node, next_node) = sibling_nodes(root, &region);
    if owner.is_some() {
        // Interior neighbors bound both seams without reparsing the owner's
        // header or closing clause. Edge edits climb to an enclosing tier.
        prev_node.as_ref()?;
        next_node.as_ref()?;
        let old = parse_fragment(&prev_text[region.start..region.end])?;
        let expected: Vec<_> = root
            .green()
            .children()
            .skip(region.first)
            .take(region.last + 1 - region.first)
            .map(|child| child.to_owned())
            .collect();
        let actual: Vec<_> = old
            .cst
            .green()
            .children()
            .map(|child| child.to_owned())
            .collect();
        if expected != actual {
            return None;
        }
    }
    let frag = parse_fragment(fragment)?;

    let positive_nodes = |node: &SyntaxNode| {
        node.children_with_tokens()
            .filter_map(|el| el.into_node())
            .filter(|n| !n.text_range().is_empty())
    };

    // Backward decoupling: a newline must separate the last significant
    // content before the region (a statement node or a loose junk token, not
    // just `prev_node`) from the fragment's first significant content (or
    // all of a trivia-only fragment). The drive loop's per-line junk
    // recovery couples same-line siblings, and the newline often lives in
    // the unchanged gap outside the region, so scan `new_text`.
    let prev_significant_end = root
        .children_with_tokens()
        .take(region.first)
        .filter(is_significant_child)
        .last()
        .map(|el| usize::from(el.text_range().end()));
    if let Some(from) = prev_significant_end {
        let anchor = frag
            .cst
            .children_with_tokens()
            .find(is_significant_child)
            .map(|el| usize::from(el.text_range().start()))
            .unwrap_or(fragment.len());
        if !new_text[from..region.start + anchor].contains('\n') {
            return None;
        }
    }

    // Forward decoupling, in both texts: the next statement is kept, not
    // reparsed, so its line must have been decoupled from the old region
    // content too (deleting `x` from `x y` puts the kept junk `y` at line
    // start, where a full parse reads it as a plain statement). The new-text
    // scan anchors at the fragment's last statement *node* end, not its last
    // token: an unterminated bracket consumes the separating newline into
    // the statement, and a consumed newline decouples nothing.
    if let Some(next) = &next_node {
        let anchor = frag
            .cst
            .children_with_tokens()
            .filter(is_significant_child)
            .last()
            .map(|el| usize::from(el.text_range().end()))
            .unwrap_or(0);
        let to = (usize::from(next.text_range().start()) as isize + delta) as usize;
        if !new_text[region.start + anchor..to].contains('\n') {
            return None;
        }

        let old_anchor = root
            .children_with_tokens()
            .skip(region.first)
            .take(region.last + 1 - region.first)
            .filter(is_significant_child)
            .last()
            .map(|el| usize::from(el.text_range().end()))
            .or(prev_significant_end)
            .unwrap_or(0);
        if !prev_text[old_anchor..usize::from(next.text_range().start())].contains('\n') {
            return None;
        }
    }

    // Cross-region coupling: when the fragment carries no significant content,
    // the two neighbors sit next to each other across it and can couple in a
    // way neither boundary parse can see, because each concatenation contains
    // only one of them. Deleting the `]` from `"doc" ]\nf() = 1` is the shape:
    // the stray closer was all that kept the string from being the statement
    // below it, and once it goes `fold_docstrings` folds the pair into a `DOC`
    // spanning both seams. Parse the neighbors together, through the real gap
    // bytes, and require both seams to survive. Only reachable when the region
    // is reduced to trivia, so the extra parse is rare and neighbor-sized.
    if !frag
        .cst
        .children_with_tokens()
        .any(|el| is_significant_child(&el))
        && let (Some(prev), Some(next)) = (&prev_node, &next_node)
    {
        let back_gap = &prev_text[usize::from(prev.text_range().end())..region.start];
        let fwd_gap = &prev_text[region.end..usize::from(next.text_range().start())];
        let guard = format!(
            "{}{}{}{}{}",
            prev.text(),
            back_gap,
            fragment,
            fwd_gap,
            next.text()
        );
        let back_seam = usize::from(prev.text_range().len()) + back_gap.len();
        let fwd_seam = back_seam + fragment.len();
        let parsed = parse_fragment(&guard)?;
        if !no_straddle(&parsed.cst, back_seam) || !no_straddle(&parsed.cst, fwd_seam) {
            return None;
        }
    }

    // A zero-width diagnostic exactly on a guard seam is disambiguated by
    // the fragment's own boundary child, mirroring `splice_diagnostics`: a
    // statement node owns a start-anchored marker at its start and an
    // end-anchored one at its end; a trivia boundary child owns nothing.
    let frag_starts_with_node = frag
        .cst
        .children_with_tokens()
        .next()
        .is_some_and(|el| el.as_node().is_some());
    let frag_ends_with_node = frag
        .cst
        .children_with_tokens()
        .last()
        .is_some_and(|el| el.as_node().is_some());

    // Backward boundary parse: previous statement + gap trivia + fragment
    // must keep a boundary exactly at the old one, and the fragment must
    // parse in context with exactly the diagnostics it produced in
    // isolation.
    if let Some(prev) = &prev_node {
        let back_gap = &prev_text[usize::from(prev.text_range().end())..region.start];
        let guard = format!("{}{}{}", prev.text(), back_gap, fragment);
        let seam = guard.len() - fragment.len();
        let parsed = parse_fragment(&guard)?;
        if !no_straddle(&parsed.cst, seam) {
            return None;
        }
        let guard_frag: Vec<ParseDiagnostic> = parsed
            .diagnostics
            .iter()
            .filter(|d| {
                d.start > seam
                    || (d.start == seam && d.end > seam)
                    || (d.start == seam && d.end == seam && frag_starts_with_node)
            })
            .map(|d| {
                let mut d = d.clone();
                d.start -= seam;
                d.end -= seam;
                d
            })
            .collect();
        if guard_frag != frag.diagnostics {
            return None;
        }
    }

    // Forward boundary parse: fragment + gap trivia + next statement (or the
    // trailing trivia to EOF when no statement follows). Three checks
    // against one parse of the concatenation: no element straddles the seam;
    // the fragment reparses exactly as it did in isolation (an EOF-adjacent
    // junk run wraps differently once text follows it); and the kept next
    // statement reappears untouched — same start, kind, and length — since a
    // fragment ending in an unterminated bracket can consume the separating
    // newline and demote the next line to flat junk without anything
    // straddling the seam.
    let tail = match &next_node {
        Some(next) => {
            let gap = &prev_text[region.end..usize::from(next.text_range().start())];
            format!("{}{}", gap, next.text())
        }
        None => prev_text[region.end..usize::from(root.text_range().end())].to_string(),
    };
    if !tail.is_empty() {
        let guard = format!("{fragment}{tail}");
        let seam = fragment.len();
        let parsed = parse_fragment(&guard)?;
        if !no_straddle(&parsed.cst, seam) {
            return None;
        }
        let signature = |el: crate::syntax::SyntaxElement| {
            (
                usize::from(el.text_range().start()),
                el.as_node().is_some(),
                el.kind(),
                usize::from(el.text_range().len()),
            )
        };
        let frag_sig: Vec<_> = frag.cst.children_with_tokens().map(signature).collect();
        let pre_seam: Vec<_> = parsed
            .cst
            .children_with_tokens()
            .filter(|el| {
                let r = el.text_range();
                // A zero-width child exactly at the seam pairs with a junk
                // run on the far side; it belongs to the tail.
                usize::from(r.end()) <= seam && !(r.is_empty() && usize::from(r.start()) == seam)
            })
            .map(signature)
            .collect();
        if pre_seam != frag_sig {
            return None;
        }
        // The fragment's in-context diagnostics must also match: a trailing
        // token at the fragment's EOF can emit differently once text follows
        // it (`:` with a newline after it flags `QuoteColonWhitespace`; at
        // EOF it does not).
        let guard_frag: Vec<ParseDiagnostic> = parsed
            .diagnostics
            .iter()
            .filter(|d| {
                d.end < seam
                    || (d.end == seam && d.start < seam)
                    || (d.start == seam && d.end == seam && frag_ends_with_node)
            })
            .cloned()
            .collect();
        if guard_frag != frag.diagnostics {
            return None;
        }
        if let Some(next) = &next_node {
            let gap_len = usize::from(next.text_range().start()) - region.end;
            let reparsed_next =
                positive_nodes(&parsed.cst).find(|n| usize::from(n.text_range().start()) >= seam);
            let intact = reparsed_next.is_some_and(|n| {
                usize::from(n.text_range().start()) == seam + gap_len
                    && n.kind() == next.kind()
                    && n.text_range().len() == next.text_range().len()
            });
            if !intact {
                return None;
            }
        }
    }

    let diagnostics = splice_diagnostics(prev_diags, &frag.diagnostics, &region, delta)?;

    let frag_green = frag.cst.green();
    let green = root.green().splice_children(
        region.first..=region.last,
        frag_green.children().map(|c| c.to_owned()),
    );
    Some(Reparsed {
        green: root.replace_with(green),
        diagnostics,
        tier: if owner.is_some() {
            ReparseTier::Block
        } else {
            ReparseTier::TopLevel
        },
    })
}

/// The parse options and source from which an incremental reparse starts.
/// `green` and `diagnostics` must come from parsing `text` with `options`.
#[derive(Clone, Copy)]
pub struct ReparseBase<'a> {
    pub text: &'a str,
    pub green: &'a GreenNode,
    pub diagnostics: &'a [ParseDiagnostic],
    pub options: ParseOptions,
}

/// Incrementally reparse a tree produced with the default parsing options.
pub fn reparse(
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edit: &Edit,
    new_text: &str,
) -> Option<Reparsed> {
    let options = ParseOptions::default();
    reparse_with_options(
        ReparseBase {
            text: prev_text,
            green: prev_green,
            diagnostics: prev_diags,
            options,
        },
        edit,
        new_text,
        options,
    )
}

/// Reparse under the selected edition, declining a base from another edition.
pub fn reparse_with_options(
    base: ReparseBase<'_>,
    edit: &Edit,
    new_text: &str,
    options: ParseOptions,
) -> Option<Reparsed> {
    if base.options != options {
        return None;
    }
    reparse_same_options(
        options,
        base.text,
        base.green,
        base.diagnostics,
        edit,
        new_text,
    )
}

/// Replay edits against a tree produced with the default parsing options.
pub fn reparse_edits(
    prev_text: &str,
    prev_green: &GreenNode,
    prev_diags: &[ParseDiagnostic],
    edits: &[Edit],
    new_text: &str,
) -> Option<Reparsed> {
    let options = ParseOptions::default();
    reparse_edits_with_options(
        ReparseBase {
            text: prev_text,
            green: prev_green,
            diagnostics: prev_diags,
            options,
        },
        edits,
        new_text,
        options,
    )
}

/// Replay an edit chain under the selected edition. Edition changes require a
/// full parse, even when the source text has not changed.
pub fn reparse_edits_with_options(
    base: ReparseBase<'_>,
    edits: &[Edit],
    new_text: &str,
    options: ParseOptions,
) -> Option<Reparsed> {
    if base.options != options {
        return None;
    }
    reparse_edits_same_options(
        options,
        base.text,
        base.green,
        base.diagnostics,
        edits,
        new_text,
    )
}

fn lex_for_options(options: ParseOptions, text: &str) -> Vec<super::lexer::Token<'_>> {
    super::core::lex_for_options(text, options)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn edit(range: std::ops::Range<usize>, insert: &str) -> Edit {
        Edit {
            range,
            insert: insert.to_string(),
        }
    }

    /// The grammar decides some tree shapes by an identifier's *text*, and the
    /// token tier's same-kind relex cannot see such a change — so every such
    /// word has to be in [`CONTEXTUAL_IDENTS`]. Nothing in the type system says
    /// so, and the oracle only surfaces an omission by luck, so scan the
    /// grammar for the two spellings those comparisons take and check the words
    /// they name.
    ///
    /// The known words are asserted to have been *found*, not just listed: if a
    /// comparison is ever rewritten into a shape this scan does not recognize,
    /// the test fails loudly rather than quietly covering nothing.
    #[test]
    fn every_contextual_ident_is_listed() {
        // `t.kind == TokKind::Ident && t.text == "word"` and any form that
        // passes `t.text` along to a comparison, `matches!(t.text, "word" |
        // "other")` above all. Both are keyed on `t.text`, which is what makes
        // them text-structural.
        fn words_after(haystack: &str, marker: &str) -> Vec<String> {
            let mut out = Vec::new();
            for tail in haystack.split(marker).skip(1) {
                // Take the literals in the comparison that follows, stopping at
                // the first token that ends it.
                let stop = tail.find([';', '{', '}']).unwrap_or(tail.len());
                let mut rest = &tail[..stop];
                while let Some(open) = rest.find('"') {
                    let Some(close) = rest[open + 1..].find('"') else {
                        break;
                    };
                    out.push(rest[open + 1..open + 1 + close].to_string());
                    rest = &rest[open + 1 + close + 1..];
                }
            }
            out
        }

        // The grammar files, read from source: this is a lint over the code,
        // not over anything the crate exposes at runtime.
        let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/parser");
        let mut found: Vec<String> = Vec::new();
        for name in ["expr.rs", "structural.rs", "core.rs", "recovery.rs"] {
            let src = std::fs::read_to_string(dir.join(name))
                .unwrap_or_else(|e| panic!("read {name}: {e}"));
            found.extend(words_after(&src, "t.text =="));
            found.extend(words_after(&src, "t.text,"));
            found.extend(words_after(&src, "next.text =="));
        }
        assert!(
            !found.is_empty(),
            "the scan matched nothing — the grammar's `Ident`-text comparisons \
             have been rewritten, and this guard is no longer guarding anything"
        );

        for word in &found {
            assert!(
                CONTEXTUAL_IDENTS.contains(&word.as_str()),
                "the grammar branches on the identifier text {word:?}, so an \
                 edit into or out of it can reshape the tree under an unchanged \
                 token kind. Add it to CONTEXTUAL_IDENTS.",
            );
        }

        // Every word the scan is known to reach today. A rewrite that hides one
        // from the scan trips this rather than silently narrowing the guard.
        for expected in ["as", "abstract", "primitive", "type", "typegroup", "public"] {
            assert!(
                found.iter().any(|w| w == expected),
                "the scan no longer finds the {expected:?} comparison",
            );
        }
    }

    #[test]
    fn fingerprint_is_stable_and_discriminating() {
        let a1 = fingerprint(&crate::parser::parse("x = 1\n").cst);
        let a2 = fingerprint(&crate::parser::parse("x = 1\n").cst);
        let b = fingerprint(&crate::parser::parse("x = 2\n").cst);
        assert_eq!(a1, a2);
        assert_ne!(a1, b);
    }

    /// A numeric literal is deliberately outside `TOKEN_REPARSE_KINDS`, and a
    /// lone `x = 1` statement is the whole `ROOT`, so the top-level tier finds
    /// no decoupled region either.
    #[test]
    fn a_number_edit_in_a_lone_statement_falls_back() {
        let parsed = crate::parser::parse("x = 1\n");
        let green = parsed.cst.green().to_owned();
        let e = diff_edit("x = 1\n", "x = 2\n");
        assert!(reparse("x = 1\n", &green, &parsed.diagnostics, &e, "x = 2\n").is_none());
    }

    /// The point of the `STRING_CONTENT` path: a docstring body edit stays at
    /// the token tier. `fold_docstrings` folds a docstring with the definition
    /// it documents into one `ROOT` child, so the statement tier would answer
    /// this by reparsing the docstring *and* the whole function under it.
    #[test]
    fn a_docstring_body_edit_stays_at_the_token_tier() {
        let old = "\"\"\"\ndoc\n\"\"\"\nfunction f(x)\n    x + 1\nend\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        let e = edit(5..5, "s");
        let new = e.apply(old);

        let out = reparse(old, &green, &parsed.diagnostics, &e, &new)
            .expect("a docstring body edit is spliceable");
        assert_eq!(out.tier, ReparseTier::Token);
    }

    /// An unterminated literal runs to EOF, so its node is the rest of the
    /// file. The diagnostic bail must reject a content edit inside one — and
    /// must do so before either relex, which is why it is hoisted to the top
    /// of `try_splice_string_content`.
    #[test]
    fn a_content_edit_in_an_unterminated_literal_falls_back() {
        let old = "x = 1\ny = \"abc\nz = 2\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        // Offset 12 is inside the content run, which here swallows the rest of
        // the file; `UnterminatedLiteral` is anchored at the literal's start.
        let token = SyntaxNode::new_root(green.clone())
            .token_at_offset(TextSize::new(12))
            .right_biased()
            .expect("a leaf at offset 12");
        assert_eq!(token.kind(), SyntaxKind::STRING_CONTENT);

        let e = edit(12..13, "X");
        let new = e.apply(old);
        assert!(
            try_splice_string_content(
                ParseOptions::default(),
                old,
                &parsed.diagnostics,
                &e,
                &token
            )
            .is_none()
        );
        // The statement tier may still answer it (the in-crate Tenet-4 assert
        // validates that splice); what matters is that the token tier declined.
        let tier = reparse(old, &green, &parsed.diagnostics, &e, &new).map(|r| r.tier);
        assert_ne!(tier, Some(ReparseTier::Token));
    }

    /// An edit that does not fit `prev_text` is rejected rather than applied at
    /// offsets that would panic on a slice. `reparse_edits` checks its edits
    /// before slicing; the single-edit entry point has to check too.
    #[test]
    fn reparse_rejects_an_edit_that_does_not_fit() {
        let old = "α = 1\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        // `new_text` is deliberately not the edit's result: a misfitting edit
        // has none, which is the whole point.
        let run = |e: Edit| reparse(old, &green, &parsed.diagnostics, &e, old);

        // Past the end of the text.
        assert!(run(edit(99..99, "x")).is_none());
        // Off a char boundary (α is two bytes).
        assert!(run(edit(1..1, "x")).is_none());
        // Inverted range (built by hand: a literal `4..2` is a clippy error).
        let inverted = Edit {
            range: std::ops::Range { start: 4, end: 2 },
            insert: "x".to_string(),
        };
        assert!(run(inverted).is_none());
    }

    /// A region wide enough that answering it costs more than the full parse it
    /// avoids is declined on the cheap evidence, before any fragment parse. The
    /// shape is a collapsed `diff_edit` of scattered edits, which spans
    /// everything between the first and the last.
    #[test]
    fn a_region_covering_most_of_a_large_file_is_declined() {
        let mut old = String::new();
        for i in 0..2000 {
            old.push_str(&format!("var_{i} = {i}\n"));
        }
        let parsed = crate::parser::parse(&old);
        let green = parsed.cst.green().to_owned();

        // A narrow edit near the end still splices: the guard keys on the
        // region, not on the file size.
        let narrow = edit(old.len()..old.len(), "tail = 1\n");
        assert!(
            reparse(
                &old,
                &green,
                &parsed.diagnostics,
                &narrow,
                &narrow.apply(&old)
            )
            .is_some()
        );

        // The same net change seen as one collapsed edit spanning most of the
        // file: `select_region` returns nearly every child, and the tier bails.
        // Both sites are inside an identifier, and the second is expressed
        // against the text the first produced (one byte longer), so the chain
        // below stays at the token tier. The last line is `var_1999 = 1999\n`,
        // 16 bytes, whose name runs from its start.
        let scattered = vec![edit(4..4, "X"), edit(old.len() - 11..old.len() - 11, "X")];
        let new = apply_edits(&old, &scattered);
        let collapsed = diff_edit(&old, &new);
        assert!(collapsed.range.len() > old.len() / 2, "{collapsed:?}");
        assert!(reparse(&old, &green, &parsed.diagnostics, &collapsed, &new).is_none());

        // Replaying the chain still answers it at the token tier — the guard
        // costs the precise-edit path nothing.
        let out = reparse_edits(&old, &green, &parsed.diagnostics, &scattered, &new)
            .expect("the precise chain is unaffected");
        assert_eq!(out.tier, ReparseTier::Token);
    }

    /// The width guard is a fraction *and* a floor: a small file's region is
    /// always worth trying, however much of the file it covers.
    #[test]
    fn the_region_width_guard_leaves_small_files_alone() {
        assert!(!region_is_too_wide(4 * 1024, 4 * 1024));
        assert!(!region_is_too_wide(64 * 1024, 1024 * 1024));
        assert!(region_is_too_wide(64 * 1024, 128 * 1024));
    }

    /// The shape `reparse_edits` exists for: two identifier edits far apart,
    /// which `diff_edit` would collapse into one span covering both statements.
    #[test]
    fn reparse_edits_chains_scattered_edits() {
        let old = "alpha = 1\nfiller = 2\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        // `alpha` -> `alphaX`, then `omega` -> `omegaX` at its post-first-edit
        // offset (one byte further along).
        let edits = vec![edit(5..5, "X"), edit(27..27, "X")];
        let new = apply_edits(old, &edits);
        assert_eq!(new, "alphaX = 1\nfiller = 2\nomegaX = 3\n");

        // The collapsed diff spans from `alpha` all the way to `omega`, so the
        // cheap token tier cannot touch it and the whole run of statements has
        // to be reparsed. Replaying the chain keeps both splices at the token
        // tier. On three statements that is a wash; on a real file the
        // difference is two orders of magnitude (`benches/reparse.rs`), which
        // is why the chain is tried first.
        let collapsed = diff_edit(old, &new);
        assert!(collapsed.range.len() > 20, "{collapsed:?} should be coarse");
        let via_diff = reparse(old, &green, &parsed.diagnostics, &collapsed, &new)
            .expect("the whole-file region is still spliceable at this size");
        assert_eq!(via_diff.tier, ReparseTier::TopLevel);

        let out = reparse_edits(old, &green, &parsed.diagnostics, &edits, &new)
            .expect("two identifier edits should chain");
        assert_eq!(out.tier, ReparseTier::Token);
        assert_eq!(
            fingerprint(&SyntaxNode::new_root(out.green)),
            fingerprint(&crate::parser::parse(&new).cst),
        );
    }

    /// The chain reports the most expensive tier it had to reach.
    #[test]
    fn reparse_edits_reports_the_max_tier() {
        let old = "alpha = 1\nfiller = 2\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        // An ident edit (token tier) plus a whole added statement (top level),
        // appended at the post-first-edit end of the buffer.
        let edits = vec![edit(5..5, "X"), edit(32..32, "beta = 4\n")];
        let new = apply_edits(old, &edits);

        let out = reparse_edits(old, &green, &parsed.diagnostics, &edits, &new)
            .expect("ident edit plus a new statement should chain");
        assert_eq!(out.tier, ReparseTier::TopLevel);
    }

    #[test]
    fn reparse_edits_declines_an_empty_chain() {
        let old = "alpha = 1\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();

        assert!(reparse_edits(old, &green, &parsed.diagnostics, &[], old).is_none());
    }

    #[test]
    fn reparse_edits_accepts_a_single_verified_edit() {
        let old = "alpha = 1\nfiller = 2\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        for change in [
            edit(5..5, "β"),
            edit(0..5, "β"),
            edit(4..5, ""),
            edit(0..0, "beta = 4\n"),
            edit(old.len()..old.len(), "beta = 4\n"),
        ] {
            let new = change.apply(old);
            let out = reparse_edits(old, &green, &parsed.diagnostics, &[change], &new)
                .expect("a single verified edit should reparse");
            let full = crate::parser::parse(&new);
            let root = SyntaxNode::new_root(out.green);
            assert_eq!(root.to_string(), new);
            assert_eq!(fingerprint(&root), fingerprint(&full.cst));
            assert_eq!(out.diagnostics, full.diagnostics);
        }
    }

    #[test]
    fn reparse_edits_rejects_a_stale_or_invalid_single_edit() {
        let old = "α = 1\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        for (change, actual) in [
            (edit(2..2, "X"), "βX = 1\nomega = 3\n"),
            (edit(2..2, "X"), "αY = 1\nomega = 3\n"),
            (edit(2..2, "X"), "αX = 1\nomega = 4\n"),
            (edit(2..2, "X"), ""),
            (edit(2..2, "X"), "α"),
            (edit(2..2, "X"), "αX"),
            (edit(500..500, "X"), old),
            (edit(std::ops::Range { start: 2, end: 1 }, "X"), old),
            (edit(1..2, "X"), old),
            (edit(0..1, "X"), old),
        ] {
            assert!(
                reparse_edits(
                    old,
                    &green,
                    &parsed.diagnostics,
                    std::slice::from_ref(&change),
                    actual,
                )
                .is_none(),
                "must reject {change:?} targeting {actual:?}"
            );
        }
    }

    /// A chain that is not the exact transform between the two texts is
    /// rejected before any offset is trusted — both when it simply does not
    /// reproduce the target, and when it does not fit the source at all.
    #[test]
    fn reparse_edits_rejects_a_stale_chain() {
        let old = "alpha = 1\nfiller = 2\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();

        // Applies cleanly, but the buffer moved on underneath it.
        let edits = vec![edit(5..5, "X"), edit(26..26, "X")];
        let elsewhere = "alphaX = 1\nfiller = 2\nomegaX = 3\nextra = 4\n";
        assert!(reparse_edits(old, &green, &parsed.diagnostics, &edits, elsewhere).is_none());

        // Does not even fit the source text.
        let past_end = vec![edit(500..500, "X"), edit(501..501, "X")];
        assert!(reparse_edits(old, &green, &parsed.diagnostics, &past_end, old).is_none());
    }

    /// One unhandleable step aborts the whole chain — the caller full-parses.
    #[test]
    fn reparse_edits_aborts_on_an_unhandleable_step() {
        let old = "alpha = 1\nfiller = 2\nomega = 3\n";
        let parsed = crate::parser::parse(old);
        let green = parsed.cst.green().to_owned();
        // The second edit opens an unterminated string, which no tier splices.
        let edits = vec![edit(5..5, "X"), edit(26..26, "\"")];
        let new = apply_edits(old, &edits);
        assert!(reparse_edits(old, &green, &parsed.diagnostics, &edits, &new).is_none());
    }
}