writ 0.14.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
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
//! Document viewport: per-line Parley layouts stacked by a prefix-sum height
//! model, plus scroll (see MIGRATION-PLAN.md, Phase 3). Replaces gpui's `ListState`
//! (which gave virtualization + scroll-to-reveal for free) with hand-rolled math.
//!
//! The height model is the top defect surface the plan flags: `tops` has length
//! `n + 1` where `tops[i]` is the top y of line `i` and `tops[n]` is the bottom of
//! the last line. One off-by-one here misplaces everything below it, so the pure
//! prefix-sum + visible-range functions are unit-tested independent of any GPU.

use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
use std::ops::Range;
use std::rc::Rc;

use parley::{Affinity, Cluster, Cursor, PositionedLayoutItem};
use ropey::Rope;
use vello::Scene;
use vello::kurbo::{Affine, Rect, Stroke};
use vello::peniko::{Brush, Color, Fill, ImageBrush};

use crate::buffer::RenderSnapshot;
use crate::consts::{MAX_CONTENT_WIDTH, UI_LINE_HEIGHT};
use crate::diff::{DiffState, InlineChange};
use crate::editor::EditorTheme;
use crate::image_cache::{ImageCache, ImageState};
use crate::inline::{
    GitHubContext, NakedUrl, RawGitHubMatch, StyledRegion, github_refs_to_styled_regions,
    naked_urls_to_styled_regions,
};
use crate::render::{
    ImageRef, InlineImageRef, LineRender, TableCtx, build_cell_render, build_line_render,
};
use crate::segment_map::SegmentMap;
use crate::table::{Align, RowKind, TableInfo, TableRow};
use crate::text_engine::{
    StyleRun, TextEngine, display_range_selection, peniko_color, peniko_color_alpha,
};
use crate::validation::GitHubValidationCache;

/// A screen-space rectangle (device px), already offset by padding + scroll.
pub type ScreenRect = (f64, f64, f64, f64);

/// A borrowed view of the in-progress IME composition for `DocLayout::build`: the
/// composing `text` and its caret/selection `cursor` (byte offsets *within* `text`,
/// `None` = hidden caret). Spliced into the caret line at render time and drawn
/// underlined; never mutates the buffer.
pub struct PreeditView<'a> {
    pub text: &'a str,
    pub cursor: Option<(usize, usize)>,
}

/// GitHub autolink data threaded into layout so validated refs render as colored,
/// possibly-shortened links. Borrowed from `core::Editor` for the build call.
pub struct GithubRenderData<'a> {
    pub refs_by_line: &'a HashMap<usize, Vec<RawGitHubMatch>>,
    pub urls_by_line: &'a HashMap<usize, Vec<NakedUrl>>,
    pub cache: &'a GitHubValidationCache,
    pub context: Option<&'a GitHubContext>,
}

impl GithubRenderData<'_> {
    /// The extra styled regions (validated refs + naked URLs) for one line.
    fn extra_regions(&self, line: usize) -> Vec<StyledRegion> {
        let mut v = self
            .refs_by_line
            .get(&line)
            .map(|m| github_refs_to_styled_regions(m, self.cache))
            .unwrap_or_default();
        if let Some(urls) = self.urls_by_line.get(&line) {
            v.extend(naked_urls_to_styled_regions(urls, self.cache, self.context));
        }
        v
    }
}

/// Content hash identifying a laid-out line. Two lines with the same key produce
/// byte-identical Parley layouts, so a cache can skip re-shaping.
fn line_key(
    text: &str,
    scale: f32,
    font_size: f32,
    line_height: f32,
    max_advance: f32,
    runs: &[StyleRun],
    content_start: usize,
) -> u64 {
    let mut h = std::collections::hash_map::DefaultHasher::new();
    text.hash(&mut h);
    scale.to_bits().hash(&mut h);
    font_size.to_bits().hash(&mut h);
    line_height.to_bits().hash(&mut h);
    max_advance.to_bits().hash(&mut h);
    // Same display text can have different content-start columns (real bullet vs a
    // literally-typed "• "), which change the hanging indent — so key on it.
    content_start.hash(&mut h);
    for r in runs {
        r.range.start.hash(&mut h);
        r.range.end.hash(&mut h);
        for c in r.color.components {
            c.to_bits().hash(&mut h);
        }
        (r.bold, r.italic, r.mono, r.underline, r.strikethrough).hash(&mut h);
    }
    h.finish()
}

/// A swept, content-addressed cache shared by the shell across rebuilds: values
/// keyed by a `u64` content hash and reset each frame via `begin`/`sweep`. `begin`
/// marks a new frame; every `get`/`set`/`get_or_build` records its key as used; and
/// `sweep` drops entries not touched that frame, so the cache stays bounded to the
/// current document (avoiding re-shaping unchanged lines every keystroke). `V` is
/// `Rc`-wrapped for the layout/render caches, so a hit is a refcount bump rather than
/// a deep copy of shaped glyphs or a `LineRender`.
pub struct SweptCache<V> {
    map: HashMap<u64, V>,
    used: HashSet<u64>,
}

// Manual `Default` (not derived) so it doesn't require `V: Default` — the maps are
// always default-constructible regardless of the value type.
impl<V> Default for SweptCache<V> {
    fn default() -> Self {
        Self {
            map: HashMap::new(),
            used: HashSet::new(),
        }
    }
}

impl<V: Clone> SweptCache<V> {
    pub fn new() -> Self {
        Self::default()
    }

    fn begin(&mut self) {
        self.used.clear();
    }

    fn get(&mut self, key: u64) -> Option<V> {
        let v = self.map.get(&key).cloned();
        if v.is_some() {
            self.used.insert(key);
        }
        v
    }

    fn get_or_build(&mut self, key: u64, build: impl FnOnce() -> V) -> V {
        self.used.insert(key);
        self.map.entry(key).or_insert_with(build).clone()
    }

    fn set(&mut self, key: u64, value: V) {
        self.used.insert(key);
        self.map.insert(key, value);
    }

    fn sweep(&mut self) {
        let used = &self.used;
        self.map.retain(|k, _| used.contains(k));
    }
}

/// Per-line Parley layout cache: skips re-shaping lines whose content + style is
/// unchanged.
pub type LineCache = SweptCache<Rc<parley::Layout<Brush>>>;

/// Key for the per-line render cache. Within one buffer `version`, line `line_idx`
/// has fixed text + tree context, so its `LineRender` depends only on whether the
/// cursor sits on it (`cursor_key` = the offset when on-line, else a sentinel — an
/// off-line render is cursor-independent). Lines carrying GitHub `extra_regions`
/// bypass the cache (those can change without a version bump, on validation).
fn render_key(version: u64, line_idx: usize, cursor_key: usize) -> u64 {
    let mut h = std::collections::hash_map::DefaultHasher::new();
    version.hash(&mut h);
    line_idx.hash(&mut h);
    cursor_key.hash(&mut h);
    h.finish()
}

/// Render-key cursor sentinel for a table row when the cursor is inside the table block
/// but on a *different* row: all such rows share one reveal state (distinct from grid's
/// `usize::MAX` and from an on-row cursor offset, which is a real, smaller position).
const REVEAL_ELSEWHERE: usize = usize::MAX - 1;

/// The cursor's contribution to a line's render: the offset if the cursor is on the
/// line (markers/regions there stay revealed), else a sentinel. Matches the on-line
/// test in `build_line_render`.
fn cursor_key_for(range: &Range<usize>, cursor: usize) -> usize {
    let on = if range.start == range.end {
        cursor == range.start
    } else {
        cursor >= range.start && cursor <= range.end
    };
    if on { cursor } else { usize::MAX }
}

/// Per-line `LineRender` cache: skips the tree-sitter style queries + segment-map
/// build for lines whose render is unchanged — so cursor moves, scroll, and
/// async-validation rebuilds (which don't bump the buffer version) recompute only the
/// handful of lines that changed.
pub type RenderCache = SweptCache<Rc<LineRender>>;

/// Per-line measured *height* cache (device px, the real line only — ghost blocks are
/// added separately). Keyed by the same `render_key` as the render cache, so it holds
/// exact heights for lines materialized on any recent frame; un-materialized (head/tail
/// virtualized) lines read their height from here when present, falling back to the
/// char-count estimate. This is what lets the layout drop to O(visible) without the
/// scroll positions of already-seen regions drifting.
pub type HeightCache = SweptCache<f32>;

/// One shaped, wrapped table cell: its layout (drawing) plus the display↔buffer
/// `SegmentMap` and empty-cell caret landing, both used to hit-test a click inside the
/// cell. `None` for a ragged/missing cell.
struct CellSlot {
    layout: Rc<parley::Layout<Brush>>,
    map: SegmentMap,
    /// For an empty cell, the caret offset just inside the opening pipe (so a click lands
    /// off the pipe); `None` for a non-empty cell, which hit-tests via `layout`/`map`.
    empty_landing: Option<usize>,
    /// Display-byte ranges of the cell's inline-code spans, used to paint the translucent
    /// chip behind the code glyphs (mirrors body lines' `code_ranges`).
    code_ranges: Vec<Range<usize>>,
}
type CellLayout = Option<CellSlot>;
type GridLayouts = Vec<Vec<CellLayout>>;
/// One measured table cell (`None` for a missing cell): display text + style runs, the
/// cell's `SegmentMap`, and its empty-cell landing — carried from the column pre-pass to
/// the shaping pass. A full grid of them.
struct MeasuredCell {
    text: String,
    runs: Vec<StyleRun>,
    map: SegmentMap,
    empty_landing: Option<usize>,
    code_ranges: Vec<Range<usize>>,
}
type CellData = Option<MeasuredCell>;
type GridCells = Vec<Vec<CellData>>;

/// Caret landing for a click on a table cell: its content start for a non-empty cell,
/// else a spot one space in past the opening `|` (a trimmed-empty cell's content range
/// collapses onto the closing-pipe boundary, so `content.start` would sit on that pipe).
fn cell_caret_landing(rope: &Rope, content: &Range<usize>) -> usize {
    if content.start < content.end {
        return content.start;
    }
    let closing = content.start;
    let mut open = closing;
    while open > 0 && rope.get_byte(open - 1) != Some(b'|') {
        open -= 1;
    }
    (open + 1).min(closing.saturating_sub(1)).max(open)
}

/// A grid-rendered table's geometry + shaped cells, cached once per edit and shared
/// (via `Rc`) by every row of the table. `col_x`/`col_w` are device px from the line
/// origin (before `pad_x`); `row_heights[0]` is the header, `row_heights[1 + i]` the
/// i-th body row; `row_layouts` is parallel (one shaped, wrapped cell layout per column,
/// `None` for a ragged/missing cell).
pub struct TableLayout {
    /// Left edge (device px, from the line origin) of each column's cell box.
    col_x: Vec<f32>,
    /// Content width (device px) available to each column's text (inside cell padding).
    col_w: Vec<f32>,
    aligns: Vec<Align>,
    /// Header then body row heights (device px), including vertical cell padding.
    row_heights: Vec<f32>,
    /// Grid height (device px) of the delimiter buffer-line. Zero: the delimiter row is
    /// not drawn in grid mode (the header's background fill already sets it apart), so the
    /// header sits directly above the first body row with no gap.
    delim_height: f32,
    /// Shaped cell layouts, parallel to `row_heights` rows × columns.
    row_layouts: GridLayouts,
}

impl TableLayout {
    /// Row index into `row_heights`/`row_layouts` for a header/body kind (`None` for the
    /// delimiter, which has no cells).
    fn row_index(kind: RowKind) -> Option<usize> {
        match kind {
            RowKind::Header => Some(0),
            RowKind::Body(i) => Some(i + 1),
            RowKind::Delimiter => None,
        }
    }

    /// Height (device px) of the line playing `kind` in this table.
    fn height(&self, kind: RowKind) -> f32 {
        match Self::row_index(kind) {
            Some(r) => self.row_heights.get(r).copied().unwrap_or(0.0),
            None => self.delim_height,
        }
    }

    /// Map a click at cell-local X `lx` (device px from the line origin, i.e.
    /// `click_x - pad_x`) on the grid row playing `kind` to a buffer offset inside the
    /// clicked cell. Picks the column by `col_x` boundaries (clamping to the grid on
    /// either side), then hit-tests within the cell's own layout → display offset →
    /// buffer offset via its `SegmentMap`; an empty cell lands just inside its opening
    /// pipe. Returns `None` for the (zero-height, caret-free) delimiter row.
    fn hit_test(&self, kind: RowKind, lx: f32, cell_pad_x: f32) -> Option<usize> {
        let row_idx = Self::row_index(kind)?;
        let row = self.row_layouts.get(row_idx)?;
        let cols = self.col_x.len();
        if cols == 0 {
            return None;
        }
        let mut col = self
            .col_x
            .partition_point(|&cx| cx <= lx)
            .saturating_sub(1)
            .min(cols - 1);
        // Ragged row: clamp down to the last cell actually present in this row.
        while col > 0 && row.get(col).map(|c| c.is_none()).unwrap_or(true) {
            col -= 1;
        }
        let slot = row.get(col)?.as_ref()?;
        if let Some(landing) = slot.empty_landing {
            return Some(landing);
        }
        let align = self.aligns.get(col).copied().unwrap_or(Align::Left);
        let extra = (self.col_w[col] - slot.layout.width()).max(0.0);
        let shift = match align {
            Align::Left => 0.0,
            Align::Right => extra,
            Align::Center => extra / 2.0,
        };
        let local_x = (lx - (self.col_x[col] + cell_pad_x + shift)).max(0.0);
        let display_off = Cursor::from_point(&slot.layout, local_x, 0.0).index();
        Some(slot.map.display_to_buffer(display_off))
    }
}

/// Per-table grid-layout cache: keyed on `(version, block start, scale, font, width)`,
/// swept each frame like the other caches. `Rc`-wrapped so every row of one table shares
/// one build and a cache hit is a refcount bump.
pub type TableCache = SweptCache<Rc<TableLayout>>;

