lang-check 0.4.4

Multilingual prose linter with tree-sitter extraction and pluggable checking engines
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
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
use tree_sitter::Node;

use super::{ProseRange, shared};

/// Commands whose arguments are not prose and are skipped entirely:
/// identifiers/addresses (`\ref`, `\import`, …) or verbatim code
/// (`\code`, `\codeblock`, `\pre`) that should not be spell/grammar checked.
const STRUCTURAL_COMMANDS: &[&str] = &[
    "\\import",
    "\\export",
    "\\transclude",
    "\\ref",
    "\\author",
    "\\contributor",
    "\\date",
    "\\parent",
    "\\tag",
    "\\taxon",
    "\\meta",
    "\\number",
    "\\def",
    "\\let",
    "\\alloc",
    "\\open",
    "\\namespace",
    "\\put",
    "\\get",
    "\\put?",
    "\\object",
    "\\patch",
    "\\call",
    "\\tex",
    "\\codeblock",
    "\\pre",
    "\\startverb",
    "\\xmlns",
    "\\query",
    "\\datalog",
    "\\code",
];

/// Block-level commands that contain prose but create scope boundaries.
/// Text across these boundaries is never merged into a single sentence.
const BLOCK_COMMANDS: &[&str] = &[
    "\\p",
    "\\li",
    "\\ol",
    "\\ul",
    "\\title",
    "\\blockquote",
    "\\figure",
    "\\figcaption",
    "\\scope",
    "\\subtree",
];

/// Inline commands whose content bridges with surrounding prose.
const INLINE_COMMANDS: &[&str] = &["\\em", "\\strong"];

/// Node kinds that are never prose and whose subtrees should be skipped.
const SKIP_KINDS: &[&str] = &[
    "inline_math",
    "display_math",
    "verbatim",
    "comment",
    "wiki_link",
    "command_name",
];

/// Extract prose ranges from a Forester AST.
///
/// Uses scoped collection: block-level commands (\p, \li, \ol, etc.) create
/// prose scope boundaries that prevent sentence merging across them. Inline
/// commands (\em, \strong) bridge with surrounding text. Unknown macros are
/// recursed into so nested known blocks are still extracted. Math (#{}, ##{})
/// is excluded both via tree-sitter nodes and a text-based safety-net scanner.
/// Verbatim, comments, and wiki links are always excluded.
pub fn extract(text: &str, root: Node) -> Vec<ProseRange> {
    let mut scopes: Vec<Vec<(usize, usize)>> = vec![vec![]];
    let mut skips: Vec<(usize, usize)> = Vec::new();
    collect_prose_nodes(root, text, &mut scopes, &mut skips, false);

    // Safety net: text-based math scanner catches regions where tree-sitter
    // truncated display_math/inline_math at escaped braces.
    skips.extend(find_math_regions(text));

    let mut result: Vec<ProseRange> = scopes
        .iter()
        .filter(|s| !s.is_empty())
        .flat_map(|scope| {
            shared::merge_ranges(
                scope,
                text,
                strip_forester_noise,
                collect_forester_exclusions,
            )
        })
        .collect();

    shared::install_skip_exclusions(&mut result, &skips, text.as_bytes());
    shared::dedup_exclusions(&mut result);
    result.retain(|r| !shared::is_fully_excluded(r));
    result
}

/// Get the command name string from a command node.
fn get_command_name<'a>(node: Node, text: &'a str) -> Option<&'a str> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "command_name" {
            return Some(&text[child.start_byte()..child.end_byte()]);
        }
    }
    None
}

/// Scan a text slice for `\command{...}` patterns and add skip regions for the
/// command name + opening brace and closing brace, so only the inner text
/// remains as prose. Handles nesting (e.g. `\strong{\em{text}}`).
fn strip_inline_commands_in_range(
    slice: &str,
    base_offset: usize,
    skips: &mut Vec<(usize, usize)>,
) {
    let bytes = slice.as_bytes();
    let len = bytes.len();
    let mut i = 0;
    while i < len {
        if bytes[i] == b'\\' && i + 1 < len && bytes[i + 1].is_ascii_alphabetic() {
            let cmd_start = i;
            i += 1;
            while i < len && (bytes[i].is_ascii_alphabetic() || bytes[i] == b'?') {
                i += 1;
            }
            if i < len && bytes[i] == b'{' {
                // Skip \command{ (command name through opening brace)
                skips.push((base_offset + cmd_start, base_offset + i + 1));
                let close = shared::skip_balanced_bytes(bytes, i + 1, b'{', b'}', None);
                // Skip the closing '}' (byte just before `close`)
                if close > i + 1 {
                    skips.push((base_offset + close - 1, base_offset + close));
                }
                // Continue scanning inside the braces for nested commands
                i += 1;
            } else {
                skips.push((base_offset + cmd_start, base_offset + i));
            }
        } else {
            i += 1;
        }
    }
}

/// Decide whether a single-letter inline-math interior should be kept as prose
/// (with the `#{}` delimiters stripped) rather than excluded like other math.
///
/// Only single **uppercase** letters other than `I` qualify. Uppercase letters
/// read as named mathematical objects (`#{F}`, `#{X}`), and keeping them as prose
/// lets a sentence such as `#{F} is a functor.` satisfy sentence-start
/// capitalization instead of flagging the following word.
///
/// Excluded — because `LanguageTool` grammar-checks them as English words, turning
/// math content into false positives:
/// - lowercase letters, including index variables `#{i}`, `#{j}`, `#{x}`
///   (`i` triggers `I_LOWERCASE` plus a cascading `PERS_PRONOUN_AGREEMENT` on the
///   neighbouring real word);
/// - the pronoun `I` (`#{I}`, the imaginary unit / identity), which triggers
///   `PERS_PRONOUN_AGREEMENT` / `NON3PRS_VERB`.
fn single_letter_kept_as_prose(trimmed: &str) -> bool {
    matches!(trimmed.as_bytes(), [b] if b.is_ascii_uppercase() && *b != b'I')
}

