writ 0.17.0

A hybrid markdown editor combining raw text editing with live inline rendering
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
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
//! Headless editor model — the gpui-free core (see MIGRATION-PLAN.md, Phase 2).
//!
//! Wraps [`EditorState`] (the pure edit engine: buffer, cursor, movement,
//! insert/delete, enter/tab, checkbox toggle) with the orchestration that used to
//! live on the gpui `Editor` entity but has no rendering dependency: inline git
//! diff against HEAD, GitHub-ref/naked-URL detection, and file load/save. No
//! `gpui::Context`/`Window` in the call graph — the winit shell drives this
//! directly, and unit tests exercise it with no UI at all.
//!
//! Async GitHub *validation* and autocomplete *fetching* (which used `cx.spawn`)
//! are intentionally not ported here yet; they return to the shell on real tokio
//! in a later phase. Detection (the synchronous scan) lives here.

use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::Duration;

use notify::{EventKind, RecommendedWatcher, RecursiveMode};
use notify_debouncer_full::{DebounceEventResult, Debouncer, RecommendedCache, new_debouncer};
use regex::Regex;
use std::time::SystemTime;

use ropey::Rope;

use crate::buffer::{Buffer, RenderSnapshot};
use crate::cursor::Selection;
use crate::diff::DiffState;
use crate::editor::{Direction, EditorState};
use crate::fold;
use crate::git::{git_watch_dirs, head_blob_text, is_git_base_path};
use crate::github::GitHubClient;
#[cfg(feature = "math")]
use crate::inline::detect_inline_math;
use crate::inline::{
    GitHubContext, GitHubRef, MathSpan, NakedUrl, RawGitHubMatch, detect_github_references_in_line,
    detect_naked_urls,
};
use crate::marker::MarkerKind;
use crate::paste::{PasteContext, transform_paste};
use crate::text_input::TextField;
use crate::validation::{GitHubValidationCache, IssueStatus};

/// What a file-watch notification refers to. The working file changing is reloaded
/// (with a self-write guard); a git dir changing only re-reads the HEAD diff base, so
/// a commit/checkout that leaves the working file untouched still updates the diff.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum WatchKind {
    /// The opened file changed on disk.
    File,
    /// A git dir moved HEAD (commit/amend/reset/checkout/rebase) — refresh the base.
    GitBase,
}

/// The kind of autocomplete triggered at the cursor.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AutocompleteTrigger {
    /// Issue/PR autocomplete triggered by `#`.
    Issue,
    /// User autocomplete triggered by `@`.
    User,
}

/// A single suggestion row. Built on the main thread from fetched GitHub data.
#[derive(Clone)]
pub enum AutocompleteSuggestion {
    IssueOrPr {
        number: u64,
        /// Unicode symbol (● issue, ⎇ PR).
        symbol: String,
        status: IssueStatus,
        title: String,
    },
    User {
        login: String,
        name: Option<String>,
    },
}

/// State for the autocomplete popup while the cursor is inside a `#`/`@` token.
#[derive(Clone)]
pub struct AutocompleteState {
    pub trigger: AutocompleteTrigger,
    /// Byte offset of the trigger char (`#`/`@`) — replacement starts here.
    pub trigger_offset: usize,
    /// The text typed after the trigger (e.g. "123" for `#123`).
    pub prefix: String,
    pub suggestions: Vec<AutocompleteSuggestion>,
    pub selected_index: usize,
    pub loading: bool,
    /// The prefix we last kicked off a fetch for (dedup).
    pub fetched_prefix: Option<String>,
}

/// Whether the find bar shows just the search field (Find) or search + replace (Replace).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum FindMode {
    Find,
    Replace,
}

/// Which text field of the find bar currently receives keystrokes.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum FieldFocus {
    Search,
    Replace,
}

/// State for the find (and replace) bar. `matches` are buffer byte ranges, sorted
/// left-to-right and non-overlapping (regex `find_iter` semantics); `active` indexes
/// into it. `scanned` records the `(version, query, case, regex)` the current `matches`
/// reflect so a cursor-only redraw can skip the rescan.
pub struct FindState {
    pub search: TextField,
    pub replace: TextField,
    pub mode: FindMode,
    pub focus: FieldFocus,
    /// Whether the find bar owns keyboard focus. While `true`, keys type into the
    /// focused field; while `false` (bar open but the document was clicked) keys fall
    /// through to the document so you can edit the buffer with the bar still visible.
    pub focused: bool,
    pub case_sensitive: bool,
    pub regex: bool,
    pub matches: Vec<Range<usize>>,
    pub active: Option<usize>,
    scanned: Option<(u64, String, bool, bool)>,
}

pub struct Editor {
    pub state: EditorState,
    file_path: Option<PathBuf>,
    input_blocked: bool,
    /// When set (via `--autosave`, used by the GhostText daemon), every buffer edit
    /// writes back to `file_path` so an external watcher sees the change immediately.
    autosave: bool,

    // --- GitHub autolink detection ---
    github_context: Option<GitHubContext>,
    github_client: Option<GitHubClient>,
    /// Shared (`Arc<Mutex>`) validation cache; the shell's tokio tasks write results
    /// into it off-thread and the render path reads it to color validated refs.
    github_validation_cache: GitHubValidationCache,
    naked_urls_by_line: HashMap<usize, Vec<NakedUrl>>,
    github_refs_by_line: HashMap<usize, Vec<RawGitHubMatch>>,
    /// Inline `$…$` math spans per line, from the viewport-windowed detection pass. Always
    /// present but only populated when the `math` feature is on (empty otherwise).
    math_spans_by_line: HashMap<usize, Vec<MathSpan>>,
    /// (buffer version, scanned line range) the cached detection reflects; lets
    /// `refresh_detection` skip the rescan when neither the text nor the scanned window
    /// changed (cursor-only rebuilds). Reset when the GitHub context changes.
    detection_key: Option<(u64, Range<usize>)>,
    /// Active `#`/`@` autocomplete popup, if the cursor is inside a trigger token.
    autocomplete: Option<AutocompleteState>,
    /// Active find bar, if find (Ctrl+F) is open. While `Some`, the shell routes all
    /// keystrokes to it instead of the document.
    find: Option<FindState>,
    /// Whether the right-docked outline panel is open. Reserves a horizontal strip so
    /// the document region insets (see `outline_width`).
    outline_open: bool,
    /// Byte offsets of folded headings (session UI state, never written to the file).
    /// Anchored by offset so a single-splice edit remaps them exactly; the hidden-line
    /// derivation re-reads live headings each frame, so a fold's reach tracks edits.
    folded_headings: HashSet<usize>,

    // --- inline git diff against HEAD ---
    /// (raw HEAD text, rendered snapshot of it) reused as the diff base.
    head_base: Option<(String, RenderSnapshot)>,
    diff_state: Option<DiffState>,

    // --- file watching ---
    /// Debounced watcher: coalesces the burst of fs events a single save emits (and
    /// handles atomic-save/rename) into one notification instead of several.
    file_watcher: Option<Debouncer<RecommendedWatcher, RecommendedCache>>,
    file_watcher_rx: Option<mpsc::Receiver<WatchKind>>,
    /// mtime after our own last save, so the watcher can skip self-writes.
    last_save_mtime: Option<SystemTime>,
}

/// Common prefix/suffix diff of two ropes → the single splice (`prefix..old_end`, a byte
/// range in `before`, plus the length `delta`) that turns `before` into `after`. Scans
/// bytes inward from both ends without allocating; ropey's `bytes()` iterates in place.
fn splice_bounds(before: &Rope, after: &Rope) -> (usize, usize, isize) {
    let (blen, alen) = (before.len_bytes(), after.len_bytes());
    let mut prefix = 0;
    for (x, y) in before.bytes().zip(after.bytes()) {
        if x != y {
            break;
        }
        prefix += 1;
    }
    let max_suffix = (blen - prefix).min(alen - prefix);
    let suffix = common_suffix_len(before, after, max_suffix);
    let old_end = blen - suffix; // end (in `before`) of the replaced region
    let delta = alen as isize - blen as isize;
    (prefix, old_end, delta)
}

/// Length (bytes, ≤ `max`) of the common suffix of two ropes. ropey 1.6's `Bytes`/`Chunks`
/// aren't double-ended, so walk each rope's chunks backward via `chunk_at_byte` (striding
/// one chunk per step) and compare their bytes tail-first — O(n) and allocation-free.
fn common_suffix_len(a: &Rope, b: &Rope, max: usize) -> usize {
    let (mut apos, mut bpos) = (a.len_bytes(), b.len_bytes());
    let (mut abuf, mut bbuf): (&[u8], &[u8]) = (&[], &[]);
    let mut n = 0;
    while n < max {
        if abuf.is_empty() {
            if apos == 0 {
                break;
            }
            let (chunk, start, _, _) = a.chunk_at_byte(apos - 1);
            abuf = &chunk.as_bytes()[..apos - start];
            apos = start;
        }
        if bbuf.is_empty() {
            if bpos == 0 {
                break;
            }
            let (chunk, start, _, _) = b.chunk_at_byte(bpos - 1);
            bbuf = &chunk.as_bytes()[..bpos - start];
            bpos = start;
        }
        let k = abuf.len().min(bbuf.len()).min(max - n);
        let mut matched = 0;
        while matched < k && abuf[abuf.len() - 1 - matched] == bbuf[bbuf.len() - 1 - matched] {
            matched += 1;
        }
        n += matched;
        if matched < k {
            break; // mismatch inside the overlap → suffix ends here
        }
        abuf = &abuf[..abuf.len() - matched];
        bbuf = &bbuf[..bbuf.len() - matched];
    }
    n
}

impl Editor {
    pub fn new(content: &str) -> Self {
        Self {
            state: EditorState::new(content),
            file_path: None,
            input_blocked: false,
            autosave: false,
            github_context: None,
            github_client: None,
            github_validation_cache: GitHubValidationCache::new(),
            naked_urls_by_line: HashMap::new(),
            github_refs_by_line: HashMap::new(),
            math_spans_by_line: HashMap::new(),
            detection_key: None,
            autocomplete: None,
            find: None,
            outline_open: false,
            folded_headings: HashSet::new(),
            head_base: None,
            diff_state: None,
            file_watcher: None,
            file_watcher_rx: None,
            last_save_mtime: None,
        }
    }

    /// Open a file from disk into a fresh editor. Loads content and refreshes the
    /// git diff base. Returns an empty buffer if the file can't be read.
    pub fn open(path: &Path) -> Self {
        let content = std::fs::read_to_string(path).unwrap_or_default();
        let mut editor = Self::new(&content);
        editor.file_path = Some(path.to_path_buf());
        editor.refresh_git_base();
        editor.seed_default_collapsed_callouts();
        editor
    }

    pub fn file_path(&self) -> Option<&Path> {
        self.file_path.as_deref()
    }

    pub fn set_file_path(&mut self, path: PathBuf) {
        self.file_path = Some(path);
    }

    // --- buffer queries -----------------------------------------------------

    pub fn text(&self) -> String {
        self.state.text()
    }