/// Horizontal / vertical padding (logical px) inside a table cell.
const TABLE_CELL_PAD_X: f32 = 8.0;
const TABLE_CELL_PAD_Y: f32 = 4.0;
/// Cell border width (logical px).
const TABLE_BORDER: f32 = 1.0;
/// A single column may grow to this multiple of its equal share of the width before it
/// is capped (wider content then wraps within the column).
const TABLE_COL_CAP_FACTOR: f32 = 2.5;
/// Grid-render only tables under these limits; larger tables fall through to raw pipe
/// text so the O(cells) pre-pass + cache can't blow up on pathological input.
const TABLE_MAX_BODY_ROWS: usize = 200;
const TABLE_MAX_CELLS: usize = 1000;

/// Whether a table is small enough to grid-render (else it renders as raw pipe text).
fn table_grid_ok(t: &TableInfo) -> bool {
    t.body.len() <= TABLE_MAX_BODY_ROWS
        && (t.body.len() + 1).saturating_mul(t.ncols.max(1)) <= TABLE_MAX_CELLS
}

/// Content hash keying a `TableLayout`: same text + width + font ⇒ identical grid.
fn table_key(
    version: u64,
    block_start: usize,
    scale: f32,
    font_size: f32,
    max_advance: f32,
) -> u64 {
    let mut h = std::collections::hash_map::DefaultHasher::new();
    version.hash(&mut h);
    block_start.hash(&mut h);
    scale.to_bits().hash(&mut h);
    font_size.to_bits().hash(&mut h);
    max_advance.to_bits().hash(&mut h);
    h.finish()
}

/// Measure a table's column widths (a per-cell pre-pass), then re-lay each cell wrapped
/// to its column to get row heights, returning the full `TableLayout`. Columns are capped
/// at a multiple of their equal share and, if the total still overflows `max_advance`,
/// scaled down proportionally so the grid always fits the content width.
fn build_table_layout(
    engine: &mut TextEngine,
    snapshot: &RenderSnapshot,
    theme: &EditorTheme,
    table: &TableInfo,
    line_styles: &[Vec<StyledRegion>],
    params: &LayoutParams,
    max_advance: f32,
) -> TableLayout {
    let LayoutParams {
        scale,
        base_font_size,
        line_height,
        fg,
        ..
    } = *params;
    let ncols = table.ncols.max(1);
    let cell_pad_x = TABLE_CELL_PAD_X * scale;
    let cell_pad_y = TABLE_CELL_PAD_Y * scale;
    let border = TABLE_BORDER * scale;
    let n_lines = snapshot.line_count();

    // Build every cell's (text, runs) once — header cells get a bold base run under
    // their content colors. Cells absent (ragged rows) stay `None`.
    let rows: Vec<&TableRow> = std::iter::once(&table.header)
        .chain(table.body.iter())
        .collect();
    let mut grid: GridCells = Vec::with_capacity(rows.len());
    let mut natural = vec![0f32; ncols];
    for (r, row) in rows.iter().enumerate() {
        let line_idx = snapshot
            .rope
            .byte_to_line(row.line.start)
            .min(n_lines.saturating_sub(1));
        let empty: Vec<StyledRegion> = Vec::new();
        let styles = line_styles.get(line_idx).unwrap_or(&empty);
        let is_header = r == 0;
        let mut rowdata = Vec::with_capacity(ncols);
        // Ragged rows have fewer cells than `ncols`, so index rather than iterate cells.
        #[allow(clippy::needless_range_loop)]
        for c in 0..ncols {
            match row.cells.get(c) {
                Some(cell) => {
                    let (text, mut runs, map, code_ranges) =
                        build_cell_render(snapshot, theme, cell.content.clone(), styles);
                    if is_header && !text.is_empty() {
                        // Bold the whole header cell; content color runs still win their spans.
                        let mut base = StyleRun::new(0..text.len(), fg);
                        base.bold = true;
                        runs.insert(0, base);
                    }
                    let w = engine
                        .build_line(&text, scale, base_font_size, line_height, fg, None, &runs)
                        .width();
                    natural[c] = natural[c].max(w);
                    let empty_landing = (cell.content.start >= cell.content.end)
                        .then(|| cell_caret_landing(&snapshot.rope, &cell.content));
                    rowdata.push(Some(MeasuredCell {
                        text,
                        runs,
                        map,
                        empty_landing,
                        code_ranges,
                    }));
                }
                None => rowdata.push(None),
            }
        }
        grid.push(rowdata);
    }

    // Column widths: cap each at a multiple of the equal share, then scale the set down
    // if the total (plus per-cell padding + borders) still overflows the content width.
    let overhead = ncols as f32 * 2.0 * cell_pad_x + (ncols as f32 + 1.0) * border;
    let avail = (max_advance - overhead).max(1.0);
    let col_cap = (avail / ncols as f32) * TABLE_COL_CAP_FACTOR;
    let mut col_w: Vec<f32> = natural.iter().map(|&w| w.min(col_cap).max(1.0)).collect();
    let sum: f32 = col_w.iter().sum();
    if sum > avail {
        let s = avail / sum;
        for w in &mut col_w {
            *w *= s;
        }
    }

    // Column x-origins (prefix sum of box widths + border).
    let mut col_x = Vec::with_capacity(ncols);
    let mut x = border;
    for &w in &col_w {
        col_x.push(x);
        x += w + 2.0 * cell_pad_x + border;
    }

    // Re-lay each cell wrapped to its column width, tracking the tallest cell per row.
    let mut row_layouts: GridLayouts = Vec::with_capacity(rows.len());
    let mut row_heights = Vec::with_capacity(rows.len());
    for rowdata in grid {
        let mut layouts = Vec::with_capacity(ncols);
        let mut max_h = 0f32;
        for (c, cell) in rowdata.into_iter().enumerate() {
            match cell {
                Some(MeasuredCell {
                    text,
                    runs,
                    map,
                    empty_landing,
                    code_ranges,
                }) => {
                    let layout = Rc::new(engine.build_line(
                        &text,
                        scale,
                        base_font_size,
                        line_height,
                        fg,
                        Some(col_w[c].max(1.0)),
                        &runs,
                    ));
                    max_h = max_h.max(layout.height());
                    layouts.push(Some(CellSlot {
                        layout,
                        map,
                        empty_landing,
                        code_ranges,
                    }));
                }
                None => layouts.push(None),
            }
        }
        // An empty row still gets one line's height so the grid never collapses to 0.
        if max_h <= 0.0 {
            max_h = base_font_size * line_height * scale;
        }
        row_heights.push(max_h + 2.0 * cell_pad_y);
        row_layouts.push(layouts);
    }

    TableLayout {
        col_x,
        col_w,
        aligns: table.aligns.clone(),
        row_heights,
        delim_height: 0.0,
        row_layouts,
    }
}

/// Vertical padding (logical px) above and below an image block.
const IMG_VPAD: f32 = 8.0;
/// Block height (logical px) reserved for a loading/failed image placeholder.
const IMG_PLACEHOLDER_H: f32 = 120.0;
/// Placeholder box size (logical px) reserved inline for an image that hasn't loaded
/// yet (or failed) — small, since the real size is unknown until decode.
const INLINE_IMG_PLACEHOLDER_W: f32 = 80.0;
const INLINE_IMG_PLACEHOLDER_H: f32 = 20.0;

/// A standalone image to paint on a line: its load state plus the sizing the draw
/// pass needs. `dest_*` are device px on screen; `nat_*` are the image's intrinsic
/// pixel dimensions (draw_image paints at native size, so the transform scales
/// `dest/nat`).
struct ImageBlock {
    alt: String,
    kind: ImageBlockKind,
}

enum ImageBlockKind {
    Loaded {
        brush: ImageBrush,
        nat_w: f32,
        nat_h: f32,
        dest_w: f32,
        dest_h: f32,
    },
    Loading,
    Failed,
}

/// Draw data for one inline image, indexed by its Parley inline-box `id`. The box's
/// on-line position + size come from the laid-out `PositionedInlineBox`; this carries
/// only what's needed to paint there — the brush + intrinsic pixel size for a loaded
/// image, or `None` for a loading/failed/absent placeholder rect.
struct InlineImageDraw {
    /// `(brush, nat_w, nat_h)` for a loaded image; `None` paints a placeholder box.
    brush: Option<(ImageBrush, f32, f32)>,
}

/// Inline git-diff decorations for one real line (added-line bg + word ranges).
#[derive(Default)]
struct LineDiff {
    is_addition: bool,
    /// Display byte ranges of word-level additions within the line.
    inline: Vec<Range<usize>>,
}

/// A deleted (ghost) line, rendered from the HEAD snapshot above the buffer line
/// it was removed before. Inert: not in the hit-test/cursor index.
struct Ghost {
    layout: parley::Layout<Brush>,
    height: f32,
    /// Display byte ranges of word-level deletions within the ghost line.
    inline: Vec<Range<usize>>,
}

/// The four translucent inline-diff colors, baked from the theme at build time.
struct DiffColors {
    added_bg: Color,
    added_inline: Color,
    deleted_bg: Color,
    deleted_inline: Color,
}

/// Stable per-frame layout constants: padding and font in logical px, plus
/// `device_width`, `scale`, and the theme foreground baked to a `Color`. Bundled
/// so `DocLayout::build` isn't a wall of `f32`s; all are fixed for a given surface
/// and theme. Viewport state (`measure_to_y`) is deliberately kept a separate arg.
pub struct LayoutParams {
    pub device_width: f32,
    pub scale: f32,
    pub pad_x: f32,
    pub pad_top: f32,
    pub pad_bottom: f32,
    pub base_font_size: f32,
    pub line_height: f32,
    pub fg: Color,
}

/// Build the ghost (deleted) lines that render above buffer line `new_line`,
/// laying out each from the HEAD snapshot. `usize::MAX` cursor keeps every marker
/// hidden in ghosts (the cursor is never "on" a ghost line).
/// Max ghost (deleted) lines shaped per block. A huge deletion only *shapes* the lines
/// nearest the real line (the ones that can be on screen); the rest contribute estimated
/// height only — bounds per-frame shaping regardless of deletion size.
const GHOST_SHAPE_CAP: usize = 300;

/// Cheap estimate (no shaping) of a deletion hunk's block height above `new_line`:
/// deleted-line count × body row height. Used for off-screen (virtualized) lines.
fn estimate_ghost_height(diff: Option<&DiffState>, new_line: usize, min_row: f32) -> f32 {
    diff.and_then(|d| d.ghost_lines_before(new_line))
        .map(|r| r.len() as f32 * min_row)
        .unwrap_or(0.0)
}

/// Build the ghost (deleted) lines that render above buffer line `new_line`. Returns the
/// shaped ghosts (bottom-aligned to the real line by the draw pass) and the block's TOTAL
/// height — shaped rows plus the estimated height of any lines beyond `GHOST_SHAPE_CAP`.
fn build_ghosts_before(
    engine: &mut TextEngine,
    diff: Option<&DiffState>,
    new_line: usize,
    theme: &EditorTheme,
    params: &LayoutParams,
    max_advance: f32,
    min_row: f32,
) -> (Vec<Ghost>, f32) {
    let LayoutParams {
        scale,
        base_font_size,
        line_height,
        fg,
        ..
    } = *params;
    let Some(d) = diff else {
        return (Vec::new(), 0.0);
    };
    let Some(old_range) = d.ghost_lines_before(new_line) else {
        return (Vec::new(), 0.0);
    };
    let old = &d.old_snapshot;
    // Shape only the last GHOST_SHAPE_CAP lines (nearest the real line, most likely on
    // screen); estimate the height of the earlier ones.
    let count = old_range.len();
    let shape_from = old_range.start + count.saturating_sub(GHOST_SHAPE_CAP);
    let mut block_height = (shape_from - old_range.start) as f32 * min_row;
    let mut out = Vec::new();
    for old_line in shape_from..old_range.end {
        if old_line >= old.line_count() {
            break;
        }
        // Only the few visible ghost lines need styling, so compute per line instead
        // of bucketing the entire HEAD snapshot on every rebuild.
        let styles = old.tree_styles_for_line(old_line);
        let lr = build_line_render(
            old,
            old_line,
            theme,
            base_font_size,
            usize::MAX,
            &styles,
            &[],
            None,
        );
        let layout = engine.build_line_hanging(
            &lr.text,
            scale,
            lr.font_size,
            line_height,
            fg,
            Some(max_advance),
            &lr.runs,
            lr.content_start,
            &[],
        );
        let line_start = old.line_markers(old_line).range.start;
        let inline = d
            .old_inline_changes(old_line)
            .map(|changes| map_changes_to_display(&lr.map, line_start, changes))
            .unwrap_or_default();
        block_height += layout.height();
        out.push(Ghost {
            height: layout.height(),
            layout,
            inline,
        });
    }
    (out, block_height)
}

/// Map line-relative inline-change byte ranges through a line's segment map into
/// non-empty display ranges. `base` is the line's buffer start byte.
fn map_changes_to_display(
    map: &SegmentMap,
    base: usize,
    changes: &[InlineChange],
) -> Vec<Range<usize>> {
    changes
        .iter()
        .filter_map(|c| {
            let dr = map.buffer_range_to_display(base + c.range.start..base + c.range.end);
            (!dr.is_empty()).then_some(dr)
        })
        .collect()
}

/// Build the image block for a standalone image and its device-px block height (used
/// as the line height). Fits to `content_w`, preserving aspect: an image at least as
/// wide as the body fills the full content width; a narrower one keeps its intrinsic
/// size (never upscaled). Height follows from the aspect — no cap, so wide/landscape
/// images aren't shrunk below the body width. Loading/failed states get a placeholder.
fn build_image_block(
    images: &ImageCache,
    img: &ImageRef,
    content_w: f32,
    scale: f32,
    vpad: f32,
) -> (ImageBlock, f32) {
    match images.get(&img.url) {
        Some(ImageState::Loaded(loaded)) => {
            // Intrinsic (logical display) size in device px (×scale). For SVG this is
            // the vector's logical size, decoupled from the supersampled raster dims.
            let iw = loaded.display_w * scale;
            let ih = loaded.display_h * scale;
            let dw = iw.min(content_w);
            let dh = if iw > 0.0 { ih * dw / iw } else { 0.0 };
            let block = ImageBlock {
                alt: img.alt.clone(),
                kind: ImageBlockKind::Loaded {
                    brush: loaded.brush.clone(),
                    nat_w: loaded.width as f32,
                    nat_h: loaded.height as f32,
                    dest_w: dw,
                    dest_h: dh,
                },
            };
            (block, dh + 2.0 * vpad)
        }
        Some(ImageState::Failed) => (
            ImageBlock {
                alt: img.alt.clone(),
                kind: ImageBlockKind::Failed,
            },
            IMG_PLACEHOLDER_H * scale,
        ),
        // Loading, or not yet spawned: a pending placeholder box.
        Some(ImageState::Loading) | None => (
            ImageBlock {
                alt: img.alt.clone(),
                kind: ImageBlockKind::Loading,
            },
            IMG_PLACEHOLDER_H * scale,
        ),
    }
}