/// Check if an `inline_math` node holds a single letter that should be kept as
/// prose (see [`single_letter_kept_as_prose`]). Returns `Some(byte_offset)` of
/// the letter when it qualifies, `None` otherwise.
///
/// Inline math in Forester looks like `#{F}`. The node text includes the
/// `#{` prefix and `}` suffix, optionally with whitespace around the letter.
fn inline_math_single_letter(node: Node, text: &str) -> Option<usize> {
    let raw = &text[node.start_byte()..node.end_byte()];
    // Strip the #{ prefix and } suffix
    let inner = raw.strip_prefix("#{")?.strip_suffix('}')?;
    let trimmed = inner.trim();
    if single_letter_kept_as_prose(trimmed) {
        // Find the byte offset of the letter in the original text
        let inner_start = node.start_byte() + "#{".len();
        let offset_in_inner = inner.find(trimmed)?;
        Some(inner_start + offset_in_inner)
    } else {
        None
    }
}

/// Push a node's byte range into the current scope.
fn push_to_scope(node: Node, scopes: &mut [Vec<(usize, usize)>]) {
    let start = node.start_byte();
    let end = node.end_byte();
    if start < end
        && let Some(scope) = scopes.last_mut()
    {
        scope.push((start, end));
    }
}

/// Handle a `markdown_link` token: extract the alias text as prose, skip
/// brackets and the `](address)` suffix. Inline commands in the alias
/// (e.g. `[\em{text}](addr)`) are stripped so only inner text remains.
fn handle_markdown_link(
    node: Node,
    text: &str,
    scopes: &mut [Vec<(usize, usize)>],
    skips: &mut Vec<(usize, usize)>,
    in_prose: bool,
) {
    let node_text = &text[node.start_byte()..node.end_byte()];
    let Some(close_bracket) = node_text.find(']') else {
        skips.push((node.start_byte(), node.end_byte()));
        return;
    };
    let alias_start = node.start_byte() + 1; // skip '['
    let alias_end = node.start_byte() + close_bracket;
    if alias_start >= alias_end {
        skips.push((node.start_byte(), node.end_byte()));
        return;
    }
    skips.push((node.start_byte(), alias_start)); // skip '['
    strip_inline_commands_in_range(&text[alias_start..alias_end], alias_start, skips);
    skips.push((alias_end, node.end_byte())); // skip '](addr)'
    // Push the entire node range so merge_ranges sees no gap between the
    // link and its surrounding text siblings. The skips above ensure only
    // the alias inner text survives.
    if in_prose && let Some(scope) = scopes.last_mut() {
        scope.push((node.start_byte(), node.end_byte()));
    }
}

/// Dispatch a `command` node to structural, block, inline, or unknown handling.
fn handle_command(
    node: Node,
    text: &str,
    scopes: &mut Vec<Vec<(usize, usize)>>,
    skips: &mut Vec<(usize, usize)>,
) {
    let cmd_name = get_command_name(node, text);

    if cmd_name.is_some_and(|n| STRUCTURAL_COMMANDS.contains(&n)) {
        return;
    }

    if cmd_name.is_some_and(|n| BLOCK_COMMANDS.contains(&n)) {
        scopes.push(vec![]);
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() != "bracket_group" {
                collect_prose_nodes(child, text, scopes, skips, true);
            }
        }
        scopes.push(vec![]);
        return;
    }

    if cmd_name.is_some_and(|n| INLINE_COMMANDS.contains(&n)) {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "brace_group" {
                skips.push((child.start_byte(), child.start_byte() + 1));
                skips.push((child.end_byte() - 1, child.end_byte()));
            }
            collect_prose_nodes(child, text, scopes, skips, true);
        }
        return;
    }

    // Unknown command: recurse but don't enable prose collection
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_prose_nodes(child, text, scopes, skips, false);
    }
}

/// Recursively collect prose leaf nodes into scoped segments.
///
/// Block-level commands push new segments to prevent cross-boundary merging.
/// Inline commands recurse normally so their content bridges. Unknown macros
/// recurse into children (so nested `\p`/`\li` are found) but don't enable
/// text collection — only known prose commands set `in_prose` to true.
fn collect_prose_nodes(
    node: Node,
    text: &str,
    scopes: &mut Vec<Vec<(usize, usize)>>,
    skips: &mut Vec<(usize, usize)>,
    in_prose: bool,
) {
    let kind = node.kind();

    // Skip entire subtrees for non-prose node kinds.
    // Exception: inline math containing a single uppercase letter other than `I`
    // (e.g. #{F}) is included as prose with the math delimiters stripped.
    if SKIP_KINDS.contains(&kind) {
        if kind == "inline_math"
            && let Some(letter_byte) = inline_math_single_letter(node, text)
        {
            // Skip the #{...} delimiters but include the letter as prose
            skips.push((node.start_byte(), letter_byte));
            skips.push((letter_byte + 1, node.end_byte()));
            if in_prose {
                push_to_scope(node, scopes);
            }
            return;
        }
        skips.push((node.start_byte(), node.end_byte()));
        return;
    }

    // Markdown links: extract alias as prose, skip brackets and address.
    if kind == "markdown_link" {
        handle_markdown_link(node, text, scopes, skips, in_prose);
        return;
    }

    // Command dispatch: structural, block, inline, or unknown.
    if kind == "command" {
        handle_command(node, text, scopes, skips);
        return;
    }

    // Parenthesised groups and leaf text nodes inside prose.
    if in_prose && (kind == "paren_group" || kind == "text") {
        push_to_scope(node, scopes);
        return;
    }

    // source_file: collect text (handles orphaned nodes from AST truncation).
    // find_math_regions will exclude any leaked math content.
    // Other nodes (brace_group, paren_group, etc.): preserve current context.
    let child_prose = in_prose || kind == "source_file";
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_prose_nodes(child, text, scopes, skips, child_prose);
    }
}