    pub fn len(&self) -> usize {
        self.state.buffer.len_bytes()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn cursor_position(&self) -> usize {
        self.state.cursor().offset
    }

    pub fn selection_range(&self) -> Option<std::ops::Range<usize>> {
        let selection = &self.state.selection;
        (!selection.is_collapsed()).then(|| selection.range())
    }

    /// The currently selected text, if any (for clipboard copy/cut).
    pub fn selected_text(&self) -> Option<String> {
        self.selection_range()
            .map(|r| self.state.buffer.slice_cow(r).into_owned())
    }

    pub fn set_cursor(&mut self, offset: usize) {
        self.state.set_cursor(offset);
    }

    /// Line index containing buffer `offset`.
    pub fn line_of(&self, offset: usize) -> usize {
        self.state.buffer.byte_to_line(offset)
    }

    pub fn is_dirty(&self) -> bool {
        self.state.buffer.is_dirty()
    }

    pub fn mark_clean(&mut self) {
        self.state.buffer.mark_clean();
    }

    pub fn set_input_blocked(&mut self, blocked: bool) {
        self.input_blocked = blocked;
    }

    pub fn set_autosave(&mut self, autosave: bool) {
        self.autosave = autosave;
    }

    /// Write the buffer back to disk after an edit when autosave is on (GhostText).
    /// Guarded by `is_dirty` so a no-op "edit" doesn't churn the file.
    fn maybe_autosave(&mut self) {
        if self.autosave
            && self.state.buffer.is_dirty()
            && let Err(e) = self.save()
        {
            eprintln!("[writ] autosave failed: {e}");
        }
    }

    pub fn input_blocked(&self) -> bool {
        self.input_blocked
    }

    // --- edit operations (delegate to the pure engine, then sync diff) ------

    /// Single choke point for buffer-mutating engine ops: run `f`, then resync the inline
    /// diff so a new mutator can't silently skip the refresh. Cursor-only ops that don't
    /// touch the buffer (click/drag/move/select_all) deliberately bypass this.
    fn edit<R>(&mut self, f: impl FnOnce(&mut EditorState) -> R) -> R {
        // Snapshot the rope (only when something is folded) so fold offsets can be remapped
        // across the edit from the actual splice — computed from the common prefix/suffix,
        // NOT the caret, since some edits (e.g. checkbox toggle) mutate away from the caret.
        // The snapshot is a `Rope::clone` (O(1) — ropey shares nodes via Arc), so this no
        // longer materializes the whole document into a `String` twice per keystroke.
        let before = (!self.folded_headings.is_empty()).then(|| self.state.buffer.rope().clone());
        let result = f(&mut self.state);
        if let Some(before) = before {
            let after = self.state.buffer.rope();
            if before != *after {
                let (prefix, old_end, delta) = splice_bounds(&before, after);
                self.remap_folds(prefix, old_end, delta);
            }
        }
        self.recompute_diff();
        self.maybe_autosave();
        result
    }

    /// Remap folded heading/list offsets across a single-splice edit `prefix..old_end`
    /// (byte range in the old text) with length `delta`. Offsets before the splice are
    /// fixed, those after shift by the delta, and any inside the replaced region drop.
    fn remap_folds(&mut self, prefix: usize, old_end: usize, delta: isize) {
        let old = std::mem::take(&mut self.folded_headings);
        self.folded_headings = old
            .into_iter()
            .filter_map(|o| {
                if o <= prefix {
                    Some(o)
                } else if o >= old_end {
                    Some((o as isize + delta) as usize)
                } else {
                    None // the anchor line was edited → fold no longer meaningful
                }
            })
            .collect();
    }

    pub fn insert_str(&mut self, text: &str) {
        self.edit(|s| s.insert_text(text));
    }

    /// Insert clipboard text with context-aware paste normalization (CRLF→LF,
    /// curly→straight quotes, blockquote-prefix continuation, code-block literal).
    pub fn paste(&mut self, text: &str) {
        let ctx = PasteContext::from_buffer(&self.state.buffer, self.cursor_position());
        let transformed = transform_paste(text, &ctx);
        self.edit(|s| s.insert_text(&transformed));
    }

    pub fn type_char(&mut self, c: char) {
        let mut buf = [0u8; 4];
        self.insert_str(c.encode_utf8(&mut buf));
    }

    pub fn backspace(&mut self) {
        self.edit(|s| s.delete_backward());
    }

    pub fn delete_forward(&mut self) {
        self.edit(|s| s.delete_forward());
    }

    pub fn enter(&mut self) {
        self.edit(|s| s.enter());
    }

    pub fn shift_enter(&mut self) {
        self.edit(|s| s.shift_enter());
    }

    pub fn shift_alt_enter(&mut self) {
        self.edit(|s| s.shift_alt_enter());
    }

    pub fn tab(&mut self) {
        self.edit(|s| s.tab());
    }

    pub fn shift_tab(&mut self) {
        self.edit(|s| s.shift_tab());
    }

    pub fn move_in_direction(&mut self, direction: Direction, extend: bool) {
        let new_cursor = self.state.cursor_in_direction(direction);
        if extend {
            self.state.selection = self.state.selection.extend_to(new_cursor.offset);
        } else {
            self.state.selection = Selection::new(new_cursor.offset, new_cursor.offset);
        }
    }

    pub fn select_all(&mut self) {
        self.state.selection = Selection::select_all(&self.state.buffer);
    }

    pub fn cursor_in_code_block(&self) -> bool {
        self.state.cursor_in_code_block()
    }

    /// Smart space (suppressed at line/blockquote-content start; literal in code blocks).
    /// Returns false if the space was suppressed so the caller can decide the fallback.
    pub fn try_insert_space(&mut self) -> bool {
        self.edit(|s| s.try_insert_space())
    }

    /// After typing `>`, auto-insert the trailing space of a blockquote marker.
    pub fn maybe_complete_blockquote_marker(&mut self) {
        self.edit(|s| s.maybe_complete_blockquote_marker());
    }

    /// After typing the third ``` `/`~`, auto-insert the closing fence.
    pub fn maybe_complete_code_fence(&mut self) {
        self.edit(|s| s.maybe_complete_code_fence());
    }

    pub fn click(&mut self, buffer_offset: usize, shift_held: bool, click_count: usize) {
        self.state
            .handle_click(buffer_offset, shift_held, click_count);
    }

    pub fn drag(&mut self, buffer_offset: usize) {
        self.state.handle_drag(buffer_offset);
    }

    pub fn toggle_checkbox(&mut self, line_number: usize) {
        self.edit(|s| s.toggle_checkbox(line_number));
    }

    /// If buffer `offset` lands on a checkbox marker (`[ ]`/`[x]`), the line to toggle.
    /// Lets a click on the box flip it instead of just placing the caret.
    pub fn checkbox_at(&self, offset: usize) -> Option<usize> {
        let line = self.state.buffer.byte_to_line(offset);
        let markers = self.state.buffer.line_markers(line);
        markers
            .markers
            .iter()
            .any(|m| matches!(m.kind, MarkerKind::Checkbox { .. }) && m.range.contains(&offset))
            .then_some(line)
    }

    pub fn undo(&mut self) {
        if let Some(cursor_pos) = self.state.buffer.undo() {
            self.state.selection = Selection::new(cursor_pos, cursor_pos);
            self.recompute_diff();
            self.maybe_autosave();
        }
    }

    pub fn redo(&mut self) {
        if let Some(cursor_pos) = self.state.buffer.redo() {
            self.state.selection = Selection::new(cursor_pos, cursor_pos);
            self.recompute_diff();
            self.maybe_autosave();
        }
    }

    pub fn can_undo(&self) -> bool {
        self.state.buffer.can_undo()
    }

    pub fn can_redo(&self) -> bool {
        self.state.buffer.can_redo()
    }

    pub fn set_text(&mut self, content: &str) {
        self.state.buffer = content.parse().unwrap_or_default();
        self.state.selection = Selection::new(0, 0);
        self.recompute_diff();
    }

    // --- GitHub autolink detection ------------------------------------------

    pub fn set_github_context(&mut self, context: GitHubContext) {
        self.github_context = Some(context);
        self.detection_key = None; // force the next refresh_detection to rescan
    }

    pub fn set_github_client(&mut self, client: GitHubClient) {
        self.github_client = Some(client);
    }

    pub fn github_client(&self) -> Option<&GitHubClient> {
        self.github_client.as_ref()
    }

    pub fn github_context(&self) -> Option<&GitHubContext> {
        self.github_context.as_ref()
    }

    /// The URL to open for a Ctrl/Cmd-click at buffer `offset`: a GitHub ref's web page,
    /// a naked URL, or a markdown link target — whichever covers the offset.
    pub fn link_at(&mut self, offset: usize) -> Option<String> {
        let line = self.line_of(offset);
        let raw = if let Some(m) = self
            .github_refs_by_line
            .get(&line)
            .and_then(|refs| refs.iter().find(|m| m.byte_range.contains(&offset)))
        {
            m.reference.url()
        } else if let Some(u) = self
            .naked_urls_by_line
            .get(&line)
            .and_then(|urls| urls.iter().find(|u| u.byte_range.contains(&offset)))
        {
            u.url.clone()
        } else {
            // Markdown link [text](url) / image: the region spans the whole link source.
            self.state
                .buffer
                .render_snapshot()
                .inline_styles_for_line(line)
                .into_iter()
                .find(|r| r.link_url.is_some() && r.full_range.contains(&offset))
                .and_then(|r| r.link_url)?
        };
        Some(self.resolve_link_target(raw))
    }

    /// A web URL / absolute path opens as-is; a relative target (a markdown link or image
    /// path) resolves against the document's directory — matching how images are loaded,
    /// so Ctrl-clicking a relative link/image opens the right file, not one relative to cwd.
    fn resolve_link_target(&self, url: String) -> String {
        if url.contains("://") || url.starts_with("mailto:") || Path::new(&url).is_absolute() {
            return url;
        }
        match self.file_path.as_ref().and_then(|p| p.parent()) {
            Some(dir) => dir.join(&url).to_string_lossy().into_owned(),
            None => url,
        }
    }

    pub fn github_validation_cache(&self) -> &GitHubValidationCache {
        &self.github_validation_cache
    }

    /// Clear the GitHub ref-validation and autocomplete caches so every detected ref
    /// re-validates from scratch — the Ctrl+R escape hatch for stale/invalid results.
    pub fn revalidate_github_refs(&self) {
        self.github_validation_cache.clear();
        if let Some(client) = self.github_client.as_ref() {
            client.clear_autocomplete_cache();
            client.clear_user_cache();
        }
    }

    pub fn github_refs_by_line(&self) -> &HashMap<usize, Vec<RawGitHubMatch>> {
        &self.github_refs_by_line
    }

    pub fn naked_urls_by_line(&self) -> &HashMap<usize, Vec<NakedUrl>> {
        &self.naked_urls_by_line
    }

    /// All GitHub references detected across the document: inline refs plus the
    /// github-ref-bearing naked URLs.
    pub fn detected_refs(&self) -> Vec<GitHubRef> {
        let mut refs: Vec<GitHubRef> = Vec::new();
        for m in self.github_refs_by_line().values().flatten() {
            refs.push(m.reference.clone());
        }
        for u in self.naked_urls_by_line().values().flatten() {
            if let Some(r) = &u.github_ref {
                refs.push(r.clone());
            }
        }
        refs
    }

    /// GitHub references detected on lines within `lines` only — the viewport-bounded
    /// counterpart of `detected_refs`, used to validate just the visible refs.
    pub fn detected_refs_in_lines(&self, lines: Range<usize>) -> Vec<GitHubRef> {
        let mut refs: Vec<GitHubRef> = Vec::new();
        for (line, matches) in self.github_refs_by_line() {
            if lines.contains(line) {
                refs.extend(matches.iter().map(|m| m.reference.clone()));
            }
        }
        for (line, urls) in self.naked_urls_by_line() {
            if lines.contains(line) {
                refs.extend(urls.iter().filter_map(|u| u.github_ref.clone()));
            }
        }
        refs
    }

    /// Detect GitHub refs and naked URLs across a line range, returning both keyed
    /// by line index. Pure scan over the render snapshot (no network).
    pub fn detect_links(
        &mut self,
        start_line: usize,
        end_line: usize,
    ) -> (
        HashMap<usize, Vec<RawGitHubMatch>>,
        HashMap<usize, Vec<NakedUrl>>,
    ) {
        let snapshot = self.state.buffer.render_snapshot();
        let mut github_matches_by_line = HashMap::new();
        let mut urls_by_line = HashMap::new();
        // Bucket inline styles once (O(n + styles)); per-line lookup was O(n²) here too.
        let styles_by_line = snapshot.inline_styles_by_line();

        #[allow(clippy::needless_range_loop)]
        for line_idx in start_line..end_line.min(snapshot.line_count()) {
            let line = snapshot.line_markers(line_idx);
            let line_range = line.range.clone();
            let line_text = snapshot
                .rope
                .slice(
                    snapshot.rope.byte_to_char(line_range.start)
                        ..snapshot.rope.byte_to_char(line_range.end),
                )
                .to_string();

            let inline_styles = &styles_by_line[line_idx];
            let code_ranges: Vec<_> = inline_styles
                .iter()
                .filter(|s| s.style.code)
                .map(|s| s.full_range.clone())
                .collect();
            // Markdown-link spans are skipped by BOTH detectors: a `#123`/`@user`/URL
            // inside `[text](url)` is part of the link, not a standalone ref/autolink.
            let link_ranges: Vec<_> = inline_styles
                .iter()
                .filter(|s| s.link_url.is_some())
                .map(|s| s.full_range.clone())
                .collect();

            if let Some(github_context) = &self.github_context {
                let matches = detect_github_references_in_line(
                    &line_text,
                    line_range.start,
                    Some(github_context),
                    &code_ranges,
                    &link_ranges,
                );
                if !matches.is_empty() {
                    github_matches_by_line.insert(line_idx, matches);
                }
            }

            let urls = detect_naked_urls(&line_text, line_range.start, &code_ranges, &link_ranges);
            if !urls.is_empty() {
                urls_by_line.insert(line_idx, urls);
            }
        }

        (github_matches_by_line, urls_by_line)
    }

    /// Detect inline `$…$` math over `[start, end)` (the same viewport window as
    /// `detect_links`). `$$…$$` display blocks are collected once per build in the layout;
    /// here they're only used to exclude a `$` that belongs to display math.
    #[cfg(feature = "math")]
    fn detect_math(&mut self, start: usize, end: usize) -> HashMap<usize, Vec<MathSpan>> {
        let snapshot = self.state.buffer.render_snapshot();
        let block_ranges: Vec<Range<usize>> = snapshot
            .math_blocks()
            .into_iter()
            .map(|m| m.block)
            .collect();
        let mut by_line = HashMap::new();
        for line_idx in start..end {
            let range = snapshot.line_byte_range(line_idx);
            let text = snapshot
                .rope
                .slice(
                    snapshot.rope.byte_to_char(range.start)..snapshot.rope.byte_to_char(range.end),
                )
                .to_string();
            if !text.contains('$') {
                continue;
            }
            let code_ranges: Vec<Range<usize>> = snapshot
                .inline_styles_for_line(line_idx)
                .iter()
                .filter(|s| s.style.code)
                .map(|s| s.full_range.clone())
                .collect();
            let spans = detect_inline_math(&text, range.start, &code_ranges, &block_ranges);
            if !spans.is_empty() {
                by_line.insert(line_idx, spans);
            }
        }
        by_line
    }

    /// Re-run GitHub-ref / naked-URL detection over `lines` (clamped to the buffer) and
    /// cache the results. The caller passes the viewport (plus overscan + cursor line);
    /// every consumer — visible-line coloring, cursor-line autocomplete, and
    /// `detected_refs_in_lines` validation — is viewport/cursor-scoped, so a whole-buffer
    /// scan is unnecessary and would be O(total lines) per keystroke on large docs.
    /// Skips the rescan when neither the buffer version nor the window changed.
    pub fn refresh_detection(&mut self, lines: Range<usize>) {
        let version = self.state.buffer.version();
        let n = self.state.buffer.line_count();
        let lines = lines.start.min(n)..lines.end.min(n);
        if self.detection_key.as_ref() == Some(&(version, lines.clone())) {
            return;
        }
        let (refs, urls) = self.detect_links(lines.start, lines.end);
        self.github_refs_by_line = refs;
        self.naked_urls_by_line = urls;
        #[cfg(feature = "math")]
        {
            self.math_spans_by_line = self.detect_math(lines.start, lines.end);
        }
        self.detection_key = Some((version, lines));
    }

    /// Inline `$…$` math spans per line (empty for lines without any). Populated by the
    /// viewport-windowed `refresh_detection` scan (only under the `math` feature).
    pub fn math_spans_by_line(&self) -> &HashMap<usize, Vec<MathSpan>> {
        &self.math_spans_by_line
    }

    // --- find bar -----------------------------------------------------------

    /// Open the find bar in Find or Replace mode, focus the search field, and rescan.
    /// An already-open bar keeps its typed query/replacement (only the mode/focus change),
    /// so toggling Find↔Replace doesn't lose what you typed.
    pub fn open_find(&mut self, replace: bool) {
        let mode = if replace {
            FindMode::Replace
        } else {
            FindMode::Find
        };
        match self.find.as_mut() {
            Some(find) => {
                find.mode = mode;
                find.focus = FieldFocus::Search;
                find.focused = true;
            }
            None => {
                self.find = Some(FindState {
                    search: TextField::new(),
                    replace: TextField::new(),
                    mode,
                    focus: FieldFocus::Search,
                    focused: true,
                    case_sensitive: false,
                    regex: false,
                    matches: Vec::new(),
                    active: None,
                    scanned: None,
                });
            }
        }
        self.find_rescan();
    }

    pub fn close_find(&mut self) {
        self.find = None;
    }

    pub fn find_state(&self) -> Option<&FindState> {
        self.find.as_ref()
    }

    pub fn find_state_mut(&mut self) -> Option<&mut FindState> {
        self.find.as_mut()
    }

    /// Toggle the right-docked outline panel, returning the new open state.
    pub fn toggle_outline(&mut self) -> bool {
        self.outline_open = !self.outline_open;
        self.outline_open
    }

    pub fn outline_open(&self) -> bool {
        self.outline_open
    }

    /// Force the outline open/closed. Used by the headless snapshot path to render a
    /// golden frame with the panel reserved.
    pub fn set_outline_open(&mut self, open: bool) {
        self.outline_open = open;
    }

    // --- heading folding (session UI state; see `fold`) ----------------------

    pub fn line_count(&self) -> usize {
        self.state.buffer.line_count()
    }

    /// Every foldable block — heading, list item, or callout — as a unified
    /// [`fold::FoldAnchor`], so all fold operations share one code path.
    pub fn fold_anchors(&self) -> Vec<fold::FoldAnchor> {
        fold::fold_anchors(
            self.state.buffer.headings(),
            self.state.buffer.list_items(),
            self.state.buffer.callouts(),
            self.state.buffer.line_count(),
        )
    }

    /// Merged, sorted line ranges the layout should collapse to zero height.
    pub fn hidden_line_ranges(&self) -> Vec<Range<usize>> {
        // Cheap early-out for the common (nothing folded) case, before building anchors.
        if self.folded_headings.is_empty() {
            return Vec::new();
        }
        fold::hidden_line_ranges(&self.fold_anchors(), &self.folded_headings)
    }

    pub fn is_heading_folded(&self, byte_offset: usize) -> bool {
        self.folded_headings.contains(&byte_offset)
    }

    /// On initial load, collapse callouts that opted into starting collapsed (`[!type]-`).
    /// Applied once (from `open`), so a manual expand later isn't re-folded on the next edit.
    pub fn seed_default_collapsed_callouts(&mut self) {
        let offs: Vec<usize> = self
            .state
            .buffer
            .callouts()
            .iter()
            .filter(|c| c.default_collapsed && c.is_foldable())
            .map(|c| c.byte_offset)
            .collect();
        self.folded_headings.extend(offs);
    }

    /// Apply a fold-gutter click at `byte_offset` — same escalating scope for every
    /// foldable kind: plain = just this block, **Shift** = recursive (this + everything
    /// nested inside it), **Ctrl** = all at this level, **Ctrl+Shift** = this level and
    /// deeper. "This level" is the only per-kind difference: headings group by heading
    /// level (H1–H6, replacing the fold set), lists/callouts additively by nesting depth.
    /// No-op if `byte_offset` isn't a foldable anchor.
    pub fn apply_fold_gesture(&mut self, byte_offset: usize, ctrl: bool, shift: bool) {
        let Some(kind) = self
            .fold_anchors()
            .iter()
            .find(|a| a.byte_offset == byte_offset)
            .map(|a| a.kind)
        else {
            return;
        };
        match (ctrl, shift) {
            (false, false) => self.toggle_fold(byte_offset),
            (false, true) => self.toggle_fold_recursive(byte_offset),
            (true, _) if kind == fold::FoldKind::Heading => {
                self.toggle_heading_level(byte_offset, shift)
            }
            (true, _) => self.toggle_depth_level(byte_offset, shift),
        }
    }

    /// Toggle the fold on the heading anchored at `byte_offset` (a gutter-chevron click).
    pub fn toggle_fold(&mut self, byte_offset: usize) {
        if !self.folded_headings.remove(&byte_offset) {
            self.folded_headings.insert(byte_offset);
        }
        self.clamp_cursor_to_visible();
    }

    /// Recursive fold toggle (Shift+click a chevron): fold the heading together with all
    /// its descendant subheadings, or — if it's already folded — unfold it and them. Folds
    /// the descendants too so unfolding the parent later reveals a still-collapsed subtree,
    /// matching the TextMate/VS Code "fold recursively" convention.
    pub fn toggle_fold_recursive(&mut self, byte_offset: usize) {
        let offs = self.recursive_fold_set(byte_offset);
        if !offs.is_empty() {
            self.apply_group_fold(byte_offset, offs);
        }
    }

    /// Fold or unfold `offs` as a group, keyed on the anchor: fold (insert all) when the
    /// anchor is currently unfolded, else unfold (remove all). Re-clamps the caret.
    fn apply_group_fold(&mut self, byte_offset: usize, offs: Vec<usize>) {
        let folding = !self.folded_headings.contains(&byte_offset);
        for off in offs {
            if folding {
                self.folded_headings.insert(off);
            } else {
                self.folded_headings.remove(&off);
            }
        }
        self.clamp_cursor_to_visible();
    }

    /// The anchor `byte_offset` plus every foldable anchor of the **same kind** nested
    /// inside its extent (deeper headings under a heading, nested items under an item,
    /// nested callouts under a callout). Empty if the offset isn't a foldable anchor.
    fn recursive_fold_set(&self, byte_offset: usize) -> Vec<usize> {
        let anchors = self.fold_anchors();
        let Some(clicked) = anchors.iter().find(|a| a.byte_offset == byte_offset) else {
            return Vec::new();
        };
        let mut offs = vec![byte_offset];
        for a in &anchors {
            if a.byte_offset != byte_offset
                && a.kind == clicked.kind
                && clicked.extent.contains(&a.line)
            {
                offs.push(a.byte_offset);
            }
        }
        offs
    }

    /// Ctrl(+Shift)+click a list or callout chevron: additively toggle every foldable
    /// anchor of the clicked one's kind at its nesting depth (or, `deep`, that depth and
    /// deeper). The additive counterpart to headings' replace-the-set level fold — it
    /// leaves folds of other kinds and other depths alone.
    fn toggle_depth_level(&mut self, byte_offset: usize, deep: bool) {
        let anchors = self.fold_anchors();
        let Some(clicked) = anchors.iter().find(|a| a.byte_offset == byte_offset) else {
            return;
        };
        let (kind, depth) = (clicked.kind, clicked.level);
        let offs: Vec<usize> = anchors
            .iter()
            .filter(|a| {
                a.kind == kind
                    && if deep {
                        a.level >= depth
                    } else {
                        a.level == depth
                    }
            })
            .map(|a| a.byte_offset)
            .collect();
        self.apply_group_fold(byte_offset, offs);
    }

    /// Fold the thing the cursor is in: the innermost non-heading block (list item /
    /// callout) the caret sits inside, else the nearest heading section at or above it.
    pub fn fold_at_cursor(&mut self) {
        let line = self.line_of(self.cursor_position());
        let inner = self
            .fold_anchors()
            .into_iter()
            .filter(|a| a.kind != fold::FoldKind::Heading && (a.line..a.extent.end).contains(&line))
            .max_by_key(|a| a.line);
        if let Some(a) = inner {
            self.folded_headings.insert(a.byte_offset);
            self.clamp_cursor_to_visible();
            return;
        }
        let line_count = self.state.buffer.line_count();
        let headings = self.state.buffer.headings();
        let Some(idx) = fold::section_heading(headings, line) else {
            return;
        };
        if !fold::heading_is_foldable(headings, idx, line_count) {
            return;
        }
        let off = headings[idx].byte_offset;
        self.folded_headings.insert(off);
        self.clamp_cursor_to_visible();
    }

    /// Unfold the section the cursor is in.
    pub fn unfold_at_cursor(&mut self) {
        let line = self.line_of(self.cursor_position());
        let headings = self.state.buffer.headings();
        let Some(idx) = fold::section_heading(headings, line) else {
            return;
        };
        let off = headings[idx].byte_offset;
        self.folded_headings.remove(&off);
    }

    pub fn fold_all_headings(&mut self) {
        let line_count = self.state.buffer.line_count();
        let headings = self.state.buffer.headings();
        let offs: Vec<usize> = headings
            .iter()
            .enumerate()
            .filter(|(i, _)| fold::heading_is_foldable(headings, *i, line_count))
            .map(|(_, h)| h.byte_offset)
            .collect();
        self.folded_headings.extend(offs);
        self.clamp_cursor_to_visible();
    }

    pub fn unfold_all(&mut self) {
        self.folded_headings.clear();
    }

    /// Ctrl(+Shift)+click a heading chevron: fold every section at the clicked heading's
    /// level (a mouse path to [`Self::fold_to_level`]), or `deep`: that level and deeper.
    /// Replaces the fold set; toggles — if the heading is already folded, unfold all.
    fn toggle_heading_level(&mut self, byte_offset: usize, deep: bool) {
        let level = self
            .state
            .buffer
            .headings()
            .iter()
            .find(|h| h.byte_offset == byte_offset)
            .map(|h| h.level);
        let Some(level) = level else {
            return;
        };
        if self.folded_headings.contains(&byte_offset) {
            self.unfold_all();
        } else if deep {
            self.fold_to_level_deep(level);
        } else {
            self.fold_to_level(level);
        }
    }

    /// Replace the fold set with every foldable heading whose level satisfies `level_ok`.
    fn fold_headings_where(&mut self, level_ok: impl Fn(u8) -> bool) {
        let line_count = self.state.buffer.line_count();
        let headings = self.state.buffer.headings();
        self.folded_headings = headings
            .iter()
            .enumerate()
            .filter(|(i, h)| {
                level_ok(h.level) && fold::heading_is_foldable(headings, *i, line_count)
            })
            .map(|(_, h)| h.byte_offset)
            .collect();
        self.clamp_cursor_to_visible();
    }

    /// Fold to a heading depth: collapse exactly the sections at `level`, replacing any
    /// current folds. Headings shallower than `level` stay visible (with their bodies),
    /// and everything below a level-`level` heading hides — so expanding one reveals its
    /// whole subtree at once. Level 1 leaves only the top-level headings showing.
    pub fn fold_to_level(&mut self, level: u8) {
        self.fold_headings_where(|l| l == level);
    }

    /// Deep variant of [`Self::fold_to_level`]: fold every foldable heading at `level` or deeper,
    /// pre-collapsing descendants so expanding a section reveals its children still folded.
    pub fn fold_to_level_deep(&mut self, level: u8) {
        self.fold_headings_where(|l| l >= level);
    }

    /// Auto-unfold any folded section the caret has entered (search-jump, outline click,
    /// arrow keys). A no-op when the caret is already on a visible line. Also drops folds
    /// whose heading was edited away (stale offset no longer matches any heading start).
    /// Returns `true` if the fold set changed (the caller must relayout).
    pub fn reveal_cursor(&mut self) -> bool {
        if self.folded_headings.is_empty() {
            return false;
        }
        let cursor_line = self.line_of(self.cursor_position());
        let to_remove: Vec<usize> = {
            let anchors = self.fold_anchors();
            self.folded_headings
                .iter()
                .copied()
                .filter(|&off| match fold::extent_for_offset(&anchors, off) {
                    Some(ext) => ext.contains(&cursor_line),
                    None => true, // stale offset (anchor edited away): drop
                })
                .collect()
        };
        for off in &to_remove {
            self.folded_headings.remove(off);
        }
        !to_remove.is_empty()
    }

    /// Keep the "caret is never on a hidden line" invariant after a fold: if a collapse
    /// hid the caret, move it up to the heading line that folds the region.
    fn clamp_cursor_to_visible(&mut self) {
        let ranges = self.hidden_line_ranges();
        if ranges.is_empty() {
            return;
        }
        let cursor_line = self.line_of(self.cursor_position());
        if let Some(r) = ranges.iter().find(|r| r.contains(&cursor_line)) {
            let heading_line = r.start.saturating_sub(1);
            let off = self.state.buffer.line_to_byte(heading_line);
            self.state.selection = Selection::new(off, off);
        }
    }

    /// Rebuild the match list for the current query. Uses the regex crate for BOTH
    /// literal and regex modes so byte offsets stay exact and case-folding is correct:
    /// a literal query is `regex::escape`d, and `(?i)` is prepended when case-insensitive.
    /// An invalid pattern (e.g. a half-typed `(`) yields no matches rather than panicking.
    /// Sets the document selection to the active match so it gets caret/highlight/scroll
    /// for free. A no-op when `(version, query, case, regex)` is unchanged.
    pub fn find_rescan(&mut self) {
        let Some(find) = self.find.as_ref() else {
            return;
        };
        let query = find.search.text().to_string();
        let case = find.case_sensitive;
        let regex = find.regex;
        let focused = find.focused;
        let version = self.state.buffer.version();
        if find.scanned.as_ref() == Some(&(version, query.clone(), case, regex)) {
            return;
        }

        if query.is_empty() {
            let find = self.find.as_mut().expect("find open");
            find.matches.clear();
            find.active = None;
            find.scanned = Some((version, query, case, regex));
            return;
        }

        let pattern = Self::build_find_pattern(&query, regex, case);

        let matches: Vec<Range<usize>> = match Regex::new(&pattern) {
            Ok(re) => {
                let text = self.state.buffer.text();
                re.find_iter(&text).map(|m| m.start()..m.end()).collect()
            }
            Err(_) => Vec::new(),
        };

        let cursor = self.cursor_position();
        let active = (!matches.is_empty())
            .then(|| matches.iter().position(|m| m.start >= cursor).unwrap_or(0));
        // Only pull the document selection onto the active match when the bar owns focus.
        // While the document is focused (bar open but unfocused), a rescan triggered by a
        // buffer edit must not yank the caret away from where the user is typing.
        if let Some(idx) = active
            && focused
        {
            let r = matches[idx].clone();
            self.state.selection = Selection::new(r.start, r.end);
        }

        let find = self.find.as_mut().expect("find open");
        find.matches = matches;
        find.active = active;
        find.scanned = Some((version, query, case, regex));
    }

    /// Advance to the next match (wrapping), moving the document selection onto it.
    /// Returns the new active match range so the shell can scroll it into view.
    pub fn find_next(&mut self) -> Option<Range<usize>> {
        self.find_step(true)
    }

    /// Retreat to the previous match (wrapping); otherwise like [`Self::find_next`].
    pub fn find_prev(&mut self) -> Option<Range<usize>> {
        self.find_step(false)
    }

    fn find_step(&mut self, forward: bool) -> Option<Range<usize>> {
        let find = self.find.as_mut()?;
        let n = find.matches.len();
        if n == 0 {
            return None;
        }
        let cur = find.active.unwrap_or(0);
        let next = if forward {
            (cur + 1) % n
        } else {
            (cur + n - 1) % n
        };
        find.active = Some(next);
        let r = find.matches[next].clone();
        self.state.selection = Selection::new(r.start, r.end);
        Some(r)
    }

    pub fn find_toggle_case(&mut self) {
        if let Some(find) = self.find.as_mut() {
            find.case_sensitive = !find.case_sensitive;
        }
        self.find_rescan();
    }

    pub fn find_toggle_regex(&mut self) {
        if let Some(find) = self.find.as_mut() {
            find.regex = !find.regex;
        }
        self.find_rescan();
    }

    /// Swap keyboard focus between the search and replace fields. A no-op in Find mode,
    /// where there is no replace field to focus.
    pub fn find_toggle_field(&mut self) {
        if let Some(find) = self.find.as_mut()
            && find.mode == FindMode::Replace
        {
            find.focus = match find.focus {
                FieldFocus::Search => FieldFocus::Replace,
                FieldFocus::Replace => FieldFocus::Search,
            };
        }
    }

    /// Recompile the exact `Regex` `find_rescan` used for the current query (literal
    /// queries are `regex::escape`d, `(?i)` prepended when case-insensitive) so replace
    /// can expand `$1`/`${name}` capture groups against a matched slice. `None` for an
    /// empty or invalid pattern.
    /// Build the regex source for a find query: literal queries are `regex::escape`d, and
    /// `(?i)` is prepended when the search is case-insensitive.
    fn build_find_pattern(query: &str, regex: bool, case_sensitive: bool) -> String {
        let escaped;
        let base = if regex {
            query
        } else {
            escaped = regex::escape(query);
            escaped.as_str()
        };
        if case_sensitive {
            base.to_string()
        } else {
            format!("(?i){base}")
        }
    }

    fn find_regex(&self) -> Option<Regex> {
        let find = self.find.as_ref()?;
        let query = find.search.text();
        if query.is_empty() {
            return None;
        }
        let pattern = Self::build_find_pattern(query, find.regex, find.case_sensitive);
        Regex::new(&pattern).ok()
    }

    /// Replace the active match with the replacement text (one undo step), then rescan
    /// and land on the next match. In regex mode the replacement's `$1`/`${name}` groups
    /// expand from the matched slice; in literal mode it is inserted verbatim. No-op when
    /// there is no active match.
    pub fn find_replace_current(&mut self) {
        let Some(find) = self.find.as_ref() else {
            return;
        };
        let Some(active) = find.active else {
            return;
        };
        let range = find.matches[active].clone();
        let replacement = find.replace.text().to_string();
        let regex_mode = find.regex;

        let expanded = if regex_mode {
            match self.find_regex() {
                Some(re) => {
                    let text = self.state.buffer.text();
                    re.replace(&text[range.clone()], replacement.as_str())
                        .into_owned()
                }
                None => replacement,
            }
        } else {
            replacement
        };

        // Replace the match range in a SINGLE undo step (a bare select+insert_text is a
        // delete then an insert — two undo entries — so one Ctrl+Z wouldn't fully revert
        // it). Same coalesce pattern as `find_replace_all`.
        self.edit(|s| {
            let head = s.buffer.undo_head();
            let text_before = s.buffer.text();
            let cursor_before = s.cursor().offset;
            s.buffer.replace(range.clone(), &expanded, cursor_before);
            let text_after = s.buffer.text();
            let cursor_after = (range.start + expanded.len()).min(text_after.len());
            s.buffer
                .coalesce_since(head, &text_before, &text_after, cursor_before, cursor_after);
            s.selection = Selection::new(cursor_after, cursor_after);
        });

        // Rescan advances `active`/selection to the first match at/after the new caret
        // (skipping any occurrence the replacement itself introduced), and the shell
        // scrolls that selection into view.
        self.find_rescan();
    }

    /// Replace every current match in a single undo step, keeping the bar open. Regex
    /// mode expands capture groups per match; literal mode inserts verbatim. Matches are
    /// applied right-to-left so earlier byte offsets stay valid as the text shifts.
    pub fn find_replace_all(&mut self) {
        let Some(find) = self.find.as_ref() else {
            return;
        };
        if find.matches.is_empty() {
            return;
        }
        let matches = find.matches.clone();
        let replacement = find.replace.text().to_string();
        let re = self.find_regex();
        let regex_mode = find.regex;

        self.edit(|s| {
            let head = s.buffer.undo_head();
            let text_before = s.buffer.text();
            let cursor_before = s.cursor().offset;

            for range in matches.iter().rev() {
                let expanded = match (regex_mode, &re) {
                    (true, Some(re)) => re
                        .replace(&text_before[range.clone()], replacement.as_str())
                        .into_owned(),
                    _ => replacement.clone(),
                };
                s.buffer.replace(range.clone(), &expanded, cursor_before);
            }

            let text_after = s.buffer.text();
            let cursor_after = cursor_before.min(text_after.len());
            // Collapse the per-match edits into one minimal undo entry.
            s.buffer
                .coalesce_since(head, &text_before, &text_after, cursor_before, cursor_after);
            s.selection = Selection::new(cursor_after, cursor_after);
        });

        self.find_rescan();
    }

    // --- GitHub autocomplete (#/@) ------------------------------------------

    pub fn autocomplete(&self) -> Option<&AutocompleteState> {
        self.autocomplete.as_ref()
    }

    pub fn close_autocomplete(&mut self) {
        self.autocomplete = None;
    }

    /// Move the selection up/down (wrapping). No-op if no popup / no suggestions.
    pub fn autocomplete_move(&mut self, forward: bool) {
        if let Some(ac) = &mut self.autocomplete
            && !ac.suggestions.is_empty()
        {
            let n = ac.suggestions.len();
            ac.selected_index = if forward {
                (ac.selected_index + 1) % n
            } else {
                (ac.selected_index + n - 1) % n
            };
        }
    }

    /// Set the selected suggestion index directly (mouse hover/click).
    pub fn autocomplete_select(&mut self, index: usize) {
        if let Some(ac) = &mut self.autocomplete
            && index < ac.suggestions.len()
        {
            ac.selected_index = index;
        }
    }

    /// Re-evaluate the popup against the current cursor. Returns true when the shell
    /// should (re)fetch suggestions for the new trigger/prefix.
    pub fn update_autocomplete_from_cursor(&mut self) -> bool {
        if self.github_context.is_none() || self.github_client.is_none() {
            self.autocomplete = None;
            return false;
        }

        let cursor = self.state.cursor().offset;
        let cursor_line = self.state.buffer.byte_to_line(cursor);

        // Cursor inside an already-detected issue ref → offer that ref's number.
        if let Some(refs) = self.github_refs_by_line.get(&cursor_line) {
            for github_match in refs {
                if cursor >= github_match.byte_range.start
                    && cursor <= github_match.byte_range.end
                    && let GitHubRef::Issue { number, .. } = &github_match.reference
                {
                    let prefix = number.to_string();
                    let trigger_offset = github_match.byte_range.start;
                    return self.set_autocomplete_state(
                        AutocompleteTrigger::Issue,
                        trigger_offset,
                        prefix,
                    );
                }
            }
        }

        // Otherwise scan back from the cursor for a `#`/`@` trigger.
        if cursor > 0 {
            let line_start = self.state.buffer.line_to_byte(cursor_line);
            let line_text = self.state.buffer.slice_cow(line_start..cursor).into_owned();
            if let Some((trigger, trigger_offset, prefix)) =
                Self::detect_autocomplete_trigger(&line_text, line_start)
            {
                return self.set_autocomplete_state(trigger, trigger_offset, prefix);
            }
        }

        self.autocomplete = None;
        false
    }

    /// Scan `line_text` (from line start up to the cursor) for the rightmost valid
    /// `#`/`@` trigger, returning its type, absolute offset, and typed prefix.
    fn detect_autocomplete_trigger(
        line_text: &str,
        line_start: usize,
    ) -> Option<(AutocompleteTrigger, usize, String)> {
        let triggers = [
            ('#', AutocompleteTrigger::Issue),
            ('@', AutocompleteTrigger::User),
        ];
        let mut best: Option<(AutocompleteTrigger, usize, String)> = None;

        for (trigger_char, trigger_type) in triggers {
            let Some(pos) = line_text.rfind(trigger_char) else {
                continue;
            };
            let at_boundary = pos == 0
                || line_text
                    .as_bytes()
                    .get(pos - 1)
                    .is_none_or(|&b| b == b' ' || b == b'\t' || b == b'\n');
            if !at_boundary {
                continue;
            }
            let prefix = line_text[pos + 1..].to_string();
            let valid = match trigger_type {
                // `# ` is a heading, not an issue ref.
                AutocompleteTrigger::Issue => !prefix.starts_with([' ', '\t']),
                AutocompleteTrigger::User => {
                    prefix.is_empty()
                        || (prefix.chars().all(|c| c.is_alphanumeric() || c == '-')
                            && !prefix.starts_with('-'))
                }
            };
            if !valid {
                continue;
            }
            let trigger_offset = line_start + pos;
            if best
                .as_ref()
                .is_none_or(|(_, off, _)| trigger_offset > *off)
            {
                best = Some((trigger_type, trigger_offset, prefix));
            }
        }
        best
    }

    /// Update the popup for a detected trigger, preserving suggestions/selection when
    /// only the prefix grew within the same trigger. Returns true if a fetch is needed.
    fn set_autocomplete_state(
        &mut self,
        trigger: AutocompleteTrigger,
        trigger_offset: usize,
        prefix: String,
    ) -> bool {
        let changed = self
            .autocomplete
            .as_ref()
            .map(|ac| ac.trigger != trigger || ac.prefix != prefix)
            .unwrap_or(true);
        if !changed {
            return false;
        }

        let old = self.autocomplete.take();
        let same_trigger = old
            .as_ref()
            .map(|ac| ac.trigger == trigger)
            .unwrap_or(false);
        let should_fetch = match trigger {
            AutocompleteTrigger::Issue => {
                let already = old
                    .as_ref()
                    .filter(|_| same_trigger)
                    .and_then(|ac| ac.fetched_prefix.as_ref())
                    == Some(&prefix);
                !already
            }
            AutocompleteTrigger::User => true,
        };

        // `old` is owned, so when the trigger matches MOVE its suggestions/selection/prefix
        // into the new state (a prefix keystroke keeps them) — no per-keystroke Vec clone.
        let (suggestions, selected_index, fetched_prefix) = match old.filter(|_| same_trigger) {
            Some(ac) => (ac.suggestions, ac.selected_index, ac.fetched_prefix),
            None => (Vec::new(), 0, None),
        };
        self.autocomplete = Some(AutocompleteState {
            trigger,
            trigger_offset,
            prefix,
            suggestions,
            selected_index,
            loading: false,
            fetched_prefix,
        });
        should_fetch
    }

    /// Mark the popup loading and return the (trigger, prefix) the shell should fetch.
    pub fn begin_autocomplete_fetch(&mut self) -> Option<(AutocompleteTrigger, String)> {
        let ac = self.autocomplete.as_mut()?;
        ac.loading = true;
        ac.fetched_prefix = Some(ac.prefix.clone());
        Some((ac.trigger, ac.prefix.clone()))
    }

    /// Install fetched suggestions if the popup is still on the same trigger+prefix.
    pub fn apply_autocomplete_suggestions(
        &mut self,
        trigger: AutocompleteTrigger,
        prefix: &str,
        suggestions: Vec<AutocompleteSuggestion>,
    ) {
        if let Some(ac) = &mut self.autocomplete
            && ac.trigger == trigger
            && ac.prefix == prefix
        {
            ac.suggestions = suggestions;
            ac.loading = false;
            ac.selected_index = 0;
        }
    }

    /// Replace the trigger token (`#…`/`@…`) with the selected suggestion. Returns
    /// true if a suggestion was accepted (buffer changed), closing the popup.
    pub fn accept_autocomplete_suggestion(&mut self) -> bool {
        let Some(ac) = self.autocomplete.take() else {
            return false;
        };
        if ac.suggestions.is_empty() {
            return false;
        }
        let replacement = match &ac.suggestions[ac.selected_index] {
            AutocompleteSuggestion::IssueOrPr { number, .. } => format!("#{number}"),
            AutocompleteSuggestion::User { login, .. } => format!("@{login}"),
        };
        let cursor = self.state.cursor().offset;
        // Select the trigger token (`#…`/`@…`) and replace it through the normal insert
        // path, so diff-recompute and undo semantics match a hand-typed edit.
        self.state.selection = Selection::new(ac.trigger_offset, cursor);
        self.insert_str(&replacement);
        true
    }

    // --- inline git diff ----------------------------------------------------

    pub fn diff_state(&self) -> Option<&DiffState> {
        self.diff_state.as_ref()
    }

    /// Recompute the inline diff of the current buffer against the cached HEAD base.
    pub fn recompute_diff(&mut self) {
        self.diff_state = self
            .head_base
            .as_ref()
            .and_then(|(base_text, base_snapshot)| {
                let current = self.state.buffer.text();
                let state = DiffState::compute(base_snapshot.clone(), base_text, &current);
                state.has_hunks().then_some(state)
            });
    }

    /// Build a diff-base entry — the base text paired with its render snapshot — parsing
    /// and snapshotting `text`. Parsing a `Buffer` is infallible.
    fn make_head_base(text: String) -> (String, RenderSnapshot) {
        let mut base: Buffer = text.parse().expect("Buffer parsing is infallible");
        let snapshot = base.render_snapshot();
        (text, snapshot)
    }

    /// Load the git HEAD blob for the current file, snapshot it as the diff base,
    /// and recompute. No-op (clears the base) if there's no file or no HEAD blob.
    pub fn refresh_git_base(&mut self) {
        self.head_base = self
            .file_path
            .as_ref()
            .and_then(|path| head_blob_text(path))
            .map(Self::make_head_base);
        self.recompute_diff();
    }

    /// Directly set the diff base (test/headless helper) from raw text.
    pub fn set_head_base(&mut self, base_text: &str) {
        self.head_base = Some(Self::make_head_base(base_text.to_string()));
        self.recompute_diff();
    }

    // --- file I/O -----------------------------------------------------------

    /// Save the buffer to `file_path` (if set), recording the write mtime so the
    /// file watcher can distinguish our own write from an external edit.
    pub fn save(&mut self) -> std::io::Result<()> {
        let Some(path) = self.file_path.clone() else {
            return Ok(());
        };
        // Stream the rope's chunks straight to the file — no whole-document String alloc.
        let file = std::fs::File::create(&path)?;
        self.state
            .buffer
            .rope()
            .write_to(std::io::BufWriter::new(file))?;
        if let Ok(metadata) = std::fs::metadata(&path) {
            self.last_save_mtime = metadata.modified().ok();
        }
        self.state.buffer.mark_clean();
        Ok(())
    }

    /// Poll the file watcher; if the file changed on disk (and it wasn't our own
    /// save), reload it and refresh the diff base. Returns true if reloaded.
    /// Hand the file-watch receiver to the shell so it can forward change
    /// notifications into the event loop (waking it) instead of polling on a timer.
    pub fn take_file_watch_rx(&mut self) -> Option<mpsc::Receiver<WatchKind>> {
        self.file_watcher_rx.take()
    }

    pub fn last_save_mtime(&self) -> Option<SystemTime> {
        self.last_save_mtime
    }

    /// The blocking half of an external-file reload: read the file + the git HEAD blob.
    /// Pure/`Send` (no editor state, no `Rc`), so the shell runs it on a blocking worker
    /// off the render thread — the diff hot path must not touch disk on the UI thread.
    /// Returns `(new_content, head_base_text)`, or `None` to skip (our own save, or a
    /// read error). The cheap parse/snapshot/diff is done on the main thread by
    /// [`Self::apply_reload`].
    /// `ignore_self_write` skips the mtime guard: a git-dir change (commit/checkout)
    /// leaves the working file's mtime — possibly equal to our last save — untouched, so
    /// the guard would wrongly skip it. `apply_reload`'s `content_eq` check makes the
    /// re-read a no-op for the (unchanged) buffer anyway; only the HEAD base updates.
    pub fn read_reload(
        path: &Path,
        last_save_mtime: Option<SystemTime>,
        ignore_self_write: bool,
    ) -> Option<(String, Option<String>)> {
        if !ignore_self_write
            && let Some(last) = last_save_mtime
            && let Ok(meta) = std::fs::metadata(path)
            && let Ok(mtime) = meta.modified()
            && mtime == last
        {
            return None; // our own write
        }
        let content = std::fs::read_to_string(path).ok()?;
        Some((content, head_blob_text(path)))
    }

    /// The main-thread half of a reload: swap in the freshly-read `content` (preserving
    /// the cursor line) and set the diff base from `base_text`, then recompute the diff.
    /// Only parse/snapshot/diff work — no IO.
    pub fn apply_reload(&mut self, content: String, base_text: Option<String>) {
        if !self.state.buffer.content_eq(&content) {
            // Persist folds across the reload: remap their offsets from the actual
            // splice (external edits are usually a localized change), the same way a
            // local edit does. `set_text` leaves `folded_headings` untouched, so without
            // this the old offsets would land on the wrong lines in the new content.
            let before =
                (!self.folded_headings.is_empty()).then(|| self.state.buffer.rope().clone());
            let cursor_line = self.state.buffer.byte_to_line(self.state.selection.head);
            self.set_text(&content);
            if let Some(before) = before {
                let after = self.state.buffer.rope();
                let (prefix, old_end, delta) = splice_bounds(&before, after);
                self.remap_folds(prefix, old_end, delta);
            }
            let line = cursor_line.min(self.state.buffer.line_count().saturating_sub(1));
            let offset = self.state.buffer.line_to_byte(line);
            self.state.selection = Selection::new(offset, offset);
        }
        self.head_base = base_text.map(Self::make_head_base);
        self.recompute_diff();
    }

    /// Start watching `file_path` for external modifications.
    pub fn watch_file(&mut self) -> notify::Result<()> {
        let Some(path) = self.file_path.clone() else {
            return Ok(());
        };
        let (tx, rx) = mpsc::channel();
        let watched_file = path.clone();
        // 150ms debounce collapses a save's event burst (and atomic-save temp→rename)
        // into a single reload notification.
        let mut debouncer = new_debouncer(
            Duration::from_millis(150),
            None,
            move |result: DebounceEventResult| {
                let Ok(events) = result else { return };
                let mut file = false;
                let mut git = false;
                for e in &events {
                    if !matches!(e.kind, EventKind::Modify(_) | EventKind::Create(_)) {
                        continue;
                    }
                    for p in &e.paths {
                        if *p == watched_file {
                            file = true;
                        } else if is_git_base_path(p) {
                            git = true;
                        }
                    }
                }
                if file {
                    let _ = tx.send(WatchKind::File);
                }
                if git {
                    let _ = tx.send(WatchKind::GitBase);
                }
            },
        )?;
        debouncer.watch(&path, RecursiveMode::NonRecursive)?;
        // Also watch the git dirs so a commit/checkout that moves HEAD refreshes the
        // inline diff even when the working file is untouched. Watch the *directories*
        // (not the files) so the watch survives git's lockfile→rename ref updates; the
        // callback filters to HEAD/refs via `is_git_base_path`. Best-effort: a missing
        // dir or a non-repo file just skips git watching.
        if let Some((git_dir, common_dir)) = git_watch_dirs(&path) {
            let mut seen: HashSet<PathBuf> = HashSet::new();
            for (dir, mode) in [
                (git_dir.clone(), RecursiveMode::NonRecursive),
                (common_dir.clone(), RecursiveMode::NonRecursive),
                (common_dir.join("refs"), RecursiveMode::Recursive),
            ] {
                if seen.insert(dir.clone()) {
                    let _ = debouncer.watch(&dir, mode);
                }
            }
        }
        self.file_watcher = Some(debouncer);
        self.file_watcher_rx = Some(rx);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;
    use std::time::Instant;

    #[test]
    fn splice_bounds_reports_the_edited_region() {
        let diff = |a: &str, b: &str| splice_bounds(&Rope::from_str(a), &Rope::from_str(b));
        // Pure insertion in the middle: prefix..old_end is empty, delta is the inserted len.
        assert_eq!(diff("abcXYZ", "abcQWXYZ"), (3, 3, 2));
        // Deletion in the middle.
        assert_eq!(diff("abcQWXYZ", "abcXYZ"), (3, 5, -2));
        // Replacement: the common prefix/suffix bracket the changed span.
        assert_eq!(diff("hello world", "hello brave world"), (6, 6, 6));
        // No change → empty splice at the end.
        assert_eq!(diff("same", "same"), (4, 4, 0));
        // Append only (no common suffix).
        assert_eq!(diff("abc", "abcdef"), (3, 3, 3));
    }

    #[test]
    fn splice_bounds_handles_large_multichunk_middle_edit() {
        // Force multiple rope chunks so the backward chunk-walk in `common_suffix_len`
        // strides across chunk boundaries rather than a single flat buffer.
        let a = "x".repeat(5000) + "OLD" + &"y".repeat(5000);
        let b = "x".repeat(5000) + "BRANDNEW" + &"y".repeat(5000);
        let (prefix, old_end, delta) = splice_bounds(&Rope::from_str(&a), &Rope::from_str(&b));
        assert_eq!(prefix, 5000);
        assert_eq!(old_end, 5003); // 5000 + "OLD"
        assert_eq!(delta, "BRANDNEW".len() as isize - "OLD".len() as isize);
    }

    /// End-to-end proof that a `git commit` which leaves the working file untouched still
    /// wakes the watcher with `WatchKind::GitBase` (so the inline diff refreshes). Uses a
    /// real temp repo + real fs events, so it's `#[ignore]`d out of CI (fs-event timing is
    /// flaky in containers); run manually with `--ignored --nocapture`.
    #[test]
    #[ignore = "spawns git + relies on fs-event timing; run manually"]
    fn git_commit_wakes_watcher() {
        let dir = std::env::temp_dir().join(format!("writ-gitwatch-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let git = |args: &[&str]| {
            let ok = Command::new("git")
                .args(args)
                .current_dir(&dir)
                .status()
                .unwrap()
                .success();
            assert!(ok, "git {args:?} failed");
        };
        git(&["init", "-q"]);
        git(&["config", "user.email", "t@t"]);
        git(&["config", "user.name", "t"]);
        let note = dir.join("note.md");
        std::fs::write(&note, "# v1\n").unwrap();
        git(&["add", "."]);
        git(&["commit", "-qm", "first"]);

        let mut editor = Editor::open(&note);
        editor.watch_file().unwrap();
        let rx = editor.take_file_watch_rx().unwrap();

        // Move HEAD WITHOUT touching note.md: commit a different file.
        std::fs::write(dir.join("other.md"), "x\n").unwrap();
        git(&["add", "other.md"]);
        git(&["commit", "-qm", "second"]);

        // The commit updated refs/heads/… → expect a GitBase wake within a few seconds.
        let kind = rx
            .recv_timeout(Duration::from_secs(5))
            .expect("watcher should wake on commit");
        assert_eq!(kind, WatchKind::GitBase);
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Secondary find-feature spike: measure whether a full-document scan per keystroke
    /// (rope→String, then `match_indices`) is cheap enough to run live. Not asserted on
    /// (timings are flaky in CI); run with `--ignored --nocapture` to see the numbers.
    #[test]
    #[ignore = "manual measurement; prints timing, no assertions"]
    fn find_scan_cost_on_large_document() {
        // ~10k lines of representative markdown (headings, prose, lists, code).
        let unit = "\
# Heading with a searchable word target here
Some prose paragraph mentioning target and other words in a sentence.
- a list item with target inside it
- another item, plainer
```
let code = target(); // fenced block line
```
> a blockquote line about targets and things
";
        let reps = 10_000 / unit.lines().count();
        let mut src = String::with_capacity(unit.len() * reps);
        for _ in 0..reps {
            src.push_str(unit);
        }
        let mut buffer = Buffer::new();
        buffer.insert(0, &src, 0);
        let line_count = buffer.text().lines().count();

        for query in ["target", "the", "nonexistent_zzz"] {
            let t = Instant::now();
            let text = buffer.text();
            let count = text.match_indices(query).count();
            let ms = t.elapsed().as_secs_f64() * 1000.0;
            println!(
                "scan {line_count} lines for {query:?}: {count} hits in {ms:.3} ms (text()+match_indices)",
            );
        }
    }

    /// Autosave (used by the GhostText daemon via `--autosave`) writes every edit back to
    /// the file with no explicit save — the daemon relays that to the browser.
    #[test]
    fn autosave_writes_on_every_edit() {
        let dir = std::env::temp_dir();
        let path = dir.join(format!("writ_autosave_test_{}.md", std::process::id()));
        std::fs::write(&path, "start\n").unwrap();

        let mut e = Editor::open(&path);
        e.set_autosave(true);
        e.set_cursor(e.len());
        e.insert_str("X");
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "start\nX");

        e.backspace();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "start\n");

        // Without autosave, edits stay in memory only.
        e.set_autosave(false);
        e.insert_str("Y");
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "start\n");

        std::fs::remove_file(&path).ok();
    }

    /// Ctrl/Cmd-click link resolution: a markdown link target, a naked URL, and a
    /// non-link position (None). Drives opening links in the browser.
    #[test]
    fn link_at_resolves_links() {
        let mut e = Editor::new("See [docs](https://example.com/docs) and https://plain.url/x\n");
        // Inside the markdown link source → its target URL (no detection needed).
        let md = e.text().find("docs]").unwrap();
        assert_eq!(
            e.link_at(md).as_deref(),
            Some("https://example.com/docs"),
            "markdown link target"
        );
        // Naked URL needs detection to populate the per-line index.
        e.refresh_detection(0..usize::MAX);
        let naked = e.text().find("plain.url").unwrap();
        assert_eq!(
            e.link_at(naked).as_deref(),
            Some("https://plain.url/x"),
            "naked URL"
        );
        // Plain text position → no link.
        assert_eq!(e.link_at(0), None);
    }

    /// Ctrl-click resolution: a relative link/image target resolves against the doc's
    /// directory; web/absolute targets pass through unchanged.
    #[test]
    fn link_at_resolves_relative_against_doc_dir() {
        let mut e = Editor::new("![img](assets/p.png) and [w](https://x.com)\n");
        e.set_file_path(std::path::PathBuf::from("/home/u/notes/doc.md"));
        let rel = e.text().find("assets").unwrap();
        assert_eq!(
            e.link_at(rel).as_deref(),
            Some("/home/u/notes/assets/p.png"),
            "relative image path resolves against the doc dir"
        );
        let web = e.text().find("x.com").unwrap();
        assert_eq!(
            e.link_at(web).as_deref(),
            Some("https://x.com"),
            "web URL as-is"
        );
    }

    /// The input behaviors restored from the gpui `on_key_down` (Home/End/doc-boundary
    /// movement, select-all, smart space, blockquote/fence auto-completion) — wired
    /// through `Editor`, so a future shell refactor can't silently drop them again.

    #[test]
    fn restored_editor_input_behaviors() {
        // Home/End = line boundary; Ctrl variants = doc boundary; Shift extends.
        let mut e = Editor::new("hello\nworld two\n");
        e.set_cursor(3);
        e.move_in_direction(Direction::LineEnd, false);
        assert_eq!(e.cursor_position(), 5, "End → end of line");
        e.move_in_direction(Direction::LineStart, false);
        assert_eq!(e.cursor_position(), 0, "Home → line start");
        e.move_in_direction(Direction::DocEnd, false);
        assert_eq!(e.cursor_position(), e.len(), "Ctrl+End → doc end");
        e.move_in_direction(Direction::DocStart, true);
        assert_eq!(
            e.selection_range(),
            Some(0..e.len()),
            "Shift+Ctrl+Home extends"
        );

        e.select_all();
        assert_eq!(e.selection_range(), Some(0..e.len()), "Ctrl+A selects all");

        // Smart space: suppressed at line start, inserted mid-line.
        let mut e = Editor::new("hello\n");
        e.set_cursor(0);
        assert!(!e.try_insert_space(), "space suppressed at line start");
        assert_eq!(e.text(), "hello\n");
        e.set_cursor(3);
        assert!(e.try_insert_space(), "space inserted mid-line");
        assert_eq!(e.text(), "hel lo\n");

        // Blockquote marker auto-spaces after `>`.
        let mut e = Editor::new("");
        e.insert_str(">");
        e.maybe_complete_blockquote_marker();
        assert_eq!(e.text(), "> ", "`>` completes to `> `");

        // Code fence auto-closes after the third backtick.
        let mut e = Editor::new("");
        for _ in 0..3 {
            e.insert_str("`");
            e.maybe_complete_code_fence();
        }
        assert_eq!(
            e.text(),
            "```\n```",
            "triple backtick auto-closes the fence"
        );
        assert!(
            e.cursor_in_code_block(),
            "cursor sits inside the new code block"
        );
        // Tab inside a code block indents with spaces (mirrors the shell's Tab arm),
        // never a stray fence character.
        if e.cursor_in_code_block() {
            e.insert_str("    ");
        }
        assert_eq!(
            e.text(),
            "```    \n```",
            "Tab in code block inserts 4 spaces"
        );

        // Paste routes through transform_paste: CRLF→LF, curly quotes→straight,
        // blockquote continuation. (Regression: shell was inserting raw text.)
        let mut e = Editor::new("> ");
        e.set_cursor(e.len());
        e.paste("line 1\r\nline 2 \u{201C}quoted\u{201D}");
        assert_eq!(
            e.text(),
            "> line 1\n> line 2 \"quoted\"",
            "paste normalizes CRLF, quotes, and continues the blockquote"
        );
    }

    /// Phase 2 DoD: a headless editor opens a doc, applies edits, toggles a
    /// checkbox, detects a GitHub ref, and computes diff state — no gpui.
    #[test]
    fn headless_edit_checkbox_github_diff() {
        // --- edit ops ---
        let mut editor = Editor::new("hello");
        editor.set_cursor(editor.len());
        editor.type_char('!');
        assert_eq!(editor.text(), "hello!");
        editor.backspace();
        assert_eq!(editor.text(), "hello");

        // --- checkbox toggle ---
        let mut editor = Editor::new("- [ ] task\n");
        editor.toggle_checkbox(0);
        assert!(
            editor.text().starts_with("- [x]"),
            "checkbox should be checked: {:?}",
            editor.text()
        );

        // --- GitHub ref detection ---
        let mut editor = Editor::new("See #123 for details\n");
        editor.set_github_context(GitHubContext {
            owner: "wilfreddenton".into(),
            repo: "writ".into(),
        });
        editor.refresh_detection(0..usize::MAX);
        assert!(
            editor.github_refs_by_line().contains_key(&0),
            "should detect #123 on line 0"
        );

        // --- inline diff against HEAD ---
        let mut editor = Editor::new("line1\nline2\n");
        assert!(editor.diff_state().is_none(), "no base yet");
        editor.set_head_base("line1\n");
        assert!(
            editor.diff_state().is_some(),
            "adding line2 vs HEAD should produce diff hunks"
        );
        // Reverting to match HEAD clears the diff.
        editor.set_text("line1\n");
        assert!(editor.diff_state().is_none(), "matching HEAD => no hunks");
    }

    fn ctx() -> GitHubContext {
        GitHubContext {
            owner: "rust-lang".into(),
            repo: "rust".into(),
        }
    }

    fn issue_suggestion(number: u64, title: &str) -> AutocompleteSuggestion {
        AutocompleteSuggestion::IssueOrPr {
            number,
            symbol: "".into(),
            status: IssueStatus::Open,
            title: title.into(),
        }
    }

    #[test]
    fn checkbox_at_detects_box_not_content() {
        let editor = Editor::new("- [ ] task\n");
        let text = editor.text();
        // A hit anywhere on `[ ]` toggles line 0.
        let box_off = text.find('[').unwrap();
        assert_eq!(editor.checkbox_at(box_off), Some(0));
        assert_eq!(editor.checkbox_at(box_off + 1), Some(0));
        // A hit on the content is not a checkbox click.
        assert_eq!(editor.checkbox_at(text.find("task").unwrap()), None);
        // A non-checkbox line has no box.
        let plain = Editor::new("just a paragraph\n");
        assert_eq!(plain.checkbox_at(3), None);
    }

    #[test]
    fn autocomplete_issue_trigger_and_accept() {
        let mut editor = Editor::new("Working on #12\n");
        editor.set_github_context(ctx());
        editor.set_github_client(GitHubClient::new("dummy".into()));
        editor.refresh_detection(0..usize::MAX);
        editor.set_cursor(14); // end of "#12"

        // Cursor inside the detected issue ref opens Issue autocomplete for "12".
        assert!(editor.update_autocomplete_from_cursor());
        let ac = editor.autocomplete().expect("popup open");
        assert_eq!(ac.trigger, AutocompleteTrigger::Issue);
        assert_eq!(ac.prefix, "12");

        // Install suggestions and accept a different one — the ref token is replaced.
        editor.begin_autocomplete_fetch();
        editor.apply_autocomplete_suggestions(
            AutocompleteTrigger::Issue,
            "12",
            vec![issue_suggestion(999, "some issue")],
        );
        assert!(editor.accept_autocomplete_suggestion());
        assert_eq!(editor.text(), "Working on #999\n");
        assert!(editor.autocomplete().is_none(), "popup closes on accept");
    }

    #[test]
    fn detected_refs_in_lines_is_viewport_bounded() {
        let mut editor = Editor::new("See #1 here\n\n\nAnd #2 there\n");
        editor.set_github_context(ctx());
        editor.set_github_client(GitHubClient::new("dummy".into()));
        editor.refresh_detection(0..usize::MAX);

        let numbers = |refs: Vec<GitHubRef>| {
            refs.iter()
                .filter_map(|r| match r {
                    GitHubRef::Issue { number, .. } => Some(*number),
                    _ => None,
                })
                .collect::<Vec<_>>()
        };

        // Line 0 only sees #1, not the line-3 #2.
        let line0 = numbers(editor.detected_refs_in_lines(0..1));
        assert!(line0.contains(&1), "line 0 range should include #1");
        assert!(!line0.contains(&2), "line 0 range should exclude line-3 #2");

        // A wide range covers both refs.
        let all = numbers(editor.detected_refs_in_lines(0..10));
        assert!(
            all.contains(&1) && all.contains(&2),
            "wide range should include both refs"
        );
    }

    #[test]
    fn folding_collapses_reveals_and_survives_edits() {
        let mut editor = Editor::new("# A\nbody1\nbody2\n## B\nsub\n# C\ntail\n");

        // Caret in A's body → folding A collapses lines 1..5 (through the deeper ## B),
        // and relocates the caret onto the heading line so it never sits on a hidden line.
        editor.set_cursor(editor.state.buffer.line_to_byte(1));
        editor.fold_at_cursor();
        assert_eq!(editor.hidden_line_ranges(), vec![1..5]);
        assert_eq!(editor.line_of(editor.cursor_position()), 0);

        // Moving the caret into the folded region auto-reveals it.
        editor.set_cursor(editor.state.buffer.line_to_byte(2));
        assert!(editor.reveal_cursor());
        assert!(editor.hidden_line_ranges().is_empty());

        // Fold the last section, then insert above it: the fold's byte anchor remaps.
        let c_off = editor.state.buffer.headings()[2].byte_offset;
        editor.toggle_fold(c_off);
        assert!(!editor.hidden_line_ranges().is_empty());
        editor.set_cursor(editor.state.buffer.line_to_byte(1));
        editor.insert_str("x");
        let c_off2 = editor.state.buffer.headings()[2].byte_offset;
        assert_eq!(c_off2, c_off + 1);
        assert!(editor.is_heading_folded(c_off2), "fold survived the edit");

        editor.fold_all_headings();
        assert!(!editor.hidden_line_ranges().is_empty());
        editor.unfold_all();
        assert!(editor.hidden_line_ranges().is_empty());
    }

    #[test]
    fn fold_to_level_collapses_that_depth() {
        // # A(0) a(1) ## B(2) b(3) # C(4) c(5)
        let mut editor = Editor::new("# A\na\n## B\nb\n# C\nc\n");

        // Level 1: fold both H1 sections → only the two H1 lines stay visible.
        editor.fold_to_level(1);
        assert_eq!(editor.hidden_line_ranges(), vec![1..4, 5..7]);

        // Level 2: fold only the H2 section; H1 lines and their intro bodies stay visible.
        editor.fold_to_level(2);
        assert_eq!(editor.hidden_line_ranges(), vec![3..4]);

        // Replaces prior folds rather than accumulating; a level with no headings clears.
        editor.fold_to_level(4);
        assert!(editor.hidden_line_ranges().is_empty());
    }

    #[test]
    fn recursive_fold_pre_folds_descendants() {
        // # A(0) a(1) ## B(2) b(3) ### C(4) c(5) # D(6) d(7)
        let mut editor = Editor::new("# A\na\n## B\nb\n### C\nc\n# D\nd\n");
        let a = editor.state.buffer.headings()[0].byte_offset;
        let b = editor.state.buffer.headings()[1].byte_offset;
        let c = editor.state.buffer.headings()[2].byte_offset;

        // Recursively folding A folds A and its descendants B and C (but not sibling D).
        editor.toggle_fold_recursive(a);
        assert!(editor.is_heading_folded(a));
        assert!(editor.is_heading_folded(b));
        assert!(editor.is_heading_folded(c));

        // Unfolding just A (plain toggle) reveals B, which is still folded underneath.
        editor.toggle_fold(a);
        assert!(!editor.is_heading_folded(a));
        assert!(editor.is_heading_folded(b), "descendant stays folded");
        assert_eq!(editor.hidden_line_ranges(), vec![3..6]); // B's subtree still collapsed

        // Recursive toggle again (now A unfolded) folds the whole A subtree back down.
        editor.toggle_fold_recursive(a);
        assert!(editor.is_heading_folded(a) && editor.is_heading_folded(c));
    }

    #[test]
    fn list_folding_task_list_children_survive_checkbox_and_reveal() {
        // A nested task list (the primary fold use case):
        //   0 - [ ] parent   1   - [ ] child1   2   - [ ] child2   3 - [ ] sibling
        let mut editor =
            Editor::new("- [ ] parent\n  - [ ] child1\n  - [ ] child2\n- [ ] sibling\n");
        let items = editor.state.buffer.list_items().to_vec();
        let parent = items
            .iter()
            .find(|i| i.line == 0)
            .expect("parent")
            .byte_offset;
        let sibling = items
            .iter()
            .find(|i| i.line == 3)
            .expect("sibling")
            .byte_offset;

        // Folding the parent hides its two child items; the sibling is untouched.
        editor.toggle_fold(parent);
        assert_eq!(editor.hidden_line_ranges(), vec![1..3]);
        let anchors = editor.fold_anchors();
        assert!(
            anchors
                .iter()
                .any(|a| a.byte_offset == parent && a.kind == fold::FoldKind::List)
        );
        assert!(!anchors.iter().any(|a| a.byte_offset == 999));
        assert!(!editor.is_heading_folded(sibling));

        // The parent checkbox still toggles while its children are folded (checking a task
        // also strikes it through), and the fold offset survives the edit — kids stay hidden.
        editor.toggle_checkbox(0);
        assert_eq!(editor.text().lines().next(), Some("- [x] ~~parent~~"));
        assert_eq!(
            editor.hidden_line_ranges(),
            vec![1..3],
            "fold survived the toggle"
        );

        // Moving the caret into a hidden child auto-reveals the fold.
        editor.set_cursor(editor.state.buffer.line_to_byte(1));
        assert!(editor.reveal_cursor());
        assert!(editor.hidden_line_ranges().is_empty());
    }

    #[test]
    fn list_folding_only_items_with_sublists_are_foldable() {
        // Leaves must NOT be foldable — regression for tree-sitter extending a leaf's
        // range into a trailing blank line (`- c`) or the next sibling's indent (`- c1`).
        //   0 - a  1 - b  2 - c  3 (blank)  4 - p  5   - c1  6   - c2  7 - q
        let mut editor = Editor::new("- a\n- b\n- c\n\n- p\n  - c1\n  - c2\n- q\n");
        let items = editor.state.buffer.list_items().to_vec();
        let foldable = |line: usize| -> bool {
            let idx = items.iter().position(|i| i.line == line).unwrap();
            crate::fold::list_item_is_foldable(&items, idx)
        };
        for leaf in [0, 1, 2, 5, 6, 7] {
            assert!(!foldable(leaf), "line {leaf} is a leaf, must not fold");
        }
        assert!(foldable(4), "`- p` has a sublist, must fold");

        // Folding `- p` hides only its two children (5,6) — not the blank, not `- q`.
        let p = items.iter().find(|i| i.line == 4).unwrap().byte_offset;
        editor.toggle_fold(p);
        assert_eq!(editor.hidden_line_ranges(), vec![5..7]);
    }

    #[test]
    fn click_snaps_out_of_list_marker_prefix() {
        // Line 1 "  - [ ] child": indent 4..6, bullet 6..8, `[` at 8, content 12.
        let mut editor = Editor::new("- p\n  - [ ] child\n");
        // Click in the indent → snaps back to the line start (nearer boundary).
        editor.click(5, false, 1);
        assert_eq!(editor.cursor_position(), 4);
        // Click just after the bullet → snaps forward to the checkbox `[`.
        editor.click(7, false, 1);
        assert_eq!(editor.cursor_position(), 8);
        // The boundaries themselves are reachable; a plain paragraph is untouched.
        editor.click(4, false, 1);
        assert_eq!(editor.cursor_position(), 4);
        editor.click(8, false, 1);
        assert_eq!(editor.cursor_position(), 8);
        let mut para = Editor::new("hello world\n");
        para.click(3, false, 1);
        assert_eq!(para.cursor_position(), 3);
    }

    #[test]
    fn list_folding_ctrl_click_folds_all_at_depth() {
        // Two top-level items with sublists, plus a deeper level:
        //   0 - p   1   - a   2     - x   3   - b   4 - q   5   - c
        let mut editor = Editor::new("- p\n  - a\n    - x\n  - b\n- q\n  - c\n");
        let items = editor.state.buffer.list_items().to_vec();
        let off = |line: usize| items.iter().find(|i| i.line == line).unwrap().byte_offset;

        // Ctrl+click a top-level (depth-1) item folds BOTH top-level foldable items (p, q),
        // not the deeper `- a`. Additive; doesn't touch depth-2.
        editor.apply_fold_gesture(off(0), true, false);
        assert!(editor.is_heading_folded(off(0)) && editor.is_heading_folded(off(4)));
        assert!(
            !editor.is_heading_folded(off(1)),
            "depth-2 item `- a` untouched"
        );

        // Toggling the same group off clears it.
        editor.apply_fold_gesture(off(0), true, false);
        assert!(editor.hidden_line_ranges().is_empty());

        // Deep variant folds this depth and deeper: p, q (depth 1) AND a (depth 2).
        editor.apply_fold_gesture(off(0), true, true);
        assert!(
            editor.is_heading_folded(off(0))
                && editor.is_heading_folded(off(4))
                && editor.is_heading_folded(off(1))
        );
    }

    #[test]
    fn list_folding_recursive_and_survives_edit() {
        // 3-deep nested list: 0 a  1  b  2   c  3 d(sibling)
        let mut editor = Editor::new("- a\n  - b\n    - c\n- d\n");
        let items = editor.state.buffer.list_items().to_vec();
        let a = items.iter().find(|i| i.line == 0).unwrap().byte_offset;
        let b = items.iter().find(|i| i.line == 1).unwrap().byte_offset;

        // Recursive fold on the root folds the whole subtree (a's descendant b too).
        editor.toggle_fold_recursive(a);
        assert!(editor.is_heading_folded(a) && editor.is_heading_folded(b));

        // Plain-unfolding a reveals b, which stays folded underneath.
        editor.toggle_fold(a);
        assert!(editor.is_heading_folded(b));
        assert_eq!(editor.hidden_line_ranges(), vec![2..3]); // c still hidden under b

        // Fold survives an edit above it: insert a new first line, offsets remap.
        editor.toggle_fold_recursive(a);
        editor.set_cursor(0);
        editor.insert_str("- z\n");
        let b2 = editor
            .state
            .buffer
            .list_items()
            .iter()
            .find(|i| i.line == 2)
            .unwrap()
            .byte_offset;
        assert!(
            editor.is_heading_folded(b2),
            "fold tracked the shifted item"
        );
    }

    #[test]
    fn ctrl_click_folds_all_at_level() {
        // # A(0) a(1) ## B(2) b(3) # C(4) c(5) ## D(6) d(7)
        let mut editor = Editor::new("# A\na\n## B\nb\n# C\nc\n## D\nd\n");
        let a = editor.state.buffer.headings()[0].byte_offset;
        let b = editor.state.buffer.headings()[1].byte_offset;
        let c = editor.state.buffer.headings()[2].byte_offset;
        let d = editor.state.buffer.headings()[3].byte_offset;

        // Ctrl+clicking the H2 "B" folds every H2 (B and D), not just B.
        editor.apply_fold_gesture(b, true, false);
        assert!(editor.is_heading_folded(b) && editor.is_heading_folded(d));
        assert!(!editor.is_heading_folded(a) && !editor.is_heading_folded(c));

        // Ctrl+clicking a now-folded heading unfolds everything.
        editor.apply_fold_gesture(b, true, false);
        assert!(editor.hidden_line_ranges().is_empty());
    }

    #[test]
    fn ctrl_shift_click_folds_level_and_deeper() {
        // # A(0) ## B(1) ### C(2) c(3) # D(4)
        let mut editor = Editor::new("# A\n## B\n### C\nc\n# D\n");
        let a = editor.state.buffer.headings()[0].byte_offset;
        let b = editor.state.buffer.headings()[1].byte_offset;
        let c = editor.state.buffer.headings()[2].byte_offset;

        // Ctrl+Shift on the H2 folds this level and deeper: B and C, but not the H1s.
        editor.apply_fold_gesture(b, true, true);
        assert!(editor.is_heading_folded(b) && editor.is_heading_folded(c));
        assert!(!editor.is_heading_folded(a));

        // Toggles off when the clicked heading is already folded.
        editor.apply_fold_gesture(b, true, true);
        assert!(editor.hidden_line_ranges().is_empty());
    }

    #[test]
    fn autocomplete_user_trigger() {
        let mut editor = Editor::new("cc @tor\n");
        editor.set_github_context(ctx());
        editor.set_github_client(GitHubClient::new("dummy".into()));
        editor.refresh_detection(0..usize::MAX);
        editor.set_cursor(7); // end of "@tor"

        assert!(editor.update_autocomplete_from_cursor());
        let ac = editor.autocomplete().expect("popup open");
        assert_eq!(ac.trigger, AutocompleteTrigger::User);
        assert_eq!(ac.prefix, "tor");
    }

    #[test]
    fn autocomplete_needs_client_and_context() {
        // Heading `# ` is not an issue trigger, and no client ⇒ never opens.
        let mut editor = Editor::new("# heading\n");
        editor.set_github_context(ctx());
        editor.set_cursor(2);
        assert!(!editor.update_autocomplete_from_cursor());
        assert!(editor.autocomplete().is_none());
    }

    #[test]
    fn save_and_reload_roundtrip() {
        let dir = std::env::temp_dir().join(format!("writ-core-test-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("doc.md");
        std::fs::write(&path, "original\n").unwrap();

        let mut editor = Editor::open(&path);
        assert_eq!(editor.text(), "original\n");
        editor.set_cursor(editor.len());
        editor.insert_str("more\n");
        editor.save().unwrap();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "original\nmore\n");
        assert!(!editor.is_dirty(), "saved buffer is clean");

        std::fs::remove_dir_all(&dir).ok();
    }

    /// Type `query` into the open find bar's search field and rescan.
    fn set_query(editor: &mut Editor, query: &str) {
        editor.find_state_mut().unwrap().search.set_text(query);
        editor.find_rescan();
    }

    #[test]
    fn find_literal_and_case_toggle() {
        let mut e = Editor::new("Cat cat CAT cot\n");
        e.set_cursor(0);
        e.open_find(false);

        // Case-insensitive (default): all three "cat"s match, not "cot".
        set_query(&mut e, "cat");
        assert_eq!(e.find_state().unwrap().matches.len(), 3);

        // Toggling case-sensitive drops "Cat" and "CAT", leaving only "cat".
        e.find_toggle_case();
        assert_eq!(e.find_state().unwrap().matches.len(), 1);
    }

    #[test]
    fn find_regex_mode_and_invalid_pattern() {
        let mut e = Editor::new("a table, a tible, a topple\n");
        e.set_cursor(0);
        e.open_find(false);
        e.find_toggle_regex();

        set_query(&mut e, "t.ble");
        assert_eq!(
            e.find_state().unwrap().matches.len(),
            2,
            "t.ble matches table and tible"
        );

        // A half-typed pattern must not panic — it just yields no matches.
        set_query(&mut e, "(");
        assert!(e.find_state().unwrap().matches.is_empty());
        assert!(e.find_state().unwrap().active.is_none());
    }

    #[test]
    fn find_adjacent_matches_are_non_overlapping() {
        let mut e = Editor::new("aaaa\n");
        e.set_cursor(0);
        e.open_find(false);
        set_query(&mut e, "aa");
        // Non-overlapping left-to-right: [0..2, 2..4], not 3.
        assert_eq!(e.find_state().unwrap().matches.len(), 2);
    }

    #[test]
    fn find_next_cycles_and_sets_selection() {
        let mut e = Editor::new("x . x . x\n");
        e.set_cursor(0);
        e.open_find(false);
        set_query(&mut e, "x");
        let m = &e.find_state().unwrap().matches;
        assert_eq!(m.len(), 3);
        let ranges: Vec<_> = m.clone();

        // active starts at the first match at/after the caret (offset 0).
        assert_eq!(e.find_state().unwrap().active, Some(0));
        assert_eq!(e.selection_range(), Some(ranges[0].clone()));

        assert_eq!(e.find_next(), Some(ranges[1].clone()));
        assert_eq!(e.find_state().unwrap().active, Some(1));
        assert_eq!(e.selection_range(), Some(ranges[1].clone()));

        assert_eq!(e.find_next(), Some(ranges[2].clone()));
        assert_eq!(e.find_next(), Some(ranges[0].clone()), "wraps to start");
        assert_eq!(e.find_state().unwrap().active, Some(0));

        assert_eq!(e.find_prev(), Some(ranges[2].clone()), "prev wraps back");
    }

    #[test]
    fn find_active_picks_match_at_or_after_caret() {
        // Caret just past the second "x": active is the third match (first at/after caret).
        let mut e = Editor::new("x . x . x\n");
        let third = e.text().rfind('x').unwrap();
        e.set_cursor(e.text()[..third].rfind('x').unwrap() + 1);
        e.open_find(false);
        set_query(&mut e, "x");
        assert_eq!(e.find_state().unwrap().active, Some(2));

        // Caret past every match wraps active back to the first.
        let mut e = Editor::new("x . x . x\n");
        e.set_cursor(e.len());
        e.open_find(false);
        set_query(&mut e, "x");
        assert_eq!(
            e.find_state().unwrap().active,
            Some(0),
            "wraps to first match"
        );
    }

    fn set_replace(editor: &mut Editor, replacement: &str) {
        editor
            .find_state_mut()
            .unwrap()
            .replace
            .set_text(replacement);
    }

    #[test]
    fn find_replace_current_literal_advances() {
        let mut e = Editor::new("cat cat cat\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "cat");
        set_replace(&mut e, "dog");
        assert_eq!(e.find_state().unwrap().active, Some(0));

        e.find_replace_current();
        assert_eq!(e.text(), "dog cat cat\n");
        // Rescan finds the remaining two "cat"s and lands on the next one.
        assert_eq!(e.find_state().unwrap().matches.len(), 2);
        assert_eq!(e.find_state().unwrap().active, Some(0));
        assert_eq!(e.selection_range(), Some(4..7));
    }

    #[test]
    fn find_replace_current_noop_without_active() {
        let mut e = Editor::new("hello\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "zzz");
        set_replace(&mut e, "x");
        assert!(e.find_state().unwrap().active.is_none());
        e.find_replace_current();
        assert_eq!(e.text(), "hello\n");
    }

    #[test]
    fn find_replace_all_literal_longer_and_shorter() {
        // Longer replacement.
        let mut e = Editor::new("a a a\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "a");
        set_replace(&mut e, "bb");
        e.find_replace_all();
        assert_eq!(e.text(), "bb bb bb\n");
        assert!(e.find_state().unwrap().matches.is_empty());

        // Shorter replacement.
        let mut e = Editor::new("aa aa\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "aa");
        set_replace(&mut e, "x");
        e.find_replace_all();
        assert_eq!(e.text(), "x x\n");
    }

    #[test]
    fn find_replace_current_regex_expands_capture() {
        let mut e = Editor::new("user_id and role_id\n");
        e.set_cursor(0);
        e.open_find(true);
        e.find_toggle_regex();
        set_query(&mut e, r"(\w+)_id");
        set_replace(&mut e, "${1}Id");
        e.find_replace_current();
        assert_eq!(e.text(), "userId and role_id\n");
    }

    #[test]
    fn find_replace_all_regex_expands_every_capture() {
        let mut e = Editor::new("user_id and role_id\n");
        e.set_cursor(0);
        e.open_find(true);
        e.find_toggle_regex();
        set_query(&mut e, r"(\w+)_id");
        set_replace(&mut e, "${1}Id");
        e.find_replace_all();
        assert_eq!(e.text(), "userId and roleId\n");
    }

    #[test]
    fn find_replace_current_replacement_containing_query_no_loop() {
        let mut e = Editor::new("cat cat\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "cat");
        set_replace(&mut e, "cat!");
        e.find_replace_current();
        assert_eq!(e.text(), "cat! cat\n");
        // The occurrence inside the just-inserted replacement is skipped; active is the
        // next real match past the caret, so repeated replace can't spin forever.
        assert_eq!(e.find_state().unwrap().matches.len(), 2);
        assert_eq!(e.find_state().unwrap().active, Some(1));
        assert_eq!(e.selection_range(), Some(5..8));
    }

    #[test]
    fn find_replace_all_is_single_undo_step() {
        let mut e = Editor::new("a a a\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "a");
        set_replace(&mut e, "bb");
        e.find_replace_all();
        assert_eq!(e.text(), "bb bb bb\n");
        e.undo();
        assert_eq!(
            e.text(),
            "a a a\n",
            "one undo reverts the whole replace-all"
        );
    }

    #[test]
    fn callout_folds_and_seeds_default_collapsed() {
        // line 0-2: foldable note; 3: blank; 4-5: default-collapsed warning.
        let doc = "> [!note]+ Foldable\n> body one\n> body two\n\n> [!warning]- Closed\n> hidden\n";
        let mut e = Editor::new(doc);
        let cs = e.state.buffer.callouts().to_vec();
        assert_eq!(cs.len(), 2);
        // `new()` doesn't seed, so nothing is folded yet.
        assert!(e.hidden_line_ranges().is_empty());

        // The gesture folds the foldable callout's body, and toggles it back.
        let note_off = cs[0].byte_offset;
        e.apply_fold_gesture(note_off, false, false);
        assert_eq!(e.hidden_line_ranges(), vec![1..3]);
        e.apply_fold_gesture(note_off, false, false);
        assert!(e.hidden_line_ranges().is_empty());

        // Seeding collapses only the `-` callout.
        e.seed_default_collapsed_callouts();
        assert_eq!(e.hidden_line_ranges(), vec![5..6]);
    }

    #[test]
    fn callout_recursive_and_level_gestures() {
        // Outer tip (depth 1, lines 0-2) with a nested note (depth 2, lines 1-2).
        let doc = "> [!tip]+ Outer\n> > [!note]+ Inner\n> > inner body\n";
        let mut e = Editor::new(doc);
        let cs = e.state.buffer.callouts().to_vec();
        assert_eq!(cs.len(), 2);
        let outer = cs.iter().find(|c| c.header_line == 0).unwrap().byte_offset;
        let inner = cs.iter().find(|c| c.header_line == 1).unwrap().byte_offset;

        // Shift-click (recursive) the outer folds it AND the nested inner.
        e.apply_fold_gesture(outer, false, true);
        assert!(e.is_heading_folded(outer) && e.is_heading_folded(inner));
        assert_eq!(e.hidden_line_ranges(), vec![1..3]);
        // Toggling recursively again unfolds both.
        e.apply_fold_gesture(outer, false, true);
        assert!(e.hidden_line_ranges().is_empty());

        // Ctrl-click (level) the inner folds only depth-2 callouts (not the outer).
        e.apply_fold_gesture(inner, true, false);
        assert!(e.is_heading_folded(inner) && !e.is_heading_folded(outer));
        assert_eq!(e.hidden_line_ranges(), vec![2..3]);
    }

    #[test]
    fn folds_persist_and_remap_across_reload() {
        let mut e = Editor::new("# One\nbody\n## Two\nmore\n");
        let two = e.text().find("## Two").unwrap();
        e.toggle_fold(two);
        assert!(e.folded_headings.contains(&two));
        // An external edit prepends a line; the reload should shift the fold offset so it
        // still lands on the (moved) heading rather than pointing at stale bytes.
        let prefix = "intro line\n";
        e.apply_reload(format!("{prefix}# One\nbody\n## Two\nmore\n"), None);
        let two_after = e.text().find("## Two").unwrap();
        assert_eq!(two_after, two + prefix.len());
        assert!(
            e.folded_headings.contains(&two_after),
            "fold should remap to the heading's new offset after reload"
        );
    }

    #[test]
    fn find_replace_current_is_single_undo_step() {
        let mut e = Editor::new("a a a\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "a");
        set_replace(&mut e, "bb");
        e.find_replace_current();
        assert_eq!(e.text(), "bb a a\n");
        e.undo();
        assert_eq!(e.text(), "a a a\n", "one undo reverts a single replace");
    }

    #[test]
    fn open_find_focuses_the_bar() {
        let mut e = Editor::new("hi\n");
        e.open_find(false);
        assert!(e.find_state().unwrap().focused, "opens focused");

        // Simulate a document click unfocusing the bar; re-opening (Ctrl+F) refocuses it.
        e.find_state_mut().unwrap().focused = false;
        e.open_find(false);
        assert!(e.find_state().unwrap().focused, "re-opening refocuses");
    }

    #[test]
    fn rescan_while_unfocused_does_not_move_selection() {
        let mut e = Editor::new("cat cat cat\n");
        e.set_cursor(0);
        e.open_find(false);
        set_query(&mut e, "cat");
        // Focused: the active match owns the document selection.
        assert_eq!(e.selection_range(), Some(0..3));

        // Unfocus the bar (as a document click does) and edit the buffer in the doc.
        e.find_state_mut().unwrap().focused = false;
        e.set_cursor(9);
        e.insert_str("X");
        e.find_rescan();

        // Highlights track the edit, but the caret stays where the user typed — the rescan
        // must not yank the selection onto a match.
        assert_eq!(e.find_state().unwrap().matches.len(), 2);
        assert!(
            e.selection_range().is_none(),
            "unfocused rescan leaves the doc caret alone"
        );
    }

    #[test]
    fn undo_of_replace_restores_text_and_rescan_refreshes_matches() {
        let mut e = Editor::new("cat cat cat\n");
        e.set_cursor(0);
        e.open_find(true);
        set_query(&mut e, "cat");
        set_replace(&mut e, "dog");
        e.find_replace_all();
        assert_eq!(e.text(), "dog dog dog\n");
        assert!(e.find_state().unwrap().matches.is_empty());

        // The find-bar undo passthrough: undo the replace, then rescan the stale matches.
        e.undo();
        e.find_rescan();
        assert_eq!(e.text(), "cat cat cat\n");
        assert_eq!(
            e.find_state().unwrap().matches.len(),
            3,
            "rescan after undo restores the full match set"
        );
    }
}