/// Resolve a line's inline images to `(inline_boxes, draws)`: for each, the box size to
/// reserve in the layout (natural device size, shrunk to fit `max_advance`) and its draw
/// spec. Loading/failed/absent images get a small fixed placeholder box. Every referenced
/// URL is collected into `image_urls` (deduped) so the load pass fetches it.
fn resolve_inline_images(
    inline_images: &[InlineImageRef],
    images: &ImageCache,
    image_urls: &mut Vec<String>,
    max_advance: f32,
    scale: f32,
) -> (Vec<(usize, f32, f32)>, Vec<InlineImageDraw>) {
    let mut inline_boxes: Vec<(usize, f32, f32)> = Vec::new();
    let draws: Vec<InlineImageDraw> = inline_images
        .iter()
        .map(|ii| {
            if !image_urls.iter().any(|u| u == &ii.url) {
                image_urls.push(ii.url.clone());
            }
            let (w, h, draw) = match images.get(&ii.url) {
                Some(ImageState::Loaded(loaded)) => {
                    let mut w = loaded.display_w * scale;
                    let mut h = loaded.display_h * scale;
                    if w > max_advance && w > 0.0 {
                        h *= max_advance / w;
                        w = max_advance;
                    }
                    let brush = Some((
                        loaded.brush.clone(),
                        loaded.width as f32,
                        loaded.height as f32,
                    ));
                    (w, h, InlineImageDraw { brush })
                }
                _ => (
                    INLINE_IMG_PLACEHOLDER_W * scale,
                    INLINE_IMG_PLACEHOLDER_H * scale,
                    InlineImageDraw { brush: None },
                ),
            };
            inline_boxes.push((ii.display_offset, w, h));
            draw
        })
        .collect();
    (inline_boxes, draws)
}

/// Splice an in-progress IME `preedit` string into a line's display `text` at display
/// byte `caret_disp`, returning the new text plus the shifted style runs with an added
/// underline run over the composition span. Runs starting at/after the caret shift right
/// by `preedit.len()` (the caret sits between clusters, so a straddling run is rare and
/// the same shift keeps it aligned). Pure (no layout) so it's unit-testable headlessly.
fn splice_preedit(
    text: &str,
    runs: &[StyleRun],
    caret_disp: usize,
    preedit: &str,
    fg: Color,
) -> (String, Vec<StyleRun>) {
    let spliced_text = format!("{}{}{}", &text[..caret_disp], preedit, &text[caret_disp..]);
    let shift = preedit.len();
    let mut spliced_runs: Vec<StyleRun> = runs
        .iter()
        .map(|r| {
            let mut r = r.clone();
            if r.range.start >= caret_disp {
                r.range = r.range.start + shift..r.range.end + shift;
            }
            r
        })
        .collect();
    // Underline the composition; keep it mono so metrics match the body (stable caret math).
    let mut under = StyleRun::new(caret_disp..caret_disp + shift, fg);
    under.underline = true;
    under.mono = true;
    spliced_runs.push(under);
    (spliced_text, spliced_runs)
}

/// Prefix-sum tops for `heights`: `out[0] = pad_top`, `out[i+1] = out[i] +
/// heights[i]`. Length is `heights.len() + 1`.
fn compute_tops(heights: &[f32], pad_top: f32) -> Vec<f32> {
    let mut tops = Vec::with_capacity(heights.len() + 1);
    let mut y = pad_top;
    tops.push(y);
    for &h in heights {
        y += h;
        tops.push(y);
    }
    tops
}

/// Extra device px measured past the viewport bottom so gentle wheel-scrolling stays
/// on materialized lines before a rebuild extends the range.
const MEASURE_OVERSCAN_PX: f32 = 800.0;
/// Extra lines materialized past the cursor line (so scroll-to-cursor lands on a real
/// layout even when the cursor briefly outruns the scroll position).
const MEASURE_OVERSCAN_LINES: usize = 30;

pub struct DocLayout {
    layouts: Vec<Rc<parley::Layout<Brush>>>,
    /// Per-line render results (shared with the RenderCache via `Rc`), parallel to
    /// `layouts`. Their `.map` is the display↔buffer map for cursor/click math.
    renders: Vec<Rc<LineRender>>,
    /// Per-line buffer byte ranges (incl. trailing newline), parallel to `layouts`.
    line_ranges: Vec<Range<usize>>,
    /// Per-line inline git-diff decorations, parallel to `layouts`.
    line_diffs: Vec<LineDiff>,
    /// Ghost (deleted) lines rendered *above* each real line, parallel to `layouts`.
    ghosts: Vec<Vec<Ghost>>,
    /// Per-line standalone-image block, parallel to `layouts`. `Some` overrides the
    /// line's height with the image (or placeholder) block and is painted by
    /// `draw_images`; `None` for ordinary lines.
    image_blocks: Vec<Option<ImageBlock>>,
    /// Per-line inline-image draw data, parallel to `layouts`. Each inner vec is indexed
    /// by the Parley inline-box `id` (= its order on the line). Empty for lines without
    /// inline images.
    inline_draws: Vec<Vec<InlineImageDraw>>,
    /// Distinct image URLs across the materialized lines, for the shell to kick off
    /// loads (diffed against the shared cache).
    image_urls: Vec<String>,
    /// Vertical padding (device px) above/below an image block, and the placeholder
    /// box colors — baked from `scale`/theme at build so the draw pass is self-contained.
    img_vpad: f32,
    img_label_size: f32,
    image_border: Color,
    image_bg: Color,
    /// Background chip painted behind inline `code` spans.
    code_bg: Color,
    /// Per-line grid-table row, parallel to `layouts`. `Some((layout, kind))` means the
    /// line renders as a table row (its text layout is empty); `None` otherwise.
    table_lines: Vec<Option<(Rc<TableLayout>, RowKind)>>,
    /// Baked table chrome (device px + theme colors) for the grid draw path.
    table_cell_pad_x: f32,
    table_cell_pad_y: f32,
    table_border: f32,
    table_border_color: Color,
    table_bg: Color,
    table_header_bg: Color,
    /// Per-line x-offsets (from the line origin) of each blockquote gutter rule, parallel
    /// to `layouts`. Empty for non-quote lines. Painted as continuous vertical rects.
    quote_bars: Vec<Vec<f32>>,
    /// Total ghost-block height above each real line, parallel to `layouts`.
    ghost_height: Vec<f32>,
    /// Deleted (ghost) rows at the very end of the document — a deletion hunk anchored
    /// past the last line, which has no host line to hang above. Drawn below the last
    /// real line; without this, trailing deletions (esp. with no final newline) vanish.
    trailing_ghosts: Vec<Ghost>,
    trailing_ghost_height: f32,
    /// Top y of each line's *ghost block*; the real line begins at
    /// `tops[i] + ghost_height[i]`. Length `layouts.len() + 1`. Device px.
    tops: Vec<f32>,
    /// The materialized (fully laid-out) band is `[measured_start, measured_count)`.
    /// Lines outside it are height-estimated placeholders. `measured_start` is 0 and
    /// `measured_count` is `line_count()` when the whole document was laid out.
    measured_start: usize,
    measured_count: usize,
    /// `(line, display byte offset)` of the IME composition caret within the spliced
    /// caret line, when a preedit is active. Drives the drawn caret + candidate popup.
    preedit_caret: Option<(usize, usize)>,
    diff_colors: DiffColors,
    /// Width (device px) and color of the painted blockquote gutter rules.
    quote_bar_width: f32,
    quote_bar_color: Color,
    pub scroll_y: f32,
    /// Surface width in device px (for full-width diff row backgrounds).
    width: f32,
    pad_top: f32,
    pad_bottom: f32,
    pad_x: f32,
}