/// Strip Forester noise from a gap string: math, commands, escapes.
/// Leaves whitespace, braces, and punctuation for bridge analysis.
fn strip_forester_noise(gap: &str) -> String {
    let mut result = String::new();
    let chars: Vec<char> = gap.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        // Display math: ##{...}
        if chars[i] == '#' && i + 2 < chars.len() && chars[i + 1] == '#' && chars[i + 2] == '{' {
            i = shared::skip_balanced_chars(&chars, i + 3, '{', '}');
            result.push(' ');
        // Inline math: #{...}
        } else if chars[i] == '#' && i + 1 < chars.len() && chars[i + 1] == '{' {
            i = shared::skip_balanced_chars(&chars, i + 2, '{', '}');
            result.push(' ');
        // Command: \name followed by optional {}, [], () args
        } else if chars[i] == '\\' && i + 1 < chars.len() && chars[i + 1].is_ascii_alphanumeric() {
            i += 1;
            while i < chars.len()
                && (chars[i].is_ascii_alphanumeric()
                    || chars[i] == '-'
                    || chars[i] == '/'
                    || chars[i] == '?'
                    || chars[i] == '*')
            {
                i += 1;
            }
            i = shared::skip_command_args_chars(&chars, i, &[('{', '}'), ('[', ']'), ('(', ')')]);
        // Escape: \X
        } else if chars[i] == '\\' && i + 1 < chars.len() {
            i += 2;
        // Comment: % to end of line
        } else if chars[i] == '%' {
            while i < chars.len() && chars[i] != '\n' {
                i += 1;
            }
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }
    result
}

/// Collect exclusion regions from a gap string between prose text nodes.
///
/// Works on bytes directly — Forester markup delimiters (`#`, `{`, `}`, `\`,
/// `%`) are all single-byte ASCII. Called by `merge_ranges` for each bridgeable
/// gap so that commands, math, escapes, and comments become exclusions.
fn collect_forester_exclusions(gap: &str, offset: usize, exclusions: &mut Vec<(usize, usize)>) {
    let b = gap.as_bytes();
    let len = b.len();
    let mut i = 0;
    while i < len {
        let start = i;
        if b[i] == b'#' && i + 2 < len && b[i + 1] == b'#' && b[i + 2] == b'{' {
            i = shared::skip_balanced_bytes(b, i + 3, b'{', b'}', Some(b'\\')); // display math
            exclusions.push((offset + start, offset + i));
        } else if b[i] == b'#' && i + 1 < len && b[i + 1] == b'{' {
            i = shared::skip_balanced_bytes(b, i + 2, b'{', b'}', Some(b'\\')); // inline math
            exclusions.push((offset + start, offset + i));
        } else if b[i] == b'\\' && i + 1 < len && b[i + 1].is_ascii_alphanumeric() {
            i = skip_command_with_args(b, i); // \name{...}[...](...)
            exclusions.push((offset + start, offset + i));
        } else if b[i] == b'\\' && i + 1 < len {
            i += 2; // escape \X
            exclusions.push((offset + start, offset + i));
        } else if b[i] == b'%' {
            while i < len && b[i] != b'\n' {
                i += 1;
            }
            exclusions.push((offset + start, offset + i));
        } else {
            i += 1;
        }
    }
}

/// Skip a `\name` command and its optional brace/bracket/paren arguments.
fn skip_command_with_args(b: &[u8], mut i: usize) -> usize {
    i += 1; // skip backslash
    while i < b.len() && (b[i].is_ascii_alphanumeric() || matches!(b[i], b'-' | b'/' | b'?' | b'*'))
    {
        i += 1;
    }
    shared::skip_command_args_bytes(b, i, &[(b'{', b'}'), (b'[', b']'), (b'(', b')')])
}

/// Scan raw text for `#{...}` and `##{...}` math regions using escape-aware
/// brace counting. Returns byte ranges covering each math region (including
/// the `#` / `##` prefix and the outermost braces).
///
/// This provides a safety net for cases where tree-sitter truncates math nodes
/// at escaped braces (`\{`, `\}`).
fn find_math_regions(text: &str) -> Vec<(usize, usize)> {
    let bytes = text.as_bytes();
    let len = bytes.len();
    let mut regions = Vec::new();
    let mut i = 0;

    while i < len {
        // Skip % comments (to end of line)
        if bytes[i] == b'%' {
            while i < len && bytes[i] != b'\n' {
                i += 1;
            }
            continue;
        }

        // Skip escape sequences: \X consumes 2 bytes (prevents \# from
        // triggering math detection)
        if bytes[i] == b'\\' && i + 1 < len {
            i += 2;
            continue;
        }

        // Display math: ##{...}
        if bytes[i] == b'#' && i + 2 < len && bytes[i + 1] == b'#' && bytes[i + 2] == b'{' {
            let start = i;
            i = shared::skip_balanced_bytes(bytes, i + 3, b'{', b'}', Some(b'\\'));
            regions.push((start, i));
            continue;
        }

        // Inline math: #{...}
        // Exception: single uppercase letter other than `I` (e.g. #{F}) is
        // included as prose with delimiter-only skip regions so the letter
        // survives.
        if bytes[i] == b'#' && i + 1 < len && bytes[i + 1] == b'{' {
            let start = i;
            let end = shared::skip_balanced_bytes(bytes, i + 2, b'{', b'}', Some(b'\\'));
            let inner = &text[start + 2..end.saturating_sub(1).max(start + 2)];
            let trimmed = inner.trim();
            if single_letter_kept_as_prose(trimmed) {
                // Skip only the delimiters, not the letter
                let letter_offset = start + 2 + inner.find(trimmed).unwrap_or(0);
                regions.push((start, letter_offset));
                regions.push((letter_offset + 1, end));
            } else {
                regions.push((start, end));
            }
            i = end;
            continue;
        }

        i += 1;
    }

    regions
}

#[cfg(test)]
mod tests {
    use crate::prose::ProseExtractor;
    use crate::prose::latex::LatexExtras;
    use anyhow::Result;

    fn forester_extractor() -> Result<ProseExtractor> {
        let language: tree_sitter::Language = crate::forester_ts::LANGUAGE.into();
        ProseExtractor::new(language)
    }