impl DocLayout {
    /// Lay out every line of `snapshot` at the current width. `pad_*`/`font_size`
    /// are logical px; `scale` converts to device px (matching the layouts).
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        engine: &mut TextEngine,
        cache: &mut LineCache,
        render_cache: &mut RenderCache,
        height_cache: &mut HeightCache,
        table_cache: &mut TableCache,
        version: u64,
        snapshot: &RenderSnapshot,
        theme: &EditorTheme,
        diff: Option<&DiffState>,
        github: Option<&GithubRenderData>,
        images: &ImageCache,
        cursor_offset: usize,
        params: &LayoutParams,
        // In-progress IME composition to splice into the caret line (render-only; no
        // buffer mutation). `None` outside composition and on the headless snapshot path.
        preedit: Option<&PreeditView>,
        // Materialize (fully lay out) a band of lines around the scroll anchor: from
        // `anchor_line - overscan` downward until the materialized height covers
        // `viewport_h` + overscan on both sides. Lines above and below the band are
        // cheaply height-*estimated* (head/tail virtualization) — O(visible) regardless
        // of scroll depth. `anchor_line = 0` + `viewport_h = f32::INFINITY` lays out the
        // whole document (the headless/first-open path).
        anchor_line: usize,
        viewport_h: f32,
    ) -> Self {
        let LayoutParams {
            device_width,
            scale,
            pad_x,
            pad_top,
            pad_bottom,
            base_font_size,
            line_height,
            fg,
        } = *params;
        // Cap the body width for readability and center it: the left margin is the base
        // padding, widened to a centering inset once the window exceeds MAX_CONTENT_WIDTH
        // plus that padding. `left` is the draw origin and both margins for `max_advance`.
        let left = (pad_x * scale).max((device_width - MAX_CONTENT_WIDTH * scale) / 2.0);
        let max_advance = (device_width - 2.0 * left).max(1.0);
        let n = snapshot.line_count();
        cache.begin();
        render_cache.begin();
        height_cache.begin();
        table_cache.begin();
        // Off-screen height estimate: average glyph advance (device px/char) from one
        // unwrapped sample line, and the body row height. Predicts soft-wrap row count
        // from a line's byte length without laying it out (see writ-virtualization-plan).
        let cal_text = "the quick brown fox jumps over the lazy dog and then runs along";
        let cal = engine.build_line(cal_text, scale, base_font_size, line_height, fg, None, &[]);
        let k = (cal.width() / cal_text.chars().count().max(1) as f32).max(0.1);
        let min_row = base_font_size * line_height * scale;
        let cursor_line = snapshot
            .rope
            .byte_to_line(cursor_offset.min(snapshot.rope.len_bytes()));
        // Line-based table membership (start of the cursor's line), matching
        // `build_line_render`'s reveal predicate so cache keys agree at row edges.
        let cursor_line_start = snapshot.line_byte_range(cursor_line).start;
        // Shared placeholders for estimated (un-laid-out) lines. They're never drawn or
        // cursor-queried (only visible lines are, and those are always materialized), so
        // an empty layout / identity render is safe; `Rc::clone` keeps it O(1).
        let empty_layout: Rc<parley::Layout<Brush>> = Rc::new(parley::Layout::new());
        let empty_render: Rc<LineRender> = Rc::new(LineRender {
            text: String::new(),
            font_size: base_font_size,
            runs: Vec::new(),
            map: SegmentMap::identity("", 0).1,
            content_start: 0,
            quote_bar_bytes: Vec::new(),
            is_hr: false,
            image: None,
            code_ranges: Vec::new(),
            inline_images: Vec::new(),
            table: None,
        });
        // The materialized band: [measure_from_line, measured_count). Lines before
        // `measure_from_line` (head) and from `measured_count` on (tail) are estimated.
        // A fixed line count of overscan is always laid out above the anchor (for smooth
        // up-scroll); coverage below is measured in pixels from the anchor.
        let measure_from_line = anchor_line.saturating_sub(MEASURE_OVERSCAN_LINES);
        // Materialize downward from the anchor until this much height is covered — the
        // viewport plus one overscan band — so the viewport can never reach a placeholder.
        let band_span = viewport_h + MEASURE_OVERSCAN_PX;
        let mut band_y = 0.0f32; // height materialized from `anchor_line` down
        let mut past_band = false; // true once the band is tall enough; tail is estimated
        let mut measured_start = n; // first materialized line (n = none materialized yet)
        let mut measured_count = n; // first estimated tail line (n = materialized to end)
        let mut preedit_caret: Option<(usize, usize)> = None;
        // Bucket inline styles per line once (O(n + styles)) instead of the O(n²)
        // per-line `styles_in_range` scan — the dominant per-keystroke cost on large
        // docs. (Ghost lines style themselves lazily; see `build_ghosts_before`.)
        let line_styles = snapshot.inline_styles_by_line();
        let mut layouts = Vec::with_capacity(n);
        let mut renders = Vec::with_capacity(n);
        let mut line_ranges = Vec::with_capacity(n);
        let mut line_diffs = Vec::with_capacity(n);
        let mut ghosts = Vec::with_capacity(n);
        let mut ghost_height = Vec::with_capacity(n);
        let mut quote_bars: Vec<Vec<f32>> = Vec::with_capacity(n);
        let mut image_blocks: Vec<Option<ImageBlock>> = Vec::with_capacity(n);
        let mut inline_draws: Vec<Vec<InlineImageDraw>> = Vec::with_capacity(n);
        let mut table_lines: Vec<Option<(Rc<TableLayout>, RowKind)>> = Vec::with_capacity(n);
        let mut image_urls: Vec<String> = Vec::new();
        // Content width available to an image (device px), same basis as `max_advance`.
        let content_w = max_advance;
        let img_vpad = IMG_VPAD * scale;
        // Each line's total height = its ghost block above + the real line.
        let mut heights = Vec::with_capacity(n);
        // Cursor component of a line's render key. Table rows share block-reveal state
        // (all flip together when the cursor enters/leaves the block) — except the row
        // the cursor actually sits on, which reveals its own markers. Three distinct
        // sentinels keep grid / reveal-here / reveal-elsewhere from colliding.
        let cursor_component = |i: usize, range: &Range<usize>| -> usize {
            let onkey = cursor_key_for(range, cursor_offset);
            match snapshot
                .table_row_at_line(i)
                .filter(|(t, _)| table_grid_ok(t))
            {
                Some((t, _)) => {
                    if !t.block.contains(&cursor_line_start) {
                        usize::MAX
                    } else if onkey != usize::MAX {
                        onkey
                    } else {
                        REVEAL_ELSEWHERE
                    }
                }
                None => onkey,
            }
        };
        // `i` indexes several parallel per-line inputs (styles, markers, diff).
        #[allow(clippy::needless_range_loop)]
        for i in 0..n {
            // Estimate the head (above the band) and the tail (once the band has covered
            // the viewport + overscan); materialize only the band in between.
            if i >= measure_from_line && !past_band && measured_start == n {
                measured_start = i;
            }
            let estimate = i < measure_from_line || past_band;
            if estimate {
                let range = snapshot.line_byte_range(i);
                // Prefer an exact height measured on a recent frame; fall back to the
                // char-count soft-wrap estimate for never-seen lines.
                let rkey = render_key(version, i, cursor_component(i, &range));
                let h = height_cache.get(rkey).unwrap_or_else(|| {
                    let byte_len = range.len().saturating_sub(1) as f32; // minus trailing '\n'
                    let est_rows = (byte_len * k / max_advance).ceil().max(1.0);
                    est_rows * min_row
                });
                // A deletion hunk anchored at this (off-screen) line still consumes height:
                // estimate it (deleted-line count × row height) so the scroll extent stays
                // stable instead of jumping when the line scrolls into the materialized band.
                let gh = estimate_ghost_height(diff, i, min_row);
                heights.push(gh + h);
                layouts.push(empty_layout.clone());
                renders.push(empty_render.clone());
                line_ranges.push(range);
                line_diffs.push(LineDiff::default());
                ghosts.push(Vec::new());
                ghost_height.push(gh);
                quote_bars.push(Vec::new());
                image_blocks.push(None);
                inline_draws.push(Vec::new());
                table_lines.push(None);
                continue;
            }
            // Ghost (deleted) lines rendered before this line, from the HEAD snapshot.
            // `gh` is the full block height (shaped rows + estimated overflow beyond the cap).
            let (line_ghosts, gh) =
                build_ghosts_before(engine, diff, i, theme, params, max_advance, min_row);

            // Line byte range + render key, computed once and shared by the render cache,
            // the height cache, and the diff mapping below. (`line_markers(i).range` is
            // byte-identical but far pricier — it runs full marker extraction per line.)
            let range = snapshot.line_byte_range(i);
            let rkey = render_key(version, i, cursor_component(i, &range));
            // Table membership (under the size cap). `TableCtx` carries the block range so
            // `build_line_render` can decide grid vs. reveal; the `&TableInfo` is re-fetched
            // below (only when grid mode engaged) to build the shared `TableLayout`.
            let table_ctx = snapshot
                .table_row_at_line(i)
                .filter(|(t, _)| table_grid_ok(t))
                .map(|(t, kind)| TableCtx {
                    block: t.block.clone(),
                    kind,
                });

            let extra = github.map(|g| g.extra_regions(i)).unwrap_or_default();
            // Reuse the cached render when nothing about this line changed. Lines with
            // GitHub extra regions bypass the cache (validation can change them without
            // a version bump); all others key on (version, line, cursor-on-line).
            let lr = if extra.is_empty() {
                let tc = table_ctx.clone();
                render_cache.get_or_build(rkey, || {
                    Rc::new(build_line_render(
                        snapshot,
                        i,
                        theme,
                        base_font_size,
                        cursor_offset,
                        &line_styles[i],
                        &[],
                        tc,
                    ))
                })
            } else {
                Rc::new(build_line_render(
                    snapshot,
                    i,
                    theme,
                    base_font_size,
                    cursor_offset,
                    &line_styles[i],
                    &extra,
                    table_ctx.clone(),
                ))
            };
            let (inline_boxes, line_inline_draws) = resolve_inline_images(
                &lr.inline_images,
                images,
                &mut image_urls,
                max_advance,
                scale,
            );
            let key = line_key(
                &lr.text,
                scale,
                lr.font_size,
                line_height,
                max_advance,
                &lr.runs,
                lr.content_start,
            );
            // Lines with inline images bypass the layout cache: the box sizes change when
            // an image loads (without any change to the cache key), so build fresh. They
            // are rare, so the cost is negligible.
            let layout = if inline_boxes.is_empty() {
                cache.get_or_build(key, || {
                    Rc::new(engine.build_line_hanging(
                        &lr.text,
                        scale,
                        lr.font_size,
                        line_height,
                        fg,
                        Some(max_advance),
                        &lr.runs,
                        lr.content_start,
                        &[],
                    ))
                })
            } else {
                Rc::new(engine.build_line_hanging(
                    &lr.text,
                    scale,
                    lr.font_size,
                    line_height,
                    fg,
                    Some(max_advance),
                    &lr.runs,
                    lr.content_start,
                    &inline_boxes,
                ))
            };
            // IME composition: on the caret's line, splice the preedit into a *fresh*
            // layout (unique per keystroke, so it bypasses the line cache). The original
            // `lr` (map/text) is kept for hit-testing; only the drawn layout changes.
            let layout = match preedit {
                Some(pv) if i == cursor_line => {
                    let caret_disp = lr.map.buffer_to_display(cursor_offset);
                    let (sp_text, sp_runs) =
                        splice_preedit(&lr.text, &lr.runs, caret_disp, pv.text, fg);
                    let caret_off = caret_disp + pv.cursor.map(|(s, _)| s).unwrap_or(pv.text.len());
                    preedit_caret = Some((i, caret_off));
                    Rc::new(engine.build_line_hanging(
                        &sp_text,
                        scale,
                        lr.font_size,
                        line_height,
                        fg,
                        Some(max_advance),
                        &sp_runs,
                        lr.content_start,
                        &[],
                    ))
                }
                _ => layout,
            };
            // Inline diff: map added word ranges (line-relative buffer bytes) through
            // this line's segment map into display ranges.
            let line_diff = match diff {
                Some(d) if d.is_addition(i) => {
                    let inline = d
                        .new_inline_changes(i)
                        .map(|changes| map_changes_to_display(&lr.map, range.start, changes))
                        .unwrap_or_default();
                    LineDiff {
                        is_addition: true,
                        inline,
                    }
                }
                _ => LineDiff::default(),
            };
            // Measure each blockquote gutter's x (on the first, un-hung row) so the draw
            // path can paint a continuous rule there. Only quote lines pay this.
            let bars: Vec<f32> = lr
                .quote_bar_bytes
                .iter()
                .filter_map(|&b| {
                    Cluster::from_byte_index(&layout, b).and_then(|c| c.visual_offset())
                })
                .collect();
            // Grid table row: build (or reuse) the shared `TableLayout` and take the row's
            // height from it; the line's own text layout is empty (hidden).
            let table_line: Option<(Rc<TableLayout>, RowKind)> = if lr.table.is_some() {
                snapshot
                    .table_row_at_line(i)
                    .filter(|(t, _)| table_grid_ok(t))
                    .map(|(t, kind)| {
                        let tkey =
                            table_key(version, t.block.start, scale, base_font_size, max_advance);
                        let tl = table_cache.get_or_build(tkey, || {
                            Rc::new(build_table_layout(
                                engine,
                                snapshot,
                                theme,
                                t,
                                &line_styles,
                                params,
                                max_advance,
                            ))
                        });
                        (tl, kind)
                    })
            } else {
                None
            };
            // Standalone image: the block (image or placeholder) sets the line height in
            // place of the (empty) text layout, and the URL is collected for loading.
            let (image_block, line_h) = if let Some((tl, kind)) = &table_line {
                (None, tl.height(*kind))
            } else {
                match &lr.image {
                    Some(img) => {
                        if !image_urls.iter().any(|u| u == &img.url) {
                            image_urls.push(img.url.clone());
                        }
                        let (block, block_h) =
                            build_image_block(images, img, content_w, scale, img_vpad);
                        (Some(block), block_h)
                    }
                    None => (None, layout.height()),
                }
            };
            // Cache the exact real-line height (ghost excluded) so a later frame that
            // only estimates this line reuses it instead of the char-count guess. Same
            // `rkey` as the render cache / estimate-branch lookup.
            height_cache.set(rkey, line_h);
            let total_h = gh + line_h;
            // Count coverage only from the anchor down (lines above are fixed-count
            // overscan); stop once the viewport + overscan below is covered.
            if i >= anchor_line {
                band_y += total_h;
                if band_y >= band_span {
                    past_band = true;
                    measured_count = i + 1;
                }
            }
            heights.push(total_h);
            layouts.push(layout);
            renders.push(lr);
            quote_bars.push(bars);
            line_ranges.push(range);
            line_diffs.push(line_diff);
            ghosts.push(line_ghosts);
            ghost_height.push(gh);
            image_blocks.push(image_block);
            inline_draws.push(line_inline_draws);
            table_lines.push(table_line);
        }
        cache.sweep();
        render_cache.sweep();
        height_cache.sweep();
        table_cache.sweep();
        // Trailing deletions: a hunk anchored at `n` (past the last line) has no host line
        // to hang above; build it here and draw it below the last real line.
        let (trailing_ghosts, trailing_ghost_height) =
            build_ghosts_before(engine, diff, n, theme, params, max_advance, min_row);
        // Row tint ~0.15 and word tint ~0.40 alpha, matching GitHub's dark-mode diff
        // line/word background strengths (the row was previously a near-invisible 0.05).
        let diff_colors = DiffColors {
            added_bg: peniko_color_alpha(theme.green, 0.15),
            added_inline: peniko_color_alpha(theme.green, 0.40),
            deleted_bg: peniko_color_alpha(theme.red, 0.15),
            deleted_inline: peniko_color_alpha(theme.red, 0.40),
        };
        Self {
            layouts,
            renders,
            line_ranges,
            line_diffs,
            ghosts,
            ghost_height,
            trailing_ghosts,
            trailing_ghost_height,
            quote_bars,
            image_blocks,
            inline_draws,
            image_urls,
            img_vpad,
            img_label_size: 14.0 * scale,
            image_border: peniko_color(theme.comment),
            image_bg: peniko_color_alpha(theme.comment, 0.08),
            code_bg: peniko_color_alpha(theme.comment, 0.22),
            table_lines,
            table_cell_pad_x: TABLE_CELL_PAD_X * scale,
            table_cell_pad_y: TABLE_CELL_PAD_Y * scale,
            table_border: TABLE_BORDER * scale,
            // Subtle grid chrome, in the status-bar surface/selection tone.
            table_border_color: peniko_color(theme.selection),
            table_bg: peniko_color_alpha(theme.comment, 0.05),
            table_header_bg: peniko_color(theme.surface),
            diff_colors,
            // A thin rule (~1/6 of a mono cell), floored so it stays visible when small.
            quote_bar_width: (k * 0.16).max(1.5),
            quote_bar_color: peniko_color(theme.comment),
            tops: compute_tops(&heights, pad_top * scale),
            measured_start: measured_start.min(measured_count),
            measured_count,
            preedit_caret,
            scroll_y: 0.0,
            width: device_width,
            pad_top: pad_top * scale,
            pad_bottom: pad_bottom * scale,
            pad_x: left,
        }
    }

    /// The display↔buffer map for a line (Phase 4 cursor/click math).
    pub fn line_map(&self, line: usize) -> Option<&SegmentMap> {
        self.renders.get(line).map(|r| &r.map)
    }

    /// Buffer line index containing `buffer_off` (clamped to the last line).
    pub fn line_of(&self, buffer_off: usize) -> usize {
        let n = self.line_ranges.len();
        if n == 0 {
            return 0;
        }
        self.line_ranges
            .partition_point(|r| r.start <= buffer_off)
            .saturating_sub(1)
            .min(n - 1)
    }

    /// Top y (device px, before scroll) of the *real* text of line `i` — i.e. below
    /// any ghost (deleted) rows stacked above it.
    fn real_top(&self, line: usize) -> f32 {
        self.tops[line] + self.ghost_height[line]
    }

    /// True if `line` currently renders as a standalone image block (its markdown text
    /// is hidden). Used before a click so the caret can be re-placed once the click
    /// reveals the raw text (whose short row no longer sits under the click's y).
    pub fn is_image_line(&self, line: usize) -> bool {
        self.renders.get(line).is_some_and(|r| r.image.is_some())
    }

    /// Buffer offset for horizontal position `x` (device px) within `line`, at its first
    /// row. Unlike `hit_test`, the caller names the line, so a click on a tall image
    /// block can be re-mapped onto the short text row it reveals, at the same `x`.
    pub fn offset_in_line_at_x(&self, line: usize, x: f32) -> Option<usize> {
        let layout = self.layouts.get(line)?;
        let lx = (x - self.pad_x).max(0.0);
        let display_off = Cursor::from_point(layout, lx, 0.0).index();
        Some(self.renders[line].map.display_to_buffer(display_off))
    }

    /// Screen-space caret rectangle at a *display* byte offset within `line`'s layout.
    /// Shared by the buffer caret and the IME preedit caret (which live in different
    /// coordinate spaces: the latter indexes the spliced layout directly).
    fn caret_rect_at(
        &self,
        line: usize,
        display_off: usize,
        caret_width: f32,
    ) -> Option<ScreenRect> {
        let layout = self.layouts.get(line)?;
        let cursor = Cursor::from_byte_index(layout, display_off, Affinity::Downstream);
        let bb = cursor.geometry(layout, caret_width);
        let dx = self.pad_x as f64;
        let dy = (self.real_top(line) - self.scroll_y) as f64;
        Some((bb.x0 + dx, bb.y0 + dy, bb.x1 + dx, bb.y1 + dy))
    }

    /// Screen-space caret rectangle for a buffer offset, or None if empty doc.
    /// `caret_width` is in device px.
    pub fn caret_rect(&self, buffer_off: usize, caret_width: f32) -> Option<ScreenRect> {
        let line = self.line_of(buffer_off);
        let display_off = self.renders.get(line)?.map.buffer_to_display(buffer_off);
        self.caret_rect_at(line, display_off, caret_width)
    }

    /// Screen-space caret rectangle *inside* an active IME composition (the spliced
    /// layout), or None when no preedit is active. Positions the drawn caret + the OS
    /// candidate popup at the composition point.
    pub fn preedit_caret_rect(&self, caret_width: f32) -> Option<ScreenRect> {
        let (line, display_off) = self.preedit_caret?;
        self.caret_rect_at(line, display_off, caret_width)
    }

    /// Screen-space fill rectangles covering the buffer selection range. Handles
    /// multi-line and wrapped-line selections (parley yields one rect per visual row).
    pub fn selection_rects(&self, selection: Range<usize>) -> Vec<ScreenRect> {
        if selection.start >= selection.end {
            return Vec::new();
        }
        let first = self.line_of(selection.start);
        let last = self.line_of(selection.end);
        let mut rects = Vec::new();
        for line in first..=last.min(self.layouts.len().saturating_sub(1)) {
            let range = &self.line_ranges[line];
            let s = selection.start.max(range.start);
            let e = selection.end.min(range.end);
            if s >= e {
                continue;
            }
            let map = &self.renders[line].map;
            let ds = map.buffer_to_display(s);
            let de = map.buffer_to_display(e);
            if ds >= de {
                continue;
            }
            let layout = &self.layouts[line];
            let sel = display_range_selection(layout, ds..de);
            let dx = self.pad_x as f64;
            let dy = (self.real_top(line) - self.scroll_y) as f64;
            for (bb, _) in sel.geometry(layout) {
                rects.push((bb.x0 + dx, bb.y0 + dy, bb.x1 + dx, bb.y1 + dy));
            }
        }
        rects
    }

    /// Map a screen point (device px) to a buffer offset via Parley hit-testing.
    /// Points landing in a ghost (deleted) block are inert — they map to the start
    /// of the real line the ghosts precede.
    pub fn hit_test(&self, x: f32, y: f32) -> Option<usize> {
        let n = self.layouts.len();
        if n == 0 {
            return None;
        }
        let cy = y + self.scroll_y;
        let line = self.tops[..n]
            .partition_point(|&t| t <= cy)
            .saturating_sub(1)
            .min(n - 1);
        let real_top = self.real_top(line);
        if cy < real_top {
            // In the ghost block above the real line: inert.
            return Some(self.line_ranges[line].start);
        }
        let layout = &self.layouts[line];
        let lx = (x - self.pad_x).max(0.0);
        let ly = (cy - real_top).max(0.0);
        // Grid table row: its own text layout is empty (hidden), so hit-test the clicked
        // cell within the shared TableLayout instead of collapsing to the line start.
        if let Some((tl, kind)) = &self.table_lines[line]
            && let Some(off) = tl.hit_test(*kind, lx, self.table_cell_pad_x)
        {
            return Some(off);
        }
        let display_off = Cursor::from_point(layout, lx, ly).index();
        Some(self.renders[line].map.display_to_buffer(display_off))
    }

    /// Scroll the minimum amount so the line containing `buffer_off` is visible.
    /// Reveals the ghost block above too (so a hunk's deletions come into view).
    pub fn scroll_to(&mut self, buffer_off: usize, viewport_h: f32) {
        let line = self.line_of(buffer_off);
        let top = self.tops[line];
        let bottom = self.tops[line + 1];
        if top < self.scroll_y {
            self.scroll_y = top;
        } else if bottom > self.scroll_y + viewport_h {
            self.scroll_y = bottom - viewport_h;
        }
        self.clamp_scroll(viewport_h);
    }

    pub fn line_count(&self) -> usize {
        self.layouts.len()
    }

    /// True if the viewport now reaches into height-estimated (un-laid-out) lines above
    /// or below the materialized band — i.e. a scroll outran the band and the shell
    /// should rebuild around the new anchor before this frame draws blanks.
    pub fn needs_remeasure(&self, viewport_h: f32) -> bool {
        let (first, last) = self.visible_range(viewport_h);
        (self.measured_start > 0 && first < self.measured_start)
            || (self.measured_count < self.layouts.len() && last > self.measured_count)
    }

    /// The scroll anchor: the topmost line touching the viewport, and how far its top is
    /// scrolled above the viewport top (device px). Re-pinning to this across a rebuild
    /// keeps visible content from shifting when off-screen height estimates change — the
    /// heart of the anchor-forward virtualization.
    pub fn scroll_anchor(&self) -> (usize, f32) {
        let n = self.layouts.len();
        if n == 0 {
            return (0, 0.0);
        }
        let line = self.tops[1..]
            .partition_point(|&b| b <= self.scroll_y)
            .min(n - 1);
        (line, self.scroll_y - self.tops[line])
    }

    /// The `scroll_y` that pins `line`'s top `offset` px above the viewport top — the
    /// inverse of `scroll_anchor`, used to re-pin after a rebuild.
    pub fn anchor_scroll_y(&self, line: usize, offset: f32) -> f32 {
        let line = line.min(self.tops.len().saturating_sub(1));
        self.tops[line] + offset
    }

    /// Total document height in device px (last line bottom + bottom padding).
    pub fn content_height(&self) -> f32 {
        self.tops.last().copied().unwrap_or(self.pad_top)
            + self.trailing_ghost_height
            + self.pad_bottom
    }

    /// Largest valid scroll offset so the document bottom aligns to the viewport.
    pub fn max_scroll(&self, viewport_h: f32) -> f32 {
        (self.content_height() - viewport_h).max(0.0)
    }

    pub fn scroll_by(&mut self, dy: f32, viewport_h: f32) {
        self.scroll_y = (self.scroll_y + dy).clamp(0.0, self.max_scroll(viewport_h));
    }

    pub fn clamp_scroll(&mut self, viewport_h: f32) {
        self.scroll_y = self.scroll_y.clamp(0.0, self.max_scroll(viewport_h));
    }

    /// Half-open range of lines intersecting `[scroll_y, scroll_y + viewport_h)`.
    pub fn visible_range(&self, viewport_h: f32) -> (usize, usize) {
        let n = self.layouts.len();
        if n == 0 {
            return (0, 0);
        }
        let top = self.scroll_y;
        let bottom = self.scroll_y + viewport_h;
        // First line whose *bottom* (tops[i+1]) is past the viewport top.
        let first = self.tops[1..].partition_point(|&b| b <= top).min(n);
        // First line whose *top* (tops[i]) is at/after the viewport bottom.
        let last = self.tops[..n].partition_point(|&t| t < bottom).min(n);
        (first, last.max(first))
    }

    /// Fill the display-byte rects of `ranges` within `layout`, offset by `(origin_x,
    /// origin_y)` — the top-left where the layout's glyphs are painted.
    fn fill_display_ranges(
        &self,
        scene: &mut Scene,
        layout: &parley::Layout<Brush>,
        ranges: &[Range<usize>],
        origin_x: f64,
        origin_y: f64,
        color: Color,
    ) {
        for r in ranges {
            let sel = display_range_selection(layout, r.clone());
            for (bb, _) in sel.geometry(layout) {
                scene.fill(
                    Fill::NonZero,
                    Affine::IDENTITY,
                    color,
                    None,
                    &Rect::new(
                        bb.x0 + origin_x,
                        bb.y0 + origin_y,
                        bb.x1 + origin_x,
                        bb.y1 + origin_y,
                    ),
                );
            }
        }
    }

    /// Fill the word-change rects of `ranges` within `layout`, offset to `top_y`. Body
    /// lines are drawn at `self.pad_x`, so that is the x origin.
    fn fill_word_ranges(
        &self,
        scene: &mut Scene,
        layout: &parley::Layout<Brush>,
        ranges: &[Range<usize>],
        top_y: f64,
        color: Color,
    ) {
        self.fill_display_ranges(scene, layout, ranges, self.pad_x as f64, top_y, color);
    }

    /// Paint line `i`'s inline images at their laid-out inline-box positions. `gy` is
    /// the real line's top (device px, scroll-applied). A box's `id` indexes
    /// `inline_draws[i]`: a loaded image is drawn scaled to the box; otherwise a faint
    /// bordered placeholder rect fills the reserved space.
    fn draw_inline_images(&self, scene: &mut Scene, i: usize, gy: f32) {
        let draws = &self.inline_draws[i];
        if draws.is_empty() {
            return;
        }
        for line in self.layouts[i].lines() {
            for item in line.items() {
                let PositionedLayoutItem::InlineBox(pib) = item else {
                    continue;
                };
                let Some(draw) = draws.get(pib.id as usize) else {
                    continue;
                };
                let x = self.pad_x as f64 + pib.x as f64;
                let y = gy as f64 + pib.y as f64;
                match &draw.brush {
                    Some((brush, nat_w, nat_h)) => {
                        // draw_image paints at native pixel size, so scale box/intrinsic.
                        let transform = Affine::translate((x, y))
                            * Affine::scale_non_uniform(
                                pib.width as f64 / *nat_w as f64,
                                pib.height as f64 / *nat_h as f64,
                            );
                        scene.draw_image(brush, transform);
                    }
                    None => {
                        let rect = Rect::new(x, y, x + pib.width as f64, y + pib.height as f64)
                            .to_rounded_rect(3.0);
                        scene.fill(Fill::NonZero, Affine::IDENTITY, self.image_bg, None, &rect);
                        scene.stroke(
                            &Stroke::new(1.0),
                            Affine::IDENTITY,
                            self.image_border,
                            None,
                            &rect,
                        );
                    }
                }
            }
        }
    }

    /// Paint visible lines: ghost (deleted) rows above each line, then real glyphs.
    pub fn draw(&self, engine: &TextEngine, scene: &mut Scene, viewport_h: f32) {
        let (first, last) = self.visible_range(viewport_h);
        for i in first..last {
            // Shaped ghost rows sit directly above the real line (bottom-aligned); for a
            // capped huge deletion the estimated overflow is the empty gap above them.
            let real_top = self.real_top(i) - self.scroll_y;
            let shaped_h: f32 = self.ghosts[i].iter().map(|g| g.height).sum();
            let mut gy = real_top - shaped_h;
            for ghost in &self.ghosts[i] {
                self.draw_ghost(engine, scene, ghost, gy, viewport_h);
                gy += ghost.height;
            }
            // Background chips behind inline code, under the real line's glyphs.
            let code = &self.renders[i].code_ranges;
            if !code.is_empty() {
                self.fill_word_ranges(scene, &self.layouts[i], code, real_top as f64, self.code_bg);
            }
            // Inline images: paint each at its laid-out box position (a loaded image, or a
            // faint bordered placeholder). Parley left a gap in the glyphs where the box is.
            self.draw_inline_images(scene, i, real_top);
            // The real line, below its ghost block.
            engine.draw_line(scene, &self.layouts[i], (self.pad_x, real_top));
        }
        // Trailing deletions below the last real line (a hunk anchored past the end).
        let mut gy = self.tops[self.layouts.len()] - self.scroll_y;
        for ghost in &self.trailing_ghosts {
            self.draw_ghost(engine, scene, ghost, gy, viewport_h);
            gy += ghost.height;
        }
    }

    /// Paint one ghost (deleted) row at `gy` — red row band, word-level deletion tint,
    /// then the text — skipping it entirely when it falls outside the viewport.
    fn draw_ghost(
        &self,
        engine: &TextEngine,
        scene: &mut Scene,
        ghost: &Ghost,
        gy: f32,
        viewport_h: f32,
    ) {
        if gy + ghost.height < 0.0 || gy > viewport_h {
            return; // off-screen — cull
        }
        let top = gy as f64;
        let bottom = (gy + ghost.height) as f64;
        scene.fill(
            Fill::NonZero,
            Affine::IDENTITY,
            self.diff_colors.deleted_bg,
            None,
            &Rect::new(
                self.pad_x as f64,
                top,
                (self.width - self.pad_x) as f64,
                bottom,
            ),
        );
        self.fill_word_ranges(
            scene,
            &ghost.layout,
            &ghost.inline,
            top,
            self.diff_colors.deleted_inline,
        );
        engine.draw_line(scene, &ghost.layout, (self.pad_x, gy));
    }

    /// Paint blockquote gutter rules: one continuous vertical rect per nesting level,
    /// spanning each quote line's full height (so it covers wrapped rows) and tiling
    /// with adjacent quote lines (so multi-line quotes read as one unbroken bar). Call
    /// BEFORE `draw`. `quote_bars[i]` holds each level's x-offset from the line origin.
    pub fn draw_blockquote_gutters(&self, scene: &mut Scene, viewport_h: f32) {
        let (first, last) = self.visible_range(viewport_h);
        for i in first..last {
            let bars = &self.quote_bars[i];
            if bars.is_empty() {
                continue;
            }
            let top = (self.real_top(i) - self.scroll_y) as f64;
            let bottom = (self.tops[i + 1] - self.scroll_y) as f64;
            for &bx in bars {
                let x = (self.pad_x + bx) as f64;
                scene.fill(
                    Fill::NonZero,
                    Affine::IDENTITY,
                    self.quote_bar_color,
                    None,
                    &Rect::new(x, top, x + self.quote_bar_width as f64, bottom),
                );
            }
        }
    }

    /// Paint thematic breaks (`---`) as a horizontal rule across the content width,
    /// centered in the line (whose `---` text is hidden). Call BEFORE `draw`.
    pub fn draw_horizontal_rules(&self, scene: &mut Scene, viewport_h: f32) {
        let (first, last) = self.visible_range(viewport_h);
        for i in first..last {
            if !self.renders[i].is_hr {
                continue;
            }
            let mid = (self.real_top(i) + self.tops[i + 1]) / 2.0 - self.scroll_y;
            let h = self.quote_bar_width as f64;
            let x0 = self.pad_x as f64;
            let x1 = (self.width - self.pad_x) as f64;
            scene.fill(
                Fill::NonZero,
                Affine::IDENTITY,
                self.quote_bar_color,
                None,
                &Rect::new(x0, mid as f64 - h / 2.0, x1, mid as f64 + h / 2.0),
            );
        }
    }

    /// Paint grid-table rows: per-cell background + border rects and the shaped cell
    /// glyphs. The header row's distinct background fill separates it from the body, so no
    /// delimiter rule is drawn (the delimiter line has zero grid height). Call BEFORE
    /// `draw` (a grid row's own text layout is empty, so no glyphs collide). Cell layouts
    /// are held in the shared `TableLayout`, so this is pure drawing — no shaping.
    pub fn draw_tables(&self, engine: &TextEngine, scene: &mut Scene, viewport_h: f32) {
        let (first, last) = self.visible_range(viewport_h);
        let pad_x = self.pad_x as f64;
        let cell_pad_x = self.table_cell_pad_x;
        let cell_pad_y = self.table_cell_pad_y;
        let border = self.table_border as f64;
        for i in first..last {
            let Some((tl, kind)) = &self.table_lines[i] else {
                continue;
            };
            // The delimiter row is not drawn: it takes zero grid height, so the header
            // (distinguished by its background fill) sits directly on the first body row.
            let Some(row_idx) = TableLayout::row_index(*kind) else {
                continue;
            };
            let top = (self.real_top(i) - self.scroll_y) as f64;
            let bottom = (self.tops[i + 1] - self.scroll_y) as f64;
            let is_header = matches!(kind, RowKind::Header);
            let bg = if is_header {
                self.table_header_bg
            } else {
                self.table_bg
            };
            for c in 0..tl.col_x.len() {
                let cx = pad_x + tl.col_x[c] as f64;
                let box_w = (tl.col_w[c] + 2.0 * cell_pad_x) as f64;
                let rect = Rect::new(cx, top, cx + box_w, bottom);
                scene.fill(Fill::NonZero, Affine::IDENTITY, bg, None, &rect);
                scene.stroke(
                    &Stroke::new(border),
                    Affine::IDENTITY,
                    self.table_border_color,
                    None,
                    &rect,
                );
                // Cell glyphs, aligned within the column's content width.
                if let Some(Some(slot)) = tl.row_layouts.get(row_idx).and_then(|r| r.get(c)) {
                    let layout = &slot.layout;
                    let align = tl.aligns.get(c).copied().unwrap_or(Align::Left);
                    let extra = (tl.col_w[c] - layout.width()).max(0.0);
                    let shift = match align {
                        Align::Left => 0.0,
                        Align::Right => extra,
                        Align::Center => extra / 2.0,
                    };
                    let gx = cx as f32 + cell_pad_x + shift;
                    let gy = top as f32 + cell_pad_y;
                    // Chip behind inline code, over the cell bg but under the glyphs.
                    if !slot.code_ranges.is_empty() {
                        self.fill_display_ranges(
                            scene,
                            layout,
                            &slot.code_ranges,
                            gx as f64,
                            gy as f64,
                            self.code_bg,
                        );
                    }
                    engine.draw_line(scene, layout, (gx, gy));
                }
            }
        }
    }

    /// Distinct standalone-image URLs across the materialized lines. The shell diffs
    /// these against the shared cache to spawn loads.
    pub fn image_urls(&self) -> &[String] {
        &self.image_urls
    }

    /// Paint standalone-image blocks: a loaded image at its fitted device rect, or a
    /// bordered placeholder box with an alt/status label (pending/broken). Left-aligned
    /// at `pad_x`, inset by `img_vpad`. Call BEFORE `draw` (glyphs, if any, sit on top).
    pub fn draw_images(&self, engine: &mut TextEngine, scene: &mut Scene, viewport_h: f32) {
        let (first, last) = self.visible_range(viewport_h);
        for i in first..last {
            let Some(block) = &self.image_blocks[i] else {
                continue;
            };
            let x = self.pad_x as f64;
            let top = (self.real_top(i) - self.scroll_y + self.img_vpad) as f64;
            match &block.kind {
                ImageBlockKind::Loaded {
                    brush,
                    nat_w,
                    nat_h,
                    dest_w,
                    dest_h,
                } => {
                    // draw_image paints at native pixel size, so scale dest/intrinsic.
                    let transform = Affine::translate((x, top))
                        * Affine::scale_non_uniform(
                            (*dest_w / *nat_w) as f64,
                            (*dest_h / *nat_h) as f64,
                        );
                    scene.draw_image(brush, transform);
                }
                ImageBlockKind::Loading | ImageBlockKind::Failed => {
                    let line_bottom = (self.tops[i + 1] - self.scroll_y) as f64;
                    let x1 = (self.width - self.pad_x) as f64;
                    let bottom = line_bottom - self.img_vpad as f64;
                    let rect = Rect::new(x, top, x1, bottom.max(top)).to_rounded_rect(4.0);
                    scene.fill(Fill::NonZero, Affine::IDENTITY, self.image_bg, None, &rect);
                    scene.stroke(
                        &Stroke::new(1.0),
                        Affine::IDENTITY,
                        self.image_border,
                        None,
                        &rect,
                    );
                    let failed = matches!(block.kind, ImageBlockKind::Failed);
                    let label = if block.alt.is_empty() {
                        if failed {
                            "broken image"
                        } else {
                            "loading image…"
                        }
                        .to_string()
                    } else if failed {
                        format!("âš  {}", block.alt)
                    } else {
                        block.alt.clone()
                    };
                    let pad = self.img_label_size * 0.5;
                    let layout = engine.build_line(
                        &label,
                        1.0,
                        self.img_label_size,
                        UI_LINE_HEIGHT,
                        self.image_border,
                        Some(((x1 - x) as f32 - 2.0 * pad).max(1.0)),
                        &[],
                    );
                    let ly = top as f32 + ((bottom - top) as f32 - layout.height()) * 0.5;
                    engine.draw_line(scene, &layout, (x as f32 + pad, ly.max(top as f32)));
                }
            }
        }
    }

    /// Paint inline-diff backgrounds for added lines (faint full-row bg + stronger
    /// word-change bg). Call BEFORE `draw` so glyphs sit on top.
    pub fn draw_added_backgrounds(&self, scene: &mut Scene, viewport_h: f32) {
        let (first, last) = self.visible_range(viewport_h);
        for i in first..last {
            let d = &self.line_diffs[i];
            if !d.is_addition {
                continue;
            }
            let top = (self.real_top(i) - self.scroll_y) as f64;
            let bottom = (self.tops[i + 1] - self.scroll_y) as f64;
            scene.fill(
                Fill::NonZero,
                Affine::IDENTITY,
                self.diff_colors.added_bg,
                None,
                &Rect::new(
                    self.pad_x as f64,
                    top,
                    (self.width - self.pad_x) as f64,
                    bottom,
                ),
            );
            self.fill_word_ranges(
                scene,
                &self.layouts[i],
                &d.inline,
                top,
                self.diff_colors.added_inline,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::consts::{FONT_SIZE, LINE_HEIGHT, PADDING};
    use crate::text_engine::peniko_color;

    /// Device-px content width shared by the image-sizing tests (scale 1.0).
    const TEST_CONTENT_W: f32 = 800.0;

    /// Frame constants for the headless tests — the same source of truth the shell uses,
    /// so the tests track any change to the real frame rather than duplicating literals.
    fn test_params(theme: &EditorTheme, device_width: f32) -> LayoutParams {
        LayoutParams {
            device_width,
            scale: 1.0,
            pad_x: PADDING,
            pad_top: PADDING,
            pad_bottom: PADDING * 2.0,
            base_font_size: FONT_SIZE,
            line_height: LINE_HEIGHT,
            fg: peniko_color(theme.foreground),
        }
    }

    /// Encode a solid RGBA image of the given size to PNG, decode it into the cache
    /// under `url`, and return that URL's `ImageRef`.
    fn cache_image(cache: &ImageCache, url: &str, w: u32, h: u32) -> ImageRef {
        use image::{ImageFormat, RgbaImage};
        use std::io::Cursor;
        let img = RgbaImage::from_pixel(w, h, image::Rgba([1, 2, 3, 255]));
        let mut buf = Vec::new();
        img.write_to(&mut Cursor::new(&mut buf), ImageFormat::Png)
            .unwrap();
        cache.set_loaded(url, crate::image_cache::decode(&buf).unwrap());
        ImageRef {
            url: url.to_string(),
            alt: String::new(),
        }
    }

    /// Any image at least as wide as the body fills the full content width (aspect
    /// preserved), with no height cap — so a tall-but-wide-enough image isn't shrunk
    /// below the body width. A smaller image keeps its intrinsic size (no upscale).
    #[test]
    fn image_block_fills_width_preserving_aspect() {
        let cache = ImageCache::new();
        let content_w = TEST_CONTENT_W;
        let vpad = IMG_VPAD;

        // Wide: 2000x100 → clamps to content width, height scaled to keep aspect.
        let wide = cache_image(&cache, "wide", 2000, 100);
        let (block, _) = build_image_block(&cache, &wide, content_w, 1.0, vpad);
        let ImageBlockKind::Loaded { dest_w, dest_h, .. } = block.kind else {
            panic!("expected loaded");
        };
        assert!(
            (dest_w - content_w).abs() < 0.5,
            "wide image fills content width"
        );
        assert!(
            (dest_h - content_w * 100.0 / 2000.0).abs() < 0.5,
            "aspect preserved"
        );

        // Tall but wide enough: 1000x2000 → still fills the width (no cap), height
        // follows aspect (1600), rather than being shrunk to fit a height limit.
        let big = cache_image(&cache, "big", 1000, 2000);
        let (block, _) = build_image_block(&cache, &big, content_w, 1.0, vpad);
        let ImageBlockKind::Loaded { dest_w, dest_h, .. } = block.kind else {
            panic!("expected loaded");
        };
        assert!(
            (dest_w - content_w).abs() < 0.5,
            "fills width even when tall"
        );
        assert!(
            (dest_h - content_w * 2000.0 / 1000.0).abs() < 0.5,
            "height uncapped"
        );

        // Small: 40x30, under the width → shown at intrinsic size (no upscale).
        let small = cache_image(&cache, "small", 40, 30);
        let (block, block_h) = build_image_block(&cache, &small, content_w, 1.0, vpad);
        let ImageBlockKind::Loaded { dest_w, dest_h, .. } = block.kind else {
            panic!("expected loaded");
        };
        assert_eq!(
            (dest_w, dest_h),
            (40.0, 30.0),
            "small image is not upscaled"
        );
        assert!(
            (block_h - (30.0 + 2.0 * vpad)).abs() < 0.5,
            "block adds vertical padding"
        );
    }

    /// An uncached (still-loading) URL gets a fixed placeholder block height.
    #[test]
    fn image_block_placeholder_height_when_unloaded() {
        let cache = ImageCache::new();
        let img = ImageRef {
            url: "pending.png".to_string(),
            alt: "x".to_string(),
        };
        let (block, block_h) = build_image_block(&cache, &img, TEST_CONTENT_W, 1.0, IMG_VPAD);
        assert!(matches!(block.kind, ImageBlockKind::Loading));
        assert_eq!(block_h, IMG_PLACEHOLDER_H);
    }

    /// The pure IME splice: text is `text[..caret] + preedit + text[caret..]`, runs
    /// before the caret are untouched, runs at/after shift by `preedit.len()`, and
    /// exactly one underline run covers the composition span.
    #[test]
    fn splice_preedit_inserts_underlined_composition() {
        let fg = Color::WHITE;
        let text = "abcdef";
        let before = StyleRun::new(0..2, fg); // entirely before the caret
        let after = StyleRun::new(4..6, fg); // entirely at/after the caret
        let runs = vec![before, after];
        let caret = 3;
        let preedit = "XY"; // len 2
        let (out_text, out_runs) = splice_preedit(text, &runs, caret, preedit, fg);
        assert_eq!(out_text, "abcXYdef");
        assert_eq!(out_runs[0].range, 0..2, "run before caret is unchanged");
        assert_eq!(
            out_runs[1].range,
            6..8,
            "run at/after caret shifts by preedit.len()"
        );
        let unders: Vec<_> = out_runs.iter().filter(|r| r.underline).collect();
        assert_eq!(unders.len(), 1, "exactly one underline run");
        assert_eq!(unders[0].range, 3..5, "underline covers the preedit span");
    }

    #[test]
    fn tops_prefix_sum_has_n_plus_one() {
        let heights = [10.0, 20.0, 5.0];
        let tops = compute_tops(&heights, 4.0);
        assert_eq!(tops.len(), 4); // n + 1
        assert_eq!(tops, vec![4.0, 14.0, 34.0, 39.0]);
    }

    #[test]
    fn tops_empty_doc() {
        let tops = compute_tops(&[], 4.0);
        assert_eq!(tops, vec![4.0]); // just the padding top
    }

    /// Build a DocLayout-like fixture from raw heights to test visibility math
    /// without a GPU/font stack.
    fn fixture(heights: &[f32], pad_top: f32, pad_bottom: f32) -> DocLayout {
        DocLayout {
            layouts: heights
                .iter()
                .map(|_| Rc::new(parley::Layout::new()))
                .collect(),
            renders: heights
                .iter()
                .map(|_| {
                    Rc::new(LineRender {
                        text: String::new(),
                        font_size: 18.0,
                        runs: Vec::new(),
                        map: SegmentMap::identity("", 0).1,
                        content_start: 0,
                        quote_bar_bytes: Vec::new(),
                        is_hr: false,
                        image: None,
                        code_ranges: Vec::new(),
                        inline_images: Vec::new(),
                        table: None,
                    })
                })
                .collect(),
            line_ranges: heights.iter().map(|_| 0..0).collect(),
            line_diffs: heights.iter().map(|_| LineDiff::default()).collect(),
            ghosts: heights.iter().map(|_| Vec::new()).collect(),
            ghost_height: heights.iter().map(|_| 0.0).collect(),
            trailing_ghosts: Vec::new(),
            trailing_ghost_height: 0.0,
            quote_bars: heights.iter().map(|_| Vec::new()).collect(),
            image_blocks: heights.iter().map(|_| None).collect(),
            inline_draws: heights.iter().map(|_| Vec::new()).collect(),
            image_urls: Vec::new(),
            img_vpad: 0.0,
            img_label_size: 14.0,
            image_border: Color::TRANSPARENT,
            image_bg: Color::TRANSPARENT,
            code_bg: Color::TRANSPARENT,
            table_lines: heights.iter().map(|_| None).collect(),
            table_cell_pad_x: 0.0,
            table_cell_pad_y: 0.0,
            table_border: 0.0,
            table_border_color: Color::TRANSPARENT,
            table_bg: Color::TRANSPARENT,
            table_header_bg: Color::TRANSPARENT,
            quote_bar_width: 2.0,
            quote_bar_color: Color::TRANSPARENT,
            measured_start: 0,
            measured_count: heights.len(),
            preedit_caret: None,
            diff_colors: DiffColors {
                added_bg: Color::TRANSPARENT,
                added_inline: Color::TRANSPARENT,
                deleted_bg: Color::TRANSPARENT,
                deleted_inline: Color::TRANSPARENT,
            },
            tops: compute_tops(heights, pad_top),
            scroll_y: 0.0,
            width: 100.0,
            pad_top,
            pad_bottom,
            pad_x: 0.0,
        }
    }

    #[test]
    fn visible_range_covers_viewport() {
        // 5 lines of height 10, pad_top 0 => tops [0,10,20,30,40,50]
        let doc = fixture(&[10.0; 5], 0.0, 0.0);
        // Viewport [0,25) => lines 0,1,2 (line 2 spans 20..30, intersects).
        assert_eq!(doc.visible_range(25.0), (0, 3));
        // Scrolled to 25, viewport height 20 => [25,45) => lines 2,3,4.
        let mut doc = doc;
        doc.scroll_y = 25.0;
        assert_eq!(doc.visible_range(20.0), (2, 5));
    }

    #[test]
    fn scroll_clamps_to_content() {
        let mut doc = fixture(&[10.0; 5], 0.0, 0.0); // content height 50
        doc.scroll_by(1000.0, 20.0);
        assert_eq!(doc.scroll_y, 30.0); // 50 - 20
        doc.scroll_by(-1000.0, 20.0);
        assert_eq!(doc.scroll_y, 0.0);
    }

    #[test]
    fn short_doc_has_no_scroll() {
        let mut doc = fixture(&[10.0; 2], 0.0, 0.0); // content 20 < viewport 100
        doc.scroll_by(50.0, 100.0);
        assert_eq!(doc.scroll_y, 0.0);
        assert_eq!(doc.max_scroll(100.0), 0.0);
    }

    /// The load-bearing Phase 4 path: a real layout, caret geometry, and click
    /// hit-testing must round-trip through Parley + the segment map. Runs headless
    /// (fonts, no GPU).
    #[test]
    fn caret_and_hit_test_roundtrip() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let mut buffer: Buffer = "hello world\nsecond line here\n".parse().unwrap();
        let snapshot = buffer.render_snapshot();
        let params = test_params(&theme, 1200.0);
        let doc = DocLayout::build(
            &mut engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            0,
            &snapshot,
            &theme,
            None,
            None,
            &ImageCache::new(),
            0,
            &params,
            None,
            0,
            f32::INFINITY,
        );
        // Several buffer offsets (incl. second line) should map to a caret rect
        // whose center hit-tests back to the same offset.
        for &off in &[0usize, 6, 11, 12, 19, 27] {
            let (x0, y0, x1, y1) = doc.caret_rect(off, 2.0).expect("caret rect");
            let cx = ((x0 + x1) / 2.0) as f32;
            let cy = ((y0 + y1) / 2.0) as f32;
            let got = doc.hit_test(cx, cy).expect("hit test");
            assert_eq!(got, off, "offset {off} round-trip (got {got})");
        }
    }

    /// Ghost (deleted) rows offset the real line below them, and clicks in a ghost
    /// block are inert. Locks the Phase 5b interleave math (the plan's defect surface).
    #[test]
    fn ghost_rows_offset_and_are_inert() {
        use crate::buffer::Buffer;
        use crate::diff::DiffState;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let old_text = "line one\nline two\n";
        let new_text = "line one changed here\nline two\n";
        let mut base: Buffer = old_text.parse().unwrap();
        let diff = DiffState::compute(base.render_snapshot(), old_text, new_text);
        assert!(diff.has_hunks());

        let mut buf: Buffer = new_text.parse().unwrap();
        let snapshot = buf.render_snapshot();
        let params = test_params(&theme, 1200.0);
        let doc = DocLayout::build(
            &mut engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            0,
            &snapshot,
            &theme,
            Some(&diff),
            None,
            &ImageCache::new(),
            usize::MAX,
            &params,
            None,
            0,
            f32::INFINITY,
        );
        // Line 0's changed version has a deleted ghost stacked above it.
        assert!(doc.ghost_height[0] > 0.0, "expected a ghost above line 0");
        // The real line begins below its ghost block, and the caret follows.
        assert!(doc.real_top(0) > doc.tops[0]);
        let (_, cy0, _, _) = doc.caret_rect(0, 2.0).unwrap();
        assert!(cy0 as f32 >= doc.real_top(0) - 1.0);
        // A click in the ghost block is inert → maps to the real line start (offset 0).
        let ghost_mid = doc.tops[0] + doc.ghost_height[0] * 0.5;
        assert_eq!(doc.hit_test(50.0, ghost_mid), Some(0));
    }

    /// Trailing deletions (a hunk anchored past the last line, with no final newline) must
    /// render as trailing ghost rows below the last line — they used to vanish entirely.
    #[test]
    fn trailing_deletions_render_as_ghosts() {
        use crate::buffer::Buffer;
        use crate::diff::DiffState;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let old_text = "keep\ndelete one\ndelete two\n";
        let new_text = "keep"; // no trailing newline; the last two lines were deleted
        let mut base: Buffer = old_text.parse().unwrap();
        let diff = DiffState::compute(base.render_snapshot(), old_text, new_text);
        assert!(diff.has_hunks());

        let mut buf: Buffer = new_text.parse().unwrap();
        let snapshot = buf.render_snapshot();
        let params = test_params(&theme, 1200.0);
        let doc = DocLayout::build(
            &mut engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            0,
            &snapshot,
            &theme,
            Some(&diff),
            None,
            &ImageCache::new(),
            usize::MAX,
            &params,
            None,
            0,
            f32::INFINITY,
        );
        assert!(
            !doc.trailing_ghosts.is_empty(),
            "trailing deletion should render ghost rows (was: vanished)"
        );
        assert!(doc.trailing_ghost_height > 0.0);
        // Trailing ghost height is included in the scroll extent.
        assert!(doc.content_height() > doc.tops[doc.line_count()]);
    }

    /// The layout cache reuses unchanged lines: a warm rebuild (cache populated)
    /// skips Parley shaping and is faster than the cold one. Also exercises that a
    /// bounded set of entries survives (one per distinct line content).
    #[test]
    fn line_cache_reuses_and_speeds_up() {
        use crate::buffer::Buffer;
        use std::time::Instant;
        let mut engine = TextEngine::new();
        let mut cache = LineCache::new();
        let theme = EditorTheme::dracula();
        // Distinct lines so each is a unique cache entry (the real scenario).
        let text: String = (0..400)
            .map(|i| format!("Line {i}: the quick brown fox jumps over the lazy dog.\n"))
            .collect();
        let mut buffer: Buffer = text.parse().unwrap();
        let snapshot = buffer.render_snapshot();
        let params = test_params(&theme, 1000.0);
        let build = |engine: &mut TextEngine, cache: &mut LineCache| {
            DocLayout::build(
                engine,
                cache,
                &mut RenderCache::new(),
                &mut HeightCache::new(),
                &mut TableCache::new(),
                0,
                &snapshot,
                &theme,
                None,
                None,
                &ImageCache::new(),
                0,
                &params,
                None,
                0,
                f32::INFINITY,
            )
        };
        let t0 = Instant::now();
        let _ = build(&mut engine, &mut cache);
        let cold = t0.elapsed();
        let t1 = Instant::now();
        let _ = build(&mut engine, &mut cache);
        let warm = t1.elapsed();
        println!("[cache] cold={cold:?} warm={warm:?}");
        assert_eq!(
            cache.map.len(),
            401,
            "one entry per distinct line (+trailing)"
        );
        assert!(
            warm < cold,
            "warm rebuild ({warm:?}) should beat cold ({cold:?})"
        );
    }

    /// The render cache must recompute the cursor's line (marker reveal is
    /// cursor-dependent) while reusing others — the "don't serve a stale cursor
    /// line" correctness catch. Two builds share one RenderCache at the same buffer
    /// version but different cursor positions; the heading's `# ` hides/reveals.
    #[test]
    fn render_cache_recomputes_cursor_line() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let mut line_cache = LineCache::new();
        let mut render_cache = RenderCache::new();
        // "# Title\n" then a body line. Heading content "Title" starts at buffer 2.
        let mut buffer: Buffer = "# Title\nbody line two\n".parse().unwrap();
        let snapshot = buffer.render_snapshot();
        let params = test_params(&theme, 1200.0);
        let build =
            |engine: &mut TextEngine, lc: &mut LineCache, rc: &mut RenderCache, cursor: usize| {
                DocLayout::build(
                    engine,
                    lc,
                    rc,
                    &mut HeightCache::new(),
                    &mut TableCache::new(),
                    7,
                    &snapshot,
                    &theme,
                    None,
                    None,
                    &ImageCache::new(),
                    cursor,
                    &params,
                    None,
                    0,
                    f32::INFINITY,
                )
            };

        // Cursor on the body line (offset 10) → heading `# ` is hidden.
        let doc_off = build(&mut engine, &mut line_cache, &mut render_cache, 10);
        assert_eq!(
            doc_off.line_map(0).unwrap().buffer_to_display(2),
            0,
            "with cursor off the heading, `# ` is hidden so content starts at display 0"
        );

        // Same cache + version, cursor now on the heading (offset 0) → `# ` revealed.
        let doc_on = build(&mut engine, &mut line_cache, &mut render_cache, 0);
        assert_eq!(
            doc_on.line_map(0).unwrap().buffer_to_display(2),
            2,
            "cursor on the heading must recompute it (not serve the stale hidden render)"
        );
    }

    /// Virtualization: with a small `measure_to_y`, a large doc materializes only the
    /// top band, leaves the rest height-estimated, keeps the visible range inside the
    /// materialized set (no blank draws), and still round-trips caret↔hit-test on-screen.
    #[test]
    fn virtualized_build_materializes_only_visible() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let text: String = (0..3000)
            .map(|i| format!("Line {i}: the quick brown fox jumps over the lazy dog.\n"))
            .collect();
        let mut buffer: Buffer = text.parse().unwrap();
        let snapshot = buffer.render_snapshot();
        let viewport_h = 600.0f32;
        let params = test_params(&theme, 1000.0);
        let doc = DocLayout::build(
            &mut engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            1,
            &snapshot,
            &theme,
            None,
            None,
            &ImageCache::new(),
            0,
            &params,
            None,
            0, // anchor at the top
            viewport_h,
        );
        let n = doc.line_count();
        assert!(n >= 3000);
        assert!(
            doc.measured_count < n,
            "should estimate the tail, got measured_count={} of {n}",
            doc.measured_count
        );
        assert!(
            doc.measured_count >= 10,
            "should materialize at least the visible band, got {}",
            doc.measured_count
        );
        // The viewport never reaches into estimated lines at the top.
        assert!(!doc.needs_remeasure(viewport_h));
        let (first, last) = doc.visible_range(viewport_h);
        assert_eq!(first, 0);
        assert!(
            last <= doc.measured_count,
            "visible range {last} must stay within materialized {}",
            doc.measured_count
        );
        // Caret at the top round-trips through a materialized line.
        let (x0, y0, x1, y1) = doc.caret_rect(0, 2.0).expect("caret");
        let got = doc
            .hit_test(((x0 + x1) / 2.0) as f32, ((y0 + y1) / 2.0) as f32)
            .expect("hit");
        assert_eq!(got, 0);
        // Total content height is finite (estimated tail contributes real numbers).
        assert!(doc.content_height().is_finite() && doc.content_height() > 0.0);
    }

    /// Building around a deep anchor virtualizes BOTH the head (lines above) and the
    /// tail — the O(visible) win regardless of scroll depth — while keeping the band
    /// around the anchor fully laid out.
    #[test]
    fn virtualized_build_around_deep_anchor() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let text: String = (0..3000)
            .map(|i| format!("Line {i}: the quick brown fox jumps over the lazy dog.\n"))
            .collect();
        let mut buffer: Buffer = text.parse().unwrap();
        let snapshot = buffer.render_snapshot();
        let viewport_h = 600.0f32;
        let params = test_params(&theme, 1000.0);
        let anchor = 1500usize;
        let doc = DocLayout::build(
            &mut engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            1,
            &snapshot,
            &theme,
            None,
            None,
            &ImageCache::new(),
            0,
            &params,
            None,
            anchor,
            viewport_h,
        );
        let n = doc.line_count();
        // Head virtualized: the band starts near the anchor, not at line 0.
        assert!(
            doc.measured_start > 0 && doc.measured_start <= anchor,
            "head should be virtualized around the anchor, got start={}",
            doc.measured_start
        );
        // Tail virtualized: the band ends past the anchor but well before the end.
        assert!(
            doc.measured_count > anchor && doc.measured_count < n,
            "band should cover the anchor and estimate the tail, got count={} of {n}",
            doc.measured_count
        );
        // Only band lines are laid out; head + tail are empty placeholders (height 0),
        // while the band's lines have real, non-zero layouts.
        assert_eq!(doc.layouts[0].height(), 0.0, "head line placeholder");
        assert_eq!(doc.layouts[n - 1].height(), 0.0, "tail line placeholder");
        assert!(
            doc.layouts[anchor].height() > 0.0,
            "anchor line materialized"
        );
        // The band is far smaller than the document (true O(visible)).
        assert!(
            doc.measured_count - doc.measured_start < 400,
            "materialized band {} should be a small window of {n}",
            doc.measured_count - doc.measured_start
        );
    }

    /// Re-pinning to a captured anchor keeps the anchor line's on-screen position fixed
    /// across a rebuild even when off-screen head heights change (the anti-jump core).
    #[test]
    fn anchor_repin_is_stable() {
        // Heights: first 100 lines are "wrong" in doc A (all 10px) vs doc B (all 40px);
        // the anchor line and below are identical. Pinning the anchor must cancel the
        // head delta so the anchor stays put.
        let mut ha = vec![10.0f32; 200];
        let mut hb = vec![40.0f32; 200];
        for i in 100..200 {
            ha[i] = 25.0;
            hb[i] = 25.0;
        }
        let mut a = fixture(&ha, 4.0, 4.0);
        let b = fixture(&hb, 4.0, 4.0);
        // Scroll doc A so line 100 sits 5px below the viewport top.
        a.scroll_y = a.tops[100] + 5.0;
        let (line, off) = a.scroll_anchor();
        assert_eq!(line, 100);
        assert!((off - 5.0).abs() < 1e-3);
        // Re-pin the same anchor in doc B (whose head is 3× taller).
        let repinned = b.anchor_scroll_y(line, off);
        // Line 100's top is 5px above the viewport top in BOTH — its on-screen position
        // (tops[100] - scroll_y) is identical, so the visible content did not jump.
        assert!((b.tops[100] - repinned - (a.tops[100] - a.scroll_y)).abs() < 1e-3);
    }

    /// The column pre-pass: `col_x` strictly increases, the widest cell drives its
    /// column's width, and the whole grid fits inside `max_advance`.
    #[test]
    fn table_layout_columns_and_widest_cell() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        // Column 1 has a very long body cell; column 0 is short throughout.
        let mut buf: Buffer =
            "| a | b |\n|---|---|\n| x | a much much much longer cell |\n| y | z |\n"
                .parse()
                .unwrap();
        let snap = buf.render_snapshot();
        let styles = snap.inline_styles_by_line();
        let params = test_params(&theme, 1200.0);
        let max_advance = 1000.0;
        let table = snap.table_containing_offset(0).unwrap();
        let tl = build_table_layout(
            &mut engine,
            &snap,
            &theme,
            table,
            &styles,
            &params,
            max_advance,
        );

        assert_eq!(tl.col_x.len(), 2);
        assert!(tl.col_x[1] > tl.col_x[0], "col_x strictly increasing");
        assert!(
            tl.col_w[1] > tl.col_w[0],
            "the long cell's column is wider: {:?}",
            tl.col_w
        );
        // Right edge of the last column's box (col origin + content width + cell padding
        // + trailing border) must fit the content width.
        let cell_pad_x = TABLE_CELL_PAD_X * params.scale;
        let border = TABLE_BORDER * params.scale;
        let total_w =
            tl.col_x.last().unwrap() + tl.col_w.last().unwrap() + 2.0 * cell_pad_x + border;
        assert!(
            total_w <= max_advance + 0.5,
            "grid fits the content width, total_w={total_w}"
        );
        // Header + two body rows → three row heights, all positive.
        assert_eq!(tl.row_heights.len(), 3);
        assert!(tl.row_heights.iter().all(|&h| h > 0.0));
    }

    /// A cell containing inline `` `code` `` carries its display-byte code ranges into the
    /// `CellSlot`, so the draw pass can paint the translucent chip behind the code glyphs.
    #[test]
    fn table_cell_inline_code_reaches_slot() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let mut buf: Buffer = "| a | b |\n|---|---|\n| plain | `code` |\n"
            .parse()
            .unwrap();
        let snap = buf.render_snapshot();
        let styles = snap.inline_styles_by_line();
        let params = test_params(&theme, 1200.0);
        let table = snap.table_containing_offset(0).unwrap();
        let tl = build_table_layout(&mut engine, &snap, &theme, table, &styles, &params, 1000.0);

        // row_layouts[1] is the single body row; col 1 holds `code`, col 0 is plain.
        let body = &tl.row_layouts[1];
        let code_cell = body[1].as_ref().expect("body code cell present");
        assert!(
            !code_cell.code_ranges.is_empty(),
            "inline-code cell must carry code ranges for the chip"
        );
        let plain_cell = body[0].as_ref().expect("body plain cell present");
        assert!(
            plain_cell.code_ranges.is_empty(),
            "a plain cell has no code ranges"
        );
    }

    /// The size cap: a table exceeding the body-row limit is not grid-rendered
    /// (`table_grid_ok` is false), so it falls through to raw pipe text.
    #[test]
    fn table_size_cap_engages() {
        use crate::buffer::Buffer;
        let mut doc = String::from("| a | b |\n|---|---|\n");
        for i in 0..(TABLE_MAX_BODY_ROWS + 5) {
            doc.push_str(&format!("| r{i} | v{i} |\n"));
        }
        let mut buf: Buffer = doc.parse().unwrap();
        let snap = buf.render_snapshot();
        let table = snap.table_containing_offset(0).unwrap();
        assert!(table.body.len() > TABLE_MAX_BODY_ROWS);
        assert!(
            !table_grid_ok(table),
            "oversized table must not grid-render"
        );

        // A small table is fine.
        let mut small: Buffer = "| a | b |\n|---|---|\n| 1 | 2 |\n".parse().unwrap();
        let snap2 = small.render_snapshot();
        assert!(table_grid_ok(snap2.table_containing_offset(0).unwrap()));
    }

    /// End-to-end grid layout: with the cursor off the table, the table's rows become
    /// `table_lines` entries, their text layouts are empty, `content_height`/`tops`
    /// reflect the grid row heights, and `tops` stays monotonic.
    #[test]
    fn doc_layout_grid_rows_off_cursor() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        // A heading, a paragraph, then a 2-col table (lines 4=header, 5=delim, 6,7=body).
        let mut buf: Buffer = "# Title\n\npara\n\n| a | b |\n|:--|--:|\n| 1 | 2 |\n| 3 | 4 |\n"
            .parse()
            .unwrap();
        let snap = buf.render_snapshot();
        let params = test_params(&theme, 1200.0);
        let doc = DocLayout::build(
            &mut engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            0,
            &snap,
            &theme,
            None,
            None,
            &ImageCache::new(),
            0, // cursor at doc start, off the table
            &params,
            None,
            0,
            f32::INFINITY,
        );
        // Table lines are the header/delimiter/body rows.
        for (line, want) in [
            (4, RowKind::Header),
            (5, RowKind::Delimiter),
            (6, RowKind::Body(0)),
            (7, RowKind::Body(1)),
        ] {
            let entry = doc.table_lines[line]
                .as_ref()
                .unwrap_or_else(|| panic!("line {line} should be a grid row"));
            assert_eq!(entry.1, want, "line {line} row kind");
            assert!(
                doc.renders[line].table.is_some(),
                "line {line} render flags a table row"
            );
            assert!(
                doc.renders[line].text.is_empty(),
                "line {line} text is hidden in grid mode"
            );
        }
        // Non-table lines carry no table entry.
        assert!(doc.table_lines[0].is_none());
        assert!(doc.table_lines[2].is_none());
        // `tops` is monotonic and content height is finite/positive.
        for w in doc.tops.windows(2) {
            assert!(w[1] >= w[0], "tops monotonic");
        }
        // Header row height comes from the grid, not a single text line.
        let header_h = doc.tops[5] - doc.real_top(4);
        assert!(header_h > 0.0);
        assert!(doc.content_height() > 0.0 && doc.content_height().is_finite());
    }

    /// The reveal flip: cursor off the table → every row is a grid row (text hidden);
    /// cursor on ANY table line → all rows revert to raw pipe text (no table ref).
    #[test]
    fn doc_layout_table_reveal_flip() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let theme = EditorTheme::dracula();
        let src = "| a | b |\n|---|---|\n| 1 | 2 |\n| 3 | 4 |\n";
        let mut buf: Buffer = src.parse().unwrap();
        let snap = buf.render_snapshot();
        let params = test_params(&theme, 1200.0);
        let build = |engine: &mut TextEngine, cursor: usize| {
            DocLayout::build(
                engine,
                &mut LineCache::new(),
                &mut RenderCache::new(),
                &mut HeightCache::new(),
                &mut TableCache::new(),
                0,
                &snap,
                &theme,
                None,
                None,
                &ImageCache::new(),
                cursor,
                &params,
                None,
                0,
                f32::INFINITY,
            )
        };
        // Cursor off the table (past the end): grid mode, header row hidden + flagged.
        let off = build(&mut engine, src.len());
        assert!(
            off.renders[0].table.is_some(),
            "header is a grid row off-cursor"
        );
        assert!(off.renders[0].text.is_empty(), "grid row text hidden");

        // Cursor inside a body row (line 3, offset ~30): all rows revert to raw text.
        let on = build(&mut engine, 30);
        for line in 0..4 {
            assert!(
                on.renders[line].table.is_none(),
                "line {line} reverts to raw text when the cursor is in the table"
            );
            assert!(
                !on.renders[line].text.is_empty(),
                "line {line} shows raw pipe text in reveal mode"
            );
            assert!(on.table_lines[line].is_none());
        }
    }

    /// Build a whole-document layout with the cursor off the table (grid mode).
    fn build_grid_doc(engine: &mut TextEngine, snap: &RenderSnapshot, cursor: usize) -> DocLayout {
        let theme = EditorTheme::dracula();
        let params = test_params(&theme, 1200.0);
        DocLayout::build(
            engine,
            &mut LineCache::new(),
            &mut RenderCache::new(),
            &mut HeightCache::new(),
            &mut TableCache::new(),
            0,
            snap,
            &theme,
            None,
            None,
            &ImageCache::new(),
            cursor,
            &params,
            None,
            0,
            f32::INFINITY,
        )
    }

    /// Clicking a grid table row lands the caret inside the clicked cell (character-level
    /// via the cell's own layout + map), not at the line start / column 0.
    #[test]
    fn hit_test_lands_inside_clicked_table_cell() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        // Leading paragraph so the table isn't at offset 0; cursor 0 keeps it grid mode.
        let src = "para\n\n| aa | bb | cc |\n| --- | --- | --- |\n| 11 | 22 | 33 |\n";
        let mut buf: Buffer = src.parse().unwrap();
        let snap = buf.render_snapshot();
        let doc = build_grid_doc(&mut engine, &snap, 0);

        let body_line = 4;
        let (tl, kind) = doc.table_lines[body_line]
            .as_ref()
            .expect("body row is a grid row");
        assert_eq!(*kind, RowKind::Body(0));
        let line_start = snap.line_byte_range(body_line).start;
        let table = snap.table_containing_offset(line_start).unwrap();
        let y = doc.real_top(body_line) + 2.0;
        for c in 0..3 {
            let content = table.body[0].cells[c].content.clone();
            let x = doc.pad_x + tl.col_x[c] + doc.table_cell_pad_x + tl.col_w[c] * 0.5;
            let off = doc.hit_test(x, y).expect("hit-test returns an offset");
            assert!(
                off >= content.start && off <= content.end,
                "click on column {c} lands in its cell content {content:?}, got {off}"
            );
            assert_ne!(off, line_start, "click does not collapse to the line start");
        }
    }

    /// Tier-2 character-level: clicking nearer a cell's end yields a larger buffer offset
    /// than clicking nearer its start.
    #[test]
    fn hit_test_table_cell_start_before_end() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let src = "para\n\n| aaaa | bbbb |\n| --- | --- |\n| wxyz | 2222 |\n";
        let mut buf: Buffer = src.parse().unwrap();
        let snap = buf.render_snapshot();
        let doc = build_grid_doc(&mut engine, &snap, 0);

        let (tl, _) = doc.table_lines[4].as_ref().unwrap();
        let y = doc.real_top(4) + 2.0;
        let base = doc.pad_x + tl.col_x[0] + doc.table_cell_pad_x;
        let near_start = doc.hit_test(base + 1.0, y).unwrap();
        let near_end = doc.hit_test(base + tl.col_w[0] - 1.0, y).unwrap();
        assert!(
            near_end > near_start,
            "click near the cell end ({near_end}) is past click near its start ({near_start})"
        );
    }

    /// Clicks left of / right of the grid clamp to the first / last column.
    #[test]
    fn hit_test_table_clamps_to_edge_columns() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let src = "para\n\n| aa | bb | cc |\n| --- | --- | --- |\n| 11 | 22 | 33 |\n";
        let mut buf: Buffer = src.parse().unwrap();
        let snap = buf.render_snapshot();
        let doc = build_grid_doc(&mut engine, &snap, 0);

        let line_start = snap.line_byte_range(4).start;
        let table = snap.table_containing_offset(line_start).unwrap();
        let y = doc.real_top(4) + 2.0;
        let first = table.body[0].cells[0].content.clone();
        let last = table.body[0].cells[2].content.clone();

        let left = doc.hit_test(0.0, y).unwrap();
        assert!(
            left >= first.start && left <= first.end,
            "far-left click clamps to the first cell {first:?}, got {left}"
        );
        let right = doc.hit_test(1199.0, y).unwrap();
        assert!(
            right >= last.start && right <= last.end,
            "far-right click clamps to the last cell {last:?}, got {right}"
        );
    }

    /// Clicking an empty grid cell lands the caret between its pipes (on a space), not on
    /// a pipe.
    #[test]
    fn hit_test_empty_table_cell_lands_off_pipe() {
        use crate::buffer::Buffer;
        let mut engine = TextEngine::new();
        let src = "para\n\n| a | b |\n| --- | --- |\n|  |  |\n";
        let mut buf: Buffer = src.parse().unwrap();
        let snap = buf.render_snapshot();
        let doc = build_grid_doc(&mut engine, &snap, 0);

        let body_line = 4;
        let (tl, _) = doc.table_lines[body_line].as_ref().unwrap();
        let y = doc.real_top(body_line) + 2.0;
        let x = doc.pad_x + tl.col_x[0] + doc.table_cell_pad_x + tl.col_w[0] * 0.5;
        let off = doc.hit_test(x, y).unwrap();
        assert_eq!(
            snap.rope.get_byte(off),
            Some(b' '),
            "empty-cell click lands on a space between the pipes, not on a pipe"
        );
    }
}