    #[test]
    fn test_forester_basic_extraction() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\title{Hello World}
\p{This is a paragraph.}
";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted.iter().any(|t| t.contains("Hello World")),
            "Should extract title text, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("This is a paragraph")),
            "Should extract paragraph text, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_math_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Display math between paragraphs (separated by blank line) should
        // not appear in prose ranges.
        let text = "\\p{Text before math.}\n\n##{\\int_0^1 f(x) \\, dx}\n\n\\p{Text after math.}\n";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted.iter().any(|t| t.contains("Text before math")),
            "Should extract text before math, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("Text after math")),
            "Should extract text after math, got: {extracted:?}"
        );
        assert!(
            !extracted.iter().any(|t| t.contains("\\int")),
            "Should NOT extract display math content, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_structural_commands_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\import{trees/basics}
\ref{tree-0001}
\p{Some actual prose.}
";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            !extracted.iter().any(|t| t.contains("trees/basics")),
            "Should NOT extract import path, got: {extracted:?}"
        );
        assert!(
            !extracted.iter().any(|t| t.contains("tree-0001")),
            "Should NOT extract ref target, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("actual prose")),
            "Should extract prose text, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_verbatim_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = "\\p{Before code.}\n```\nfn main() {}\n```\n\\p{After code.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            !extracted.iter().any(|t| t.contains("fn main")),
            "Should NOT extract verbatim content, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("Before code")),
            "Should extract text before verbatim, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("After code")),
            "Should extract text after verbatim, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_inline_commands_bridge() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{This has \em{emphasized} words in it.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted.iter().any(|t| t.contains("This has")
                && t.contains("emphasized")
                && t.contains("words in it")),
            "Sentence with inline command should bridge into single chunk, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_display_math_exclusion() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{We know that
##{
  x^2 + y^2 = z^2
}
which proves our claim.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        // Should bridge across display math
        let bridged = ranges.iter().find(|r| {
            let raw = &text[r.start_byte..r.end_byte];
            raw.contains("know that") && raw.contains("proves our claim")
        });
        assert!(
            bridged.is_some(),
            "Should bridge across display math, got: {:?}",
            ranges
                .iter()
                .map(|r| &text[r.start_byte..r.end_byte])
                .collect::<Vec<_>>()
        );

        let range = bridged.unwrap();
        assert!(
            !range.exclusions.is_empty(),
            "Should have exclusions for display math"
        );

        let clean_text = range.extract_text(text);
        assert!(
            !clean_text.contains("x^2"),
            "extract_text should not contain math content, got: {:?}",
            clean_text
        );
        assert!(
            clean_text.contains("know that"),
            "extract_text should still contain prose, got: {:?}",
            clean_text
        );

        Ok(())
    }

    #[test]
    fn test_forester_list_items_separate_scopes() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\ol{\li{Item one}\li{Item two}\li{Item three}}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        // Each \li should be a separate prose range — never merged into one sentence
        assert!(
            ranges.len() >= 3,
            "Each list item should be a separate prose range, got {} ranges: {:?}",
            ranges.len(),
            ranges
                .iter()
                .map(|r| &text[r.start_byte..r.end_byte])
                .collect::<Vec<_>>()
        );
        // No single range should span across list items
        assert!(
            !ranges.iter().any(|r| {
                let t = &text[r.start_byte..r.end_byte];
                t.contains("one") && t.contains("two")
            }),
            "List items should not merge into a single range"
        );

        Ok(())
    }

    #[test]
    fn test_forester_inline_math_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{The value #{x + y} is positive.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted.iter().any(|t| t.contains("The value")),
            "Should extract prose around inline math, got: {extracted:?}"
        );

        // The range that contains the math should have an exclusion for it
        let range_with_math = ranges.iter().find(|r| {
            let raw = &text[r.start_byte..r.end_byte];
            raw.contains("value") && raw.contains("positive")
        });
        if let Some(range) = range_with_math {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("x + y"),
                "Inline math should be excluded from clean text, got: {:?}",
                clean
            );
        }

        Ok(())
    }

    #[test]
    fn test_inline_math_single_letter_included() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{Let #{F} be a functor.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();

        assert!(
            clean.contains('F'),
            "Single-letter inline math should be included as prose, got: {clean:?}"
        );
        assert!(
            clean.contains("Let") && clean.contains("be a functor"),
            "Surrounding prose should be preserved, got: {clean:?}"
        );
        assert!(
            !clean.contains('#'),
            "Math delimiters should be stripped, got: {clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_inline_math_single_letter_various() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Lowercase letter — excluded (e.g. index variables); LanguageTool would
        // otherwise grammar-check it as an English word.
        let text = r"\p{The quantity #{v} is fixed here.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();
        assert!(
            !clean.contains('v') && clean.contains("quantity") && clean.contains("fixed"),
            "Lowercase single letter should be excluded, got: {clean:?}"
        );

        // Lowercase `i` — excluded; would trigger I_LOWERCASE + agreement errors.
        // Surrounding prose is chosen to contain no other 'i'.
        let text = r"\p{When #{i} equals zero we stop.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();
        assert!(
            !clean.contains('i') && clean.contains("equals zero"),
            "Lowercase i should be excluded, got: {clean:?}"
        );

        // Uppercase pronoun `I` — excluded; reads as the pronoun, not a math
        // object. Surrounding prose contains no other capital 'I'.
        let text = r"\p{Here #{I} denotes the unit.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();
        assert!(
            !clean.contains('I') && clean.contains("denotes the unit"),
            "Uppercase pronoun I should be excluded, got: {clean:?}"
        );

        // Multi-character math is still excluded
        let text = r"\p{The value #{xy} is large.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();
        assert!(
            !clean.contains("xy"),
            "Multi-character math should still be excluded, got: {clean:?}"
        );

        // Digit is not a letter — should be excluded
        let text = r"\p{The number #{3} is odd.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();
        assert!(
            !clean.contains('3'),
            "Single digit should still be excluded, got: {clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_inline_math_single_letter_with_spaces() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Whitespace around the letter inside math
        let text = r"\p{Let #{ G } be a group.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let clean: String = ranges.iter().map(|r| r.extract_text(text)).collect();

        assert!(
            clean.contains('G'),
            "Single letter with spaces should be included, got: {clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_single_letter_kept_as_prose_predicate() {
        use super::single_letter_kept_as_prose;
        // Uppercase math objects are kept.
        for s in ["F", "X", "G", "A", "Z"] {
            assert!(single_letter_kept_as_prose(s), "{s:?} should be kept");
        }
        // Lowercase, the pronoun I, digits, and multi-char are excluded.
        for s in ["i", "j", "x", "v", "I", "3", "xy", "", "+"] {
            assert!(!single_letter_kept_as_prose(s), "{s:?} should be excluded");
        }
    }

    #[test]
    fn test_forester_block_math_multiline_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Block math with real newlines inside \p — tree-sitter parses ##{...}
        // as display_math which is in SKIP_KINDS, so it should be excluded.
        let text = "\\p{Consider the equation\n##{  x^2 + y^2 = z^2 }\nwhich is well known.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        // The math content should not appear in extracted prose
        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("x^2"),
                "Block math content should not appear in clean prose, got: {:?}",
                clean
            );
        }

        // Surrounding prose should still be extracted
        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("Consider the equation"),
            "Prose before block math should be extracted, got: {:?}",
            all_text
        );
        assert!(
            all_text.contains("well known"),
            "Prose after block math should be extracted, got: {:?}",
            all_text
        );

        Ok(())
    }

    #[test]
    fn test_forester_unknown_macros_recurse() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Unknown macros now recurse into children, so nested \p is extracted
        let text = r"\solution{
  \p{Prose inside unknown wrapper.}
}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted
                .iter()
                .any(|t| t.contains("Prose inside unknown wrapper")),
            "Nested \\p inside unknown macro should be extracted, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_unknown_macros_plain_text_skipped() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Plain text inside unknown macros is NOT collected — only text
        // inside known prose commands (\p, \li, etc.) is prose.
        let text = r"\p{Real prose here.}
\mymacro{macro content}
\p{More real prose.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            !extracted.iter().any(|t| t.contains("macro content")),
            "Text inside unknown macro should NOT be extracted, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("Real prose")),
            "Known commands should still extract prose, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_nested_blocks_separate_scopes() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // A title and paragraph inside a subtree should be separate scopes
        let text = r"\subtree{
\title{My Section}
\p{First paragraph.}
\p{Second paragraph.}
}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted.iter().any(|t| t.contains("My Section")),
            "Title inside subtree should be extracted, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("First paragraph")),
            "Paragraph inside subtree should be extracted, got: {extracted:?}"
        );
        // Title and paragraphs should not merge into one range
        assert!(
            !ranges.iter().any(|r| {
                let t = &text[r.start_byte..r.end_byte];
                t.contains("My Section") && t.contains("First paragraph")
            }),
            "Title and paragraph should be separate scopes"
        );

        Ok(())
    }

    #[test]
    fn test_forester_display_math_align_inside_li() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Real-world pattern: display math with \begin{align*} inside \li.
        // The braces inside the math used to confuse the old brace-counting
        // exclusion collector, leaking LaTeX commands into prose.
        let text = r"\ol{\li{We have the equation
##{
  \begin{align*}
    \mathcal{C} &\vDash \forall x.\, \varphi(x) \\
    &\Rightarrow \psi
  \end{align*}
}
which completes the proof.}}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("\\mathcal"),
                "LaTeX \\mathcal should not leak into prose, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\vDash"),
                "LaTeX \\vDash should not leak into prose, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\forall"),
                "LaTeX \\forall should not leak into prose, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\begin"),
                "LaTeX \\begin should not leak into prose, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("We have the equation"),
            "Prose before display math should be extracted, got: {all_text:?}"
        );
        assert!(
            all_text.contains("completes the proof"),
            "Prose after display math should be extracted, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_forester_em_command_name_not_leaked() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{This has \em{emphasized} words.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        let range = ranges
            .iter()
            .find(|r| {
                let t = &text[r.start_byte..r.end_byte];
                t.contains("emphasized")
            })
            .expect("Should find range containing 'emphasized'");

        let clean = range.extract_text(text);
        assert!(
            !clean.contains("\\em"),
            "Command name \\em should not appear in clean text, got: {clean:?}"
        );
        assert!(
            clean.contains("emphasized"),
            "Word 'emphasized' should be in clean text, got: {clean:?}"
        );
        assert!(
            clean.contains("This has"),
            "Surrounding prose should be in clean text, got: {clean:?}"
        );
        // Braces from \em{...} should not leak into clean text
        assert!(
            !clean.contains('{'),
            "Opening brace should not leak into clean text, got: {clean:?}"
        );
        assert!(
            !clean.contains('}'),
            "Closing brace should not leak into clean text, got: {clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_unknown_inline_macro_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Unknown inline macros like \cf{...} should be excluded from prose
        // (their content is not checked) while surrounding prose bridges.
        let text = r"\li{The carrier \cf{Fin A.n} is important.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("Fin"),
                "\\cf content should be excluded, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("The carrier"),
            "Prose before \\cf, got: {all_text:?}"
        );
        assert!(
            all_text.contains("is important"),
            "Prose after \\cf, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_display_math_escaped_braces_top_level() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Display math with \{...\} at top level between \p blocks.
        // Tree-sitter truncates display_math at escaped braces; the text-based
        // scanner should catch the leaked content.
        let text = r"\p{Consider the structure:}
##{
  U = \{A, B\} \quad I = \{\texttt{taller} \mapsto \{\langle A, B\rangle\}\}
}
\p{Is it a model?}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("\\texttt"),
                "\\texttt should not leak into prose, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\mapsto"),
                "\\mapsto should not leak into prose, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\langle"),
                "\\langle should not leak into prose, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("Consider the structure"),
            "Prose before math should be extracted, got: {all_text:?}"
        );
        assert!(
            all_text.contains("Is it a model"),
            "Prose after math should be extracted, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_display_math_align_top_level() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Display math with \begin{align*} at top level
        let text = r"\p{Define the interpretation:}
##{
  \begin{align*}
  I &= \{a \mapsto \alpha\} \\
  I &= \{f(\alpha) \mapsto \beta\}
  \end{align*}
}
\p{Evaluate the terms.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("\\begin"),
                "\\begin should not leak, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\end"),
                "\\end should not leak, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\mapsto"),
                "\\mapsto should not leak, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("Define the interpretation"),
            "Got: {all_text:?}"
        );
        assert!(all_text.contains("Evaluate the terms"), "Got: {all_text:?}");

        Ok(())
    }

    #[test]
    fn test_display_math_escaped_braces_inside_li() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Display math with \{...\} inside \li — the hardest case because
        // tree-sitter's brace_group interacts with display_math.
        let text = r"\li{
    If we change the interpretation to
    ##{
      I = \{\texttt{taller} \mapsto \{\langle A, B\rangle\}\}
    }
    is the structure now a model?
  }";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("\\texttt"),
                "\\texttt should not leak, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\mapsto"),
                "\\mapsto should not leak, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("change the interpretation"),
            "Prose before math in \\li, got: {all_text:?}"
        );
        assert!(
            all_text.contains("is the structure now a model"),
            "Prose after math in \\li, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_unknown_macro_wrapping_blocks() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // \solution is not in any command list — should recurse into children
        let text = r"\solution{
  \p{As a reminder we are working with the axiom.}
  \ol{
    \li{First item.}
    \li{Second item.}
  }
}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let extracted: Vec<&str> = ranges
            .iter()
            .map(|r| &text[r.start_byte..r.end_byte])
            .collect();

        assert!(
            extracted
                .iter()
                .any(|t| t.contains("working with the axiom")),
            "\\p inside \\solution should be extracted, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("First item")),
            "\\li inside \\solution should be extracted, got: {extracted:?}"
        );
        assert!(
            extracted.iter().any(|t| t.contains("Second item")),
            "\\li inside \\solution should be extracted, got: {extracted:?}"
        );

        Ok(())
    }

    #[test]
    fn test_multiple_inline_math_in_li() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Multiple inline math expressions in a single \li — all should be excluded
        let text = r"\li{#{p(a)} evaluates to #{\top} because #{a = \alpha}.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("p(a)"),
                "Inline math p(a) should be excluded, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\top"),
                "Inline math \\top should be excluded, got: {clean:?}"
            );
            assert!(
                !clean.contains("\\alpha"),
                "Inline math \\alpha should be excluded, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("evaluates to"),
            "Prose between math should be extracted, got: {all_text:?}"
        );
        assert!(
            all_text.contains("because"),
            "Prose between math should be extracted, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_fully_excluded_ranges_filtered() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Display math with escaped braces at top level — tree-sitter truncates
        // and orphan text nodes leak. After dedup + retain, those all-excluded
        // ranges should be gone.
        let text = r"\p{Consider:}
##{
  U = \{A, B\}
}
\p{Done.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            let trimmed = clean.trim();
            assert!(
                !trimmed.is_empty(),
                "No all-blank ranges should remain, got range [{}, {}) with {} exclusions",
                range.start_byte,
                range.end_byte,
                range.exclusions.len()
            );
        }

        Ok(())
    }

    /// Helper: parse `source` with tree-sitter and assert no ERROR or MISSING nodes.
    fn assert_no_errors(source: &str) {
        let language: tree_sitter::Language = crate::forester_ts::LANGUAGE.into();
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&language).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let root = tree.root_node();
        assert!(
            !root.has_error(),
            "Parse tree has ERROR/MISSING nodes:\n{}\nSource:\n{source}",
            root.to_sexp()
        );
    }

    #[test]
    fn test_math_escape_inline_braces() {
        // Inline math with escaped braces: \{ and \}
        assert_no_errors(r"#{\ \mathcal F = \{W, R\} }");
    }

    #[test]
    fn test_math_escape_display_row_separator() {
        // Display math with \\ row separator and escaped braces
        assert_no_errors(
            r"##{
  \begin{align*}
    x &= \{a, b\} \\
    y &= \{c, d\}
  \end{align*}
}",
        );
    }

    #[test]
    fn test_bare_hash_in_text() {
        // Bare # in a URL should parse as text, not cause an error
        assert_no_errors(r"\p{See https://q.uiver.app/#q=WzAsMl0= for details.}");
    }

    #[test]
    fn test_math_escape_inline_extraction() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{The family #{\mathcal F = \{W, R\} } is coherent.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        // Math should be excluded
        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("\\mathcal"),
                "Math content should be excluded, got: {clean:?}"
            );
        }

        // Surrounding prose should be extracted
        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("The family"),
            "Prose before math, got: {all_text:?}"
        );
        assert!(
            all_text.contains("is coherent"),
            "Prose after math, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_inline_command_braces_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Real-world pattern: \strong{...} and \em{...} delimiters must not
        // leak into prose output as stray brackets.
        let text = r"\li{
\strong{Explanation}: We know this since the \em{necessarily} modality.
}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains('{'),
                "Opening brace should not leak, got: {clean:?}"
            );
            assert!(
                !clean.contains('}'),
                "Closing brace should not leak, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("Explanation"),
            "Content inside \\strong should be extracted, got: {all_text:?}"
        );
        assert!(
            all_text.contains("necessarily"),
            "Content inside \\em should be extracted, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_display_math_row_separator_extraction() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{We have
##{
  a &= b \\
  c &= \{d, e\}
}
so the result follows.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        for range in &ranges {
            let clean = range.extract_text(text);
            assert!(
                !clean.contains("&="),
                "Math content should not leak, got: {clean:?}"
            );
        }

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");
        assert!(
            all_text.contains("We have"),
            "Prose before math, got: {all_text:?}"
        );
        assert!(
            all_text.contains("result follows"),
            "Prose after math, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_markdown_link_parse() {
        // Markdown-style links [alias](target) should parse without errors
        assert_no_errors(r"\p{See [frame](006j) for details.}");
    }

    #[test]
    fn test_markdown_link_alias_extracted() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{See [frame](006j) for details.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            !all_text.contains("006j"),
            "Link target should be excluded, got: {all_text:?}"
        );
        assert!(
            !all_text.contains('['),
            "Link bracket syntax should be excluded, got: {all_text:?}"
        );
        assert!(
            all_text.contains("See"),
            "Prose before link, got: {all_text:?}"
        );
        assert!(
            all_text.contains("frame"),
            "Link alias should be extracted as prose, got: {all_text:?}"
        );
        assert!(
            all_text.contains("for details"),
            "Prose after link, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_markdown_link_alias_merged_into_prose() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{apply [De Morgan's laws](000g) to push}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text))
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            all_text.contains("De Morgan's laws"),
            "Link alias should be merged into prose, got: {all_text:?}"
        );
        assert!(
            !all_text.contains("000g"),
            "Link address should be excluded, got: {all_text:?}"
        );
        // Verify surrounding prose is intact
        assert!(
            all_text.contains("apply"),
            "Prose before link, got: {all_text:?}"
        );
        assert!(
            all_text.contains("to push"),
            "Prose after link, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_codeblock_with_errors_no_leakage() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Simulate the pattern from 001b.tree: backticks in \codeblock cause
        // ERROR nodes, but the whole \codeblock is structural and skipped.
        let text = r"\p{Here is a recursor example.}
\codeblock{lean}{
  macro_rules
    | `(ex{ $n:num }) => `(Expr.const $n)
    | `(ex{ $x:ident }) => pure $x
}
\p{This defines the syntax.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_clean: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            !all_clean.contains("macro_rules"),
            "Code should not leak from \\codeblock, got: {all_clean:?}"
        );
        assert!(
            !all_clean.contains("Expr.const"),
            "Code should not leak from \\codeblock, got: {all_clean:?}"
        );
        assert!(
            all_clean.contains("recursor example"),
            "Prose should be extracted, got: {all_clean:?}"
        );
        assert!(
            all_clean.contains("defines the syntax"),
            "Prose should be extracted, got: {all_clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_inline_code_content_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Inline \code{...} holds code/identifiers, not prose: its content must
        // be skipped while the surrounding sentence is still extracted.
        let text = r"\p{Call \code{teh_function} to begin the proccess.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_clean: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            !all_clean.contains("teh_function"),
            "Inline \\code content should not be checked, got: {all_clean:?}"
        );
        assert!(
            all_clean.contains("Call"),
            "Prose before \\code should be extracted, got: {all_clean:?}"
        );
        assert!(
            all_clean.contains("proccess"),
            "Prose after \\code should be extracted, got: {all_clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_prose_block_continuation_merges() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // The second \p is a logical continuation: the first didn't end in
        // .!? and the second starts lowercase. They must be checked as one
        // block so "continuation" isn't flagged as an uncapitalized sentence.
        let text = r"\p{Here is something} ##{x = 5} \p{continuation of the idea.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        assert_eq!(
            ranges.len(),
            1,
            "continuation should merge, got: {ranges:?}"
        );
        let merged = ranges[0].extract_text(text);
        assert!(merged.contains("Here is something"), "got: {merged:?}");
        assert!(
            merged.contains("continuation of the idea"),
            "got: {merged:?}"
        );
        assert!(
            !merged.contains("x = 5"),
            "math stays excluded, got: {merged:?}"
        );
        Ok(())
    }

    #[test]
    fn test_prose_blocks_not_merged_when_separate_sentences() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Both blocks are complete, capitalized sentences — keep them separate.
        let text = r"\p{First complete sentence.} \p{Second complete sentence.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        assert_eq!(
            ranges.len(),
            2,
            "distinct sentences stay separate, got: {ranges:?}"
        );
        Ok(())
    }

    #[test]
    fn test_block_directive_forces_merge() -> Result<()> {
        let mut extractor = forester_extractor()?;

        // Two complete sentences would normally stay separate; a
        // `lang-check-begin block` region forces them into one checked unit.
        let text = "% lang-check-begin block\n\\p{First complete sentence.}\n\\p{Second complete sentence.}\n% lang-check-end";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        assert_eq!(
            ranges.len(),
            1,
            "block directive forces one merged block, got: {ranges:?}"
        );
        Ok(())
    }

    #[test]
    fn test_subtree_bracket_id_excluded() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\subtree[006s]{
  \taxon{Definition}
  \title{Local truth}
  \p{Some prose about local truth.}
}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_clean: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            !all_clean.contains("006s"),
            "Subtree ID should not appear in prose, got: {all_clean:?}"
        );
        assert!(
            all_clean.contains("Local truth"),
            "Title should be extracted, got: {all_clean:?}"
        );
        assert!(
            all_clean.contains("local truth"),
            "Paragraph should be extracted, got: {all_clean:?}"
        );

        Ok(())
    }

    #[test]
    fn test_paren_group_in_title_preserved() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\title{Negation Normal Form (NNF)}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            all_text.contains("(NNF)"),
            "Parenthesised abbreviation should be preserved, got: {all_text:?}"
        );
        assert!(
            all_text.contains("Negation Normal Form"),
            "Title text should be extracted, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_paren_group_in_paragraph_preserved() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{A formula is in Negation Normal Form (NNF) if negation is only applied to literals.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            all_text.contains("(NNF)"),
            "Parenthesised abbreviation should be preserved, got: {all_text:?}"
        );
        assert!(
            all_text.contains("if negation"),
            "Text after parens should be extracted, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_markdown_link_inline_command_stripped() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{See [\em{some text}](link-to-ignore) for details.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            all_text.contains("some text"),
            "Inner text of inline command in link alias should be prose, got: {all_text:?}"
        );
        assert!(
            !all_text.contains("\\em"),
            "Command name should be stripped, got: {all_text:?}"
        );
        assert!(
            !all_text.contains("link-to-ignore"),
            "Link target should be excluded, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_markdown_link_nested_commands_stripped() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{See [\strong{\em{nested}}](addr) here.}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;
        let all_text: String = ranges
            .iter()
            .map(|r| r.extract_text(text).into_owned())
            .collect::<Vec<_>>()
            .join(" ");

        assert!(
            all_text.contains("nested"),
            "Deeply nested text should be extracted, got: {all_text:?}"
        );
        assert!(
            !all_text.contains("\\strong"),
            "Outer command should be stripped, got: {all_text:?}"
        );
        assert!(
            !all_text.contains("\\em"),
            "Inner command should be stripped, got: {all_text:?}"
        );

        Ok(())
    }

    #[test]
    fn test_markdown_link_with_command_single_range() -> Result<()> {
        let mut extractor = forester_extractor()?;

        let text = r"\p{formulas are in [\em{Negation Normal Form}](000e) which in short}";
        let ranges = extractor.extract(text, "forester", &LatexExtras::default())?;

        assert_eq!(
            ranges.len(),
            1,
            "Link with inline command should not split the paragraph, got {} ranges",
            ranges.len()
        );

        let all_text = ranges[0].extract_text(text);
        assert!(
            all_text.contains("Negation Normal Form"),
            "Link alias text should be present, got: {all_text:?}"
        );
        assert!(
            all_text.contains("formulas are in"),
            "Text before link, got: {all_text:?}"
        );
        assert!(
            all_text.contains("which in short"),
            "Text after link, got: {all_text:?}"
        );

        Ok(())
    }
}