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
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
use anyhow::{Context, Result};
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
text::{Line, Span, Text},
widgets::{Block, Borders, Paragraph, Widget, Wrap},
};
use std::ops::{Deref, DerefMut};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use crate::highlight::{
HiSpan, LangKind, LangRegistry, compute_line_starts, decode_semantic_tokens, highlight_text,
lang_for_extension,
};
use crate::widgets::scrollbar;
/// Hard cap on the size of a single text file the editor will load.
/// 50MB comfortably accommodates real-world LSP / build / debug logs
/// (the user hit "File too large" on a 7.4MB lsp.log under the old 5MB
/// cap) while still capping pathological inputs that would freeze the
/// UI thread during initial `Vec<String>` allocation + tree-sitter
/// highlight recompute. The cap applies AFTER the dedicated image /
/// PDF / spreadsheet branches in `open`, so larger media keep their
/// own per-format limits.
const MAX_FILE_BYTES: u64 = 50 * 1024 * 1024;
const MAX_IMAGE_BYTES: u64 = 25 * 1024 * 1024;
const IMAGE_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "bmp", "webp"];
/// Read-only image preview attached to a tab. Holds the raw file bytes so
/// the OSC-1337 inline-image bake can re-fit on resize without rereading
/// from disk, plus parsed metadata for the header line that's painted in
/// the buffer (so non-image-capable terminals still see meaningful info).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageView {
pub bytes: Vec<u8>,
pub format_label: String,
pub pixel_w: u32,
pub pixel_h: u32,
pub byte_size: u64,
/// Monotonic content stamp, fresh on every (re)load of the bytes. Part
/// of the overlay's re-emit key: a PDF rebuilt on disk reloads to the
/// same path, rect and page, and without this stamp the baked overlay
/// kept showing the pre-rebuild pixels until a page turn.
pub generation: u64,
/// Set when this preview was rasterised from a PDF page; tracks the
/// page-navigation state so re-renders on Page Down/Up know which
/// page to ask the rasteriser for next.
pub pdf: Option<PdfState>,
}
/// Next value for [`ImageView::generation`]: process-wide monotonic counter,
/// bumped on every byte (re)load anywhere.
fn next_image_generation() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PdfState {
pub source_path: PathBuf,
pub current_page: u32,
pub page_count: Option<u32>,
pub backend: crate::pdf::PdfBackend,
pub source_byte_size: u64,
/// Link regions of `current_page`, extracted lazily on the first click
/// (never on render — a page flip must not pay a second subprocess).
/// `None` = not extracted yet; cleared on every page change.
pub links: Option<crate::pdf::PageLinks>,
}
/// The background the image/sheet/diff canvases fill with. On iTerm2,
/// `Reset` inherits the session bg that `SetColors` re-tints per theme, so
/// the canvas matches the surrounding panes for free. Every other host
/// (Ghostty, Kitty, sixel) ignores `SetColors`, so `Reset` falls through to
/// the terminal's own background — and the frame prefill cannot save it,
/// because these fills override the prefill (ratatui applies any `Some`
/// bg, and `Some(Reset)` is `Some`). Those hosts paint the theme bg
/// explicitly instead, or a CSV/PDF/diff tab sits as a host-black island
/// inside an otherwise themed frame.
fn canvas_bg(host_is_iterm2: bool, theme: crate::theme::Theme) -> Color {
if host_is_iterm2 {
Color::Reset
} else {
theme.editor_bg()
}
}
/// The background the image/PDF canvas fills with — a separate choice from
/// [`canvas_bg`] because the picture is an inline image the canvas must not
/// occlude. Kitty places the preview at `KITTY_Z_BELOW_TEXT_AND_BG` and
/// draws any cell with a non-default background OVER an image that deep, so
/// the canvas has to keep the DEFAULT background there; iTerm2 keeps
/// `Reset` for the [`canvas_bg`] reason (the `SetColors` session bg).
/// Neither host shows an untinted island while a picture is up: the bake
/// letterboxes with the theme bg pixel, covering every canvas cell. Sixel
/// blits into the cell buffer over the canvas, and a no-graphics host shows
/// only the placeholder, so both keep the themed fill.
fn image_canvas_bg(
protocol: crate::iterm2_inline::InlineImageProtocol,
theme: crate::theme::Theme,
) -> Color {
use crate::iterm2_inline::InlineImageProtocol;
match protocol {
InlineImageProtocol::ITerm2 | InlineImageProtocol::Kitty => Color::Reset,
InlineImageProtocol::Sixel | InlineImageProtocol::None => theme.editor_bg(),
}
}
fn render_image_placeholder(
image: &ImageView,
path: Option<&Path>,
inner: Rect,
buf: &mut Buffer,
bg: Color,
) {
// Solid bg fill so the OSC-1337 inline image (emitted post-frame on
// capable terminals) sits on a clean canvas; on non-capable terminals
// the metadata header below is the only content the user sees.
let bg_style = Style::default().bg(bg);
for y in inner.y..inner.y + inner.height {
for x in inner.x..inner.x + inner.width {
buf[(x, y)].set_style(bg_style);
buf[(x, y)].set_symbol(" ");
}
}
let name = path
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| String::from("(unnamed image)"));
let header = if let Some(pdf) = image.pdf.as_ref() {
let page_label = match pdf.page_count {
Some(total) => format!("page {} / {}", pdf.current_page, total),
None => format!("page {}", pdf.current_page),
};
format!(
" {} · {} · {}×{} · {} · PDF (← / → to flip) ",
name,
page_label,
image.pixel_w,
image.pixel_h,
format_bytes_human(pdf.source_byte_size),
)
} else {
format!(
" {} · {}×{} · {} · {} ",
name,
image.pixel_w,
image.pixel_h,
format_bytes_human(image.byte_size),
image.format_label,
)
};
buf.set_string(
inner.x,
inner.y,
&header,
Style::default()
.fg(Color::White)
.bg(Color::Rgb(0x09, 0x4d, 0x77))
.add_modifier(Modifier::BOLD),
);
}
fn render_sheet(
view: &crate::sheet::SheetView,
path: Option<&Path>,
inner: Rect,
buf: &mut Buffer,
bg: Color,
) {
// Bg fill so the spreadsheet sits on a clean canvas regardless of
// what the previous tab left behind (see [`canvas_bg`]).
let bg_style = Style::default().bg(bg);
for y in inner.y..inner.y + inner.height {
for x in inner.x..inner.x + inner.width {
buf[(x, y)].set_style(bg_style);
buf[(x, y)].set_symbol(" ");
}
}
if inner.height < 3 || inner.width < 8 {
return;
}
let name = path
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| String::from("(unnamed sheet)"));
let sheet = match view.sheets.get(view.current_sheet) {
Some(s) => s,
None => return,
};
let row_count = sheet.row_count();
let col_count = sheet.col_count();
let header = format!(
" {} · {} · sheet {}/{} ({}) · {} rows × {} cols ",
name,
view.kind.label(),
view.current_sheet + 1,
view.sheets.len(),
sheet.name,
row_count,
col_count,
);
buf.set_string(
inner.x,
inner.y,
&header,
Style::default()
.fg(Color::White)
.bg(Color::Rgb(0x09, 0x4d, 0x77))
.add_modifier(Modifier::BOLD),
);
let grid_top = inner.y + 1;
let grid_height = inner.height.saturating_sub(2); // header + status row
let grid_w = inner.width;
if grid_height < 2 || col_count == 0 {
return;
}
// Reserve a row-number gutter so the user can see the absolute row
// index even after horizontal scrolling.
let gutter_w = (row_count.max(1).to_string().len() as u16 + 2).max(4);
if grid_w <= gutter_w + 2 {
return;
}
let body_x = inner.x + gutter_w;
let body_w = grid_w - gutter_w;
let header_y = grid_top;
let data_top = grid_top + 1;
let data_rows = grid_height.saturating_sub(1) as usize;
// Header row backdrop.
let head_style = Style::default()
.fg(Color::White)
.bg(Color::Rgb(0x07, 0x33, 0x55))
.add_modifier(Modifier::BOLD);
for x in inner.x..inner.x + inner.width {
buf[(x, header_y)].set_style(head_style);
buf[(x, header_y)].set_symbol(" ");
}
// Lay out visible columns from `scroll_col` rightwards until we run
// out of horizontal space.
let mut visible: Vec<(usize, u16)> = Vec::new(); // (col_idx, x_offset)
let mut x_off = 0u16;
for (c, w) in sheet.col_widths.iter().enumerate().skip(sheet.scroll_col) {
if x_off + w + 1 > body_w {
break;
}
visible.push((c, x_off));
x_off += w + 1; // +1 for inter-column gap
}
// Header text.
for (c, x_off) in &visible {
let label = sheet.headers.get(*c).map(|s| s.as_str()).unwrap_or("");
let cell_x = body_x + *x_off;
let w = sheet.col_widths[*c];
write_cell(buf, cell_x, header_y, w, label, head_style);
}
// Data rows.
let row_end = (sheet.scroll_row + data_rows).min(row_count);
// Base rows wear the canvas bg (see [`canvas_bg`]); alternating rows
// keep an explicit lift for the zebra stripe.
let row_style = Style::default().fg(Color::White).bg(bg);
let alt_row_style = Style::default()
.fg(Color::White)
.bg(Color::Rgb(0x24, 0x29, 0x37));
let gutter_style = Style::default().fg(Color::DarkGray);
for (display_row, row_idx) in (sheet.scroll_row..row_end).enumerate() {
let y = data_top + display_row as u16;
let style = if display_row % 2 == 0 {
row_style
} else {
alt_row_style
};
for x in inner.x..inner.x + inner.width {
buf[(x, y)].set_style(style);
buf[(x, y)].set_symbol(" ");
}
let row_label = format!(" {:>width$} ", row_idx + 1, width = (gutter_w - 2) as usize);
buf.set_string(
inner.x,
y,
&row_label,
gutter_style.bg(style.bg.unwrap_or(Color::Reset)),
);
let row = &sheet.rows[row_idx];
for (c, x_off) in &visible {
let cell_text = row.get(*c).map(|s| s.as_str()).unwrap_or("");
let w = sheet.col_widths[*c];
write_cell(buf, body_x + *x_off, y, w, cell_text, style);
}
}
// Status row at the bottom showing scroll position + nav hint.
let status_y = inner.y + inner.height - 1;
let visible_first = sheet.scroll_row + 1;
let visible_last = (sheet.scroll_row + data_rows).min(row_count);
let visible_col_first = sheet.scroll_col + 1;
let visible_col_last = visible
.last()
.map(|(c, _)| *c + 1)
.unwrap_or(visible_col_first);
let status = format!(
" rows {visible_first}–{visible_last} of {row_count} · cols {visible_col_first}–{visible_col_last} of {col_count} · ←/→ ↑/↓ PgUp/PgDn Tab=next sheet "
);
buf.set_string(
inner.x,
status_y,
&status,
Style::default()
.fg(Color::Gray)
.bg(Color::Rgb(0x14, 0x18, 0x22)),
);
}
/// Returns the hit-test rects of the prev / next change arrows painted
/// in the diff header (in that order). Both are `Rect::default()` when
/// the header was too narrow to allocate them.
fn render_diff(
diff: &mut crate::widgets::diff::DiffData,
inner: Rect,
buf: &mut Buffer,
bg: Color,
) -> (Rect, Rect) {
use crate::widgets::diff::DiffRow;
// Background fill so the diff sits on a clean canvas (see [`canvas_bg`]).
let bg_style = Style::default().bg(bg);
for y in inner.y..inner.y + inner.height {
for x in inner.x..inner.x + inner.width {
buf[(x, y)].set_style(bg_style);
buf[(x, y)].set_symbol(" ");
}
}
if inner.height < 3 || inner.width < 16 {
return (Rect::default(), Rect::default());
}
if diff.unified {
return render_unified_deletion(diff, inner, buf, bg);
}
let left_name = diff
.left_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| diff.left_path.display().to_string());
let right_is_real = diff.right_path != std::path::Path::new("/dev/null")
&& !diff.right_path.as_os_str().is_empty();
let right_name = if right_is_real {
Some(
diff.right_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| diff.right_path.display().to_string()),
)
} else {
None
};
let header = match (&right_name, diff.bytes_differ_but_lines_equal) {
(Some(r), true) => format!(
" diff: {left_name} \u{2194} {r} \u{2022} whitespace-only change (trailing newline / CRLF / BOM) — no line-level diff "
),
(Some(r), false) => format!(" diff: {left_name} \u{2194} {r} "),
// Synthetic git-diff text view: no real right-side path, so the
// header reads as the diff command instead of trailing "↔ null".
(None, _) => format!(" {left_name} "),
};
let head_bg = if diff.bytes_differ_but_lines_equal {
Color::Rgb(0x8a, 0x4a, 0x10)
} else {
Color::Rgb(0x09, 0x4d, 0x77)
};
let head_style = Style::default()
.fg(Color::White)
.bg(head_bg)
.add_modifier(Modifier::BOLD);
for x in inner.x..inner.x + inner.width {
buf[(x, inner.y)].set_style(head_style);
buf[(x, inner.y)].set_symbol(" ");
}
buf.set_string(inner.x, inner.y, &header, head_style);
let (prev_arrow, next_arrow) = paint_diff_nav_arrows(inner, head_bg, buf);
// Two columns split exactly down the middle. Each column has a small
// line-number gutter on its left and a 1-cell sign column showing
// -, +, or space.
let body_top = inner.y + 1;
let body_height = inner.height.saturating_sub(2);
let status_y = inner.y + inner.height - 1;
if body_height == 0 {
return (prev_arrow, next_arrow);
}
let half = inner.width / 2;
if half < 8 {
return (prev_arrow, next_arrow);
}
let left_max = diff.left_lines.len();
let right_max = diff.right_lines.len();
let l_gutter = (left_max + 1).to_string().len() as u16 + 1;
let r_gutter = (right_max + 1).to_string().len() as u16 + 1;
// Per-column layout: gutter + sign + content.
let l_x = inner.x;
let l_sign_x = l_x + l_gutter;
let l_text_x = l_sign_x + 2;
let l_text_w = half.saturating_sub(l_gutter + 2 + 1); // -1 spacer
let r_x = inner.x + half + 1;
let r_sign_x = r_x + r_gutter;
let r_text_x = r_sign_x + 2;
let r_text_w = (inner.width - (half + 1)).saturating_sub(r_gutter + 2);
// Vertical seam between the two columns.
let seam_x = inner.x + half;
for y in body_top..body_top + body_height {
buf[(seam_x, y)].set_symbol("\u{2502}");
buf[(seam_x, y)].set_style(Style::default().fg(Color::Rgb(0x3a, 0x42, 0x52)));
}
let total = diff.rows.len();
let viewport = body_height as usize;
let max_scroll = total.saturating_sub(viewport);
if diff.scroll > max_scroll {
diff.scroll = max_scroll;
}
let removed_bg = Color::Rgb(0x4a, 0x1f, 0x1f);
let removed_fg = Color::Rgb(0xff, 0xb3, 0xb3);
let added_bg = Color::Rgb(0x1f, 0x42, 0x2a);
let added_fg = Color::Rgb(0xb6, 0xee, 0xc4);
let equal_fg = Color::Rgb(0xc5, 0xcd, 0xd9);
let gutter_fg = Color::Rgb(0x6c, 0x7d, 0x9c);
let find_needle = diff.find.needle.clone();
let find_opts = diff.find.opts;
let find_active = diff.find.active;
// VS Code-style selection highlight: when a single word/run is selected,
// every OTHER occurrence on screen gets a muted-blue box, mirroring the
// plain editor. Painted per row below, under the brighter active band.
let occ_needle = diff.selection_occurrence_needle();
let end = (diff.scroll + viewport).min(total);
for (vis_row, row_idx) in (diff.scroll..end).enumerate() {
let y = body_top + vis_row as u16;
let row = diff.rows[row_idx];
let (l_cell_bg, l_sign, l_text) = match row {
DiffRow::Equal { left, .. } => (
bg,
' ',
diff.left_lines.get(left).cloned().unwrap_or_default(),
),
DiffRow::Removed { left } => (
removed_bg,
'-',
diff.left_lines.get(left).cloned().unwrap_or_default(),
),
DiffRow::Replaced { left, .. } => (
removed_bg,
'-',
diff.left_lines.get(left).cloned().unwrap_or_default(),
),
DiffRow::Added { .. } => (added_bg, ' ', String::new()),
};
let (r_cell_bg, r_sign, r_text) = match row {
DiffRow::Equal { right, .. } => (
bg,
' ',
diff.right_lines.get(right).cloned().unwrap_or_default(),
),
DiffRow::Added { right } => (
added_bg,
'+',
diff.right_lines.get(right).cloned().unwrap_or_default(),
),
DiffRow::Replaced { right, .. } => (
added_bg,
'+',
diff.right_lines.get(right).cloned().unwrap_or_default(),
),
DiffRow::Removed { .. } => (removed_bg, ' ', String::new()),
};
// Left column.
let l_left_idx = match row {
DiffRow::Equal { left, .. }
| DiffRow::Removed { left }
| DiffRow::Replaced { left, .. } => Some(left),
DiffRow::Added { .. } => None,
};
let l_label = l_left_idx
.map(|i| format!("{:>width$} ", i + 1, width = l_gutter as usize - 1))
.unwrap_or_else(|| " ".repeat(l_gutter as usize));
buf.set_string(
l_x,
y,
&l_label,
Style::default().fg(gutter_fg).bg(l_cell_bg),
);
buf.set_string(
l_sign_x,
y,
format!("{l_sign} "),
Style::default()
.fg(if l_cell_bg == removed_bg {
removed_fg
} else {
equal_fg
})
.bg(l_cell_bg)
.add_modifier(Modifier::BOLD),
);
let l_clipped: String = l_text
.chars()
.skip(diff.scroll_x)
.take(l_text_w as usize)
.collect();
let mut l_padded = l_clipped.clone();
let l_pad = (l_text_w as usize).saturating_sub(l_padded.chars().count());
for _ in 0..l_pad {
l_padded.push(' ');
}
buf.set_string(
l_text_x,
y,
&l_padded,
Style::default()
.fg(if l_cell_bg == removed_bg {
removed_fg
} else {
equal_fg
})
.bg(l_cell_bg),
);
// Right column.
let r_right_idx = match row {
DiffRow::Equal { right, .. }
| DiffRow::Added { right }
| DiffRow::Replaced { right, .. } => Some(right),
DiffRow::Removed { .. } => None,
};
let r_label = r_right_idx
.map(|i| format!("{:>width$} ", i + 1, width = r_gutter as usize - 1))
.unwrap_or_else(|| " ".repeat(r_gutter as usize));
buf.set_string(
r_x,
y,
&r_label,
Style::default().fg(gutter_fg).bg(r_cell_bg),
);
buf.set_string(
r_sign_x,
y,
format!("{r_sign} "),
Style::default()
.fg(if r_cell_bg == added_bg {
added_fg
} else {
equal_fg
})
.bg(r_cell_bg)
.add_modifier(Modifier::BOLD),
);
let r_clipped: String = r_text
.chars()
.skip(diff.scroll_x)
.take(r_text_w as usize)
.collect();
let mut r_padded = r_clipped.clone();
let r_pad = (r_text_w as usize).saturating_sub(r_padded.chars().count());
for _ in 0..r_pad {
r_padded.push(' ');
}
buf.set_string(
r_text_x,
y,
&r_padded,
Style::default()
.fg(if r_cell_bg == added_bg {
added_fg
} else {
equal_fg
})
.bg(r_cell_bg),
);
// Inline-find highlight: overpaint every occurrence of the needle
// on top of the text just laid down, lighting the active match in
// a stronger colour. Reuses the plain editor's painter so the diff
// and text views look identical under Cmd+F.
if let Some(needle) = find_needle.as_deref() {
use crate::widgets::diff::DiffSide;
if l_left_idx.is_some() && l_text_w > 0 {
let active = find_active
.filter(|a| a.row == row_idx && a.side == DiffSide::Left)
.map(|a| (a.col_chars, a.len_chars));
paint_search_highlight(
buf,
l_text_x,
y,
l_text_w,
&l_text,
needle,
find_opts,
diff.scroll_x,
active,
&[],
);
}
if r_right_idx.is_some() && r_text_w > 0 {
let active = find_active
.filter(|a| a.row == row_idx && a.side == DiffSide::Right)
.map(|a| (a.col_chars, a.len_chars));
paint_search_highlight(
buf,
r_text_x,
y,
r_text_w,
&r_text,
needle,
find_opts,
diff.scroll_x,
active,
&[],
);
}
}
// Selection-occurrence highlight: muted-blue box over every match of
// the selected word on both columns. Painted after the find pass so
// the two layers compose like the plain editor; the active selection
// band overpaints these in a brighter blue below the row loop.
if let Some(needle) = occ_needle.as_deref() {
if l_left_idx.is_some() && l_text_w > 0 {
paint_selection_occurrences(
buf,
l_text_x,
y,
l_text_w,
&l_text,
needle,
diff.scroll_x,
&[],
);
}
if r_right_idx.is_some() && r_text_w > 0 {
paint_selection_occurrences(
buf,
r_text_x,
y,
r_text_w,
&r_text,
needle,
diff.scroll_x,
&[],
);
}
}
}
paint_diff_selection_band(
diff,
body_top,
body_height,
l_text_x,
l_text_w,
r_text_x,
r_text_w,
end,
buf,
);
// Status footer.
let visible_first = diff.scroll + 1;
let visible_last = end;
let status = format!(
" {visible_first}–{visible_last} of {total} · ↑/↓ PgUp/PgDn · ‹/› click arrows or F7 for next/prev change "
);
buf.set_string(
inner.x,
status_y,
&status,
Style::default()
.fg(Color::Gray)
.bg(Color::Rgb(0x14, 0x18, 0x22)),
);
(prev_arrow, next_arrow)
}
/// Overlay the diff's drag-select highlight on top of whatever the row
/// loop just painted. Walks the visible window once and paints the same
/// `paint_selection_band` overlay the regular text editor uses, so the
/// user sees an identical blue band over selected cells on whichever
/// column they're dragging in.
// Render helper: each argument is an independent painting input (data,
// geometry, target buffer); bundling them into a struct would add indirection
// without improving clarity.
#[allow(clippy::too_many_arguments)]
fn paint_diff_selection_band(
diff: &crate::widgets::diff::DiffData,
body_top: u16,
body_height: u16,
l_text_x: u16,
l_text_w: u16,
r_text_x: u16,
r_text_w: u16,
end: usize,
buf: &mut Buffer,
) {
use crate::widgets::diff::DiffSide;
let Some(sel) = diff.selection else {
return;
};
if !sel.has_area() {
return;
}
let (start, stop) = sel.normalized();
let (text_x, text_w) = match sel.side {
DiffSide::Left => (l_text_x, l_text_w),
DiffSide::Right => (r_text_x, r_text_w),
};
if text_w == 0 {
return;
}
let first_visible = diff.scroll;
let last_visible = end;
let row_start = start.0.max(first_visible);
let row_end = stop.0.min(last_visible.saturating_sub(1));
if row_end < row_start {
return;
}
for row_idx in row_start..=row_end {
let y = body_top + (row_idx - first_visible) as u16;
if y >= body_top + body_height {
break;
}
let (cs, ce) = if start.0 == stop.0 {
(start.1, stop.1)
} else if row_idx == start.0 {
(start.1, usize::MAX)
} else if row_idx == stop.0 {
(0, stop.1)
} else {
(0, usize::MAX)
};
let cs_screen = cs.saturating_sub(diff.scroll_x);
let ce_screen = ce.saturating_sub(diff.scroll_x);
paint_selection_band(buf, text_x, y, text_w, cs_screen, ce_screen);
}
}
/// Hit-test a click against the side-by-side diff body. Returns
/// `Some((side, diff_row_idx, char_col))` when the click landed inside
/// either column's text area; `None` when the click was in the header,
/// status footer, seam, gutter outside the body, or when the diff isn't
/// renderable at this size. The math mirrors `render_diff` exactly so a
/// click on a character returns the char column the user can see at that
/// cell.
pub fn diff_hit_test(
diff: &crate::widgets::diff::DiffData,
last_inner: Rect,
col: u16,
row: u16,
) -> Option<(crate::widgets::diff::DiffSide, usize, usize)> {
use crate::widgets::diff::DiffSide;
if diff.unified {
return None;
}
if last_inner.width < 16 || last_inner.height < 3 {
return None;
}
let body_top = last_inner.y + 1;
let body_height = last_inner.height.saturating_sub(2);
if body_height == 0 {
return None;
}
if row < body_top || row >= body_top + body_height {
return None;
}
let half = last_inner.width / 2;
if half < 8 {
return None;
}
let l_gutter = (diff.left_lines.len() + 1).to_string().len() as u16 + 1;
let r_gutter = (diff.right_lines.len() + 1).to_string().len() as u16 + 1;
let l_x = last_inner.x;
let l_sign_x = l_x + l_gutter;
let l_text_x = l_sign_x + 2;
let l_text_w = half.saturating_sub(l_gutter + 2 + 1);
let r_x = last_inner.x + half + 1;
let r_sign_x = r_x + r_gutter;
let r_text_x = r_sign_x + 2;
let r_text_w = (last_inner.width - (half + 1)).saturating_sub(r_gutter + 2);
let seam_x = last_inner.x + half;
let vis_row = (row - body_top) as usize;
let row_idx = diff.scroll + vis_row;
if row_idx >= diff.rows.len() {
return None;
}
let (side, text_x, text_w) = if col < seam_x {
(DiffSide::Left, l_text_x, l_text_w)
} else if col > seam_x {
(DiffSide::Right, r_text_x, r_text_w)
} else {
return None;
};
let screen_col = if col >= text_x {
(col - text_x).min(text_w.saturating_sub(1)) as usize
} else {
0
};
let char_col = screen_col + diff.scroll_x;
Some((side, row_idx, char_col))
}
/// Paint `‹` and `›` glyphs at the right edge of a diff header so the
/// user can click between change hunks without scrolling. Returns the
/// hit-test rects (prev, next) so the click handler can route mouse
/// events back to `prev_change_row` / `next_change_row`. Returns two
/// empty rects when the header doesn't have room for the glyphs (very
/// narrow editor pane).
fn paint_diff_nav_arrows(inner: Rect, head_bg: Color, buf: &mut Buffer) -> (Rect, Rect) {
// Reserve a 7-cell strip on the right: " ‹ › ".
let strip_w: u16 = 7;
if inner.width < strip_w + 4 {
return (Rect::default(), Rect::default());
}
let strip_right = inner.x + inner.width;
let prev_x = strip_right - strip_w + 1;
let next_x = strip_right - 3;
let y = inner.y;
let arrow_style = Style::default()
.fg(Color::White)
.bg(head_bg)
.add_modifier(Modifier::BOLD);
buf.set_string(prev_x, y, "\u{2039}", arrow_style);
buf.set_string(next_x, y, "\u{203a}", arrow_style);
(
Rect {
x: prev_x,
y,
width: 1,
height: 1,
},
Rect {
x: next_x,
y,
width: 1,
height: 1,
},
)
}
/// One-column unified diff for a deleted file. Every visible row is a
/// `Removed` row painted with a red band, `-` sign, and the HEAD line
/// number in the gutter — visually identical to `git diff` for a
/// removed file, but rendered inside the editor pane. Returns the
/// header-arrow hit rects the same way `render_diff` does so the click
/// handler can navigate deletion-view changes too.
fn render_unified_deletion(
diff: &mut crate::widgets::diff::DiffData,
inner: Rect,
buf: &mut Buffer,
bg: Color,
) -> (Rect, Rect) {
use crate::widgets::diff::DiffRow;
let name = diff
.left_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| diff.left_path.display().to_string());
let header = format!(" diff: {name} \u{2022} deleted (HEAD \u{2192} /dev/null) ");
let head_style = Style::default()
.fg(Color::White)
.bg(Color::Rgb(0x6b, 0x1f, 0x1f))
.add_modifier(Modifier::BOLD);
for x in inner.x..inner.x + inner.width {
buf[(x, inner.y)].set_style(head_style);
buf[(x, inner.y)].set_symbol(" ");
}
buf.set_string(inner.x, inner.y, &header, head_style);
let (prev_arrow, next_arrow) = paint_diff_nav_arrows(inner, Color::Rgb(0x6b, 0x1f, 0x1f), buf);
let body_top = inner.y + 1;
let body_height = inner.height.saturating_sub(2);
let status_y = inner.y + inner.height - 1;
if body_height == 0 {
return (prev_arrow, next_arrow);
}
let left_max = diff.left_lines.len();
let gutter = (left_max + 1).to_string().len() as u16 + 1;
let sign_x = inner.x + gutter;
let text_x = sign_x + 2;
let text_w = inner.width.saturating_sub(gutter + 2);
let total = diff.rows.len();
let viewport = body_height as usize;
let max_scroll = total.saturating_sub(viewport);
if diff.scroll > max_scroll {
diff.scroll = max_scroll;
}
let removed_bg = Color::Rgb(0x4a, 0x1f, 0x1f);
let removed_fg = Color::Rgb(0xff, 0xb3, 0xb3);
let gutter_fg = Color::Rgb(0x6c, 0x7d, 0x9c);
let end = (diff.scroll + viewport).min(total);
for (vis_row, row_idx) in (diff.scroll..end).enumerate() {
let y = body_top + vis_row as u16;
let row = diff.rows[row_idx];
let (left_idx, sign, cell_bg) = match row {
DiffRow::Removed { left } | DiffRow::Replaced { left, .. } => {
(Some(left), '-', removed_bg)
}
DiffRow::Equal { left, .. } => (Some(left), ' ', bg),
DiffRow::Added { .. } => (None, ' ', bg),
};
let label = left_idx
.map(|i| format!("{:>width$} ", i + 1, width = gutter as usize - 1))
.unwrap_or_else(|| " ".repeat(gutter as usize));
buf.set_string(
inner.x,
y,
&label,
Style::default().fg(gutter_fg).bg(cell_bg),
);
buf.set_string(
sign_x,
y,
format!("{sign} "),
Style::default()
.fg(if cell_bg == removed_bg {
removed_fg
} else {
gutter_fg
})
.bg(cell_bg)
.add_modifier(Modifier::BOLD),
);
let text = left_idx
.and_then(|i| diff.left_lines.get(i).cloned())
.unwrap_or_default();
let clipped: String = text
.chars()
.skip(diff.scroll_x)
.take(text_w as usize)
.collect();
let mut padded = clipped;
let pad = (text_w as usize).saturating_sub(padded.chars().count());
for _ in 0..pad {
padded.push(' ');
}
buf.set_string(
text_x,
y,
&padded,
Style::default()
.fg(if cell_bg == removed_bg {
removed_fg
} else {
Color::Rgb(0xc5, 0xcd, 0xd9)
})
.bg(cell_bg),
);
}
let visible_first = diff.scroll + 1;
let visible_last = end;
let status = format!(
" {visible_first}\u{2013}{visible_last} of {total} \u{00b7} every line removed at HEAD \u{00b7} \u{2191}/\u{2193} PgUp/PgDn to scroll "
);
buf.set_string(
inner.x,
status_y,
&status,
Style::default()
.fg(Color::Gray)
.bg(Color::Rgb(0x14, 0x18, 0x22)),
);
(prev_arrow, next_arrow)
}
fn write_cell(buf: &mut Buffer, x: u16, y: u16, w: u16, text: &str, style: Style) {
let max_chars = w as usize;
// Overflow is cut plainly with no trailing ellipsis marker (the user wants
// no ellipsis anywhere in croft).
let content: String = text.chars().take(max_chars).collect();
let mut padded = content;
let pad_count = max_chars.saturating_sub(padded.chars().count());
for _ in 0..pad_count {
padded.push(' ');
}
buf.set_string(x, y, &padded, style);
}
fn format_bytes_human(n: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
if n >= MB {
format!("{:.1} MB", n as f64 / MB as f64)
} else if n >= KB {
format!("{:.1} KB", n as f64 / KB as f64)
} else {
format!("{n} B")
}
}
pub fn extension_is_image(ext: &str) -> bool {
let lc = ext.to_ascii_lowercase();
IMAGE_EXTENSIONS.iter().any(|e| *e == lc)
}
pub fn extension_is_pdf(ext: &str) -> bool {
ext.eq_ignore_ascii_case("pdf")
}
pub fn image_format_label_from_ext(ext: &str) -> String {
match ext.to_ascii_lowercase().as_str() {
"jpg" | "jpeg" => "JPEG".to_string(),
other => other.to_ascii_uppercase(),
}
}
/// Inclusive char-indexed range `(row, col)` anchor and head, where head
/// follows the cursor as the user drags / shift-arrows. `normalised()` returns
/// the pair in row-major order so callers don't have to care which end came
/// first.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EditorSelection {
pub anchor: (usize, usize),
pub head: (usize, usize),
}
impl EditorSelection {
pub fn new(row: usize, col: usize) -> Self {
Self {
anchor: (row, col),
head: (row, col),
}
}
pub fn normalised(&self) -> ((usize, usize), (usize, usize)) {
if self.anchor <= self.head {
(self.anchor, self.head)
} else {
(self.head, self.anchor)
}
}
pub fn has_area(&self) -> bool {
self.anchor != self.head
}
}
/// A single text replacement in char-indexed `(row, col)` coordinates, as
/// produced by an LSP rename `WorkspaceEdit`. Applied by
/// [`apply_span_edits_to_lines`] and [`Editor::apply_span_edits`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TextSpanEdit {
pub start: (usize, usize),
pub end: (usize, usize),
pub new_text: String,
}
/// Apply `edits` to `lines` (char-indexed coords), bottom-to-top so an
/// earlier replacement never shifts the coordinates of a later one. Returns
/// the number of edits applied. Out-of-range edits are skipped.
pub fn apply_span_edits_to_lines(lines: &mut Vec<String>, edits: &[TextSpanEdit]) -> usize {
let mut order: Vec<&TextSpanEdit> = edits.iter().collect();
order.sort_by_key(|e| std::cmp::Reverse(e.start));
let mut applied = 0;
for e in order {
if replace_span(lines, e) {
applied += 1;
}
}
applied
}
fn replace_span(lines: &mut Vec<String>, e: &TextSpanEdit) -> bool {
let (sr, sc) = e.start;
let (er, ec) = e.end;
if sr >= lines.len() || er >= lines.len() || (er, ec) < (sr, sc) {
return false;
}
let from = char_byte(&lines[sr], sc);
let to = char_byte(&lines[er], ec);
let head = lines[sr][..from].to_string();
let tail = lines[er][to..].to_string();
// Build the replacement text in full, then split it back on '\n' so a
// multi-line `new_text` (e.g. ruff's "Organize Imports", a whole-document
// reformat) becomes separate rows instead of one row with embedded newline
// bytes. A single-line edit (rename) yields exactly one row, unchanged.
let combined = format!("{head}{}{tail}", e.new_text);
let new_lines: Vec<String> = combined.split('\n').map(str::to_string).collect();
let _ = lines.splice(sr..=er, new_lines);
true
}
/// Coarse classification of the most recent edit, used so consecutive
/// `InsertChar` ops coalesce into a single undo step (typing burst) but
/// any other edit kind always opens a new step.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum EditKind {
InsertChar,
Newline,
Backspace,
DeleteForward,
Paste,
DeleteSelection,
DuplicateLines,
DeleteLines,
KillToEol,
KillToBol,
OpenLine,
/// Block indent / outdent (Tab / Shift+Tab over a line range). Never
/// coalesces, so each Tab press is its own undo step like VS Code.
Indent,
/// A single keystroke applied simultaneously across every multi-cursor
/// caret. Never coalesces, so each multi-edit is its own undo step.
MultiEdit,
/// Move a line / block up or down (Alt+Up / Alt+Down). Its own step.
MoveLines,
/// Toggle line or block comment (Cmd+/ / Shift+Alt+A). Its own step.
ToggleComment,
/// Join selected lines into one. Its own step.
JoinLines,
/// Upper / lower / title-case a selection. Its own step.
TransformCase,
/// Sort the selected lines. Its own step.
SortLines,
/// Trim trailing whitespace across the buffer. Its own step.
TrimWhitespace,
/// Transpose the two characters around the cursor (Ctrl+T). Its own step.
Transpose,
/// Convert leading indentation between tabs and spaces. Its own step.
IndentConvert,
/// Remove trailing blank lines at end of file. Its own step.
TrimFinalNewlines,
/// Find-bar Replace / Replace All. Never coalesces, so each replace is
/// its own undo step like VS Code.
Replace,
}
/// The three case transforms VS Code exposes as
/// `editor.action.transformTo{Upper,Lower,Title}case`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CaseTransform {
Upper,
Lower,
Title,
}
/// A single keystroke to fan out across every multi-cursor caret.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CaretEdit {
Insert(char),
Backspace,
DeleteForward,
}
#[derive(Clone, Debug)]
struct Snapshot {
lines: Vec<String>,
cursor_row: usize,
cursor_col: usize,
selection: Option<EditorSelection>,
carets: Vec<EditorSelection>,
dirty: bool,
/// The editor's `save_seq` when this snapshot was taken. A restore that
/// crosses a save point re-dirties the buffer: its content no longer
/// matches disk even though the snapshot predates the edit.
save_seq: u64,
}
const UNDO_STACK_LIMIT: usize = 500;
/// Max selected-text length that still drives occurrence highlighting, matching
/// VS Code's default `editor.selectionHighlightMaxLength`.
const SELECTION_HIGHLIGHT_MAX_LEN: usize = 200;
/// Outcome of an FS-sync sweep over a single tab (`reload_or_flag_conflict`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExternalChange {
/// Disk matches the buffer's last-synced stamp; nothing to do.
Unchanged,
/// The buffer was clean and has been reloaded from disk.
Reloaded,
/// Disk changed but the reload failed (e.g. the file became unreadable).
ReloadFailed,
/// The buffer had unsaved edits, so the external change was flagged as a
/// conflict instead of being applied.
Conflict,
}
/// Outcome of a guarded `save_to_disk`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SaveOutcome {
/// The buffer was written to disk.
Saved,
/// The file changed on disk since we last synced; the save was refused
/// to avoid clobbering the external edit. The caller must force it.
DiskConflict,
/// The buffer holds characters the chosen encoding cannot represent, so
/// writing would replace them with HTML numeric character references and
/// lose the originals irreversibly (the buffer itself is unchanged, so
/// undo cannot bring them back). The save was refused; the caller
/// surfaces it and sets `lossy_save_armed` to write anyway.
EncodingLoss,
}
/// Per-tab results of an FS-sync sweep across all open tabs
/// (`EditorTabs::reload_externally_changed_tabs`).
#[derive(Debug, Default)]
pub struct ExternalReloadReport {
/// Paths of clean tabs that were silently reloaded from disk.
pub reloaded: Vec<PathBuf>,
/// Paths of dirty tabs flagged as conflicts (not reloaded, not saved).
pub conflicts: Vec<PathBuf>,
/// Paths whose disk copy changed but whose reload FAILED (the file
/// became unreadable, was replaced by a directory, a PDF's re-render
/// died). The tab keeps its last good view; dropping these on the
/// floor left the user staring at stale content with no indication
/// the sweep even tried (#37).
pub failed: Vec<PathBuf>,
}
impl ExternalReloadReport {
pub fn is_empty(&self) -> bool {
self.reloaded.is_empty() && self.conflicts.is_empty() && self.failed.is_empty()
}
}
/// The navigator's identity color: its comment boxes and its caret (the
/// same orange its note ◆ wore). Fixed like the git decoration colors —
/// legible on every dark background.
pub(crate) const NAVIGATOR_ACCENT: Color = Color::Rgb(0xff, 0x9d, 0x2f);
/// Columns of the comment-box footer tail ` ✕ Ignore ╯`.
const IGNORE_TAIL_COLS: usize = 11;
/// One comment box: the AI pair programmer's voice, anchored below a buffer
/// line. Rendered as an unnumbered block (title, body, reply field + Ignore
/// button); it never touches the buffer text and is never saved. The App
/// feeds these per tick from the pair host's note snapshot.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommentBox {
/// The note's stable id in the pilot's state (Ignore removes it, a
/// reply appends to it).
pub id: u64,
/// 0-based buffer line the box hangs under.
pub line: usize,
/// The navigator's caret name, shown in the title row.
pub author: String,
/// Body text; replies append as further lines.
pub body: String,
}
/// What a click inside a comment box landed on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CommentHit {
/// The title or body area: focus the box.
Body,
/// The footer's reply field: focus and place the caret.
Reply,
/// The footer's ✕ Ignore button: dismiss the box.
Ignore,
}
/// The reply draft in the focused box's footer field. While set, typing goes
/// here instead of the buffer (the App routes keys).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommentFocus {
/// Which box is focused (its [`CommentBox::id`]).
pub id: u64,
/// The draft text.
pub reply: String,
/// Caret position in chars within `reply`.
pub cursor: usize,
}
/// One visual row of the painted layout: a buffer line's wrap segment
/// (`Text`), or one row of a comment box, which belongs to no buffer line.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum VisRow {
/// `line`'s chars `[start, end)` (the whole visible span when not
/// wrapping).
Text {
line: usize,
start: usize,
end: usize,
},
/// Row `box_row` (0-based within the block) of `comment_boxes[box_idx]`.
Box { box_idx: usize, box_row: usize },
}
/// The line-ending style of the open buffer, shown in the status bar and
/// applied when saving. Detected on open (CRLF if any `\r\n` is present);
/// the user can switch it from the status bar, converting on the next save.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum LineEnding {
#[default]
Lf,
Crlf,
}
impl LineEnding {
pub fn label(self) -> &'static str {
match self {
Self::Lf => "LF",
Self::Crlf => "CRLF",
}
}
pub fn sequence(self) -> &'static str {
match self {
Self::Lf => "\n",
Self::Crlf => "\r\n",
}
}
}
/// The buffer's indentation preference for newly typed indentation: the tab
/// width and whether Tab inserts spaces or a literal tab. Surfaced in the
/// status bar's "Spaces: N" / "Tab Size: N" pill, which the user can click to
/// change (VS Code's "Indent Using Spaces / Tabs" + "Change Tab Display Size").
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct IndentStyle {
pub width: u32,
pub use_spaces: bool,
}
impl IndentStyle {
/// The indentation unit as text: `width` spaces, or a single tab.
pub fn unit(self) -> String {
if self.use_spaces {
" ".repeat(self.width as usize)
} else {
"\t".to_string()
}
}
/// Status-bar label, matching VS Code ("Spaces: 4" / "Tab Size: 4").
pub fn label(self) -> String {
let kind = if self.use_spaces {
"Spaces"
} else {
"Tab Size"
};
format!("{kind}: {}", self.width)
}
}
/// How a buffer line differs from its committed (HEAD) version, for the git
/// gutter. The colour carries the meaning, exactly like VS Code's gutter:
/// green added, blue modified, red where lines were deleted.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum GitMark {
Added,
Modified,
/// One or more lines were removed just above this surviving line (or at
/// end of file, on the last line). VS Code paints a triangle here; the
/// gutter shows a red bar on the boundary line.
Deleted,
}
/// An in-progress snippet expansion: the caret is cycling the tab stops of a
/// just-expanded snippet. See [`Editor::expand_snippet`].
#[derive(Debug, Clone)]
struct SnippetSession {
/// Start `(row, col)` of the current stop's placeholder.
anchor: (usize, usize),
/// Placeholder length (chars) of the current stop, selected on landing.
cur_len: usize,
/// Remaining stops to visit, in order: `(row, col, placeholder_len)`.
stops: std::collections::VecDeque<(usize, usize, usize)>,
}
// Counts `rebuild_hidden_ranges` calls so a test can pin that the fold lookup
// is derived once per fold change and not per rendered row. Plain comment, not
// a doc comment: those cannot attach to a `thread_local!`.
#[cfg(test)]
thread_local! {
static FOLD_RANGE_REBUILDS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
pub struct Editor {
pub path: Option<PathBuf>,
pub lines: Vec<String>,
/// Debugger breakpoints, keyed by file path, as 1-based line numbers.
/// Rendered as red dots in the gutter and pushed to the DAP adapter on
/// launch. Keyed by path (not just the active file) so switching the buffer
/// to another file and back keeps its breakpoints.
pub breakpoints: std::collections::HashMap<PathBuf, std::collections::BTreeSet<usize>>,
/// Where the debugger is currently paused (path, 1-based line), or None when
/// not stopped. Drawn as a highlighted row with a gutter arrow.
pub stop_line: Option<(PathBuf, usize)>,
/// Breakpoint lines the adapter reported as NOT verified (could not bind,
/// e.g. a blank/comment line). Rendered as a hollow ○ instead of a solid ●
/// so the user sees the breakpoint is inert. Keyed by path.
pub unverified_breakpoints:
std::collections::HashMap<PathBuf, std::collections::BTreeSet<usize>>,
/// Optional condition expression per breakpoint line (path -> line ->
/// condition). A conditional breakpoint only pauses when the expression is
/// true. Rendered with a distinct gutter glyph.
pub breakpoint_conditions:
std::collections::HashMap<PathBuf, std::collections::HashMap<usize, String>>,
/// Optional log message per breakpoint line (path -> line -> message): a
/// logpoint. The adapter interpolates `{expr}` holes and prints instead
/// of pausing. Rendered as an amber diamond in the gutter.
pub breakpoint_logs:
std::collections::HashMap<PathBuf, std::collections::HashMap<usize, String>>,
/// Monotonic counter that bumps on every buffer mutation. The App's
/// per-tick sync_lsp diff reads this to know when to forward a
/// did_change to the LSP server, so building lines.join("\n") only
/// happens on actual changes, not every frame.
pub edit_seq: u64,
/// The `edit_seq` the collab session last synced this buffer at (Phase D
/// independent viewports, docs/MULTIPLAYER.md). The tick diff extracts
/// ops only when `edit_seq` has moved past this, and it is re-pinned
/// after a remote edit is applied so the diff never rebroadcasts one
/// (same lazy-recompute pattern as `git_marks_seq`).
pub collab_synced_seq: u64,
/// The [`CollabDoc::text_gen`](crate::collab::CollabDoc::text_gen) this
/// buffer last synced at, 0 = never attached to the live doc. A buffer
/// created after its file went live (a split duplicate, a reopen) holds
/// stale disk text: it must never extract (its diff would revert every
/// peer) and is instead seeded from the replica when this lags.
pub collab_doc_gen: u64,
/// HEAD baseline for the git gutter: the committed version's lines. Set by
/// the app (read off the workspace git root once per file / HEAD change).
/// `None` when the file is untracked, outside a repo, or not yet fetched.
pub git_head_lines: Option<Vec<String>>,
/// The path `git_head_lines` was fetched for. The app refetches when this
/// stops matching `path` (the tab switched files) so the gutter can never
/// show another file's diff.
pub git_baseline_for: Option<PathBuf>,
/// Per 0-based buffer line, how it differs from HEAD. Recomputed from
/// `git_head_lines` vs `lines` whenever `edit_seq` moves (the diff is a
/// whole-file Myers pass, so it runs once per edit-batch, not per frame).
git_marks: std::collections::HashMap<usize, GitMark>,
/// The `edit_seq` `git_marks` was computed at; `u64::MAX` forces a first
/// recompute once a baseline arrives.
git_marks_seq: u64,
/// Merge-conflict blocks in the buffer, lazily recomputed whenever
/// `edit_seq` moves (same pattern as the git-gutter marks).
conflicts: Vec<crate::merge::ConflictBlock>,
/// The `edit_seq` `conflicts` was computed at; `u64::MAX` forces the
/// first scan.
conflicts_seq: u64,
/// Per-source-line git blame for the current file, index 0 = line 1. Set
/// by the app off-thread once per (file, HEAD); `None` until fetched or
/// when blame is disabled. Drives the GitLens-style current-line inline
/// annotation.
pub blame_lines: Option<Vec<crate::git::BlameLine>>,
/// The path `blame_lines` was fetched for; the app refetches when this
/// stops matching `path` so blame can never show another file's authors.
pub blame_for: Option<PathBuf>,
/// Whether to paint the current-line blame annotation (user pref, default
/// on). The blame data is still fetched so toggling is instant.
pub blame_enabled: bool,
pub scroll: usize,
/// In soft-wrap mode, the index of the first visible visual segment within
/// the top logical line (`self.scroll`). Lets the viewport start partway
/// through a paragraph that wraps to more rows than the pane is tall.
/// Always 0 outside wrap mode.
scroll_sub: usize,
/// Cached total visual-row counts for soft-wrap, one `(width, total)` entry
/// per width probed this frame (the renderer tests both the wide and the
/// vbar-narrowed width, so a single slot would thrash). Cleared when the
/// buffer changes (alongside `hscroll_content_cols`). Drives the vertical
/// scrollbar's extent in wrap mode without an O(total chars) sweep/frame.
wrap_total_cache: Vec<(usize, usize)>,
/// Horizontal scroll offset in CHARACTERS. Long lines (e.g. minified
/// JS, base64 blobs in HTML) need to scroll right so search hits and
/// cursor positions deep in a line are reachable. The render slices
/// each line at this column and `move_right`/`move_left` keep the
/// cursor visible.
pub scroll_col: usize,
/// Cursor column as a CHARACTER index (not bytes), for the current line.
pub cursor_row: usize,
pub cursor_col: usize,
pub focused: bool,
/// When focused, draw the orange→green gradient border (Black theme)
/// instead of the solid blue one. Set by the app's focus/theme sync.
pub focus_gradient: bool,
/// Active color theme; drives the scrollbar track/thumb colors. Set by the
/// app's theme sync.
pub theme: crate::theme::Theme,
/// Whether the bundled PDF and CSV viewer extensions are enabled. Set by the
/// app from its disabled-extensions set; when off, `open` skips the inline
/// viewer and falls through to opening the file as plain text. Default true
/// so a freshly constructed editor (and every test) keeps the viewers on.
pub pdf_viewer_enabled: bool,
pub csv_viewer_enabled: bool,
pub dirty: bool,
/// Bumped on every SAVE (`mark_synced_with_disk`, reached only from
/// `write_buffer_to_disk`). Undo snapshots record it so a restore across
/// a save point re-dirties. Load/reload paths do not bump it; they clear
/// the undo stacks instead, so no stale snapshot can span them — a
/// change that preserves undo history across a reload must bump it too.
save_seq: u64,
pub status: String,
pub last_area: Rect,
pub last_inner: Rect,
pub last_scrollbar: Rect,
/// Hit-test rect for the horizontal scrollbar painted on the bottom inner
/// row when the longest line overflows the text column. `Rect::default()`
/// when no line overflows (the row is given back to text). `App` consults
/// this on click/drag to map an X coordinate onto `scroll_col`.
pub last_hscrollbar: Rect,
/// Number of rows actually used for text in the last render, i.e.
/// `inner.height` minus the horizontal scrollbar row when present. The
/// vertical viewport math (page size, vertical thumb) reads this so the
/// cursor never hides behind the horizontal bar.
last_text_rows: u16,
/// Cached longest-line width in CHARACTERS. Recomputing it every frame is
/// O(total chars), so it is invalidated (set `None`) only when the buffer
/// content changes (`open`/`mark_buffer_changed`) and recomputed lazily.
hscroll_content_cols: Option<usize>,
/// Visual-row layout captured by the most recent render: one entry per
/// painted screen row, each `(logical_line, char_start, char_end)`. In
/// soft-wrap mode a logical line spans several entries; otherwise there is
/// one entry per visible line with the range `[scroll_col, scroll_col +
/// text_width)`. `cursor_screen_pos`/`buffer_pos_at` invert it so the
/// cursor and clicks land on the right cell regardless of wrapping.
/// Comment-box rows appear inline as `VisRow::Box` entries.
last_wrap_rows: Vec<VisRow>,
pub last_gutter_width: u16,
pub selection: Option<EditorSelection>,
/// Secondary carets for multi-cursor editing (VS Code "Change All
/// Occurrences"). The primary caret stays in `cursor_row`/`cursor_col`
/// plus `selection`; these are the *extra* carets. Empty in the common
/// single-cursor case, which keeps every existing edit path unchanged.
/// Each entry is a full selection (anchor and head); head is that caret's
/// cursor. Edits apply to the primary and all of these as one undo step.
pub carets: Vec<EditorSelection>,
/// Collaborators' caret positions in this file (multiplayer sessions,
/// docs/MULTIPLAYER.md): (row, char col, participant color). Painted as
/// colored block cells like secondary carets; the App rebuilds this
/// before each render from the session roster.
pub ghost_carets: Vec<(usize, usize, Color)>,
/// Name tags for ghost carets still inside their fade window (VS Code
/// Live Share style): (caret row, caret char col, name, participant
/// color). Painted on the visual row above the caret, falling back
/// below when the caret sits on the viewport's top row. The App
/// rebuilds this before each render alongside `ghost_carets`.
pub ghost_caret_labels: Vec<(usize, usize, String, Color)>,
/// The 0-based buffer line wearing the AI-stream stop button in the
/// sign margin (`croft pair`): clicking it — or Cmd+K X — cancels the
/// stream and reverts the streamed text. The App rebuilds this before
/// each render from the stream state and the pilot's ghost caret.
pub stream_stop_line: Option<usize>,
/// The AI pair programmer's comment boxes for this buffer: each renders
/// as an unnumbered block between its anchor line and the next line
/// (title, body, reply field + Ignore button). The App rebuilds this per
/// tick from the pilot host's anchored-note snapshot for the active
/// file; boxes belong to no buffer line and never touch the text.
pub comment_boxes: Vec<CommentBox>,
/// The reply draft riding the focused box's footer field. While set,
/// the App routes typing here instead of the buffer.
pub comment_focus: Option<CommentFocus>,
/// Enclosing scope header lines to pin at the top of the viewport (VS Code
/// "Sticky Scroll"), outermost first, set by `App` from the outline scope
/// chain of the top visible line. Empty disables the feature (e.g. wrap
/// mode, no symbols, or nothing scrolled off).
pub sticky_lines: Vec<u32>,
/// Screen `(row, logical line)` pairs the sticky bar painted last render, so
/// `App` can map a click on a pinned header to a jump.
sticky_click_rows: Vec<(u16, u32)>,
/// Anchor of an in-progress column (box) selection (VS Code's Shift+Alt+drag),
/// as a `(row, col)` buffer position. `Some` while the box drag is live; each
/// drag rebuilds `carets` from the rectangle between it and the pointer.
box_anchor: Option<(usize, usize)>,
/// Active snippet tab-stop session (VS Code snippet expansion). `Some` while
/// the caret is cycling through `$1`, `$2`, … stops of a just-expanded
/// snippet; Tab advances, and any structural key cancels it. See
/// [`Editor::expand_snippet`].
snippet: Option<SnippetSession>,
/// Currently-collapsed fold headers, as 0-based logical line indexes. A
/// header's body (the more-indented lines below it, up to `fold_range`'s
/// end) is hidden from the render while its index is in this set. Purely a
/// view: the buffer is untouched, so every edit/undo path ignores it.
folded: std::collections::BTreeSet<usize>,
/// Merged, ascending, disjoint spans of lines hidden by `folded`, rebuilt
/// only when the fold set or the buffer changes. `is_line_hidden` runs on
/// every rendered row of every frame, and deriving it from `folded` there
/// meant re-scanning each folded region per row — Fold All on a large file
/// turned one frame into millions of iterations.
hidden_ranges: Vec<(usize, usize)>,
/// `self.lines.len()` at the moment `folded` was last set. Fold headers are
/// line indexes, so an insert/delete anywhere shifts them; when the count
/// no longer matches, the render drops every fold rather than hide the
/// wrong lines. ponytail: whole-buffer invalidation, not per-fold anchor
/// tracking; upgrade to sticky anchors if folds-survive-edits is wanted.
fold_epoch_lines: usize,
/// True when this tab is the single replaceable "preview" slot. Single-
/// click / plain-Enter opens replace the preview tab's contents in place;
/// double-click / Ctrl+Enter / typing into the buffer pin the tab
/// (preview = false) so subsequent previews don't overwrite it.
pub preview: bool,
/// True when the user has pinned this tab (VS Code "Pin"). Pinned tabs are
/// kept leftmost in the strip, survive Close Others / Close to the Right,
/// and show a thumb-tack glyph in place of the close `\u{2715}`. A pinned
/// tab is never the replaceable preview slot (pinning clears `preview`).
pub pinned: bool,
undo_stack: Vec<Snapshot>,
/// States popped off `undo_stack` by `undo`, awaiting `redo`. Cleared by any
/// fresh edit (`push_undo`) so a new edit branches history like VS Code.
redo_stack: Vec<Snapshot>,
last_edit_kind: Option<EditKind>,
lang: Option<LangKind>,
/// Explicit indentation preference set from the status-bar pill. `None`
/// falls back to the language default (2 spaces for YAML, 4 otherwise);
/// `Some` pins spaces-vs-tabs and width for newly typed indentation.
indent_override: Option<IndentStyle>,
/// Line-ending style, detected on open and applied on save. Surfaced in the
/// status bar; the user can switch it there.
pub eol: LineEnding,
/// Text encoding the buffer was decoded from and is re-encoded to on save.
/// Defaults to UTF-8; the status bar's "Reopen with Encoding" switches it.
pub encoding: &'static encoding_rs::Encoding,
/// Whether the file on disk began with a byte-order mark. `decode` strips
/// it, so without remembering it every save silently dropped it.
bom: bool,
/// Per-tab override for soft-wrap (VS Code "View: Toggle Word Wrap",
/// Alt+Z). `None` means follow the language default (`wrap_enabled`
/// wraps Markdown only); `Some(true)`/`Some(false)` force it on/off for
/// this buffer. Reset to `None` on every `open` so each file starts at
/// its language default.
wrap_override: Option<bool>,
highlights: Vec<Vec<HiSpan>>,
/// LSP semantic-token overlay decoded per line (byte offsets within
/// the line), painted over `highlights` at render so a parameter (and
/// other resolved symbols) keep their color everywhere they appear,
/// not just at the declaration. Empty until the server replies. This
/// is the editor half of the VS Code / Zed "combined" model.
semantic_overlay: Vec<Vec<HiSpan>>,
/// The raw last semantic-token batch (relative-encoded data + the
/// server legend), retained so the overlay can be re-decoded against
/// the buffer after each edit. `semantic_path` records which file the
/// batch is for, so a stale batch is ignored after a tab switch.
semantic_data: Vec<u32>,
semantic_legend: Option<std::sync::Arc<Vec<String>>>,
semantic_path: Option<PathBuf>,
/// Whether the retained batch covers the WHOLE document (a
/// `semanticTokens/full` reply) rather than just the opening viewport (a
/// `semanticTokens/range` reply). Once a full batch lands for a file, a
/// late range reply for that same file is dropped so it cannot blank the
/// off-screen colour the full batch painted.
semantic_is_full: bool,
/// Diagnostics for the loaded file, retained with their raw LSP UTF-16
/// positions so `diagnostic_spans` can be re-decoded against the buffer.
/// `diagnostics_path` guards a stale batch from underlining the wrong file
/// after a tab switch, exactly as `semantic_path` does for tokens.
diagnostics: Vec<crate::lsp::manager::Diagnostic>,
diagnostics_path: Option<PathBuf>,
/// Decoded per-logical-line underline runs `(start_char, end_char,
/// severity)`, recomputed from `diagnostics` whenever they or the buffer
/// change. The render loop paints a coloured underline over each run.
diagnostic_spans: Vec<Vec<(usize, usize, crate::lsp::manager::DiagnosticSeverity)>>,
/// Inlay hints for the loaded file, retained with their raw LSP UTF-16
/// positions so `inlay_spans` can be re-decoded against the buffer after
/// each edit. `inlay_path` guards a stale batch from annotating the wrong
/// file after a tab switch, exactly as `diagnostics_path` does.
inlay_hints: Vec<crate::lsp::manager::InlayHintItem>,
inlay_path: Option<PathBuf>,
/// Occurrences of the symbol under the caret (LSP documentHighlight),
/// already converted to `(row, start_char, end_char, write)` against the
/// current buffer. Cleared on every edit — the server's columns are
/// stale the moment the text changes — and replaced when the app's next
/// idle-caret request answers.
occurrences: Vec<(usize, usize, usize, bool)>,
/// Decoded per-logical-line hint runs `(char_col, label)`, sorted by
/// column. The render loop splices each label into the row as dim italic
/// virtual cells at its anchor; every overlay painter, the caret, and
/// mouse mapping translate buffer columns past them.
inlay_spans: Vec<Vec<(usize, String)>>,
registry: LangRegistry,
/// When set, every occurrence of this string in the visible portion of
/// the buffer is overpainted with the search-match style after the
/// syntax-highlighted line is laid down. Mirrors the highlight in the
/// Search panel so the user sees their query lit up in the file too.
pub search_highlight: Option<String>,
/// Toggle state that drives `search_highlight` matching: case
/// sensitivity, whole-word boundaries, regex. Mirrors `SearchPanel.opts`
/// so the editor's yellow highlight stays consistent with what the
/// search panel claims is a match.
pub search_highlight_opts: crate::widgets::search::SearchOpts,
/// When the inline editor Find overlay (Cmd+F) is open and the cursor
/// has landed on a match, this records (row, col_chars, len_chars) so
/// the renderer can paint the active match in a stronger orange while
/// every other match keeps the regular yellow. Mirrors VS Code's
/// "current match" highlight. Cleared when the find bar closes or when
/// the highlight term is cleared.
pub active_search_match: Option<(usize, usize, usize)>,
/// Some when this tab is a read-only image preview rather than a text
/// buffer. The text fields (`lines`, undo, highlights, …) are left in
/// their default empty state and the renderer paints metadata only;
/// the actual pixels are emitted as an OSC-1337 inline image overlay
/// by `App` after each frame.
/// Rendered Markdown preview (Cmd/Ctrl+Shift+V) replacing the source view
/// while set; rebuilt lazily when `built_seq` falls behind `edit_seq`.
pub markdown_preview: Option<crate::markdown::MarkdownPreview>,
pub image: Option<ImageView>,
/// A reload's request that `open_pdf` come up on this page instead of
/// page 1, so the reader's place survives an external rebuild in ONE
/// rasterisation. Rendering page 1 first and re-rendering afterwards
/// opened a window where a transient failure silently snapped the
/// reader back to page 1 (#72). Set by `reload_from_disk`, consumed
/// (or cleared) by the `open` it wraps.
pdf_restore_page: Option<u32>,
/// Read-only spreadsheet preview for `.csv` / `.tsv` / `.xlsx` / etc.
/// Mutually exclusive with `image` and the text path; none of the
/// editor's text fields are populated when this is `Some`.
pub sheet: Option<crate::sheet::SheetView>,
/// Read-only side-by-side diff view. Mutually exclusive with the text
/// path, `image`, and `sheet` — when set the renderer paints two
/// columns based on `diff.rows` and ignores `lines`.
pub diff: Option<crate::widgets::diff::DiffData>,
/// Hit-test rect for the "previous change" arrow painted in the diff
/// header. Empty when the tab isn't a diff or the header was clipped.
/// `App` consults this on left-click to jump to the previous hunk.
pub diff_prev_arrow: Rect,
/// Hit-test rect for the "next change" arrow painted in the diff
/// header. Mirror of `diff_prev_arrow`.
pub diff_next_arrow: Rect,
/// Disk identity (mtime, len) captured the last time this tab was in
/// sync with disk — i.e. at open and after a successful save. The FS
/// sync sweep compares the file's current stamp against this to decide
/// whether an external process changed the file underneath us. `None`
/// for a tab with no file (the blank initial buffer).
disk_stamp: Option<(SystemTime, u64)>,
/// Set when an external on-disk change is detected while this buffer has
/// unsaved edits. Reloading would discard the user's edits and saving
/// would clobber the external change, so neither happens automatically:
/// the flag drives the conflict warning and makes `save_to_disk` refuse
/// to overwrite until the user forces it.
pub disk_conflict: bool,
/// Set when a save was refused because `encoding` cannot represent the
/// buffer. Latches like `disk_conflict` so auto save stops retrying (and
/// stops re-reporting) a file only an explicit Cmd+S can resolve; cleared
/// by a successful write and by any reopen, which may change `encoding`.
pub encoding_loss: bool,
/// One-shot consent to write characters `encoding` cannot represent,
/// answering an `EncodingLoss` refusal. Set by the explicit-save path
/// only (never by auto save) and consumed by the write it authorises.
pub lossy_save_armed: bool,
/// When the buffer last changed, driving the auto-save delay. `None`
/// until the first edit.
pub last_edit_at: Option<std::time::Instant>,
}
impl Editor {
pub fn new() -> Self {
Self {
path: None,
lines: Vec::new(),
breakpoints: std::collections::HashMap::new(),
stop_line: None,
unverified_breakpoints: std::collections::HashMap::new(),
breakpoint_conditions: std::collections::HashMap::new(),
breakpoint_logs: std::collections::HashMap::new(),
edit_seq: 0,
collab_synced_seq: 0,
collab_doc_gen: 0,
git_head_lines: None,
git_baseline_for: None,
git_marks: std::collections::HashMap::new(),
git_marks_seq: u64::MAX,
conflicts: Vec::new(),
conflicts_seq: u64::MAX,
blame_lines: None,
blame_for: None,
blame_enabled: true,
scroll: 0,
scroll_sub: 0,
wrap_total_cache: Vec::new(),
scroll_col: 0,
cursor_row: 0,
cursor_col: 0,
focused: false,
focus_gradient: false,
theme: crate::theme::Theme::default(),
pdf_viewer_enabled: true,
csv_viewer_enabled: true,
dirty: false,
save_seq: 0,
status: String::from("No file open"),
last_area: Rect::default(),
last_inner: Rect::default(),
last_scrollbar: Rect::default(),
last_hscrollbar: Rect::default(),
last_text_rows: 0,
hscroll_content_cols: None,
last_wrap_rows: Vec::new(),
last_gutter_width: 0,
selection: None,
carets: Vec::new(),
ghost_carets: Vec::new(),
ghost_caret_labels: Vec::new(),
stream_stop_line: None,
comment_boxes: Vec::new(),
comment_focus: None,
sticky_lines: Vec::new(),
sticky_click_rows: Vec::new(),
box_anchor: None,
snippet: None,
folded: std::collections::BTreeSet::new(),
hidden_ranges: Vec::new(),
fold_epoch_lines: 0,
preview: false,
pinned: false,
undo_stack: Vec::new(),
redo_stack: Vec::new(),
last_edit_kind: None,
lang: None,
indent_override: None,
eol: LineEnding::Lf,
encoding: encoding_rs::UTF_8,
bom: false,
wrap_override: None,
highlights: Vec::new(),
semantic_overlay: Vec::new(),
semantic_data: Vec::new(),
semantic_legend: None,
semantic_path: None,
semantic_is_full: false,
diagnostics: Vec::new(),
diagnostics_path: None,
diagnostic_spans: Vec::new(),
inlay_hints: Vec::new(),
inlay_path: None,
occurrences: Vec::new(),
inlay_spans: Vec::new(),
registry: LangRegistry::new(),
search_highlight: None,
search_highlight_opts: crate::widgets::search::SearchOpts::default(),
active_search_match: None,
markdown_preview: None,
image: None,
pdf_restore_page: None,
sheet: None,
diff: None,
diff_prev_arrow: Rect::default(),
diff_next_arrow: Rect::default(),
disk_stamp: None,
disk_conflict: false,
encoding_loss: false,
lossy_save_armed: false,
last_edit_at: None,
}
}
pub fn set_search_highlight(&mut self, term: Option<String>) {
self.search_highlight = term.filter(|s| !s.is_empty());
}
/// Toggle a debugger breakpoint on the cursor's current line for the active
/// file. Returns `(path, 1-based line, now_set)` where `now_set` is `true`
/// if a breakpoint was just added and `false` if one was removed, or `None`
/// when no file is open. Empty sets are dropped so the map stays clean for
/// the gutter renderer.
pub fn toggle_breakpoint(&mut self) -> Option<(PathBuf, usize, bool)> {
self.toggle_breakpoint_line(self.cursor_row + 1) // gutter + DAP are 1-based
}
/// Toggle a breakpoint on an explicit 1-based `line` (rather than the
/// cursor). Drives the gutter right-click "Add / Remove Breakpoint" menu,
/// where the target line is the row the user clicked, not where the caret
/// happens to sit. Returns `(path, line, now_set)`, or `None` with no open
/// file.
pub fn toggle_breakpoint_line(&mut self, line: usize) -> Option<(PathBuf, usize, bool)> {
let path = self.path.clone()?;
let set = self.breakpoints.entry(path.clone()).or_default();
let now_set = if set.remove(&line) {
// Removing a breakpoint removes the whole breakpoint: an
// orphaned condition or log message would silently re-attach to
// the next plain breakpoint set on this line (a resurrected
// logpoint never pauses at all).
if let Some(conds) = self.breakpoint_conditions.get_mut(&path) {
conds.remove(&line);
if conds.is_empty() {
self.breakpoint_conditions.remove(&path);
}
}
if let Some(logs) = self.breakpoint_logs.get_mut(&path) {
logs.remove(&line);
if logs.is_empty() {
self.breakpoint_logs.remove(&path);
}
}
false
} else {
set.insert(line);
true
};
if set.is_empty() {
self.breakpoints.remove(&path);
}
Some((path, line, now_set))
}
/// The 0-based buffer line under screen `(col, row)`, but ONLY when `col`
/// falls in the left gutter (glyph margin + line-number columns) rather
/// than the text body. Returns `None` over the body, past the last line,
/// or outside the viewport. Complements [`Editor::buffer_pos_at`] (which
/// resolves body clicks) and drives the gutter right-click breakpoint menu,
/// mirroring VS Code's glyph-margin context menu.
pub fn gutter_line_at(&self, col: u16, row: u16) -> Option<usize> {
if self.last_inner.height == 0 || self.lines.is_empty() {
return None;
}
if row < self.last_inner.y || row >= self.last_inner.y + self.last_inner.height {
return None;
}
// text_x mirrors `buffer_pos_at`: the gutter occupies every column to
// its left, so `col < text_x` is exactly "in the gutter".
let text_x = self.last_inner.x + self.last_gutter_width + 1;
if col < self.last_inner.x || col >= text_x {
return None;
}
// `last_wrap_rows` is the row->line map the render built, which already
// skips folded-away lines, so it is correct for both wrap and fold.
// Before the first paint it is empty; fall back to the linear map, which
// is exact then since nothing can be wrapped or folded yet.
let vis_row = (row - self.last_inner.y) as usize;
let line = if self.last_wrap_rows.is_empty() {
self.scroll + vis_row
} else {
self.text_row(vis_row)?.0
};
(line < self.lines.len()).then_some(line)
}
/// If `(col, row)` lands on a fold chevron in the gutter, toggle that fold
/// and return `true`. The chevron sits at `inner.x + 1` on a foldable
/// header's first visual row (see the render's fold-chevron block).
pub fn fold_chevron_at(&mut self, col: u16, row: u16) -> bool {
if col != self.last_inner.x + 1 || row < self.last_inner.y {
return false;
}
let Some((line, start, _)) = self.text_row((row - self.last_inner.y) as usize) else {
return false;
};
// The chevron draws only on a header's first visual row.
if self.wrap_enabled() && start != 0 {
return false;
}
if self.is_foldable(line) {
self.toggle_fold(line);
return true;
}
false
}
/// If `(col, row)` lands on a test fn's gutter play glyph, the test's
/// name — the app runs it. Same geometry as the fold chevron, one cell
/// left (`inner.x`, the sign margin the breakpoint dot shares).
pub fn test_glyph_at(&self, col: u16, row: u16) -> Option<String> {
if col != self.last_inner.x || row < self.last_inner.y {
return None;
}
let (line, start, _) = self.text_row((row - self.last_inner.y) as usize)?;
// The glyph draws only on the definition's first visual row.
if self.wrap_enabled() && start != 0 {
return None;
}
// Render precedence: the stop arrow, breakpoint glyphs, and the
// AI-stream square all outrank the play bead in the shared sign
// cell — a click on one of those glyphs must not start a test run.
if self.sign_cell_taken(line) {
return None;
}
crate::testing::locate::test_fn_on_line(self.path.as_deref(), &self.lines, line)
}
/// Whether the sign cell of 0-based `line` is claimed by a glyph that
/// outranks the test play bead: the debugger's stop arrow, any
/// breakpoint glyph (dot, diamond, hollow ring), or the AI-stream stop
/// square — the same precedence the render pass applies.
fn sign_cell_taken(&self, line: usize) -> bool {
if self.stream_stop_line == Some(line) {
return true;
}
let Some(path) = self.path.as_deref() else {
return false;
};
let here = line + 1; // gutter is 1-based
self.stop_line
.as_ref()
.is_some_and(|(p, l)| p == path && *l == here)
|| self
.breakpoints
.get(path)
.is_some_and(|s| s.contains(&here))
}
/// Whether `(col, row)` lands on the AI-stream stop button in the sign
/// margin: same geometry as [`test_glyph_at`](Self::test_glyph_at), only
/// on the streamed line's first visual row.
pub fn stream_stop_at(&self, col: u16, row: u16) -> bool {
let Some(stop_line) = self.stream_stop_line else {
return false;
};
if col != self.last_inner.x || row < self.last_inner.y {
return false;
}
let Some((line, start, _)) = self.text_row((row - self.last_inner.y) as usize) else {
return false;
};
if self.wrap_enabled() && start != 0 {
return false;
}
line == stop_line
}
/// This frame's screen row of `line`'s first visual row, when visible
/// (None = scrolled off or folded away). Anchors line-tied popups.
pub fn screen_row_of_line(&self, line: usize) -> Option<u16> {
self.last_wrap_rows
.iter()
.position(|r| matches!(r, VisRow::Text { line: l, .. } if *l == line))
.map(|idx| self.last_inner.y.saturating_add(idx as u16))
}
/// 1-based breakpoint lines for `path`, ascending, for a DAP
/// `setBreakpoints` request.
pub fn breakpoint_lines(&self, path: &Path) -> Vec<u32> {
self.breakpoints
.get(path)
.map(|s| s.iter().map(|&l| l as u32).collect())
.unwrap_or_default()
}
/// Breakpoints for `path` as DAP `SourceBreakpoint`s, attaching any stored
/// per-line condition and log message. `lines` is the path's breakpoint
/// set (passed in to avoid a second lookup at the call site).
pub fn source_breakpoints(
&self,
path: &Path,
lines: &std::collections::BTreeSet<usize>,
) -> Vec<crate::dap::session::SourceBreakpoint> {
let conds = self.breakpoint_conditions.get(path);
let logs = self.breakpoint_logs.get(path);
lines
.iter()
.map(|&l| crate::dap::session::SourceBreakpoint {
line: l as u32,
condition: conds.and_then(|c| c.get(&l)).cloned(),
log_message: logs.and_then(|m| m.get(&l)).cloned(),
})
.collect()
}
fn mark_buffer_changed(&mut self) {
self.dirty = true;
self.last_edit_at = Some(std::time::Instant::now());
self.edit_seq = self.edit_seq.wrapping_add(1);
self.hscroll_content_cols = None;
self.wrap_total_cache.clear();
self.occurrences.clear();
// Lossy-save consent named the unmappable characters the buffer held
// when the prompt fired; an edit changes what a write would destroy,
// so stale consent must not carry over (unlike `force_save_armed`,
// whose overwrite consent is about the DISK state, which typing does
// not touch). Clearing the latch also lets auto save retry a buffer
// the user fixed by deleting the offending characters.
self.encoding_loss = false;
self.lossy_save_armed = false;
}
/// Install (or clear) the git-gutter HEAD baseline for `path`. The app
/// calls this once per file (and again after HEAD moves) with the
/// committed version's lines; `None` means untracked / not a repo. Forces
/// the per-line marks to recompute on the next render.
pub fn set_git_head_lines(&mut self, path: PathBuf, head: Option<Vec<String>>) {
self.git_baseline_for = Some(path);
self.git_head_lines = head;
self.git_marks_seq = u64::MAX;
}
/// Install (or clear) per-line git blame for `path`. The app fetches this
/// off-thread once per file and after HEAD moves.
pub fn set_blame(&mut self, path: PathBuf, blame: Option<Vec<crate::git::BlameLine>>) {
self.blame_for = Some(path);
self.blame_lines = blame;
}
/// The GitLens-style inline annotation for the cursor's line, or `None`
/// when blame is off, unavailable, or the line was edited past the blamed
/// range. Committed lines read `author, age • summary`; a line git blames
/// against the zero hash (a working-tree edit) reads `Uncommitted changes`.
pub fn current_line_blame_annotation(&self) -> Option<String> {
if !self.blame_enabled {
return None;
}
// A reused tab (the preview) switches path before the new fetch
// lands; the old file's blame must not paint on the new one.
if self.blame_for != self.path {
return None;
}
let blame = self.blame_lines.as_ref()?;
// A line the git gutter flags as added/modified diverges from HEAD, so
// its committed author is meaningless — show the uncommitted marker
// (this also covers unsaved edits, since the gutter recomputes per
// edit while `blame` is only refetched per file / HEAD change).
if matches!(
self.git_mark_at(self.cursor_row),
Some(GitMark::Added | GitMark::Modified)
) {
return Some("Uncommitted changes".to_string());
}
// Insertions/deletions shift line indices out from under the blamed
// snapshot; only trust the 1:1 mapping while the line counts match.
if blame.len() != self.lines.len() {
return None;
}
let line = blame.get(self.cursor_row)?;
if line.uncommitted {
return Some("Uncommitted changes".to_string());
}
Some(format!(
"{}, {} • {}",
line.author,
crate::git::humanize_age(line.age_secs),
line.summary
))
}
/// The buffer's merge-conflict blocks, rescanned only when the buffer
/// changed since the last call — cheap enough for the render loop and
/// every cursor-position query.
pub fn conflicts(&mut self) -> &[crate::merge::ConflictBlock] {
if self.conflicts_seq != self.edit_seq {
self.conflicts_seq = self.edit_seq;
self.conflicts = crate::merge::find_conflicts(&self.lines);
}
&self.conflicts
}
/// Resolve the conflict containing `row` (VS Code's Accept Current /
/// Incoming / Both): one undo step replacing the whole marker block.
/// Returns false when `row` is not inside a conflict.
pub fn resolve_conflict_at(&mut self, row: usize, res: crate::merge::Resolution) -> bool {
let Some(block) = crate::merge::conflict_containing(self.conflicts(), row) else {
return false;
};
let replacement = crate::merge::resolution_lines(&self.lines, &block, res);
self.pin_on_edit();
self.push_undo(EditKind::Replace);
self.clear_selection();
self.lines
.splice(block.ours_start..=block.theirs_end, replacement);
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.cursor_row = block.ours_start.min(self.lines.len() - 1);
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.mark_buffer_changed();
self.recompute_highlights();
true
}
/// Recompute the per-line git marks from the cached HEAD baseline if the
/// buffer has changed since they were last built. Cheap no-op when the
/// `edit_seq` is unchanged, so the render loop can call it every frame.
fn refresh_git_marks(&mut self) {
use crate::widgets::diff::DiffRow;
if self.git_marks_seq == self.edit_seq {
return;
}
self.git_marks_seq = self.edit_seq;
self.git_marks.clear();
let Some(head) = self.git_head_lines.as_ref() else {
return;
};
// `build_diff_rows` pairs consecutive delete+insert into `Replaced`, so
// a bare `Removed` run is a real deletion: attach a `Deleted` marker to
// the next surviving line (the gutter's deletion boundary, VS Code-style).
let rows = crate::widgets::diff::build_diff_rows(head, &self.lines);
let mut pending_delete = false;
for row in &rows {
match *row {
DiffRow::Added { right } => {
self.git_marks.insert(right, GitMark::Added);
pending_delete = false;
}
DiffRow::Replaced { right, .. } => {
self.git_marks.insert(right, GitMark::Modified);
pending_delete = false;
}
DiffRow::Removed { .. } => pending_delete = true,
DiffRow::Equal { right, .. } => {
if pending_delete {
self.git_marks.insert(right, GitMark::Deleted);
pending_delete = false;
}
}
}
}
// A deletion at end of file has no surviving line after it: mark the
// last line so a trailing removal still shows.
if pending_delete && !self.lines.is_empty() {
self.git_marks
.entry(self.lines.len() - 1)
.or_insert(GitMark::Deleted);
}
}
/// The git-gutter mark for 0-based buffer line `line`, if any. Reads the
/// last computed marks (call after a render, or after `refresh_git_marks`).
pub fn git_mark_at(&self, line: usize) -> Option<GitMark> {
self.git_marks.get(&line).copied()
}
/// Soft-wrap mode: long lines fold onto multiple visual rows instead of
/// scrolling horizontally. Matches VS Code, which ships word-wrap on by
/// default for Markdown only (`"[markdown]": { "editor.wordWrap": "on" }`).
/// `toggle_wrap` (Alt+Z) sets a per-buffer override that wins over the
/// language default.
pub fn wrap_enabled(&self) -> bool {
self.wrap_override
.unwrap_or(matches!(self.lang, Some(LangKind::Markdown)))
}
/// Longest line width in characters, cached until the buffer changes.
/// Drives the horizontal scrollbar's content extent and the `scroll_col`
/// clamp. `&mut self` so the lazy recompute can populate the cache.
fn content_cols(&mut self) -> usize {
if let Some(cols) = self.hscroll_content_cols {
return cols;
}
let cols = self
.lines
.iter()
.enumerate()
.map(|(i, l)| {
// Spliced hint cells widen the line's display, so the
// horizontal extent must include them or the tail of a long
// hinted line could never scroll into view.
let extra: usize = self
.inlay_spans
.get(i)
.map(|hs| hs.iter().map(|(_, label)| label.chars().count()).sum())
.unwrap_or(0);
l.chars().count() + extra
})
.max()
.unwrap_or(0);
self.hscroll_content_cols = Some(cols);
cols
}
// -- Comment-box geometry ----------------------------------------------
// Comment boxes are unnumbered blocks rendered between a buffer line and
// the next. They add visual rows without owning buffer positions; every
// helper here is O(#boxes) and short-circuits to zero when no box is
// present, so the hot path pays nothing.
/// The layout entry at visual index `i` when it is a buffer-text row
/// (None = out of range or a comment-box row). The one funnel every
/// visual-row inversion goes through.
fn text_row(&self, i: usize) -> Option<(usize, usize, usize)> {
match self.last_wrap_rows.get(i)? {
VisRow::Text { line, start, end } => Some((*line, *start, *end)),
VisRow::Box { .. } => None,
}
}
/// The wrapped display lines of a box body at `bw` columns (its inner
/// text width). Bodies are short; this allocates only while boxes exist.
fn comment_body_lines(body: &str, bw: usize) -> Vec<String> {
let bw = bw.max(8);
let mut out = Vec::new();
for line in body.split('\n') {
let chars: Vec<char> = line.chars().collect();
for &(s, e) in &wrap_segments(&chars, bw) {
out.push(chars[s..e].iter().collect());
}
}
if out.is_empty() {
out.push(String::new());
}
out
}
/// Total rows `comment_boxes[idx]` occupies at text width `tw`:
/// title + wrapped body + footer.
fn comment_box_height(&self, idx: usize, tw: usize) -> usize {
let body = &self.comment_boxes[idx].body;
2 + Self::comment_body_lines(body, tw.saturating_sub(4)).len()
}
/// Rows contributed by the boxes anchored under `line` at width `tw`.
fn box_rows_at_line(&self, line: usize, tw: usize) -> usize {
if self.comment_boxes.is_empty() {
return 0;
}
(0..self.comment_boxes.len())
.filter(|&i| self.comment_boxes[i].line == line)
.map(|i| self.comment_box_height(i, tw))
.sum()
}
/// Rows contributed by boxes anchored under lines `[from, to)`.
fn box_rows_between(&self, from: usize, to: usize, tw: usize) -> usize {
if self.comment_boxes.is_empty() {
return 0;
}
(0..self.comment_boxes.len())
.filter(|&i| {
let l = self.comment_boxes[i].line;
// A box on a line hidden inside a collapsed fold is never
// painted (the layout skips hidden lines wholesale), so it
// must not count toward any scroll geometry either - it
// kept a scrollbar alive for invisible content and let the
// wheel walk the viewport into blank space.
l >= from && l < to && !self.is_line_hidden(l)
})
.map(|i| self.comment_box_height(i, tw))
.sum()
}
/// Paint one row of a comment box: blank unnumbered gutter, then the
/// block chrome. Rows: 0 = title (author), middle = body, last = footer
/// (reply field left, ✕ Ignore right).
#[allow(clippy::too_many_arguments)]
fn paint_comment_box_row(
&self,
buf: &mut ratatui::buffer::Buffer,
inner: Rect,
y: u16,
gutter_width: u16,
text_x: u16,
text_width: u16,
box_idx: usize,
box_row: usize,
) {
// The whole gutter goes blank: no number, no glyphs, no git bar.
buf.set_line(
inner.x,
y,
&Line::from(" ".repeat(gutter_width as usize + 1)),
gutter_width + 1,
);
let w = text_width as usize;
if w < 8 {
return;
}
let b = &self.comment_boxes[box_idx];
let accent = Style::default()
.fg(NAVIGATOR_ACCENT)
.bg(self.theme.sticky_scroll_bg());
let body_st = Style::default().bg(self.theme.sticky_scroll_bg());
let dim = Style::default()
.fg(Color::DarkGray)
.bg(self.theme.sticky_scroll_bg());
let height = self.comment_box_height(box_idx, w);
let pad = |s: &str, n: usize| -> String {
let mut out: String = s.chars().take(n).collect();
while out.chars().count() < n {
out.push(' ');
}
out
};
let line = if box_row == 0 {
let head = format!("\u{256d}\u{2500} \u{25c6} {} ", b.author);
let fill = w.saturating_sub(head.chars().count() + 1);
Line::from(Span::styled(
format!("{head}{}\u{256e}", "\u{2500}".repeat(fill)),
accent,
))
} else if box_row + 1 < height {
let body_lines = Self::comment_body_lines(&b.body, w.saturating_sub(4));
let text = body_lines.get(box_row - 1).cloned().unwrap_or_default();
Line::from(vec![
Span::styled("\u{2502} ", accent),
Span::styled(pad(&text, w - 4), body_st),
Span::styled(" \u{2502}", accent),
])
} else {
// Footer: `╰ ❯ <reply> ✕ Ignore ╯`
let focus = self.comment_focus.as_ref().filter(|f| f.id == b.id);
let field_w = w.saturating_sub(4 + IGNORE_TAIL_COLS);
// Window the draft around the caret: a reply longer than the
// field otherwise froze at its first field_w chars and the
// user typed blind, with no caret drawn anywhere.
let win = focus.map_or(0, |f| f.cursor.saturating_sub(field_w.saturating_sub(1)));
let (draft, style) = match focus {
Some(f) => {
let visible: String = f.reply.chars().skip(win).take(field_w).collect();
(pad(&visible, field_w), body_st)
}
None => (pad("Reply", field_w), dim),
};
let mut spans = vec![
Span::styled("\u{2570} ", accent),
Span::styled("\u{276f} ", accent),
];
match focus {
Some(f) if field_w > 0 && f.cursor - win < field_w => {
// Split the draft around the caret cell so it shows.
let cursor = f.cursor - win;
let chars: Vec<char> = draft.chars().collect();
let before: String = chars[..cursor].iter().collect();
let at: String = chars[cursor..=cursor].iter().collect();
let after: String = chars[cursor + 1..].iter().collect();
spans.push(Span::styled(before, style));
spans.push(Span::styled(
at,
style.add_modifier(ratatui::style::Modifier::REVERSED),
));
spans.push(Span::styled(after, style));
}
_ => spans.push(Span::styled(draft, style)),
}
spans.push(Span::styled(" \u{2715} Ignore ", accent));
spans.push(Span::styled("\u{256f}", accent));
Line::from(spans)
};
buf.set_line(text_x, y, &line, text_width);
// Ensure the block's background reaches the right edge even when a
// span fell short (padding already covers the normal case).
for x in text_x..text_x + text_width {
if buf[(x, y)].symbol() == " " && buf[(x, y)].style().bg.is_none() {
buf[(x, y)].set_style(body_st);
}
}
}
/// What a click at screen `(col, row)` hits inside a comment box:
/// Ignore on the footer's ✕ Ignore cells, Reply on the rest of the
/// footer, Body anywhere else in the block. None = not a box row.
pub fn comment_box_hit(&self, col: u16, row: u16) -> Option<(u64, CommentHit)> {
if self.comment_boxes.is_empty() {
return None;
}
let inner = self.last_inner;
if row < inner.y || col < inner.x {
return None;
}
let VisRow::Box { box_idx, box_row } =
*self.last_wrap_rows.get((row - inner.y) as usize)?
else {
return None;
};
let b = &self.comment_boxes[box_idx];
let text_x = inner.x + self.last_gutter_width + 1;
let w = self.visible_text_width();
if box_row + 1 == self.comment_box_height(box_idx, w) {
// Footer: the ` ✕ Ignore ╯` tail owns its cells.
let rel = (col.saturating_sub(text_x)) as usize;
if rel + IGNORE_TAIL_COLS >= w && rel < w {
return Some((b.id, CommentHit::Ignore));
}
return Some((b.id, CommentHit::Reply));
}
Some((b.id, CommentHit::Body))
}
// -- Soft-wrap visual-row geometry ------------------------------------
// In wrap mode a logical line maps to one or more visual rows. These
// helpers convert between logical positions and the flattened visual-row
// space the viewport scrolls over. They are only called on the wrap path;
// the non-wrap path keeps its cheap one-row-per-line arithmetic.
/// Wrap segments of a single logical line at the given text width.
fn line_segments(&self, line: usize, width: usize) -> Vec<(usize, usize)> {
let chars: Vec<char> = self
.lines
.get(line)
.map(|l| l.chars().collect())
.unwrap_or_default();
wrap_segments(&chars, width)
}
/// Number of visual rows a logical line occupies. Avoids allocating the
/// segment vector for the common case of a line that already fits.
fn line_visual_rows(&self, line: usize, width: usize) -> usize {
if width == 0 {
return 1;
}
let len = self.line_char_len(line);
if len <= width {
1
} else {
self.line_segments(line, width).len()
}
}
/// Visual rows of `line`'s whole group: its wrap segments plus the
/// comment-box rows hanging under it. All flattened visual-row math
/// (totals, viewport top, cursor row, decomposition) speaks group rows
/// so boxes shift everything below them consistently.
fn group_visual_rows(&self, line: usize, width: usize) -> usize {
self.line_visual_rows(line, width) + self.box_rows_at_line(line, width)
}
/// Total visual rows across the whole buffer. The text-only total is
/// cached per width; box rows are added fresh (boxes are few and can
/// change between frames without touching the buffer).
fn total_visual_rows(&mut self, width: usize) -> usize {
let text =
if let Some(&(_, total)) = self.wrap_total_cache.iter().find(|&&(w, _)| w == width) {
total
} else {
let total: usize = (0..self.lines.len())
.map(|l| self.line_visual_rows(l, width))
.sum();
self.wrap_total_cache.push((width, total));
total
};
text + self.box_rows_between(0, self.lines.len(), width)
}
/// Visual-row index of the current viewport top `(scroll, scroll_sub)`.
fn top_visual_row(&self, width: usize) -> usize {
(0..self.scroll)
.map(|l| self.group_visual_rows(l, width))
.sum::<usize>()
+ self.scroll_sub
}
/// Visual-row index of the segment containing the cursor. A box under
/// the cursor's own line renders below its segments, so only boxes on
/// earlier lines shift the cursor.
fn cursor_visual_row(&self, width: usize) -> usize {
let before: usize = (0..self.cursor_row)
.map(|l| self.group_visual_rows(l, width))
.sum();
before + self.segment_index_of_col(self.cursor_row, self.cursor_col, width)
}
/// Which segment of `line` holds character column `col`. A column sitting
/// exactly on a wrap boundary belongs to the *next* segment (its start),
/// matching where `cursor_screen_pos` paints the caret; only the true line
/// end maps to the final segment. Keeping this predicate identical to the
/// one in `cursor_screen_pos` is what keeps vertical motion consistent with
/// where the caret actually renders.
fn segment_index_of_col(&self, line: usize, col: usize, width: usize) -> usize {
let segs = self.line_segments(line, width);
let line_len = self.line_char_len(line);
segs.iter()
.position(|&(s, e)| col >= s && (col < e || (col == e && e == line_len)))
.unwrap_or(segs.len().saturating_sub(1))
}
/// Point the viewport top at a global visual-row index, decomposing it back
/// into `(scroll, scroll_sub)`. Parks at the last segment when `target`
/// runs past the end of the buffer.
fn set_top_to_visual_row(&mut self, target: usize, width: usize) {
let mut acc = 0;
for line in 0..self.lines.len() {
let n = self.group_visual_rows(line, width);
if acc + n > target {
self.scroll = line;
// scroll_sub may point into the line's box region (past its
// segments); the layout builder starts mid-box then.
self.scroll_sub = target - acc;
return;
}
acc += n;
}
self.scroll = self.lines.len().saturating_sub(1);
self.scroll_sub = self.group_visual_rows(self.scroll, width).saturating_sub(1);
}
/// Logical `(line, char_col)` at the start of a global visual row -
/// the inverse of `cursor_visual_row` for the row's first column. A
/// target inside a line's comment-box region snaps to that line's last
/// segment (a box row has no buffer position of its own).
fn logical_pos_at_visual_row(&self, target: usize, width: usize) -> (usize, usize) {
let mut acc = 0;
for line in 0..self.lines.len() {
let segs = self.line_segments(line, width);
let group = segs.len() + self.box_rows_at_line(line, width);
if acc + group > target {
let idx = (target - acc).min(segs.len().saturating_sub(1));
return (line, segs[idx].0);
}
acc += group;
}
(self.lines.len().saturating_sub(1), 0)
}
/// True when global visual row `target` falls inside a comment-box
/// region (no buffer position). Cursor motion steps over these.
fn visual_row_is_box(&self, target: usize, width: usize) -> bool {
if self.comment_boxes.is_empty() {
return false;
}
let mut acc = 0;
for line in 0..self.lines.len() {
let segs = self.line_visual_rows(line, width);
let group = segs + self.box_rows_at_line(line, width);
if acc + group > target {
return target - acc >= segs;
}
acc += group;
}
false
}
/// Move the wrap viewport so the given visual row is at the top, clamped to
/// the document, then pull the cursor into the new viewport so the render's
/// cursor-visibility clamp doesn't immediately undo the scroll. Used by the
/// wheel and the vertical scrollbar in wrap mode.
fn wrap_set_top(&mut self, top_target: usize) {
let width = self.visible_text_width();
let viewport = self.text_rows();
if width == 0 || viewport == 0 {
return;
}
let total = self.total_visual_rows(width);
let new_top = top_target.min(total.saturating_sub(viewport));
self.set_top_to_visual_row(new_top, width);
let cursor_vrow = self.cursor_visual_row(width);
let target_row = if cursor_vrow < new_top {
Some(new_top)
} else if cursor_vrow >= new_top + viewport {
Some(new_top + viewport - 1)
} else {
None
};
if let Some(vrow) = target_row {
let (line, col) = self.logical_pos_at_visual_row(vrow, width);
self.cursor_row = line;
self.cursor_col = col.min(self.line_char_len(line));
}
self.last_edit_kind = None;
}
/// Move the cursor one visual row up (`dir = -1`) or down (`dir = 1`) in
/// wrap mode, preserving the visual column within the row. Unlike the
/// logical `move_up`/`move_down`, this steps through wrapped segments of a
/// paragraph rather than skipping the whole logical line.
fn wrap_move_vertical(&mut self, dir: isize) {
let width = self.visible_text_width().max(1);
let segs = self.line_segments(self.cursor_row, width);
let seg_idx = self.segment_index_of_col(self.cursor_row, self.cursor_col, width);
let goal_col = self.cursor_col - segs[seg_idx].0;
let current = self.cursor_visual_row(width) as isize;
let total = self.total_visual_rows(width) as isize;
let mut target = current + dir;
// Step over comment-box rows: the caret never enters a box.
while target >= 0 && target < total && self.visual_row_is_box(target as usize, width) {
target += dir;
}
if target < 0 || target >= total {
return;
}
let (line, seg_start) = self.logical_pos_at_visual_row(target as usize, width);
let (s, e) = self
.line_segments(line, width)
.into_iter()
.find(|&(s, _)| s == seg_start)
.unwrap_or((seg_start, seg_start));
self.cursor_row = line;
self.cursor_col = (s + goal_col).min(e).min(self.line_char_len(line));
}
/// Identifier chars immediately to the left of the cursor on the
/// current line. Used as the LSP completion popup's filter prefix
/// so the menu narrows to items the user is actually typing toward.
pub fn word_before_cursor(&self) -> String {
let Some(line) = self.lines.get(self.cursor_row) else {
return String::new();
};
let chars: Vec<char> = line.chars().collect();
let end = self.cursor_col.min(chars.len());
let mut start = end;
while start > 0 && is_word_char(chars[start - 1]) {
start -= 1;
}
chars[start..end].iter().collect()
}
pub fn open(&mut self, path: &Path) -> Result<()> {
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
if extension_is_image(ext) {
return self.open_image(path);
}
if extension_is_pdf(ext) && self.pdf_viewer_enabled {
return self.open_pdf(path);
}
if crate::sheet::extension_is_sheet(ext) && self.csv_viewer_enabled {
return self.open_sheet(path);
}
let meta = std::fs::metadata(path)?;
if meta.len() > MAX_FILE_BYTES {
anyhow::bail!("File too large ({} bytes)", meta.len());
}
let bytes = std::fs::read(path)?;
// Decode with the file's BOM-declared encoding if it has one, else
// UTF-8. A later "Reopen with Encoding" overrides this.
let sniffed = encoding_rs::Encoding::for_bom(&bytes);
let enc = sniffed.map(|(e, _)| e).unwrap_or(encoding_rs::UTF_8);
// The BOM sniff comes FIRST: UTF-16 text is half NUL bytes, so the
// binary heuristic would reject every UTF-16 file — including the ones
// croft itself writes when the user picks UTF-16 from the encoding
// menu. A byte-order mark is a positive declaration of text, so it
// settles the question the heuristic is only guessing at.
let bom_declares_text = enc == encoding_rs::UTF_16LE || enc == encoding_rs::UTF_16BE;
if !bom_declares_text && is_binary(&bytes) {
anyhow::bail!("Binary file");
}
let changed_file = self.path.as_deref() != Some(path);
// `open` is also the same-path reload behind the FS-sync sweep and
// every revert, so auto-detection here would throw away an encoding
// the user picked through "Reopen with Encoding" — decoding the same
// bytes as UTF-8 and handing the next save the mojibake to write back.
// The choice sticks until the file itself declares otherwise.
let enc = if changed_file || sniffed.is_some() {
enc
} else {
self.encoding
};
self.encoding = enc;
// `decode` strips the BOM, so remember it or the next save drops it.
self.bom = sniffed.is_some();
let text = enc.decode(&bytes).0.into_owned();
// Detect the file's line-ending style before normalisation so a save
// preserves it (and the status bar reports it). A single `\r\n` marks
// the file CRLF, matching VS Code's "files.eol auto" heuristic.
self.eol = if text.contains("\r\n") {
LineEnding::Crlf
} else {
LineEnding::Lf
};
self.lines = split_into_lines(&text);
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.hscroll_content_cols = None;
self.wrap_total_cache.clear();
self.scroll_sub = 0;
self.path = Some(path.to_path_buf());
self.disk_stamp = Self::disk_stamp_of(path);
self.disk_conflict = false;
// A whole-buffer swap: whatever the previous contents could not be
// encoded as is no longer this tab's problem.
self.encoding_loss = false;
self.lossy_save_armed = false;
self.lang = path
.extension()
.and_then(|e| e.to_str())
.and_then(lang_for_extension);
self.wrap_override = None;
// Folds are line numbers into the OLD file. A preview tab is reused for
// the next single-click, so without this the incoming file arrives with
// the previous one's blocks collapsed. Only for a DIFFERENT file
// though: `open` is also the same-path reload behind every FS-sync
// sweep, and dropping the set there popped the user's blocks open
// whenever anything rewrote the file. That case is already covered by
// the render-time `fold_epoch_lines` guard, which drops folds if the
// reload changed the line count.
if changed_file {
self.folded.clear();
self.hidden_ranges.clear();
self.fold_epoch_lines = 0;
} else if !self.folded.is_empty() {
// The retained headers were measured against text that has just
// been replaced. Re-measure their spans, or a reload that keeps the
// line count while moving the indentation leaves the cache hiding
// lines the folds no longer cover — the epoch guard below only
// catches a change in line COUNT.
self.rebuild_hidden_ranges();
}
self.scroll = 0;
self.cursor_row = 0;
self.cursor_col = 0;
self.dirty = false;
self.selection = None;
self.undo_stack.clear();
self.redo_stack.clear();
self.last_edit_kind = None;
self.image = None;
self.sheet = None;
self.markdown_preview = None;
// A real file supersedes any diff view this editor was showing —
// without this a restore-then-reload keeps rendering the stale diff.
self.diff = None;
self.status = format!("Opened {}", path.display());
// The buffer now matches disk; bump the edit seq so the LSP doc sync
// sees the new content (an external reload lands here too, and without
// the bump the server keeps analysing the old text and never sends
// fresh semantic tokens — codeberg issue #39).
self.edit_seq = self.edit_seq.wrapping_add(1);
// A path CHANGE detaches the buffer from any collab doc it was
// synced to: the old generation belongs to another file, and a
// reused tab (the preview tab) keeping it would let the next collab
// tick broadcast this file's disk text as edits to that doc. A
// same-path reload keeps its attachment deliberately — that is how
// an owner's reload-diff (external change, Replace All) converges
// into the session as ops.
if changed_file {
self.collab_doc_gen = 0;
self.collab_synced_seq = self.edit_seq;
}
// Drop any semantic-token batch measured against the previous content:
// decoding it over the new lines paints the old file's colors at the
// wrong offsets. Tree-sitter colors cover the gap until the fresh
// batch triggered by the seq bump arrives.
self.semantic_data = Vec::new();
self.semantic_overlay = Vec::new();
self.semantic_is_full = false;
// Same for inlay hints: anchors measured against another file's text
// must never splice into this one.
self.inlay_hints = Vec::new();
self.inlay_path = None;
self.inlay_spans = Vec::new();
self.recompute_highlights();
Ok(())
}
fn open_image(&mut self, path: &Path) -> Result<()> {
let meta = std::fs::metadata(path)?;
if meta.len() > MAX_IMAGE_BYTES {
anyhow::bail!("Image too large ({} bytes)", meta.len());
}
let bytes = std::fs::read(path)?;
let (pixel_w, pixel_h) = image::load_from_memory(&bytes)
.map(|img| (img.width(), img.height()))
.map_err(|e| anyhow::anyhow!("Could not decode image: {e}"))?;
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
let format_label = image_format_label_from_ext(ext);
self.path = Some(path.to_path_buf());
self.disk_stamp = Self::disk_stamp_of(path);
self.disk_conflict = false;
// A whole-buffer swap: whatever the previous contents could not be
// encoded as is no longer this tab's problem.
self.encoding_loss = false;
self.lossy_save_armed = false;
self.lines = vec![String::new()];
// A whole-buffer swap like every other opener: caches memoised on
// edit_seq (conflicts, git marks) must not survive into this tab.
self.edit_seq = self.edit_seq.wrapping_add(1);
self.lang = None;
self.scroll = 0;
self.cursor_row = 0;
self.cursor_col = 0;
self.dirty = false;
self.selection = None;
self.undo_stack.clear();
self.redo_stack.clear();
self.last_edit_kind = None;
self.highlights = vec![Vec::new()];
self.image = Some(ImageView {
bytes,
format_label,
pixel_w,
pixel_h,
byte_size: meta.len(),
generation: next_image_generation(),
pdf: None,
});
self.sheet = None;
self.status = format!("Opened image {}", path.display());
Ok(())
}
fn open_sheet(&mut self, path: &Path) -> Result<()> {
let view = crate::sheet::open_sheet(path)
.map_err(|e| anyhow::anyhow!("Spreadsheet open failed: {e}"))?;
self.path = Some(path.to_path_buf());
self.disk_stamp = Self::disk_stamp_of(path);
self.disk_conflict = false;
// A whole-buffer swap: whatever the previous contents could not be
// encoded as is no longer this tab's problem.
self.encoding_loss = false;
self.lossy_save_armed = false;
self.lines = vec![String::new()];
// A whole-buffer swap like every other opener: caches memoised on
// edit_seq (conflicts, git marks) must not survive into this tab.
self.edit_seq = self.edit_seq.wrapping_add(1);
self.lang = None;
self.scroll = 0;
self.cursor_row = 0;
self.cursor_col = 0;
self.dirty = false;
self.selection = None;
self.undo_stack.clear();
self.redo_stack.clear();
self.last_edit_kind = None;
self.highlights = vec![Vec::new()];
self.image = None;
self.status = format!("Opened {} ({})", path.display(), view.kind.label());
self.sheet = Some(view);
Ok(())
}
fn open_pdf(&mut self, path: &Path) -> Result<()> {
let backend = crate::pdf::detect_backend()
.ok_or_else(|| anyhow::anyhow!("Install poppler (pdftoppm) to preview PDFs"))?;
let meta = std::fs::metadata(path)?;
let page_count = crate::pdf::detect_page_count(path);
// A reload comes up on the reader's page in this single
// rasterisation — clamped when the count is known, so a genuinely
// shrunken document lands on its new last page. On failure the
// whole open fails and the caller's failed-reload path keeps the
// last good render AND the place (#72).
let page = match self.pdf_restore_page.take() {
Some(p) => p.clamp(1, page_count.unwrap_or(p).max(1)),
None => 1,
};
let bytes = crate::pdf::rasterize_page(path, page, backend)
.map_err(|e| anyhow::anyhow!("PDF render failed: {e}"))?;
let (pixel_w, pixel_h) = image::load_from_memory(&bytes)
.map(|img| (img.width(), img.height()))
.map_err(|e| anyhow::anyhow!("Could not decode rasterised PDF: {e}"))?;
self.path = Some(path.to_path_buf());
self.disk_stamp = Self::disk_stamp_of(path);
self.disk_conflict = false;
// A whole-buffer swap: whatever the previous contents could not be
// encoded as is no longer this tab's problem.
self.encoding_loss = false;
self.lossy_save_armed = false;
self.lines = vec![String::new()];
// A whole-buffer swap like every other opener: caches memoised on
// edit_seq (conflicts, git marks) must not survive into this tab.
self.edit_seq = self.edit_seq.wrapping_add(1);
self.lang = None;
self.scroll = 0;
self.cursor_row = 0;
self.cursor_col = 0;
self.dirty = false;
self.selection = None;
self.undo_stack.clear();
self.redo_stack.clear();
self.last_edit_kind = None;
self.highlights = vec![Vec::new()];
self.image = Some(ImageView {
bytes,
format_label: String::from("PDF"),
pixel_w,
pixel_h,
byte_size: meta.len(),
generation: next_image_generation(),
pdf: Some(PdfState {
source_path: path.to_path_buf(),
current_page: page,
page_count,
backend,
source_byte_size: meta.len(),
links: None,
}),
});
self.sheet = None;
self.status = format!("Opened PDF {}", path.display());
Ok(())
}
/// The page an open PDF preview is showing, if this tab is one.
pub fn pdf_page(&self) -> Option<u32> {
self.image.as_ref()?.pdf.as_ref().map(|p| p.current_page)
}
/// Step the active PDF preview by `delta` pages. Returns true if the page
/// actually changed, so the caller can flag the OSC overlay for re-bake.
/// Wraps around at the document boundaries when the page count is known;
/// clamps at page 1 below otherwise.
pub fn change_pdf_page(&mut self, delta: i32) -> bool {
let Some(pdf) = self.image.as_ref().and_then(|i| i.pdf.clone()) else {
return false;
};
let new_page = if let Some(total) = pdf.page_count {
if total == 0 {
return false;
}
let cur = pdf.current_page as i64;
let next = ((cur - 1 + delta as i64).rem_euclid(total as i64)) + 1;
next as u32
} else if delta > 0 {
pdf.current_page.saturating_add(delta as u32)
} else {
pdf.current_page.saturating_sub((-delta) as u32).max(1)
};
self.render_pdf_page(new_page)
}
/// Jump the active PDF preview to an absolute page, clamped to the
/// document. `u32::MAX` therefore means "last page" without the caller
/// knowing the count - which is what Home / End need, and what stepping
/// by a huge delta cannot express (a step wraps).
pub fn set_pdf_page(&mut self, page: u32) -> bool {
let Some(pdf) = self.image.as_ref().and_then(|i| i.pdf.clone()) else {
return false;
};
// "Last page" is unanswerable without a page count (sips-only Macs
// where mdls reports nothing): rendering the sentinel itself would
// just spray "PDF page 4294967295 failed" into the status bar.
if page == u32::MAX && pdf.page_count.is_none() {
self.status = String::from("PDF page count unknown; cannot jump to the last page");
return false;
}
let last = pdf.page_count.unwrap_or(u32::MAX).max(1);
self.render_pdf_page(page.clamp(1, last))
}
fn render_pdf_page(&mut self, new_page: u32) -> bool {
let Some(image) = self.image.as_mut() else {
return false;
};
let Some(pdf) = image.pdf.clone() else {
return false;
};
if new_page == pdf.current_page {
return false;
}
let bytes = match crate::pdf::rasterize_page(&pdf.source_path, new_page, pdf.backend) {
Ok(b) => b,
Err(e) => {
self.status = format!("PDF page {new_page} failed: {e}");
return false;
}
};
let (pixel_w, pixel_h) = match image::load_from_memory(&bytes) {
Ok(img) => (img.width(), img.height()),
Err(_) => return false,
};
image.bytes = bytes;
image.generation = next_image_generation();
image.pixel_w = pixel_w;
image.pixel_h = pixel_h;
if let Some(state) = image.pdf.as_mut() {
state.current_page = new_page;
state.links = None;
}
true
}
/// Recompute this editor's syntax spans against the current global palette.
/// Called on a theme switch: cached spans carry baked colors, so without
/// this the open file keeps the old theme's code colors until the next edit.
pub fn rehighlight_for_theme(&mut self) {
self.recompute_highlights();
// The markdown preview bakes the theme into its lines at build time
// and rebuilds only when the buffer edits (`built_seq`), so a theme
// switch must rebuild it here or it keeps the old colors until the
// user touches the file. Requires `self.theme` to already carry the
// new theme (the caller assigns it first).
if self.markdown_preview.is_some() {
let text = self.lines.join("\n");
let lines = crate::markdown::render_markdown(&text, self.theme, &mut self.registry);
if let Some(md) = self.markdown_preview.as_mut() {
md.lines = lines;
md.built_seq = self.edit_seq;
}
}
}
fn recompute_highlights(&mut self) {
match self.lang {
Some(kind) => {
let text = self.lines.join("\n");
let bytes = text.as_bytes();
let line_starts = compute_line_starts(bytes);
self.highlights = highlight_text(&mut self.registry, kind, bytes, &line_starts);
}
None => {
self.highlights = vec![Vec::new(); self.lines.len()];
}
}
self.recompute_semantic_overlay();
self.recompute_diagnostic_spans();
self.recompute_inlay_spans();
}
/// Store a fresh semantic-token batch from the LSP and decode it into
/// the per-line overlay. Called by the app when `drain_semantic_tokens`
/// yields a batch for this editor's file.
pub fn apply_semantic_tokens(
&mut self,
path: PathBuf,
data: Vec<u32>,
legend: std::sync::Arc<Vec<String>>,
is_full: bool,
) {
// A viewport-only (range) batch must never clobber a whole-document
// batch already in place for the same file: the full set colours the
// off-screen lines the range set omits, so letting a late range reply
// win would blank them. Full batches always win; a range batch only
// applies as the first paint, before the full reply arrives.
let same_file = self.semantic_path.as_deref() == Some(path.as_path());
if !is_full && same_file && self.semantic_is_full {
return;
}
self.semantic_path = Some(path);
self.semantic_data = data;
self.semantic_legend = Some(legend);
self.semantic_is_full = is_full;
self.recompute_semantic_overlay();
}
/// Store a fresh inlay-hint set from the LSP and decode it into per-line
/// `(char_col, label)` runs. Called by the app when `drain_inlay_hints`
/// yields a batch for this editor's file (an empty set clears them).
pub fn apply_inlay_hints(
&mut self,
path: PathBuf,
hints: Vec<crate::lsp::manager::InlayHintItem>,
) {
self.inlay_path = Some(path);
self.inlay_hints = hints;
self.recompute_inlay_spans();
}
/// Install the occurrences of the symbol under the caret, converting the
/// server's UTF-16 columns to character columns row by row. A multi-line
/// occurrence (rare, but legal) tints its first row from the start column,
/// its last row up to the end column, and any rows between end to end.
pub fn apply_occurrences(&mut self, items: Vec<crate::lsp::manager::OccurrenceItem>) {
self.occurrences.clear();
for item in items {
for row in item.start_line..=item.end_line {
let row = row as usize;
let Some(text) = self.lines.get(row) else {
continue;
};
let cols = text.chars().count();
let start = if row as u32 == item.start_line {
utf16_to_char_col(text, item.start_char).min(cols)
} else {
0
};
let end = if row as u32 == item.end_line {
utf16_to_char_col(text, item.end_char).min(cols)
} else {
cols
};
if end > start {
self.occurrences.push((row, start, end, item.write));
}
}
}
}
/// Drop the occurrence tints (caret moved; the app re-requests on idle).
pub fn clear_occurrences(&mut self) -> bool {
let had = !self.occurrences.is_empty();
self.occurrences.clear();
had
}
/// Drop every inlay hint (the "Editor: Toggle Inlay Hints" off switch).
pub fn clear_inlay_hints(&mut self) {
self.inlay_hints = Vec::new();
self.inlay_path = None;
self.recompute_inlay_spans();
}
/// Re-decode the retained hints into per-logical-line `(char_col, label)`
/// runs against the current buffer. A no-op (clears the runs) unless the
/// batch belongs to the loaded file. Between an edit and the next reply
/// the anchors drift with the old positions (clamped to the line), exactly
/// like VS Code until its refresh lands; the app replaces the set once the
/// server answers for the new edit seq.
fn recompute_inlay_spans(&mut self) {
// Hint cells change the display width of their lines.
self.hscroll_content_cols = None;
let same_file = self.inlay_path.as_deref() == self.path.as_deref();
if !same_file || self.inlay_hints.is_empty() {
self.inlay_spans = Vec::new();
return;
}
let mut spans: Vec<Vec<(usize, String)>> = vec![Vec::new(); self.lines.len()];
for h in &self.inlay_hints {
let Some(text) = self.lines.get(h.line as usize) else {
continue;
};
let col = utf16_to_char_col(text, h.character).min(text.chars().count());
spans[h.line as usize].push((col, h.label.clone()));
}
for line in &mut spans {
line.sort_by_key(|(c, _)| *c);
}
self.inlay_spans = spans;
}
/// Inlay hints of logical line `line`, or nothing in wrap mode: the
/// wrapped row segmentation knows nothing about hint cells.
/// ponytail: hints skip wrap mode; code files don't wrap by default, and
/// Markdown (the wrapping default) has no hint-serving server.
fn row_inlay_spans(&self, line: usize) -> &[(usize, String)] {
if self.wrap_enabled() {
return &[];
}
self.inlay_spans.get(line).map(Vec::as_slice).unwrap_or(&[])
}
/// The buffer text to key a semantic-token cache entry on, but only when
/// the buffer is clean: a dirty buffer no longer matches the on-disk
/// content the next open will read, so caching it would key the entry under
/// the wrong content. Returns the same normalised (`\n`-joined) text the
/// app sends to the server, so the key matches on both store and load.
pub fn clean_cache_text(&self) -> Option<String> {
(!self.dirty).then(|| self.lines.join("\n"))
}
#[cfg(test)]
pub(crate) fn semantic_overlay_for_test(&self) -> &[Vec<HiSpan>] {
&self.semantic_overlay
}
#[cfg(test)]
pub(crate) fn occurrence_count_for_test(&self) -> usize {
self.occurrences.len()
}
#[cfg(test)]
pub(crate) fn lines_for_test(&self) -> &[String] {
&self.lines
}
/// Store a fresh diagnostics set for `path` and decode it into per-line
/// underline runs. Called by the app when `drain_diagnostics` yields a
/// batch for this editor's file (an empty `diagnostics` clears them, which
/// is how an "all clear" republish erases the squiggles).
pub fn apply_diagnostics(
&mut self,
path: PathBuf,
diagnostics: Vec<crate::lsp::manager::Diagnostic>,
) {
self.diagnostics_path = Some(path);
self.diagnostics = diagnostics;
self.recompute_diagnostic_spans();
}
/// The file the editor's current diagnostics belong to, if any. The app
/// reads this to re-apply the stored set after a tab switch (diagnostics
/// are server-pushed, never re-requested on focus like semantic tokens).
pub fn diagnostics_path(&self) -> Option<&Path> {
self.diagnostics_path.as_deref()
}
/// Clones of the diagnostics whose range intersects the inclusive logical
/// line span `[start_line, end_line]`. Passed as `codeAction` context so the
/// language server can attach quick fixes to the errors under the cursor.
pub fn diagnostics_in_line_range(
&self,
start_line: u32,
end_line: u32,
) -> Vec<crate::lsp::manager::Diagnostic> {
self.diagnostics
.iter()
.filter(|d| d.start_line <= end_line && d.end_line >= start_line)
.cloned()
.collect()
}
#[cfg(test)]
pub(crate) fn diagnostic_spans_for_test(
&self,
) -> &[Vec<(usize, usize, crate::lsp::manager::DiagnosticSeverity)>] {
&self.diagnostic_spans
}
/// Re-decode the retained diagnostics into per-logical-line character runs
/// against the current buffer. A no-op (clears the runs) unless the batch
/// belongs to the loaded file, so stale squiggles never bleed across a tab
/// switch. A zero-width diagnostic (e.g. a "missing token" pointing between
/// two characters) is widened to one cell so it is still visible, matching
/// VS Code drawing a squiggle under at least one character.
fn recompute_diagnostic_spans(&mut self) {
let same_file = self.diagnostics_path.as_deref() == self.path.as_deref();
if !same_file || self.diagnostics.is_empty() {
self.diagnostic_spans = Vec::new();
return;
}
let mut spans: Vec<Vec<(usize, usize, crate::lsp::manager::DiagnosticSeverity)>> =
vec![Vec::new(); self.lines.len()];
for d in &self.diagnostics {
let start_line = d.start_line as usize;
let end_line = d.end_line as usize;
// Zip bounds the walk to the buffer, replacing the per-line
// `lines.get` check a stale diagnostic range would otherwise need.
for (line, (text, line_spans)) in self
.lines
.iter()
.zip(spans.iter_mut())
.enumerate()
.take(end_line + 1)
.skip(start_line)
{
let line_chars = text.chars().count();
let from = if line == start_line {
utf16_to_char_col(text, d.start_char)
} else {
0
};
let to = if line == end_line {
utf16_to_char_col(text, d.end_char)
} else {
line_chars
};
// Widen an empty run to one cell so a point diagnostic is seen.
let to = to.max(from + 1);
line_spans.push((from, to, d.severity));
}
}
self.diagnostic_spans = spans;
}
/// Re-decode the retained semantic-token batch against the current
/// buffer. A no-op (clears the overlay) unless the batch belongs to
/// the file currently loaded, so a stale batch never colors the wrong
/// file after a tab switch.
fn recompute_semantic_overlay(&mut self) {
let same_file = self.semantic_path.as_deref() == self.path.as_deref();
let Some(legend) = self.semantic_legend.as_ref().filter(|_| same_file) else {
self.semantic_overlay = Vec::new();
return;
};
if self.semantic_data.is_empty() {
self.semantic_overlay = Vec::new();
return;
}
let text = self.lines.join("\n");
let bytes = text.as_bytes();
let line_starts = compute_line_starts(bytes);
self.semantic_overlay =
decode_semantic_tokens(&self.semantic_data, legend, bytes, &line_starts);
}
/// Number of logical lines in the buffer (the inlay-hint request range).
pub fn line_count(&self) -> usize {
self.lines.len()
}
/// Markdown: Toggle Preview (Cmd/Ctrl+Shift+V). Returns false when the
/// active tab is not a Markdown text buffer (the caller reports why).
pub fn toggle_markdown_preview(&mut self) -> bool {
if self.markdown_preview.take().is_some() {
return true;
}
let is_md_text = matches!(self.lang, Some(LangKind::Markdown))
&& self.image.is_none()
&& self.sheet.is_none()
&& self.diff.is_none();
if !is_md_text {
return false;
}
let text = self.lines.join("\n");
let lines = crate::markdown::render_markdown(&text, self.theme, &mut self.registry);
self.markdown_preview = Some(crate::markdown::MarkdownPreview {
lines,
scroll: 0,
built_seq: self.edit_seq,
});
true
}
/// Scroll the active Markdown preview; returns false when none is open
/// so the caller falls through to normal buffer scrolling.
pub fn scroll_markdown_preview(&mut self, delta: i32) -> bool {
let Some(md) = self.markdown_preview.as_mut() else {
return false;
};
md.scroll = md.scroll.saturating_add_signed(delta as i16);
true
}
fn line_char_len(&self, row: usize) -> usize {
self.lines.get(row).map(|s| s.chars().count()).unwrap_or(0)
}
fn byte_index(&self, row: usize, col: usize) -> usize {
let line = &self.lines[row];
line.char_indices()
.nth(col)
.map(|(i, _)| i)
.unwrap_or(line.len())
}
/// Any user-driven mutation pins the buffer (matches VS Code: typing in
/// a preview tab promotes it to a regular tab so a subsequent
/// single-click on another file can't silently replace your edits).
fn pin_on_edit(&mut self) {
self.preview = false;
// Every mutating entry point calls this, so it is the one place that
// can guarantee no edit ever lands on a line hidden inside a fold.
self.reveal_cursor_fold();
}
pub fn insert_char(&mut self, c: char) {
self.pin_on_edit();
// Selection-replace counts as one logical edit (Replace), not two.
// Coalesce subsequent typed chars onto the same step only when the
// previous edit was also a single-char insert with no selection.
let had_selection = self.selection.map(|s| s.has_area()).unwrap_or(false);
let kind = if had_selection {
EditKind::DeleteSelection
} else {
EditKind::InsertChar
};
self.push_undo(kind);
self.delete_selection_inner();
if self.lines.is_empty() {
self.lines.push(String::new());
}
let row = self.cursor_row;
let col = self.cursor_col;
let byte = self.byte_index(row, col);
self.lines[row].insert(byte, c);
self.cursor_col += 1;
self.mark_buffer_changed();
self.recompute_highlights();
}
/// Apply LSP rename edits to this buffer in-memory as a single undo step,
/// marking the tab dirty (the user saves to persist). Returns the number
/// of edits applied.
pub fn apply_span_edits(&mut self, edits: &[TextSpanEdit]) -> usize {
if edits.is_empty() {
return 0;
}
self.pin_on_edit();
self.push_undo(EditKind::Paste);
let n = apply_span_edits_to_lines(&mut self.lines, edits);
if n > 0 {
self.mark_buffer_changed();
self.recompute_highlights();
}
n
}
/// The buffer's active indentation style: the status-bar override if set,
/// else the language default (2 spaces for YAML, 4 spaces otherwise).
pub fn indent_style(&self) -> IndentStyle {
self.indent_override.unwrap_or(IndentStyle {
width: indent_unit_for(self.lang).chars().count() as u32,
use_spaces: true,
})
}
/// Pin the buffer's indentation style (status-bar "Indent Using …" /
/// "Change Tab Display Size"). Affects newly typed indentation only; use
/// [`Editor::convert_indentation`] to rewrite existing lines.
pub fn set_indent_style(&mut self, style: IndentStyle) {
self.indent_override = Some(style);
}
/// The indentation unit as text for the active style: N spaces or one tab.
fn indent_unit(&self) -> String {
self.indent_style().unit()
}
/// The editor's indentation preference as the LSP `FormattingOptions`
/// fields (`tab_size`, `insert_spaces`).
pub fn indent_preference(&self) -> (u32, bool) {
let s = self.indent_style();
(s.width, s.use_spaces)
}
/// Human label for the buffer's language mode, for the status bar.
pub fn language_label(&self) -> &'static str {
language_label(self.lang)
}
/// The active language, for the status-bar "Change Language Mode" picker
/// to pre-select the current entry.
pub fn language(&self) -> Option<LangKind> {
self.lang
}
/// Override the buffer's language mode (status-bar "Change Language Mode").
/// Re-runs tree-sitter highlighting under the new grammar; the app re-syncs
/// the LSP separately off the changed language.
pub fn set_language(&mut self, lang: Option<LangKind>) {
self.lang = lang;
self.recompute_highlights();
}
/// Re-read the open file and decode it with `enc` (status-bar "Reopen with
/// Encoding"). Replaces the buffer, resets the cursor, and re-highlights;
/// subsequent saves re-encode with `enc`. No-op without a backing file.
pub fn reopen_with_encoding(&mut self, enc: &'static encoding_rs::Encoding) -> Result<()> {
let path = self
.path
.clone()
.ok_or_else(|| anyhow::anyhow!("No file open"))?;
let bytes = std::fs::read(&path)?;
// `decode` does its own BOM sniffing and OVERRIDES the encoding passed
// in when the file carries one, returning what it actually used. Take
// that, or the buffer would hold text decoded one way while claiming to
// be another — and the save would then re-encode it wrongly.
let (decoded, used, _) = enc.decode(&bytes);
let text = decoded.into_owned();
self.encoding = used;
// Re-sniff against the bytes just read: reinterpreting the file under a
// new encoding must not carry the previous one's BOM answer over.
self.bom = encoding_rs::Encoding::for_bom(&bytes).is_some();
self.eol = if text.contains("\r\n") {
LineEnding::Crlf
} else {
LineEnding::Lf
};
self.lines = split_into_lines(&text);
if self.lines.is_empty() {
self.lines.push(String::new());
}
// A whole-buffer swap invalidates both history stacks: a redo/undo
// popped afterwards would reinstate the buffer as decoded under the
// OLD encoding, silently discarding the re-decode.
self.undo_stack.clear();
self.redo_stack.clear();
self.hscroll_content_cols = None;
self.wrap_total_cache.clear();
self.scroll = 0;
self.scroll_sub = 0;
self.cursor_row = 0;
self.cursor_col = 0;
self.selection = None;
// The buffer now matches disk decoded as `enc`, so it is clean; bump the
// edit seq so the LSP/outline/highlight resync sees the new content.
self.dirty = false;
// The encoding just changed, so a refusal recorded against the old
// one is stale — without this, auto save would keep skipping a tab
// whose new encoding covers the buffer fine.
self.encoding_loss = false;
self.lossy_save_armed = false;
self.edit_seq = self.edit_seq.wrapping_add(1);
self.recompute_highlights();
Ok(())
}
/// Clamp the cursor back inside the buffer after an edit that may have
/// removed rows or shortened the cursor's line (e.g. a whole-document
/// reformat). Without this a stale `cursor_row`/`cursor_col` can index past
/// the end of `lines` on the next keystroke.
pub fn clamp_cursor(&mut self) {
if self.lines.is_empty() {
self.cursor_row = 0;
self.cursor_col = 0;
return;
}
self.cursor_row = self.cursor_row.min(self.lines.len() - 1);
let max_col = self.lines[self.cursor_row].chars().count();
self.cursor_col = self.cursor_col.min(max_col);
}
pub fn insert_str(&mut self, s: &str) {
self.pin_on_edit();
self.push_undo(EditKind::Paste);
if self.selection.is_some() {
self.delete_selection_inner();
}
for c in s.chars() {
if c == '\n' {
self.insert_newline_raw();
} else {
self.insert_char_raw(c);
}
}
self.recompute_highlights();
}
fn insert_char_raw(&mut self, c: char) {
if self.lines.is_empty() {
self.lines.push(String::new());
}
let row = self.cursor_row;
let col = self.cursor_col;
let byte = self.byte_index(row, col);
self.lines[row].insert(byte, c);
self.cursor_col += 1;
self.mark_buffer_changed();
}
/// The language id (VS Code identifier) of this buffer, for snippet scoping.
pub fn scope_id(&self) -> &'static str {
language_scope_id(self.lang)
}
/// True while a snippet's tab stops are being cycled with Tab.
pub fn snippet_active(&self) -> bool {
self.snippet.is_some()
}
/// Abandon any active snippet session (a structural key moved the caret
/// away, so Tab should return to its normal meaning).
pub fn cancel_snippet(&mut self) {
self.snippet = None;
}
/// Expand snippet `body` at the caret. `prefix_len` chars of the already
/// typed prefix are removed first, then the resolved text is inserted with
/// continuation lines indented to the current line. The caret lands on the
/// first tab stop (selecting its placeholder); when more stops remain a
/// session starts so Tab cycles through them.
pub fn expand_snippet(&mut self, body: &str, prefix_len: usize) {
self.snippet = None;
for _ in 0..prefix_len {
self.backspace();
}
let start = (self.cursor_row, self.cursor_col);
let indent: String = self
.lines
.get(start.0)
.map(|l| {
l.chars()
.take(start.1)
.take_while(|c| *c == ' ' || *c == '\t')
.collect()
})
.unwrap_or_default();
let indent_len = indent.chars().count();
let parsed = crate::snippets::parse_body(body);
let inserted = if indent.is_empty() {
parsed.text.clone()
} else {
parsed.text.replace('\n', &format!("\n{indent}"))
};
self.insert_str(&inserted);
let mut abs: std::collections::VecDeque<(usize, usize, usize)> = parsed
.stops
.iter()
.map(|s| {
let (ld, col) = crate::snippets::offset_to_line_col(&parsed.text, s.offset);
let row = start.0 + ld;
let c = if ld == 0 {
start.1 + col
} else {
indent_len + col
};
(row, c, s.len)
})
.collect();
let Some(first) = abs.pop_front() else {
return; // no stops: leave the caret at the end of the insertion
};
self.place_at_snippet_stop(first);
if !abs.is_empty() {
self.snippet = Some(SnippetSession {
anchor: (first.0, first.1),
cur_len: first.2,
stops: abs,
});
}
}
/// Advance to the next snippet tab stop. Later stops on the current line are
/// shifted by the net chars typed at the current stop first. Returns false
/// when no session is active (so the caller falls back to indenting).
pub fn snippet_next(&mut self) -> bool {
let Some(mut sess) = self.snippet.take() else {
return false;
};
// ponytail: only edits made at the current stop, on its own line, are
// tracked. Typing newlines or editing elsewhere mid-session drifts the
// later stops; upgrade to real markers if that workflow matters.
if self.cursor_row == sess.anchor.0 {
let typed = self.cursor_col as isize - sess.anchor.1 as isize;
let shift = typed - sess.cur_len as isize;
if shift != 0 {
let after = sess.anchor.1 + sess.cur_len;
for s in sess.stops.iter_mut() {
if s.0 == sess.anchor.0 && s.1 >= after {
s.1 = (s.1 as isize + shift).max(0) as usize;
}
}
}
}
let Some(next) = sess.stops.pop_front() else {
return false;
};
self.place_at_snippet_stop(next);
if !sess.stops.is_empty() {
sess.anchor = (next.0, next.1);
sess.cur_len = next.2;
self.snippet = Some(sess);
}
true
}
/// Move the caret to a resolved tab stop, selecting its placeholder text so
/// the next keystroke replaces it.
fn place_at_snippet_stop(&mut self, stop: (usize, usize, usize)) {
// A stop can outlive its row (a backspace join mid-session shrinks
// the buffer): clamp to the live grid before planting anything —
// clamping only the cursor left a selection whose row indexed past
// the buffer end on the next edit.
let (row, col, len) = stop;
let row = row.min(self.lines.len().saturating_sub(1));
let line_len = self.line_char_len(row);
let col = col.min(line_len);
let end = (col + len).min(line_len);
self.cursor_row = row;
self.cursor_col = end;
self.selection = (end > col).then_some(EditorSelection {
anchor: (row, col),
head: (row, end),
});
}
fn insert_newline_raw(&mut self) {
if self.lines.is_empty() {
self.lines.push(String::new());
}
let row = self.cursor_row;
let col = self.cursor_col;
let byte = self.byte_index(row, col);
let right = self.lines[row].split_off(byte);
self.lines.insert(row + 1, right);
self.cursor_row += 1;
self.cursor_col = 0;
self.mark_buffer_changed();
}
pub fn insert_newline(&mut self) {
self.pin_on_edit();
self.push_undo(EditKind::Newline);
self.delete_selection_inner();
self.smart_insert_newline_inner();
self.recompute_highlights();
}
fn smart_insert_newline_inner(&mut self) {
if self.lines.is_empty() {
self.lines.push(String::new());
}
let row = self.cursor_row;
let col = self.cursor_col;
let line = self.lines[row].clone();
let leading: String = line
.chars()
.take(col)
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
let prefix_chars: Vec<char> = line.chars().take(col).collect();
let last_non_ws = prefix_chars
.iter()
.rev()
.find(|c| !c.is_whitespace())
.copied();
let next_char = line.chars().nth(col);
let unit = self.indent_unit();
let extra = if extra_indent_triggered(self.lang, last_non_ws) {
unit.as_str()
} else {
""
};
let pair_split = is_bracket_pair_split(self.lang, last_non_ws, next_char);
self.insert_newline_raw();
let new_indent = format!("{leading}{extra}");
for c in new_indent.chars() {
self.insert_char_raw(c);
}
if pair_split {
self.insert_newline_raw();
for c in leading.chars() {
self.insert_char_raw(c);
}
self.cursor_row -= 1;
self.cursor_col = new_indent.chars().count();
}
}
pub fn backspace(&mut self) {
self.pin_on_edit();
self.push_undo(EditKind::Backspace);
self.backspace_raw();
self.recompute_highlights();
}
/// Backspace body without snapshotting or re-highlighting. Callers that
/// batch multiple edits (multi-cursor) snapshot once and recompute once.
fn backspace_raw(&mut self) {
if self.delete_selection_inner() {
return;
}
if self.cursor_col > 0 {
let row = self.cursor_row;
let col = self.cursor_col - 1;
let from = self.byte_index(row, col);
let to = self.byte_index(row, col + 1);
self.lines[row].replace_range(from..to, "");
self.cursor_col -= 1;
self.mark_buffer_changed();
} else if self.cursor_row > 0 {
let cur = self.lines.remove(self.cursor_row);
self.cursor_row -= 1;
self.cursor_col = self.line_char_len(self.cursor_row);
self.lines[self.cursor_row].push_str(&cur);
self.mark_buffer_changed();
}
}
pub fn delete_forward(&mut self) {
self.pin_on_edit();
self.push_undo(EditKind::DeleteForward);
self.delete_forward_raw();
self.recompute_highlights();
}
/// Forward-delete body without snapshotting or re-highlighting.
fn delete_forward_raw(&mut self) {
if self.delete_selection_inner() {
return;
}
let row = self.cursor_row;
let len = self.line_char_len(row);
if self.cursor_col < len {
let from = self.byte_index(row, self.cursor_col);
let to = self.byte_index(row, self.cursor_col + 1);
self.lines[row].replace_range(from..to, "");
self.mark_buffer_changed();
} else if row + 1 < self.lines.len() {
let next = self.lines.remove(row + 1);
self.lines[row].push_str(&next);
self.mark_buffer_changed();
}
}
/// True while multi-cursor mode is active (one or more secondary carets
/// in addition to the primary cursor).
pub fn has_multi_cursor(&self) -> bool {
!self.carets.is_empty()
}
/// Drop every secondary caret, leaving just the primary cursor. Called on
/// Esc, mouse clicks, and any movement that isn't a multi-cursor edit.
pub fn collapse_carets(&mut self) {
self.carets.clear();
}
/// VS Code "Change All Occurrences" (Cmd+F2): select every whole-word,
/// case-sensitive textual match of the identifier under the cursor in the
/// current file and turn each into a caret. The match at the cursor (or
/// the first, if the cursor isn't on one) becomes the primary selection;
/// the rest become secondary carets. Returns the number of occurrences
/// selected (0 when the cursor isn't on a word, leaving state untouched).
pub fn select_all_occurrences_of_word_at_cursor(&mut self) -> usize {
let Some((start, end)) = self.word_at(self.cursor_row, self.cursor_col) else {
return 0;
};
let word: Vec<char> = self.lines[self.cursor_row]
.chars()
.skip(start)
.take(end - start)
.collect();
if word.is_empty() {
return 0;
}
let mut occ: Vec<EditorSelection> = Vec::new();
for (row, line) in self.lines.iter().enumerate() {
let chars: Vec<char> = line.chars().collect();
for col in find_word_occurrences(&chars, &word) {
occ.push(EditorSelection {
anchor: (row, col),
head: (row, col + word.len()),
});
}
}
if occ.is_empty() {
return 0;
}
// The occurrence containing the cursor becomes primary so typing
// continues where the user is looking; otherwise fall back to the
// first match in the file.
let primary_idx = occ
.iter()
.position(|s| {
s.anchor.0 == self.cursor_row
&& self.cursor_col >= s.anchor.1
&& self.cursor_col <= s.head.1
})
.unwrap_or(0);
let primary = occ[primary_idx];
self.selection = Some(primary);
self.cursor_row = primary.head.0;
self.cursor_col = primary.head.1;
self.carets = occ
.into_iter()
.enumerate()
.filter(|(i, _)| *i != primary_idx)
.map(|(_, s)| s)
.collect();
self.carets.len() + 1
}
/// VS Code "Add Selection to Next Find Match" (`Cmd+D`): the first press
/// selects the word under the cursor; each further press finds the next
/// occurrence of the selected text (document order, wrapping) that is not
/// already a caret and adds it as a secondary caret, moving the primary to
/// it so the view follows. Returns the number of cursors (0 when the first
/// press lands off a word, leaving state untouched).
pub fn select_next_occurrence(&mut self) -> usize {
// First press with no selection: select the word under the cursor.
let Some(sel) = self.selection else {
let Some((s, e)) = self.word_at(self.cursor_row, self.cursor_col) else {
return 0;
};
self.selection = Some(EditorSelection {
anchor: (self.cursor_row, s),
head: (self.cursor_row, e),
});
self.cursor_col = e;
self.ensure_cursor_col_visible();
return 1;
};
// Only single-line word selections drive the incremental match search.
let (start, end) = sel.normalised();
if start.0 != end.0 {
return self.carets.len() + 1;
}
let word: Vec<char> = self.lines[start.0]
.chars()
.skip(start.1)
.take(end.1 - start.1)
.collect();
if word.is_empty() {
return self.carets.len() + 1;
}
// Every occurrence in document order, and the set already selected
// (the primary plus each existing caret), keyed by its start.
let mut occ: Vec<(usize, usize)> = Vec::new();
for (row, line) in self.lines.iter().enumerate() {
let chars: Vec<char> = line.chars().collect();
for col in find_word_occurrences(&chars, &word) {
occ.push((row, col));
}
}
let mut taken: Vec<(usize, usize)> = self.carets.iter().map(|c| c.normalised().0).collect();
taken.push(start);
// Scan forward from the primary, wrapping, for the first match not yet
// taken.
let from = occ.iter().position(|&o| o == start).unwrap_or(0);
let n = occ.len();
let next = (1..=n)
.map(|k| occ[(from + k) % n])
.find(|o| !taken.contains(o));
let Some((row, col)) = next else {
return self.carets.len() + 1;
};
// Demote the current primary to a caret, promote the new match.
self.carets.push(sel);
let head = (row, col + word.len());
self.selection = Some(EditorSelection {
anchor: (row, col),
head,
});
self.cursor_row = head.0;
self.cursor_col = head.1;
self.ensure_cursor_col_visible();
self.carets.len() + 1
}
/// Insert a character at every caret. One undo step.
pub fn multi_insert_char(&mut self, c: char) {
self.multi_apply(CaretEdit::Insert(c));
}
/// Backspace at every caret. One undo step.
pub fn multi_backspace(&mut self) {
self.multi_apply(CaretEdit::Backspace);
}
/// Forward-delete at every caret. One undo step.
pub fn multi_delete_forward(&mut self) {
self.multi_apply(CaretEdit::DeleteForward);
}
/// Apply `edit` at the primary caret and every secondary caret as a
/// single undo step. Carets are processed bottom-to-top so an edit never
/// shifts the coordinates of a caret still to be processed.
fn multi_apply(&mut self, edit: CaretEdit) {
self.pin_on_edit();
self.push_undo(EditKind::MultiEdit);
let primary = self
.selection
.unwrap_or_else(|| EditorSelection::new(self.cursor_row, self.cursor_col));
let mut items: Vec<(bool, EditorSelection)> = Vec::with_capacity(self.carets.len() + 1);
items.push((true, primary));
for s in &self.carets {
items.push((false, *s));
}
// Descending by start so the lowest caret is edited first.
items.sort_by_key(|item| std::cmp::Reverse(item.1.normalised().0));
// Clear so the per-caret raw ops can't see stale carets.
self.carets.clear();
let mut new_primary = (self.cursor_row, self.cursor_col);
let mut new_secondary: Vec<(usize, usize)> = Vec::new();
for (is_primary, sel) in items {
if sel.has_area() {
self.selection = Some(sel);
} else {
self.selection = None;
}
let last = self.lines.len().saturating_sub(1);
self.cursor_row = sel.head.0.min(last);
self.cursor_col = sel.head.1.min(self.line_char_len(self.cursor_row));
self.apply_caret_edit_raw(edit);
let pos = (self.cursor_row, self.cursor_col);
if is_primary {
new_primary = pos;
} else {
new_secondary.push(pos);
}
}
self.selection = None;
let last = self.lines.len().saturating_sub(1);
self.cursor_row = new_primary.0.min(last);
self.cursor_col = new_primary.1.min(self.line_char_len(self.cursor_row));
new_secondary.sort_unstable();
self.carets = new_secondary
.into_iter()
.map(|(r, c)| EditorSelection::new(r, c))
.collect();
self.recompute_highlights();
}
/// One caret's worth of a multi-edit: no snapshot, no re-highlight (the
/// batching caller in `multi_apply` does both once).
fn apply_caret_edit_raw(&mut self, edit: CaretEdit) {
match edit {
CaretEdit::Insert(c) => {
self.delete_selection_inner();
self.insert_char_raw(c);
}
CaretEdit::Backspace => self.backspace_raw(),
CaretEdit::DeleteForward => self.delete_forward_raw(),
}
}
pub fn start_selection_at_cursor(&mut self) {
self.selection = Some(EditorSelection::new(self.cursor_row, self.cursor_col));
}
pub fn extend_selection_to_cursor(&mut self) {
if let Some(sel) = self.selection.as_mut() {
sel.head = (self.cursor_row, self.cursor_col);
} else {
self.start_selection_at_cursor();
}
}
pub fn clear_selection(&mut self) {
self.selection = None;
}
pub fn select_all(&mut self) {
if self.lines.is_empty() {
self.selection = None;
return;
}
let last_row = self.lines.len() - 1;
let last_col = self.line_char_len(last_row);
self.selection = Some(EditorSelection {
anchor: (0, 0),
head: (last_row, last_col),
});
self.cursor_row = last_row;
self.cursor_col = last_col;
}
/// Extract the selection text (`\n`-joined across rows) using the
/// editor's char-indexed coordinates. Returns "" when there's no
/// selection or the selection is zero-area.
pub fn selection_text(&self) -> String {
let Some(sel) = self.selection else {
return String::new();
};
if !sel.has_area() {
return String::new();
}
let ((sr, sc), (er, ec)) = sel.normalised();
if sr == er {
let line = &self.lines[sr];
let from = char_byte(line, sc);
let to = char_byte(line, ec);
return line[from..to].to_string();
}
let mut out = String::new();
// first row: from sc to end of line
let first = &self.lines[sr];
let from = char_byte(first, sc);
out.push_str(&first[from..]);
out.push('\n');
// full middle rows
for r in (sr + 1)..er {
out.push_str(&self.lines[r]);
out.push('\n');
}
// last row: from start to ec
let last = &self.lines[er];
let to = char_byte(last, ec);
out.push_str(&last[..to]);
out
}
/// The literal text to light up as "other occurrences" of the current
/// selection (VS Code's selection highlight). Returns `None` when the
/// selection shouldn't drive occurrence highlighting at all. Computed once
/// per render and fed to `paint_selection_occurrences` for every visible
/// line.
fn selection_occurrence_needle(&self) -> Option<String> {
let sel = self.selection?;
if !sel.has_area() {
return None;
}
let ((sr, sc), (er, ec)) = sel.normalised();
if sr != er {
// VS Code only highlights occurrences for single-line selections.
return None;
}
let line = &self.lines[sr];
let text = &line[char_byte(line, sc)..char_byte(line, ec)];
// Mirror VS Code's non-empty-selection branch: skip whitespace-only
// selections and anything past the 200-char ceiling
// (`editor.selectionHighlightMaxLength`); otherwise highlight the
// literal substring. No minimum length, no whole-word filter.
if text.trim().is_empty() || text.chars().count() > SELECTION_HIGHLIGHT_MAX_LEN {
return None;
}
Some(text.to_string())
}
/// Delete the current selection if it has area. Returns true iff content
/// was removed. Cursor lands at the start of the deleted range and the
/// selection is cleared. Pushes an undo step.
pub fn delete_selection(&mut self) -> bool {
if !self.selection.map(|s| s.has_area()).unwrap_or(false) {
self.selection = None;
return false;
}
self.push_undo(EditKind::DeleteSelection);
let removed = self.delete_selection_inner();
if removed {
self.recompute_highlights();
}
removed
}
/// Same as `delete_selection` but does NOT push an undo step or
/// recompute highlights — used by other public mutators that have
/// already snapshotted state and will recompute themselves.
fn delete_selection_inner(&mut self) -> bool {
let Some(sel) = self.selection else {
return false;
};
if !sel.has_area() {
self.selection = None;
return false;
}
let ((sr, sc), (er, ec)) = sel.normalised();
if sr == er {
let line = &mut self.lines[sr];
let from = char_byte(line, sc);
let to = char_byte(line, ec);
line.replace_range(from..to, "");
} else {
let last = self.lines.remove(er);
for _ in (sr + 1)..er {
self.lines.remove(sr + 1);
}
let first = &mut self.lines[sr];
let from = char_byte(first, sc);
first.truncate(from);
let to = char_byte(&last, ec);
first.push_str(&last[to..]);
}
self.cursor_row = sr;
self.cursor_col = sc;
self.selection = None;
self.mark_buffer_changed();
true
}
/// Leading-whitespace width of `line`, or `None` for a blank line (a blank
/// line belongs to whatever block surrounds it, so it carries no indent of
/// its own). Tabs and spaces each count as one column, which is all the
/// fold heuristic needs: it only ever compares indents with `>`.
fn indent_width(&self, line: usize) -> Option<usize> {
let s = self.lines.get(line)?;
(!s.trim().is_empty()).then(|| s.chars().take_while(|c| *c == ' ' || *c == '\t').count())
}
/// The foldable region headed by `line`, as `(line, last_line_inclusive)`,
/// or `None` when nothing below `line` is more indented. Interior blank
/// lines are absorbed; trailing blanks and the first line back at (or
/// below) the header's indent are not. This is VS Code's default
/// indentation-based folding: a pure function of the text, no LSP or
/// tree-sitter round-trip.
pub fn fold_range(&self, line: usize) -> Option<(usize, usize)> {
let base = self.indent_width(line)?;
let mut end = line;
let mut i = line + 1;
while i < self.lines.len() {
match self.indent_width(i) {
None => {} // blank: possibly interior, keep scanning
Some(w) if w > base => end = i,
Some(_) => break, // back to base indent or shallower
}
i += 1;
}
(end > line).then_some((line, end))
}
/// Whether `line` heads a collapsible region.
pub fn is_foldable(&self, line: usize) -> bool {
self.fold_range(line).is_some()
}
/// The nearest foldable header at or above `line` whose region contains
/// `line`, so "fold at cursor" collapses the enclosing block when the
/// cursor sits in a body line rather than on the header itself.
fn enclosing_fold_header(&self, line: usize) -> Option<usize> {
if self.is_foldable(line) {
return Some(line);
}
(0..line)
.rev()
.find(|&h| matches!(self.fold_range(h), Some((_, end)) if line <= end))
}
/// Whether `line` is hidden inside a collapsed region. A binary search over
/// the merged spans in [`Self::hidden_ranges`], which the fold writes keep
/// current — this is called for every rendered row of every frame.
pub fn is_line_hidden(&self, line: usize) -> bool {
self.hidden_ranges
.binary_search_by(|&(header, end)| {
if line <= header {
std::cmp::Ordering::Greater
} else if line > end {
std::cmp::Ordering::Less
} else {
std::cmp::Ordering::Equal
}
})
.is_ok()
}
/// Recompute [`Self::hidden_ranges`] from `folded`. Every write to `folded`
/// must call this. Headers are visited in ascending order and any header
/// already inside the span being built is skipped: an inner fold's range is
/// contained by its outer's, so it can add nothing.
fn rebuild_hidden_ranges(&mut self) {
#[cfg(test)]
FOLD_RANGE_REBUILDS.with(|c| c.set(c.get() + 1));
let mut out: Vec<(usize, usize)> = Vec::new();
for &h in &self.folded {
if let Some(&(_, end)) = out.last()
&& h <= end
{
continue;
}
if let Some(span) = self.fold_range(h) {
out.push(span);
}
}
self.hidden_ranges = out;
}
/// Unfold whatever collapsed region the cursor is sitting inside. Movement
/// is not the only way in — search, go-to-definition and goto-line all set
/// `cursor_row` directly — and a caret on a hidden line cannot be painted
/// at all (`cursor_screen_pos` has no row for it), so an edit there would
/// change content the user cannot see. Cheap when nothing is folded.
pub fn reveal_cursor_fold(&mut self) {
if self.folded.is_empty() {
return;
}
let row = self.cursor_row;
let covering: Vec<usize> = self
.folded
.iter()
.copied()
.filter(|&h| matches!(self.fold_range(h), Some((_, end)) if h < row && row <= end))
.collect();
if covering.is_empty() {
// Nothing to reveal, so the spans are still current. `pin_on_edit`
// calls this on every mutation: rebuilding here would rescan the
// whole buffer once per keystroke for every folded header, which
// after Fold All is the expensive case this cache exists to avoid.
return;
}
for h in covering {
self.folded.remove(&h);
}
self.rebuild_hidden_ranges();
}
/// Next visible line at or after `line`, walking out of any collapsed
/// region. `None` past the end of the buffer.
fn next_visible_line(&self, line: usize) -> Option<usize> {
let mut l = line;
while l < self.lines.len() {
if !self.is_line_hidden(l) {
return Some(l);
}
l += 1;
}
None
}
/// Previous visible line at or before `line`.
fn prev_visible_line(&self, line: usize) -> usize {
let mut l = line;
while l > 0 && self.is_line_hidden(l) {
l -= 1;
}
l
}
/// Toggle the fold owning `line` (VS Code `editor.toggleFold`). Collapsing a
/// block that contains the cursor snaps the cursor up to the header so it
/// never strands on a hidden line.
pub fn toggle_fold(&mut self, line: usize) {
let Some(header) = self.enclosing_fold_header(line) else {
return;
};
let collapsing = !self.folded.remove(&header);
if collapsing {
self.folded.insert(header);
}
// Both branches changed the fold set, so both must refresh the spans.
self.rebuild_hidden_ranges();
if collapsing && self.is_line_hidden(self.cursor_row) {
let len = self
.lines
.get(header)
.map(|s| s.chars().count())
.unwrap_or(0);
self.cursor_row = header;
self.cursor_col = self.cursor_col.min(len);
}
self.fold_epoch_lines = self.lines.len();
}
/// Collapse every foldable region (VS Code "Fold All", Cmd+K Cmd+0),
/// snapping the cursor up to the nearest visible line.
pub fn fold_all(&mut self) {
self.folded = (0..self.lines.len())
.filter(|&l| self.is_foldable(l))
.collect();
self.fold_epoch_lines = self.lines.len();
self.rebuild_hidden_ranges();
while self.cursor_row > 0 && self.is_line_hidden(self.cursor_row) {
self.cursor_row -= 1;
}
}
/// Expand every fold (VS Code "Unfold All", Cmd+K Cmd+J).
pub fn unfold_all(&mut self) {
self.folded.clear();
self.hidden_ranges.clear();
}
fn snapshot(&self) -> Snapshot {
Snapshot {
lines: self.lines.clone(),
cursor_row: self.cursor_row,
cursor_col: self.cursor_col,
selection: self.selection,
carets: self.carets.clone(),
dirty: self.dirty,
save_seq: self.save_seq,
}
}
/// Push an undo entry tagged with the kind of edit about to happen.
/// Coalesces consecutive `InsertChar` ops into one step so a typing
/// burst is undone as one unit; everything else opens a new step.
fn push_undo(&mut self, kind: EditKind) {
let coalesce =
kind == EditKind::InsertChar && self.last_edit_kind == Some(EditKind::InsertChar);
if !coalesce {
self.undo_stack.push(self.snapshot());
if self.undo_stack.len() > UNDO_STACK_LIMIT {
self.undo_stack.remove(0);
}
}
// A fresh edit branches history: whatever was undone can no longer be
// redone (VS Code's model). Cleared even on a coalesced keystroke so a
// typing burst after an undo discards the redo branch on its first char.
self.redo_stack.clear();
self.last_edit_kind = Some(kind);
}
/// Undo the most recent edit step. Returns true iff state was changed.
pub fn undo(&mut self) -> bool {
let Some(snap) = self.undo_stack.pop() else {
return false;
};
// Stash the pre-undo state so `redo` can reinstate it.
self.redo_stack.push(self.snapshot());
self.restore_snapshot(snap);
true
}
/// Reapply the most recently undone edit step. Returns true iff state was
/// changed. Mirrors `undo`, moving the current state back onto the undo
/// stack so the redone edit can itself be undone.
pub fn redo(&mut self) -> bool {
let Some(snap) = self.redo_stack.pop() else {
return false;
};
self.undo_stack.push(self.snapshot());
self.restore_snapshot(snap);
true
}
/// Replace the buffer state with a snapshot (shared by undo/redo), clamping
/// the caret into the restored buffer and refreshing highlights.
fn restore_snapshot(&mut self, snap: Snapshot) {
self.lines = snap.lines;
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.cursor_row = snap.cursor_row.min(self.lines.len().saturating_sub(1));
self.cursor_col = snap.cursor_col.min(self.line_char_len(self.cursor_row));
self.selection = snap.selection;
self.carets = snap.carets;
// Crossing a save point (auto save or Cmd+S since the snapshot)
// means the restored text differs from disk: stay dirty so the next
// sweep reconverges them instead of leaving a silent divergence.
self.dirty = snap.dirty || self.save_seq != snap.save_seq;
self.last_edit_kind = None;
// A restore changes the buffer content, so bump the change counter (the
// app resyncs the LSP / git gutter off `edit_seq`) and drop the wrap /
// hscroll geometry caches keyed to the old text. `dirty` is taken from
// the snapshot above, so this is not `mark_buffer_changed` (which would
// force it true and lose an undo back to a saved state).
self.edit_seq = self.edit_seq.wrapping_add(1);
self.hscroll_content_cols = None;
self.wrap_total_cache.clear();
self.recompute_highlights();
}
/// Open a new undo step for the next edit (so a typing run doesn't
/// merge with whatever comes after a movement / mouse / focus change).
pub fn break_undo_coalescing(&mut self) {
self.last_edit_kind = None;
}
/// Mouse-down: position the cursor at the click point and start a
/// fresh zero-area selection there. A subsequent drag widens it.
pub fn mouse_down(&mut self, col: u16, row: u16) {
self.click(col, row);
self.start_selection_at_cursor();
}
/// Mouse-drag: move the cursor to the drag point and extend the selection
/// head to the new cursor. Anchors at the current cursor if no prior
/// selection exists.
pub fn mouse_drag(&mut self, col: u16, row: u16) {
if self.selection.is_none() {
self.start_selection_at_cursor();
}
self.click(col, row);
self.extend_selection_to_cursor();
}
/// Returns true if the on-disk file at `event_path` is the file currently
/// open in this editor (used by the filesystem watcher to decide whether
/// to reload).
pub fn matches_open_path(&self, event_path: &Path) -> bool {
let Some(open) = self.path.as_ref() else {
return false;
};
if open == event_path {
return true;
}
if let (Ok(a), Ok(b)) = (open.canonicalize(), event_path.canonicalize()) {
return a == b;
}
false
}
/// Read the file's identity (mtime, len) for change detection. `None`
/// when there is no path or the file can't be stat'd (e.g. it was
/// deleted out from under us).
fn disk_stamp_of(path: &Path) -> Option<(SystemTime, u64)> {
let meta = std::fs::metadata(path).ok()?;
Some((meta.modified().ok()?, meta.len()))
}
/// Record that this buffer is now in sync with disk. Called at open and
/// after a successful save so the next external write is detected.
fn mark_synced_with_disk(&mut self) {
self.disk_stamp = self.path.as_deref().and_then(Self::disk_stamp_of);
self.disk_conflict = false;
// A new buffer<->disk sync point; undo snapshots from before it
// restore as dirty (see `restore_snapshot`).
self.save_seq = self.save_seq.wrapping_add(1);
}
/// True when the file on disk no longer matches the (mtime, len) we last
/// loaded or saved — i.e. some other process wrote it. A file with no
/// recorded stamp (blank buffer, or never synced) is treated as
/// unchanged so we never reload or block on a phantom diff.
pub fn disk_changed_externally(&self) -> bool {
let Some(path) = self.path.as_deref() else {
return false;
};
match self.disk_stamp {
Some(known) => Self::disk_stamp_of(path) != Some(known),
None => false,
}
}
/// Reload from disk *only if* there are no unsaved local edits. Returns
/// `Some(Ok(()))` if a reload happened, `Some(Err(_))` if reload failed,
/// `None` if reload was skipped because the buffer is dirty (caller
/// should surface a "file changed on disk" warning instead). Retained
/// for the explicit-revert path; the FS sync sweep uses
/// `reload_or_flag_conflict`.
pub fn reload_if_clean(&mut self) -> Option<Result<()>> {
if self.dirty {
return None;
}
Some(self.reload_from_disk())
}
/// Reload the buffer from disk, preserving the cursor/scroll position as
/// far as the new contents allow. Updates the disk stamp so the reloaded
/// state is the new sync point.
fn reload_from_disk(&mut self) -> Result<()> {
let Some(path) = self.path.as_ref().cloned() else {
return Ok(());
};
let prev_row = self.cursor_row;
let prev_col = self.cursor_col;
let prev_scroll = self.scroll;
// A PDF's page is its cursor: a rebuild on disk (pdflatex finishing)
// must not snap the reader back to page 1. `open_pdf` consumes the
// request and comes up on that page directly — restoring it with a
// second render after the open left a window where that render's
// transient failure lost the reader's place (#72).
self.pdf_restore_page = self.pdf_page().filter(|&p| p > 1);
let result = self.open(&path);
// Cleared even when the open bailed before reaching `open_pdf`, so
// the request can never leak into an unrelated later open.
self.pdf_restore_page = None;
// Clamp the restored cursor to the new contents so it stays valid
// even if the file shrank.
self.cursor_row = prev_row.min(self.lines.len().saturating_sub(1));
self.cursor_col = prev_col.min(self.line_char_len(self.cursor_row));
self.scroll = prev_scroll.min(self.lines.len().saturating_sub(1));
result
}
/// Discard local edits and reload from disk unconditionally — the
/// "Revert" half of a conflict resolution.
pub fn revert_to_disk(&mut self) -> Result<()> {
// Reload FIRST: clearing `dirty` before a reload that then fails
// (file deleted between the conflict popup and the Enter) would
// launder unsaved edits as clean — the tab loses its marker and
// the next FS sweep silently auto-reverts them away.
self.reload_from_disk()?;
self.dirty = false;
Ok(())
}
/// FS-sync sweep entry point, applied to every open tab. Compares the
/// file's current disk stamp against the buffer's last-synced stamp and:
/// * clean buffer + external change -> silently reload (VS Code's
/// non-dirty auto-revert behaviour),
/// * dirty buffer + external change -> flag a conflict so neither the
/// buffer nor the disk is clobbered,
/// * no change -> nothing.
pub fn reload_or_flag_conflict(&mut self) -> ExternalChange {
if !self.disk_changed_externally() {
return ExternalChange::Unchanged;
}
if self.dirty {
// Announce the conflict only on the transition into it. A buffer
// that stays dirty while disk keeps differing must not re-fire on
// every FS poll, or the confirm popup would reopen endlessly.
let newly_conflicted = !self.disk_conflict;
self.disk_conflict = true;
return if newly_conflicted {
ExternalChange::Conflict
} else {
ExternalChange::Unchanged
};
}
match self.reload_from_disk() {
Ok(()) => ExternalChange::Reloaded,
Err(_) => ExternalChange::ReloadFailed,
}
}
/// Save, refusing to overwrite if the file changed on disk since we last
/// synced (an external edit would be silently lost otherwise). Returns
/// `DiskConflict` instead of writing in that case; the caller surfaces
/// the conflict and may call `save_to_disk_force` to overwrite anyway.
pub fn save_to_disk(&mut self) -> Result<SaveOutcome> {
if self.disk_changed_externally() {
self.disk_conflict = true;
return Ok(SaveOutcome::DiskConflict);
}
self.write_buffer_to_disk()
}
/// Save past a disk conflict, overwriting the external change. The
/// "Overwrite" half of a conflict resolution.
///
/// It does NOT bypass the encoding-loss guard: consenting to overwrite
/// someone else's edit is not consent to mangle your own characters, so a
/// buffer with both problems still reports `EncodingLoss` here and needs
/// `lossy_save_armed` on top.
pub fn save_to_disk_force(&mut self) -> Result<SaveOutcome> {
self.write_buffer_to_disk()
}
/// Encode the buffer for disk in the encoding it claims, re-emitting the
/// byte-order mark if the file had one.
///
/// UTF-16 is hand-rolled because `encoding_rs` deliberately cannot produce
/// it: the WHATWG Encoding Standard makes UTF-16 decode-only, so
/// `Encoding::encode` routes UTF-16LE/BE (and REPLACEMENT) through
/// `output_encoding()` to UTF-8 and hands back the string's UTF-8 bytes.
/// Taking that output at face value wrote a file that disagreed with the
/// encoding the status bar reported for it.
///
/// Returns the bytes and whether the encoder had to substitute: for a
/// legacy encoding `encoding_rs` does NOT write `?` the way iconv-lite
/// (and so VS Code) does — per the WHATWG Encoding Standard it emits an
/// HTML numeric character reference, which is valid content that no
/// longer round-trips. Callers must not write that without consent.
fn encode_for_disk(&self, content: &str) -> (Vec<u8>, bool) {
let enc = self.encoding;
if enc == encoding_rs::UTF_16LE || enc == encoding_rs::UTF_16BE {
let le = enc == encoding_rs::UTF_16LE;
let mut out = Vec::with_capacity(content.len() * 2 + 2);
// ALWAYS, regardless of whether the source had one: UTF-16 is
// otherwise indistinguishable from binary, so a file written
// without it cannot be reopened by croft or auto-detected by
// anything else. VS Code's UTF-16 entries write it unconditionally
// for the same reason.
out.extend_from_slice(if le { &[0xFF, 0xFE] } else { &[0xFE, 0xFF] });
for unit in content.encode_utf16() {
out.extend_from_slice(&if le {
unit.to_le_bytes()
} else {
unit.to_be_bytes()
});
}
// Every `char` encodes to UTF-16 losslessly (a `str` cannot hold
// an unpaired surrogate), so this branch never substitutes.
return (out, false);
}
let (encoded, _, had_errors) = enc.encode(content);
if self.bom && enc == encoding_rs::UTF_8 {
let mut out = Vec::with_capacity(encoded.len() + 3);
out.extend_from_slice(&[0xEF, 0xBB, 0xBF]);
out.extend_from_slice(&encoded);
return (out, had_errors);
}
(encoded.into_owned(), had_errors)
}
/// The distinct characters `encoding` cannot represent, in first-seen
/// order and capped, for the message an `EncodingLoss` refusal shows.
///
/// Walks the buffer and encodes each non-ASCII character on its own, so
/// call it once to build that message rather than per frame. Empty for
/// UTF-8 and UTF-16, which cover every `char`.
pub fn unmappable_chars(&self) -> Vec<char> {
/// Enough to name the problem without turning the status line into a
/// character dump.
const MAX: usize = 8;
let enc = self.encoding;
if enc == encoding_rs::UTF_8 || enc == encoding_rs::UTF_16LE || enc == encoding_rs::UTF_16BE
{
return Vec::new();
}
let mut out: Vec<char> = Vec::new();
let mut buf = [0u8; 4];
for line in &self.lines {
// ASCII is mappable in every encoding croft offers, and is the
// overwhelming majority of a source file: skip it without paying
// for an encode call.
for ch in line.chars().filter(|c| !c.is_ascii()) {
if out.contains(&ch) {
continue;
}
let (_, _, had_errors) = enc.encode(ch.encode_utf8(&mut buf));
if had_errors {
out.push(ch);
if out.len() == MAX {
return out;
}
}
}
}
out
}
fn write_buffer_to_disk(&mut self) -> Result<SaveOutcome> {
let path = self
.path
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No file open"))?
.clone();
let content = self.lines.join(self.eol.sequence());
let (encoded, had_errors) = self.encode_for_disk(&content);
// The single choke point every save funnels through, so no caller can
// route around the guard. The consent flag survives a failed write
// (the user still consented) and is cleared only once the bytes land.
if had_errors && !self.lossy_save_armed {
self.encoding_loss = true;
return Ok(SaveOutcome::EncodingLoss);
}
std::fs::write(&path, encoded)?;
self.encoding_loss = false;
self.lossy_save_armed = false;
self.dirty = false;
// The next keystroke opens a fresh undo step: coalescing across the
// save point would make one Cmd+Z discard work from both sides.
self.last_edit_kind = None;
self.status = format!("Saved {}", path.display());
// The buffer now matches disk, so this is the new sync point and any
// prior conflict is resolved.
self.mark_synced_with_disk();
Ok(SaveOutcome::Saved)
}
pub fn move_up(&mut self) {
if self.wrap_enabled() {
self.wrap_move_vertical(-1);
} else if self.cursor_row > 0 {
// Step over a collapsed region, as `move_down` does.
self.cursor_row = self.prev_visible_line(self.cursor_row - 1);
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
}
self.last_edit_kind = None;
}
pub fn move_down(&mut self) {
if self.wrap_enabled() {
self.wrap_move_vertical(1);
} else if self.cursor_row + 1 < self.lines.len() {
// Step OVER a collapsed region rather than into it, the way VS Code
// does: the fold stays shut and the caret lands on the next line
// the user can actually see.
if let Some(next) = self.next_visible_line(self.cursor_row + 1) {
self.cursor_row = next;
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
}
}
self.last_edit_kind = None;
}
pub fn goto_top(&mut self) {
self.clear_selection();
self.cursor_row = 0;
self.cursor_col = 0;
self.last_edit_kind = None;
}
pub fn goto_bottom(&mut self) {
self.clear_selection();
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.cursor_row = self.lines.len() - 1;
self.cursor_col = 0;
self.last_edit_kind = None;
}
/// Copy the caret + scroll position from another editor onto this one,
/// clamped to this buffer's bounds. Used when the editor is split so
/// the duplicate opens exactly where the source was - including the
/// wrap sub-line offset (`scroll_sub`), which is private to this
/// module and can't be set from `App`.
pub fn copy_view_position_from(&mut self, src: &Editor) {
let max_row = self.lines.len().saturating_sub(1);
self.cursor_row = src.cursor_row.min(max_row);
let max_col = self
.lines
.get(self.cursor_row)
.map_or(0, |l| l.chars().count());
self.cursor_col = src.cursor_col.min(max_col);
self.scroll = src.scroll.min(max_row);
self.scroll_sub = src.scroll_sub;
self.scroll_col = src.scroll_col;
}
pub fn goto_line(&mut self, one_based: usize) {
self.clear_selection();
if self.lines.is_empty() {
self.lines.push(String::new());
}
let max = self.lines.len() - 1;
let target = one_based.saturating_sub(1).min(max);
self.cursor_row = target;
self.cursor_col = 0;
self.last_edit_kind = None;
}
pub fn yank_lines(&self, count: usize) -> String {
let n = count.max(1);
let start = self.cursor_row;
self.lines_slice_text(start, n)
}
pub fn delete_lines(&mut self, count: usize) -> String {
self.pin_on_edit();
if self.lines.is_empty() {
return String::new();
}
self.push_undo(EditKind::DeleteLines);
self.clear_selection();
let n = count.max(1);
let start = self.cursor_row;
let end = (start + n).min(self.lines.len());
let yanked = self.lines_slice_text(start, end - start);
self.lines.drain(start..end);
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.cursor_row = start.min(self.lines.len() - 1);
self.cursor_col = 0;
self.mark_buffer_changed();
self.recompute_highlights();
yanked
}
/// Delete every line touched by the primary cursor or selection and by
/// each secondary caret, yanking the removed text. VS Code's
/// `editor.action.deleteLines` works across all cursors, and `Cmd+D`
/// exists to make them: deleting only the primary's line would strand the
/// others, still painted on rows that moved out from under them.
pub fn delete_caret_lines(&mut self) -> String {
self.pin_on_edit();
if self.lines.is_empty() {
return String::new();
}
self.push_undo(EditKind::DeleteLines);
let last = self.lines.len() - 1;
let primary = self
.selection
.unwrap_or_else(|| EditorSelection::new(self.cursor_row, self.cursor_col));
// Mark while collecting, then scan once: the rows come out sorted and
// deduped without a sort, and Change All Occurrences (Cmd+F2) can put
// a caret on every match in the file.
let mut doomed = vec![false; self.lines.len()];
for sel in std::iter::once(primary).chain(self.carets.iter().copied()) {
let (a, h) = sel.normalised();
for slot in &mut doomed[a.0.min(last)..=h.0.min(last)] {
*slot = true;
}
}
let rows: Vec<usize> = doomed
.iter()
.enumerate()
.filter_map(|(r, &drop)| drop.then_some(r))
.collect();
let yanked = rows
.iter()
.map(|&r| self.lines[r].as_str())
.collect::<Vec<_>>()
.join("\n")
+ "\n";
// One retain pass, not a `remove` per row: removing tens of thousands
// of rows one at a time is quadratic — a multi-second freeze on a large
// file, on the render thread.
let mut i = 0;
self.lines.retain(|_| {
let keep = !doomed[i];
i += 1;
keep
});
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.selection = None;
self.carets.clear();
self.cursor_row = rows[0].min(self.lines.len() - 1);
self.cursor_col = 0;
self.mark_buffer_changed();
self.recompute_highlights();
yanked
}
pub fn kill_to_bol(&mut self) -> String {
self.pin_on_edit();
if self.lines.is_empty() {
return String::new();
}
self.clear_selection();
let row = self.cursor_row;
if self.cursor_col == 0 {
return String::new();
}
self.push_undo(EditKind::KillToBol);
let from = self.byte_index(row, 0);
let to = self.byte_index(row, self.cursor_col);
let killed = self.lines[row][from..to].to_string();
self.lines[row].replace_range(from..to, "");
self.cursor_col = 0;
self.mark_buffer_changed();
self.recompute_highlights();
killed
}
pub fn open_line_below(&mut self) {
self.pin_on_edit();
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.push_undo(EditKind::OpenLine);
self.clear_selection();
let row = self.cursor_row;
let indent: String = self.lines[row]
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
self.lines.insert(row + 1, indent.clone());
self.cursor_row = row + 1;
self.cursor_col = indent.chars().count();
self.mark_buffer_changed();
self.recompute_highlights();
}
pub fn open_line_above(&mut self) {
self.pin_on_edit();
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.push_undo(EditKind::OpenLine);
self.clear_selection();
let row = self.cursor_row;
let indent: String = self.lines[row]
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
self.lines.insert(row, indent.clone());
self.cursor_col = indent.chars().count();
self.mark_buffer_changed();
self.recompute_highlights();
}
pub fn kill_to_eol(&mut self) -> String {
self.pin_on_edit();
if self.lines.is_empty() {
return String::new();
}
self.clear_selection();
let row = self.cursor_row;
let line_len = self.line_char_len(row);
if self.cursor_col >= line_len {
return String::new();
}
self.push_undo(EditKind::KillToEol);
let from = self.byte_index(row, self.cursor_col);
let to = self.byte_index(row, line_len);
let killed = self.lines[row][from..to].to_string();
self.lines[row].replace_range(from..to, "");
self.mark_buffer_changed();
self.recompute_highlights();
killed
}
/// Splice `text` over the `len_chars` characters starting at
/// `(row, col_chars)` — the find bar's Replace. One undo step; the
/// cursor lands at the end of the inserted text so replace-and-advance
/// continues from there.
pub fn replace_find_match(
&mut self,
row: usize,
col_chars: usize,
len_chars: usize,
text: &str,
) {
if row >= self.lines.len() {
return;
}
let line_len = self.line_char_len(row);
if col_chars.saturating_add(len_chars) > line_len {
return;
}
self.pin_on_edit();
self.push_undo(EditKind::Replace);
self.clear_selection();
let from = self.byte_index(row, col_chars);
let to = self.byte_index(row, col_chars + len_chars);
let text = &normalize_newlines(text);
let breaks = text.matches('\n').count();
if breaks == 0 {
self.lines[row].replace_range(from..to, text);
self.cursor_row = row;
self.cursor_col = col_chars + text.chars().count();
} else {
// A replacement newline (VS Code's regex `\n`) becomes a real
// line break, never an embedded control char in one line.
let line = std::mem::take(&mut self.lines[row]);
let merged = format!("{}{}{}", &line[..from], text, &line[to..]);
self.lines
.splice(row..=row, merged.split('\n').map(str::to_string));
self.cursor_row = row + breaks;
self.cursor_col = text
.rsplit('\n')
.next()
.map_or(0, |tail| tail.chars().count());
}
self.mark_buffer_changed();
self.recompute_highlights();
}
/// Swap in a whole new buffer as one undo step — the find bar's
/// Replace All. The cursor clamps to the new buffer's bounds.
pub fn replace_all_lines(&mut self, new_lines: Vec<String>) {
self.pin_on_edit();
self.push_undo(EditKind::Replace);
self.clear_selection();
self.lines = new_lines;
if self.lines.is_empty() {
self.lines.push(String::new());
}
self.cursor_row = self.cursor_row.min(self.lines.len() - 1);
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.mark_buffer_changed();
self.recompute_highlights();
}
fn lines_slice_text(&self, start: usize, count: usize) -> String {
if count == 0 || start >= self.lines.len() {
return String::new();
}
let end = (start + count).min(self.lines.len());
let mut out = self.lines[start..end].join("\n");
out.push('\n');
out
}
pub fn move_left(&mut self) {
if self.cursor_col > 0 {
self.cursor_col -= 1;
} else if self.cursor_row > 0 {
self.cursor_row -= 1;
self.cursor_col = self.line_char_len(self.cursor_row);
}
self.ensure_cursor_col_visible();
self.last_edit_kind = None;
}
pub fn move_right(&mut self) {
if self.cursor_col < self.line_char_len(self.cursor_row) {
self.cursor_col += 1;
} else if self.cursor_row + 1 < self.lines.len() {
self.cursor_row += 1;
self.cursor_col = 0;
}
self.ensure_cursor_col_visible();
self.last_edit_kind = None;
}
/// Word-step right (Option+Right on macOS, Ctrl+Right elsewhere). Skips
/// any non-word run starting at the cursor — across line boundaries —
/// then skips the following word run on the line where it landed and
/// stops. Word chars are ASCII alphanumerics + underscore; everything
/// else (whitespace, punctuation, EOL) is non-word. Mirrors readline
/// `forward-word` and VS Code's "Cursor Word Right" command.
pub fn move_word_right(&mut self) {
loop {
let chars: Vec<char> = self.lines[self.cursor_row].chars().collect();
while self.cursor_col < chars.len() && !is_word_char(chars[self.cursor_col]) {
self.cursor_col += 1;
}
if self.cursor_col < chars.len() {
break;
}
if self.cursor_row + 1 < self.lines.len() {
self.cursor_row += 1;
self.cursor_col = 0;
} else {
self.ensure_cursor_col_visible();
self.last_edit_kind = None;
return;
}
}
let chars: Vec<char> = self.lines[self.cursor_row].chars().collect();
while self.cursor_col < chars.len() && is_word_char(chars[self.cursor_col]) {
self.cursor_col += 1;
}
self.ensure_cursor_col_visible();
self.last_edit_kind = None;
}
/// VS Code's "Copy Line Down" (Shift+Option+Down on macOS). Duplicates
/// the current line — or, when a selection is active, every line the
/// selection touches as one block — directly below the original, then
/// moves the cursor (and the selection, if any) onto the duplicate so
/// the next keystroke acts on the copy. Recorded as a single undo
/// step via the dedicated `EditKind::DuplicateLines`.
pub fn duplicate_lines_down(&mut self) {
self.push_undo(EditKind::DuplicateLines);
let (start_row, end_row) = self.selected_or_cursor_row_range();
let block: Vec<String> = self.lines[start_row..=end_row].to_vec();
let block_len = block.len();
let insert_at = end_row + 1;
for (i, line) in block.into_iter().enumerate() {
self.lines.insert(insert_at + i, line);
}
self.cursor_row += block_len;
if let Some(sel) = self.selection.as_mut() {
sel.anchor.0 += block_len;
sel.head.0 += block_len;
}
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code's "Copy Line Up" (Shift+Option+Up on macOS). Mirror of
/// `duplicate_lines_down`: inserts the copy *above* the original
/// block, leaving the cursor on the upper copy at the same row index
/// it started at (the original is pushed down by `block_len`).
pub fn duplicate_lines_up(&mut self) {
self.push_undo(EditKind::DuplicateLines);
let (start_row, end_row) = self.selected_or_cursor_row_range();
let block: Vec<String> = self.lines[start_row..=end_row].to_vec();
for (i, line) in block.into_iter().enumerate() {
self.lines.insert(start_row + i, line);
}
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
fn selected_or_cursor_row_range(&self) -> (usize, usize) {
match &self.selection {
Some(sel) => {
let (start, end) = sel.normalised();
(start.0, end.0)
}
None => (self.cursor_row, self.cursor_row),
}
}
/// Char-based slice of the buffer between two `(row, char_col)` points
/// (`start <= end`), joining spanned rows with `\n`.
fn char_range_text(&self, start: (usize, usize), end: (usize, usize)) -> String {
if start.0 == end.0 {
return self.lines[start.0]
.chars()
.skip(start.1)
.take(end.1.saturating_sub(start.1))
.collect();
}
let mut out: String = self.lines[start.0].chars().skip(start.1).collect();
for line in &self.lines[start.0 + 1..end.0] {
out.push('\n');
out.push_str(line);
}
out.push('\n');
let last: String = self.lines[end.0].chars().take(end.1).collect();
out.push_str(&last);
out
}
/// Replace the char-range `start..end` with `new`, re-splitting on `\n`
/// so a replacement that adds or removes newlines reshapes `lines`.
fn replace_char_range(&mut self, start: (usize, usize), end: (usize, usize), new: &str) {
let prefix: String = self.lines[start.0].chars().take(start.1).collect();
let suffix: String = self.lines[end.0].chars().skip(end.1).collect();
let combined = format!("{prefix}{new}{suffix}");
let replacement: Vec<String> = combined.split('\n').map(str::to_string).collect();
self.lines.splice(start.0..=end.0, replacement);
}
/// VS Code "Move Line Down" (Alt+Down): swap the current line / selected
/// block with the line below it, carrying the cursor and selection along.
/// No-op when the block already touches the last line.
pub fn move_lines_down(&mut self) {
let (start, end) = self.selected_or_cursor_row_range();
if end + 1 >= self.lines.len() {
return;
}
self.push_undo(EditKind::MoveLines);
self.lines[start..=end + 1].rotate_right(1);
self.cursor_row += 1;
if let Some(sel) = self.selection.as_mut() {
sel.anchor.0 += 1;
sel.head.0 += 1;
}
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Move Line Up" (Alt+Up): mirror of `move_lines_down`. No-op
/// when the block already touches the first line.
pub fn move_lines_up(&mut self) {
let (start, end) = self.selected_or_cursor_row_range();
if start == 0 {
return;
}
self.push_undo(EditKind::MoveLines);
self.lines[start - 1..=end].rotate_left(1);
self.cursor_row -= 1;
if let Some(sel) = self.selection.as_mut() {
sel.anchor.0 -= 1;
sel.head.0 -= 1;
}
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Toggle Line Comment" (Cmd+/): comment or uncomment every line
/// the cursor or selection touches using the language's line-comment
/// marker. Comments are inserted at the block's common (minimum)
/// indentation so the markers align; the whole block uncomments only when
/// every non-blank line is already commented. Returns false (a no-op) for
/// languages with no line comment.
pub fn toggle_line_comment(&mut self) -> bool {
let Some(token) = line_comment_token(self.lang) else {
return false;
};
let (start, end) = self.selected_or_cursor_row_range();
let non_blank: Vec<usize> = (start..=end)
.filter(|&r| !self.lines[r].trim().is_empty())
.collect();
if non_blank.is_empty() {
return false;
}
self.push_undo(EditKind::ToggleComment);
let all_commented = non_blank
.iter()
.all(|&r| self.lines[r].trim_start().starts_with(token));
if all_commented {
for &r in &non_blank {
let line = self.lines[r].clone();
let off = line.len() - line.trim_start().len();
let mut rest = line[off..].to_string();
rest.drain(..token.len());
if rest.starts_with(' ') {
rest.drain(..1);
}
self.lines[r] = format!("{}{}", &line[..off], rest);
}
} else {
let min_indent = non_blank
.iter()
.map(|&r| {
let l = &self.lines[r];
l.len() - l.trim_start().len()
})
.min()
.unwrap_or(0);
for &r in &non_blank {
self.lines[r].insert_str(min_indent, &format!("{token} "));
}
}
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
true
}
/// VS Code "Toggle Block Comment" (Shift+Alt+A): wrap the selection (or,
/// with no selection, the current line) in the language's block-comment
/// delimiters, or strip them when the selection already is a block
/// comment. Returns false for languages with no block comment (e.g. YAML).
pub fn toggle_block_comment(&mut self) -> bool {
let Some((open, close)) = block_comment_tokens(self.lang) else {
return false;
};
let (start, end) = match &self.selection {
Some(sel) => sel.normalised(),
None => {
let len = self.line_char_len(self.cursor_row);
((self.cursor_row, 0), (self.cursor_row, len))
}
};
let text = self.char_range_text(start, end);
let trimmed = text.trim();
if trimmed.is_empty() {
return false;
}
self.push_undo(EditKind::ToggleComment);
let wrapped = trimmed.starts_with(open)
&& trimmed.ends_with(close)
&& trimmed.len() >= open.len() + close.len();
let new = if wrapped {
trimmed[open.len()..trimmed.len() - close.len()]
.trim()
.to_string()
} else {
format!("{open} {trimmed} {close}")
};
self.replace_char_range(start, end, &new);
self.selection = None;
self.cursor_row = start.0;
self.cursor_col = self.line_char_len(start.0);
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
true
}
/// VS Code "Join Lines": collapse the selected lines (or the current line
/// with the one below it) into one, trimming each joined line's leading
/// whitespace and separating with a single space. No-op when there is no
/// following line to join.
pub fn join_lines(&mut self) {
let (start, end) = self.selected_or_cursor_row_range();
let last = if start == end { start + 1 } else { end };
if last >= self.lines.len() {
return;
}
self.push_undo(EditKind::JoinLines);
let mut result = self.lines[start].trim_end().to_string();
let cursor_col = result.chars().count();
for line in &self.lines[start + 1..=last] {
let piece = line.trim_start();
if !result.is_empty() && !result.ends_with(char::is_whitespace) && !piece.is_empty() {
result.push(' ');
}
result.push_str(piece);
}
self.lines.splice(start..=last, std::iter::once(result));
self.cursor_row = start;
self.cursor_col = cursor_col;
self.selection = None;
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Transform to Upper/Lower/Title Case": rewrite the selection
/// (or, with no selection, the word under the cursor) in the requested
/// case. No-op when there is nothing to transform.
pub fn transform_selection_case(&mut self, kind: CaseTransform) {
let range = match &self.selection {
Some(sel) => Some(sel.normalised()),
None => self
.word_at(self.cursor_row, self.cursor_col)
.map(|(s, e)| ((self.cursor_row, s), (self.cursor_row, e))),
};
let Some((start, end)) = range else {
return;
};
let text = self.char_range_text(start, end);
if text.is_empty() {
return;
}
self.push_undo(EditKind::TransformCase);
let new = match kind {
CaseTransform::Upper => text.to_uppercase(),
CaseTransform::Lower => text.to_lowercase(),
CaseTransform::Title => title_case(&text),
};
self.replace_char_range(start, end, &new);
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Sort Lines Ascending/Descending": sort the selected lines
/// lexicographically, or the whole buffer when nothing is selected. No-op
/// on a single line.
pub fn sort_lines(&mut self, ascending: bool) {
let (start, end) = match &self.selection {
Some(sel) => {
let (s, e) = sel.normalised();
(s.0, e.0)
}
None => (0, self.lines.len().saturating_sub(1)),
};
if start >= end {
return;
}
self.push_undo(EditKind::SortLines);
let mut block: Vec<String> = self.lines[start..=end].to_vec();
block.sort();
if !ascending {
block.reverse();
}
self.lines.splice(start..=end, block);
self.mark_buffer_changed();
self.recompute_highlights();
}
/// VS Code "Trim Trailing Whitespace": strip trailing spaces and tabs from
/// every line, clamping the cursor onto the new line end. Returns false
/// (a no-op) when the buffer is already clean.
pub fn trim_trailing_whitespace(&mut self) -> bool {
let any = self
.lines
.iter()
.any(|l| l.trim_end_matches([' ', '\t']).len() != l.len());
if !any {
return false;
}
self.push_undo(EditKind::TrimWhitespace);
for line in &mut self.lines {
let trimmed_len = line.trim_end_matches([' ', '\t']).len();
line.truncate(trimmed_len);
}
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.mark_buffer_changed();
self.recompute_highlights();
true
}
/// The character at `(row, char_col)`, or None when out of range. Used by
/// the bracket-matching helpers, which work in char indices.
fn char_at(&self, row: usize, col: usize) -> Option<char> {
self.lines.get(row)?.chars().nth(col)
}
/// Forward-scan from the opening bracket at `open` to its matching close,
/// counting nesting of the SAME pair type. Returns the `(row, col)` of the
/// closing bracket, or None when unbalanced.
fn match_bracket_forward(&self, open: BPos) -> Option<BPos> {
let oc = self.char_at(open.0, open.1)?;
let cc = matching_close(oc)?;
let mut depth = 0i32;
let (mut row, mut col) = open;
loop {
match self.char_at(row, col) {
Some(ch) => {
if ch == oc {
depth += 1;
} else if ch == cc {
depth -= 1;
if depth == 0 {
return Some((row, col));
}
}
col += 1;
}
None => {
row += 1;
col = 0;
if row >= self.lines.len() {
return None;
}
}
}
}
}
/// Backward-scan from the closing bracket at `close` to its matching open.
fn match_bracket_backward(&self, close: BPos) -> Option<BPos> {
let cc = self.char_at(close.0, close.1)?;
let oc = matching_open(cc)?;
let mut depth = 0i32;
let mut row = close.0;
let mut col = close.1 as isize;
loop {
if col < 0 {
if row == 0 {
return None;
}
row -= 1;
col = self.line_char_len(row) as isize - 1;
continue;
}
let ch = self.char_at(row, col as usize)?;
if ch == cc {
depth += 1;
} else if ch == oc {
depth -= 1;
if depth == 0 {
return Some((row, col as usize));
}
}
col -= 1;
}
}
/// The innermost bracket pair enclosing the cursor, found by walking
/// backwards for the first unmatched opening bracket and matching it
/// forwards. Mirrors VS Code's `findEnclosingBrackets`.
fn enclosing_brackets(&self) -> Option<(BPos, BPos)> {
let mut row = self.cursor_row;
let mut col = self.cursor_col as isize - 1;
let mut expected: Vec<char> = Vec::new();
loop {
if col < 0 {
if row == 0 {
return None;
}
row -= 1;
col = self.line_char_len(row) as isize - 1;
continue;
}
let ch = self.char_at(row, col as usize)?;
if is_close_bracket(ch) {
expected.push(matching_open(ch)?);
} else if is_open_bracket(ch) {
match expected.last() {
Some(&want) if want == ch => {
expected.pop();
}
_ => {
let open = (row, col as usize);
let close = self.match_bracket_forward(open)?;
return Some((open, close));
}
}
}
col -= 1;
}
}
/// The first bracket pair at or after the cursor (`findNextBracket`).
fn next_bracket_pair(&self) -> Option<(BPos, BPos)> {
let mut row = self.cursor_row;
let mut col = self.cursor_col;
loop {
match self.char_at(row, col) {
Some(ch) if is_open_bracket(ch) => {
let open = (row, col);
return self.match_bracket_forward(open).map(|c| (open, c));
}
Some(ch) if is_close_bracket(ch) => {
let close = (row, col);
return self.match_bracket_backward(close).map(|o| (o, close));
}
Some(_) => col += 1,
None => {
row += 1;
col = 0;
if row >= self.lines.len() {
return None;
}
}
}
}
}
/// Resolve the bracket pair relevant to the cursor, returning
/// `(open, close, anchored_on_close)`. `anchored_on_close` is true when the
/// cursor sits on the closing bracket, so a jump should target the open
/// bracket instead. Adjacency priority mirrors VS Code's `matchBracket`:
/// a close to the left wins, then an open to the right, then an open to the
/// left, then a close to the right; failing all four it falls back to the
/// enclosing pair, then the next bracket forward.
fn find_bracket_pair(&self) -> Option<(BPos, BPos, bool)> {
if let Some(pair) = self.adjacent_bracket_pair() {
return Some(pair);
}
if let Some((open, close)) = self.enclosing_brackets() {
return Some((open, close, false));
}
self.next_bracket_pair().map(|(o, c)| (o, c, false))
}
/// The bracket pair whose bracket sits immediately to the left or right of
/// the caret, as `(open, close, anchored_on_close)`. `None` when the caret
/// touches no bracket. Adjacency priority mirrors VS Code's `matchBracket`:
/// a close to the left wins, then an open to the right, then an open to the
/// left, then a close to the right.
fn adjacent_bracket_pair(&self) -> Option<(BPos, BPos, bool)> {
let row = self.cursor_row;
let col = self.cursor_col;
let left = col.checked_sub(1).and_then(|c| self.char_at(row, c));
let right = self.char_at(row, col);
if left.is_some_and(is_close_bracket) {
let close = (row, col - 1);
return self.match_bracket_backward(close).map(|o| (o, close, true));
}
if right.is_some_and(is_open_bracket) {
let open = (row, col);
return self.match_bracket_forward(open).map(|c| (open, c, false));
}
if left.is_some_and(is_open_bracket) {
let open = (row, col - 1);
return self.match_bracket_forward(open).map(|c| (open, c, false));
}
if right.is_some_and(is_close_bracket) {
let close = (row, col);
return self.match_bracket_backward(close).map(|o| (o, close, true));
}
None
}
/// The bracket pair to highlight (VS Code's `editorBracketMatch`): the
/// bracket adjacent to the caret and its match, as `(open, close)`. Unlike
/// [`Self::find_bracket_pair`] it never falls back to the enclosing or next
/// pair, so the highlight appears only when the caret is beside a bracket.
pub fn bracket_match_pair(&self) -> Option<(BPos, BPos)> {
self.adjacent_bracket_pair().map(|(o, c, _)| (o, c))
}
/// VS Code "Go to Bracket" (`editor.action.jumpToBracket`, Cmd+Shift+\):
/// move the cursor to the matching bracket. From an opening bracket it
/// lands on the closing one and vice versa; from inside a pair it lands on
/// the enclosing closing bracket. Returns false (a no-op) when no bracket
/// is in play.
pub fn jump_to_matching_bracket(&mut self) -> bool {
let Some((open, close, anchored_on_close)) = self.find_bracket_pair() else {
return false;
};
let target = if anchored_on_close { open } else { close };
self.clear_selection();
self.cursor_row = target.0;
self.cursor_col = target.1;
self.last_edit_kind = None;
self.ensure_cursor_col_visible();
true
}
/// VS Code "Select to Bracket" (`editor.action.selectToBracket`): select
/// the region between the matching brackets, including the brackets
/// themselves (from the opening bracket's start to the closing bracket's
/// end). Returns false (a no-op) when no bracket pair is in play.
pub fn select_to_matching_bracket(&mut self) -> bool {
let Some((open, close, _)) = self.find_bracket_pair() else {
return false;
};
let head = (close.0, close.1 + 1);
self.selection = Some(EditorSelection { anchor: open, head });
self.cursor_row = head.0;
self.cursor_col = head.1;
self.last_edit_kind = None;
self.ensure_cursor_col_visible();
true
}
/// VS Code "Transpose Characters around the Cursor"
/// (`editor.action.transpose`): swap the character before the cursor with
/// the one after it and advance the cursor (emacs `transpose-chars`). At
/// the end of a non-final line the last character moves across the line
/// break to the start of the next line. At the end of the final line it is
/// a no-op.
pub fn transpose_chars(&mut self) {
let row = self.cursor_row;
let col = self.cursor_col;
let len = self.line_char_len(row);
if col >= len {
if row + 1 >= self.lines.len() {
return;
}
self.push_undo(EditKind::Transpose);
if len > 0 {
let mut chars: Vec<char> = self.lines[row].chars().collect();
let moved = chars.pop().unwrap();
self.lines[row] = chars.into_iter().collect();
self.lines[row + 1].insert(0, moved);
self.cursor_col = 1;
} else {
self.cursor_col = 0;
}
self.cursor_row = row + 1;
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
return;
}
if col == 0 {
// VS Code's single-character range at column 1: no swap, just step
// the cursor right.
self.cursor_col = 1;
self.ensure_cursor_col_visible();
return;
}
self.push_undo(EditKind::Transpose);
let mut chars: Vec<char> = self.lines[row].chars().collect();
chars.swap(col - 1, col);
self.lines[row] = chars.into_iter().collect();
self.cursor_col = col + 1;
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Convert Indentation to Spaces" (`editor.action.indentationToSpaces`):
/// replace every tab in each line's leading indentation with `tabSize`
/// spaces. Only leading whitespace is touched.
pub fn indentation_to_spaces(&mut self) {
self.convert_indentation(true);
}
/// VS Code "Convert Indentation to Tabs" (`editor.action.indentationToTabs`):
/// replace each run of `tabSize` spaces in the leading indentation with a
/// tab, leaving any remainder. Only leading whitespace is touched.
pub fn indentation_to_tabs(&mut self) {
self.convert_indentation(false);
}
fn convert_indentation(&mut self, to_spaces: bool) {
let tab_w = (self.indent_style().width as usize).max(1);
let spaces = " ".repeat(tab_w);
let mut new_lines = self.lines.clone();
let mut changed = false;
for line in new_lines.iter_mut() {
let indent_len = line.chars().take_while(|c| *c == ' ' || *c == '\t').count();
let indent: String = line.chars().take(indent_len).collect();
let converted = if to_spaces {
indent.replace('\t', &spaces)
} else {
indent.replace(&spaces, "\t")
};
if converted != indent {
let rest: String = line.chars().skip(indent_len).collect();
*line = format!("{converted}{rest}");
changed = true;
}
}
if !changed {
return;
}
self.push_undo(EditKind::IndentConvert);
self.lines = new_lines;
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code's `files.trimFinalNewlines`, surfaced as a command: drop trailing
/// empty lines at the end of the buffer, always keeping at least one line.
/// Returns false (a no-op) when there are no trailing blank lines.
pub fn trim_final_newlines(&mut self) -> bool {
let mut end = self.lines.len();
while end > 1 && self.lines[end - 1].is_empty() {
end -= 1;
}
if end == self.lines.len() {
return false;
}
self.push_undo(EditKind::TrimFinalNewlines);
self.lines.truncate(end);
if self.cursor_row >= self.lines.len() {
self.cursor_row = self.lines.len() - 1;
}
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.mark_buffer_changed();
self.recompute_highlights();
true
}
/// VS Code "Add Cursor Below" (Cmd+Alt+Down): drop a secondary caret on
/// the row below the lowest current caret, at the same column (clamped to
/// that line's length). No-op at the last row.
pub fn add_cursor_below(&mut self) {
let max_row = self
.carets
.iter()
.map(|c| c.head.0)
.chain(std::iter::once(self.cursor_row))
.max()
.unwrap_or(self.cursor_row);
let target = max_row + 1;
if target >= self.lines.len() {
return;
}
let col = self.cursor_col.min(self.line_char_len(target));
self.carets.push(EditorSelection {
anchor: (target, col),
head: (target, col),
});
}
/// VS Code Alt+click: add a caret at the screen cell `(col, row)`, keeping
/// the existing cursor(s). The old primary (with any selection) becomes a
/// caret and the clicked point becomes the new primary. Clicking an
/// existing zero-width caret removes it (toggle). Returns false when the
/// click misses the text area.
pub fn add_caret_at_screen(&mut self, col: u16, row: u16) -> bool {
let Some((r, c)) = self.buffer_pos_at(col, row) else {
return false;
};
// Clicking the current primary is a no-op (nothing to add).
if (r, c) == (self.cursor_row, self.cursor_col) {
return true;
}
// Clicking an existing zero-width caret removes it (toggle off).
if let Some(idx) = self
.carets
.iter()
.position(|s| !s.has_area() && s.head == (r, c))
{
self.carets.remove(idx);
return true;
}
// Demote the current primary (preserving any selection) to a caret and
// promote the clicked point.
let primary = self.selection.unwrap_or(EditorSelection {
anchor: (self.cursor_row, self.cursor_col),
head: (self.cursor_row, self.cursor_col),
});
self.carets.push(primary);
self.selection = None;
self.cursor_row = r;
self.cursor_col = c;
self.ensure_cursor_col_visible();
true
}
/// Begin a column (box) selection anchored at the screen cell `(col, row)`
/// (VS Code's Shift+Alt+drag). Clears any existing selection/carets and
/// parks the primary at the anchor. Returns false if the cell misses text.
pub fn begin_box_select(&mut self, col: u16, row: u16) -> bool {
let Some((r, c)) = self.buffer_pos_at(col, row) else {
return false;
};
self.box_anchor = Some((r, c));
self.carets.clear();
self.selection = None;
self.cursor_row = r;
self.cursor_col = c;
true
}
/// Whether a column (box) selection drag is in progress.
pub fn box_selecting(&self) -> bool {
self.box_anchor.is_some()
}
/// Extend the live column selection to the screen cell `(col, row)`,
/// rebuilding one caret per spanned row. No-op if no box drag is active.
pub fn box_drag_to_screen(&mut self, col: u16, row: u16) {
if self.box_anchor.is_none() {
return;
}
if let Some((r, c)) = self.buffer_pos_at(col, row) {
self.box_select_to(r, c);
}
}
/// Rebuild the column selection so it spans the rectangle from the box
/// anchor to `(head_row, head_col)`: every row in the range gets a caret
/// over the shared column span (clamped to that line's length); the head
/// row is the primary. Zero-width columns become bare carets.
fn box_select_to(&mut self, head_row: usize, head_col: usize) {
let Some((ar, ac)) = self.box_anchor else {
return;
};
let (r0, r1) = (ar.min(head_row), ar.max(head_row));
let (c0, c1) = (ac.min(head_col), ac.max(head_col));
self.carets.clear();
self.selection = None;
for r in r0..=r1 {
let len = self.line_char_len(r);
let s = c0.min(len);
let e = c1.min(len);
let sel = EditorSelection {
anchor: (r, s),
head: (r, e),
};
if r == head_row {
self.selection = (e > s).then_some(sel);
self.cursor_row = r;
self.cursor_col = e;
} else {
self.carets.push(sel);
}
}
self.ensure_cursor_col_visible();
}
/// End the column-selection drag, keeping the carets it produced.
pub fn end_box_select(&mut self) {
self.box_anchor = None;
}
/// VS Code "Add Cursor Above" (Cmd+Alt+Up): mirror of `add_cursor_below`.
/// No-op at the first row.
pub fn add_cursor_above(&mut self) {
let min_row = self
.carets
.iter()
.map(|c| c.head.0)
.chain(std::iter::once(self.cursor_row))
.min()
.unwrap_or(self.cursor_row);
if min_row == 0 {
return;
}
let target = min_row - 1;
let col = self.cursor_col.min(self.line_char_len(target));
self.carets.push(EditorSelection {
anchor: (target, col),
head: (target, col),
});
}
/// VS Code "View: Toggle Word Wrap" (Alt+Z): flip this buffer's soft-wrap
/// state, overriding the language default until the file is reopened.
pub fn toggle_wrap(&mut self) {
let now = self.wrap_enabled();
self.wrap_override = Some(!now);
}
/// True when the active selection spans more than one row, so Tab should
/// indent the whole block (VS Code) rather than replace the selection
/// with a single indentation unit.
pub fn selection_is_multiline(&self) -> bool {
match &self.selection {
Some(sel) => {
let (start, end) = sel.normalised();
start.0 != end.0
}
None => false,
}
}
/// VS Code's plain-Tab indent (no multi-line selection): insert spaces up
/// to the next tab stop from the cursor column, replacing any single-line
/// selection first. With `editor.insertSpaces` + `editor.useTabStops`
/// (both on by default) a Tab at column 2 with a width-4 unit adds 2
/// spaces to reach column 4, not a flat 4.
pub fn indent_at_cursor(&mut self) {
self.pin_on_edit();
self.push_undo(EditKind::Indent);
if self.selection.is_some() {
self.delete_selection_inner();
}
if self.lines.is_empty() {
self.lines.push(String::new());
}
let style = self.indent_style();
let row = self.cursor_row;
let byte = self.byte_index(row, self.cursor_col);
let (ins, advance): (String, usize) = if style.use_spaces {
let unit_w = (style.width as usize).max(1);
let pad = unit_w - (self.cursor_col % unit_w);
(" ".repeat(pad), pad)
} else {
// A tab-indented buffer inserts one literal tab (useTabStops).
("\t".to_string(), 1)
};
self.lines[row].insert_str(byte, &ins);
self.cursor_col += advance;
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Indent Lines" (Tab with a multi-line selection): prepend one
/// indentation unit to every line the selection touches. Empty lines are
/// left untouched, matching VS Code's "do not indent empty lines" rule
/// for block indent. Cursor and selection columns shift right by the unit
/// width on the lines that actually gained indentation so the same text
/// stays highlighted.
pub fn indent_lines(&mut self) {
self.pin_on_edit();
self.push_undo(EditKind::Indent);
let unit = self.indent_unit();
let unit_w = unit.chars().count();
let (start_row, end_row) = self.selected_or_cursor_row_range();
for row in start_row..=end_row {
if !self.lines[row].is_empty() {
self.lines[row].insert_str(0, &unit);
}
}
// A row gained indentation iff it was non-empty (empty lines were
// skipped and stay empty), so a row that is non-empty now is exactly
// one that shifted right by `unit_w`.
let shift = |row: usize, col: usize, lines: &[String]| -> usize {
if row >= start_row && row <= end_row && !lines[row].is_empty() {
col + unit_w
} else {
col
}
};
self.cursor_col = shift(self.cursor_row, self.cursor_col, &self.lines);
if let Some(mut sel) = self.selection {
sel.anchor.1 = shift(sel.anchor.0, sel.anchor.1, &self.lines);
sel.head.1 = shift(sel.head.0, sel.head.1, &self.lines);
self.selection = Some(sel);
}
self.mark_buffer_changed();
self.recompute_highlights();
self.ensure_cursor_col_visible();
}
/// VS Code "Outdent" (Shift+Tab): strip one indentation level from every
/// line the selection touches, or just the current line when there is no
/// selection. Removal is tab-stop aligned (the `useTabStops` default): a
/// line indented 6 spaces drops to 4, a line indented 3 drops to 0. A
/// single leading tab counts as one level. Cursor and selection columns
/// follow the removal on their own row.
pub fn dedent_lines(&mut self) {
self.pin_on_edit();
self.push_undo(EditKind::Indent);
let unit_w = (self.indent_style().width as usize).max(1);
let (start_row, end_row) = self.selected_or_cursor_row_range();
let mut removed = vec![0usize; self.lines.len()];
let mut any = false;
for (row, slot) in removed
.iter_mut()
.enumerate()
.take(end_row + 1)
.skip(start_row)
{
let n = leading_outdent_chars(&self.lines[row], unit_w);
if n > 0 {
self.lines[row].replace_range(0..n, "");
*slot = n;
any = true;
}
}
let pull = |row: usize, col: usize, removed: &[usize]| -> usize {
if row < removed.len() {
col.saturating_sub(removed[row])
} else {
col
}
};
self.cursor_col = pull(self.cursor_row, self.cursor_col, &removed);
if let Some(mut sel) = self.selection {
sel.anchor.1 = pull(sel.anchor.0, sel.anchor.1, &removed);
sel.head.1 = pull(sel.head.0, sel.head.1, &removed);
self.selection = Some(sel);
}
if any {
self.mark_buffer_changed();
self.recompute_highlights();
}
self.ensure_cursor_col_visible();
}
/// Word-step left (Option+Left on macOS). Symmetric to `move_word_right`:
/// skips non-word chars backwards across line boundaries, then walks
/// back over the preceding word run, landing on its first char.
pub fn move_word_left(&mut self) {
loop {
let chars: Vec<char> = self.lines[self.cursor_row].chars().collect();
while self.cursor_col > 0 && !is_word_char(chars[self.cursor_col - 1]) {
self.cursor_col -= 1;
}
if self.cursor_col > 0 {
break;
}
if self.cursor_row > 0 {
self.cursor_row -= 1;
self.cursor_col = self.line_char_len(self.cursor_row);
} else {
self.ensure_cursor_col_visible();
self.last_edit_kind = None;
return;
}
}
let chars: Vec<char> = self.lines[self.cursor_row].chars().collect();
while self.cursor_col > 0 && is_word_char(chars[self.cursor_col - 1]) {
self.cursor_col -= 1;
}
self.ensure_cursor_col_visible();
self.last_edit_kind = None;
}
pub fn scroll_left_by(&mut self, n: usize) {
self.scroll_col = self.scroll_col.saturating_sub(n);
}
pub fn scroll_right_by(&mut self, n: usize) {
self.scroll_col = self.scroll_col.saturating_add(n);
}
/// Recompute the visible text width from `last_inner` / `last_gutter_width`
/// (set during the most recent render) and use it to decide whether the
/// current cursor column is on-screen. This is the same arithmetic the
/// renderer uses, kept in one place so move-and-scroll stays in lock-step.
fn visible_text_width(&self) -> usize {
let scrollbar_w = u16::from(self.last_scrollbar.width > 0);
self.last_inner
.width
.saturating_sub(self.last_gutter_width + 2 + scrollbar_w) as usize
}
/// Pull `scroll_col` so the cursor sits inside the current viewport.
/// Mirrors `ensure_cursor_visible` for vertical scroll - keyboard nav
/// follows; wheel scrolling does not call this so the user's wheel
/// position is sacrosanct. `pub(crate)` so callers like
/// `App::open_search_hit` can sync horizontal scroll after a jump.
pub(crate) fn ensure_cursor_col_visible(&mut self) {
if self.wrap_enabled() {
return;
}
let width = self.visible_text_width();
if width == 0 {
return;
}
if self.cursor_col < self.scroll_col {
self.scroll_col = self.cursor_col;
} else {
// Hint cells between the scroll origin and the caret consume
// viewport width too. Scrolling right drops leading hints out of
// the row, so re-measure until the caret fits (the loop is
// bounded: `scroll_col` only grows, capped at `cursor_col`).
loop {
let extra = self.inlay_cells_before_cursor(self.cursor_row, self.scroll_col);
if self.cursor_col + extra < self.scroll_col + width {
break;
}
let target = (self.cursor_col + extra + 1).saturating_sub(width);
if target <= self.scroll_col {
break;
}
self.scroll_col = target.min(self.cursor_col);
}
}
}
/// One screen worth of rows, derived from the editor's last rendered
/// inner height. Falls back to a sensible default before the first
/// render (when `last_inner.height` is still 0).
pub fn page_size(&self) -> usize {
let from_rows = self.text_rows();
if from_rows > 0 { from_rows } else { 20 }
}
/// Move the viewport down by exactly one screen so the first
/// previously-unseen row becomes the new top of the viewport, and place
/// the cursor on that new top row. Clamps at end of file.
pub fn page_down_one_screen(&mut self) {
let page = self.page_size();
if self.wrap_enabled() {
let top = self.top_visual_row(self.visible_text_width());
self.wrap_set_top(top + page);
return;
}
let max_row = self.lines.len().saturating_sub(1);
let new_top = (self.scroll + page).min(max_row);
self.scroll = new_top;
self.cursor_row = new_top;
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.last_edit_kind = None;
}
/// Move the viewport up by exactly one screen so the new top is `page`
/// rows above the previous top. Cursor lands on the new top row.
/// Clamps at the start of file.
pub fn page_up_one_screen(&mut self) {
let page = self.page_size();
if self.wrap_enabled() {
let top = self.top_visual_row(self.visible_text_width());
self.wrap_set_top(top.saturating_sub(page));
return;
}
let new_top = self.scroll.saturating_sub(page);
self.scroll = new_top;
self.cursor_row = new_top;
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.last_edit_kind = None;
}
pub fn home_line(&mut self) {
self.cursor_col = 0;
self.last_edit_kind = None;
}
pub fn end_line(&mut self) {
self.cursor_col = self.line_char_len(self.cursor_row);
self.last_edit_kind = None;
}
/// Scroll an open spreadsheet preview by `rows` (negative scrolls up).
/// Returns true when this tab is a spreadsheet, so the generic scroll
/// paths can hand it off instead of moving the (empty) text buffer.
pub fn scroll_sheet_rows(&mut self, rows: isize) -> bool {
let Some(sheet) = self.sheet.as_mut() else {
return false;
};
let Some(data) = sheet.sheets.get_mut(sheet.current_sheet) else {
return false;
};
let last = data.rows.len().saturating_sub(1);
data.scroll_row = data.scroll_row.saturating_add_signed(rows).min(last);
true
}
pub fn scroll_up(&mut self, n: usize) {
if self.scroll_markdown_preview(-(n as i32)) {
return;
}
if self.scroll_sheet_rows(-(n as isize)) {
return;
}
if self.wrap_enabled() {
let top = self.top_visual_row(self.visible_text_width());
self.wrap_set_top(top.saturating_sub(n));
} else if self.comment_boxes.is_empty() {
self.scroll_view_to(self.scroll.saturating_sub(n));
} else {
self.nonwrap_set_top(self.nonwrap_top_content_row().saturating_sub(n));
}
}
pub fn scroll_down(&mut self, n: usize) {
if self.scroll_markdown_preview(n as i32) {
return;
}
if self.scroll_sheet_rows(n as isize) {
return;
}
if self.wrap_enabled() {
let top = self.top_visual_row(self.visible_text_width());
self.wrap_set_top(top.saturating_add(n));
} else if self.comment_boxes.is_empty() {
self.scroll_view_to(self.scroll.saturating_add(n));
} else {
self.nonwrap_set_top(self.nonwrap_top_content_row().saturating_add(n));
}
}
pub fn scroll_to_bar_y(&mut self, y: u16) -> bool {
let viewport = self.text_rows();
if self.wrap_enabled() {
let width = self.visible_text_width();
let total = self.total_visual_rows(width);
let Some(metrics) = scrollbar::vertical_metrics(
self.last_scrollbar,
total,
viewport,
self.top_visual_row(width),
) else {
return false;
};
self.wrap_set_top(scrollbar::scroll_for_y(metrics, y));
return true;
}
// Same content length the render sized the bar with: lines plus
// comment-box rows. Mapping through bare `lines.len()` made the bar
// of a short file with a tall navigator comment dead (metrics said
// "no overflow") and a long file's thumb run away from the pointer.
let bw = self.visible_text_width();
let content = self.lines.len() + self.box_rows_between(0, self.lines.len(), bw);
let Some(metrics) = scrollbar::vertical_metrics(
self.last_scrollbar,
content,
viewport,
self.nonwrap_top_content_row(),
) else {
return false;
};
self.nonwrap_set_top(scrollbar::scroll_for_y(metrics, y));
true
}
/// Map a click/drag X coordinate on the horizontal scrollbar onto
/// `scroll_col`. Returns false when there is no horizontal bar (no
/// overflow), so the caller can fall through to other hit-tests.
pub fn scroll_to_bar_x(&mut self, x: u16) -> bool {
let width = self.visible_text_width();
let Some(metrics) = scrollbar::horizontal_metrics(
self.last_hscrollbar,
self.content_cols(),
width,
self.scroll_col,
) else {
return false;
};
self.scroll_col = scrollbar::scroll_for_x(metrics, x);
true
}
/// Rows used for text in the last render (inner height minus the
/// horizontal scrollbar row when present). Falls back to the full inner
/// height before the first render populates `last_text_rows`.
pub(crate) fn text_rows(&self) -> usize {
let rows = self.last_text_rows as usize;
if rows > 0 {
rows
} else {
self.last_inner.height as usize
}
}
/// Number of text rows currently visible (the minimap viewport box height).
pub fn visible_rows(&self) -> usize {
self.text_rows()
}
/// Inclusive `(start_row, end_row)` of the active selection, or `None` when
/// there's no selection with area. Drives the minimap selection band.
pub fn selection_rows(&self) -> Option<(usize, usize)> {
let sel = self.selection?;
if !sel.has_area() {
return None;
}
let (start, end) = sel.normalised();
Some((start.0, end.0))
}
/// Move the cursor to `line` and center the viewport on it (minimap click /
/// drag navigation). The cursor moves too because the render's scroll-follow
/// snaps the viewport back to keep the caret visible; centering the caret
/// here means that follow logic leaves the chosen scroll position alone.
pub fn goto_line_centered(&mut self, line: usize) {
if self.lines.is_empty() {
return;
}
let line = line.min(self.lines.len() - 1);
self.cursor_row = line;
self.cursor_col = self.cursor_col.min(self.line_char_len(line));
let viewport = self.text_rows();
self.scroll = line
.saturating_sub(viewport / 2)
.min(self.lines.len().saturating_sub(viewport));
self.last_edit_kind = None;
}
/// Rasterize the whole document into a `w`x`h` RGBA buffer for the minimap:
/// one column per character (clipped to `w`), each non-whitespace char
/// painted in its syntax color. Whitespace stays `bg`, so indentation reads
/// as the file's shape, like VS Code's minimap. The lines are mapped into
/// `content_h` rows (top-aligned) rather than the full `h`: a short file
/// uses a fixed small per-line height and leaves the rest of the strip
/// blank, instead of stretching six lines down the whole column. For a file
/// long enough to fill the strip, `content_h == h` and the mapping is the
/// plain whole-file fit.
/// ponytail: 1px per char, clipped at strip width; sub-pixel char scaling
/// only if a wider strip ever needs to show more columns.
pub fn minimap_rgba(
&self,
w: u32,
h: u32,
content_h: u32,
bg: (u8, u8, u8),
fg: (u8, u8, u8),
) -> Vec<u8> {
let mut buf = vec![0u8; (w * h * 4) as usize];
for px in buf.chunks_exact_mut(4) {
px[0] = bg.0;
px[1] = bg.1;
px[2] = bg.2;
px[3] = 0xff;
}
let total = self.lines.len().max(1) as u64;
for (i, line) in self.lines.iter().enumerate() {
let y0 = (i as u64 * content_h as u64 / total) as u32;
if y0 >= h {
break;
}
let y1 = (((i as u64 + 1) * content_h as u64 / total) as u32).clamp(y0 + 1, h);
let merged = merge_overlay(
self.highlights.get(i).map(Vec::as_slice).unwrap_or(&[]),
self.semantic_overlay
.get(i)
.map(Vec::as_slice)
.unwrap_or(&[]),
);
let mut si = 0usize;
for (col, (byte, ch)) in (0_u32..).zip(line.char_indices()) {
if col >= w {
break;
}
while si < merged.len() && merged[si].end <= byte {
si += 1;
}
if !ch.is_whitespace() {
let (r, g, b) = if si < merged.len() && merged[si].start <= byte {
span_rgb(merged[si].style, fg)
} else {
fg
};
for y in y0..y1 {
let idx = ((y * w + col) * 4) as usize;
buf[idx] = r;
buf[idx + 1] = g;
buf[idx + 2] = b;
}
}
}
}
buf
}
/// The non-wrap viewport top in CONTENT rows: buffer lines plus the
/// comment-box rows above (and inside, via `scroll_sub`) the top line.
fn nonwrap_top_content_row(&self) -> usize {
let bw = self.visible_text_width();
self.scroll + self.box_rows_between(0, self.scroll, bw) + self.scroll_sub
}
/// Set the non-wrap viewport top to content row `row`, landing mid-box
/// when it falls inside a comment box (`scroll_sub` counts rows past
/// the top line's own text row). Mirrors `wrap_set_top` for the
/// box-extended non-wrap layout; a tall box is unreadable and its
/// Reply/Ignore footer unreachable with a line-granular top. Pulls the
/// cursor along like `scroll_view_to`.
fn nonwrap_set_top(&mut self, row: usize) {
let viewport = self.text_rows();
if viewport == 0 || self.lines.is_empty() {
self.scroll = 0;
self.scroll_sub = 0;
return;
}
if self.comment_boxes.is_empty() {
self.scroll_sub = 0;
self.scroll_view_to(row);
return;
}
let bw = self.visible_text_width();
let total = self.lines.len() + self.box_rows_between(0, self.lines.len(), bw);
let row = row.min(total.saturating_sub(viewport));
let mut acc = 0usize;
let mut line = 0usize;
while line < self.lines.len() {
let group = 1 + self.box_rows_between(line, line + 1, bw);
if acc + group > row {
break;
}
acc += group;
line += 1;
}
self.scroll = line.min(self.lines.len().saturating_sub(1));
self.scroll_sub = row.saturating_sub(acc);
let last_visible = (self.scroll + viewport - 1).min(self.lines.len().saturating_sub(1));
let floor = self.caret_floor_row().min(last_visible);
if self.cursor_row < floor {
self.cursor_row = floor;
} else if self.cursor_row > last_visible {
self.cursor_row = last_visible;
}
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.last_edit_kind = None;
}
fn scroll_view_to(&mut self, top: usize) {
let viewport = self.last_inner.height as usize;
if viewport == 0 || self.lines.is_empty() {
self.scroll = 0;
self.cursor_row = 0;
self.cursor_col = 0;
self.last_edit_kind = None;
return;
}
self.scroll = top.min(self.lines.len().saturating_sub(viewport));
let last_visible = (self.scroll + viewport - 1).min(self.lines.len().saturating_sub(1));
let floor = self.caret_floor_row().min(last_visible);
if self.cursor_row < floor {
self.cursor_row = floor;
} else if self.cursor_row > last_visible {
self.cursor_row = last_visible;
}
self.cursor_col = self.cursor_col.min(self.line_char_len(self.cursor_row));
self.last_edit_kind = None;
}
/// Topmost line the caret may be dragged to when the viewport scrolls out
/// from under it: the first row the sticky band does not cover. Parking it
/// on `scroll` itself puts it under the pinned headers, where it cannot be
/// seen and where the band would have to yield to stay honest — which
/// deleted the band for the whole of a wheel scroll.
///
/// The band is measured from the previous frame's `sticky_lines` (the app
/// layer rebuilds them from `scroll` on the next render). A burst of wheel
/// events can therefore be one header out mid-gesture, which the render's
/// own caret guard absorbs by trimming the band for that frame.
fn caret_floor_row(&self) -> usize {
if self.wrap_enabled() || self.sticky_lines.is_empty() {
return self.scroll;
}
let rows_avail = (self.last_inner.height as usize).saturating_sub(1);
self.scroll + self.sticky_lines.len().min(rows_avail)
}
/// Double-click word selection: place the cursor at the click point, then
/// select the maximal run of word characters covering it and leave the
/// caret at the right edge of that run (VS Code parity). A click on
/// whitespace or past the end of an empty line clears any selection.
pub fn select_word_at(&mut self, col: u16, row: u16) {
self.click(col, row);
if self.lines.is_empty() {
self.selection = None;
return;
}
let r = self.cursor_row;
let chars: Vec<char> = self.lines[r].chars().collect();
let c = self.cursor_col;
let pivot = if c < chars.len() && is_word_char(chars[c]) {
Some(c)
} else if c == chars.len() && c > 0 && is_word_char(chars[c - 1]) {
Some(c - 1)
} else {
None
};
let Some(p) = pivot else {
self.selection = None;
return;
};
let mut start = p;
while start > 0 && is_word_char(chars[start - 1]) {
start -= 1;
}
let mut end = p + 1;
while end < chars.len() && is_word_char(chars[end]) {
end += 1;
}
self.cursor_col = end;
self.selection = Some(EditorSelection {
anchor: (r, start),
head: (r, end),
});
self.last_edit_kind = None;
}
/// Move the cursor to the screen coordinates (col, row). Used for mouse clicks.
pub fn click(&mut self, col: u16, row: u16) {
if self.lines.is_empty() || self.last_inner.height == 0 {
return;
}
if row < self.last_inner.y || row >= self.last_inner.y + self.last_inner.height {
return;
}
let row_idx = (row - self.last_inner.y) as usize;
let text_x = self.last_inner.x + self.last_gutter_width + 1;
// Gutter clicks land on the row's first column (saturates to 0).
let visible = col.saturating_sub(text_x) as usize;
// Map the screen row through the last render's visual-row layout (the
// same map `buffer_pos_at` reads) so wrapped and folded lines resolve
// to the line actually shown on that row; the linear map drifts by one
// for every wrapped continuation row above the pointer. A press below
// the last visual row clamps to it. The map is empty only before the
// first paint, when nothing can be wrapped or folded and the linear
// map is exact.
// A press on a comment-box row leaves the caret alone (the App
// intercepts box clicks; this is the belt-and-braces path). A press
// below the last visual row clamps to the nearest text row above.
let clamped = row_idx.min(self.last_wrap_rows.len().saturating_sub(1));
let nearest_text = (0..=clamped)
.rev()
.find_map(|i| self.text_row(i).map(|t| (i, t)));
if !self.last_wrap_rows.is_empty()
&& row_idx < self.last_wrap_rows.len()
&& self.text_row(row_idx).is_none()
{
return;
}
// `start + visible` is a DISPLAY column, so inlay hints spliced into
// the row have to be mapped back out of it — the same inversion
// `buffer_pos_at` (and therefore hover) applies. Without this the
// caret lands `hint` cells right of the pointer.
let (target_line, target_col) = match nearest_text {
Some((_, (line, start, end))) => {
// In wrap mode a drag past a segment's right edge stops at the
// segment end; non-wrap keeps the click-to-line-end behaviour.
let cap = if self.wrap_enabled() { end } else { usize::MAX };
(
line,
self.buffer_col_at_display(line, start + visible).min(cap),
)
}
None => {
let line = (self.scroll + row_idx).min(self.lines.len().saturating_sub(1));
(
line,
self.buffer_col_at_display(line, visible + self.scroll_col),
)
}
};
self.cursor_row = target_line;
self.cursor_col = target_col.min(self.line_char_len(target_line));
self.last_edit_kind = None;
// A click is a deliberate reposition; any multi-cursor session ends.
self.carets.clear();
}
pub fn buffer_pos_at(&self, col: u16, row: u16) -> Option<(usize, usize)> {
if self.last_inner.height == 0 || self.lines.is_empty() || row < self.last_inner.y {
return None;
}
let text_x = self.last_inner.x + self.last_gutter_width + 1;
if self.wrap_enabled() {
// Map the screen row through the last render's visual-row layout so
// a click lands on the right logical line/column even when wrapped.
// The blank tail of a folded row maps to the segment end, letting
// you click to end-of-visual-line.
let (line, start, end) = self.text_row((row - self.last_inner.y) as usize)?;
if col < text_x {
return None;
}
let visible_col = (col - text_x) as usize;
let char_col = (start + visible_col).min(end).min(self.line_char_len(line));
return Some((line, char_col));
}
// Non-wrap: map through the painted layout (comment boxes shift the
// rows below them); before the first paint the linear map is exact.
if row >= self.last_inner.y + self.last_inner.height {
return None;
}
let line = if self.last_wrap_rows.is_empty() {
self.scroll + (row - self.last_inner.y) as usize
} else {
self.text_row((row - self.last_inner.y) as usize)?.0
};
if line >= self.lines.len() || col < text_x {
return None;
}
let text_width = self
.last_inner
.width
.saturating_sub(self.last_gutter_width + 2 + u16::from(self.last_scrollbar.width > 0));
let visible_col = (col - text_x) as usize;
if visible_col >= text_width as usize {
return None;
}
Some((
line,
self.buffer_col_at_display(line, self.scroll_col + visible_col),
))
}
/// Invert the hint-splice display map: the first buffer column of `line`
/// whose display cell reaches absolute display column `display_col`. A
/// point landing inside a hint's own cells resolves to its anchor column,
/// so the caret snaps beside the code the hint annotates. Without hints
/// this is the identity (clamped to the line length).
fn buffer_col_at_display(&self, line: usize, display_col: usize) -> usize {
let line_len = self.line_char_len(line);
let hints = self.row_inlay_spans(line);
if hints.is_empty() {
return display_col.min(line_len);
}
let mut c = self.scroll_col;
while c < line_len {
let extra: usize = hints
.iter()
.filter(|(hc, _)| *hc >= self.scroll_col && *hc <= c)
.map(|(_, l)| l.chars().count())
.sum();
if c + extra >= display_col {
break;
}
c += 1;
}
c.min(line_len)
}
pub fn word_at(&self, line: usize, col: usize) -> Option<(usize, usize)> {
let chars: Vec<char> = self.lines.get(line)?.chars().collect();
if col >= chars.len() || !is_word_char(chars[col]) {
return None;
}
let mut start = col;
while start > 0 && is_word_char(chars[start - 1]) {
start -= 1;
}
let mut end = col + 1;
while end < chars.len() && is_word_char(chars[end]) {
end += 1;
}
Some((start, end))
}
/// The identifier text under `(line, col)`, or None over non-word
/// characters. Used by debug hover-to-evaluate to name the variable.
pub fn word_string_at(&self, line: usize, col: usize) -> Option<String> {
let (start, end) = self.word_at(line, col)?;
let chars: Vec<char> = self.lines.get(line)?.chars().collect();
Some(chars[start..end].iter().collect())
}
/// Severities and messages of every diagnostic whose range covers the
/// character position `(line, ch)`. Decoded on demand from the retained
/// `diagnostics` (UTF-16 positions) rather than the per-line span cache so
/// the messages don't have to be duplicated into the render-time cache.
/// Only runs on a hover dwell, never per frame. A zero-width diagnostic is
/// widened to one cell to match the underline, so a point diagnostic is
/// still hoverable. Returns an empty vec when no squiggle is under the
/// point (and when the retained batch belongs to another file).
pub fn diagnostics_at(
&self,
line: usize,
ch: usize,
) -> Vec<(crate::lsp::manager::DiagnosticSeverity, String)> {
if self.diagnostics_path.as_deref() != self.path.as_deref() {
return Vec::new();
}
let mut out = Vec::new();
for d in &self.diagnostics {
let start_line = d.start_line as usize;
let end_line = d.end_line as usize;
if line < start_line || line > end_line {
continue;
}
let Some(text) = self.lines.get(line) else {
continue;
};
let from = if line == start_line {
utf16_to_char_col(text, d.start_char)
} else {
0
};
let to = if line == end_line {
utf16_to_char_col(text, d.end_char)
} else {
text.chars().count()
};
let to = to.max(from + 1);
if ch >= from && ch < to {
out.push((d.severity, d.message.clone()));
}
}
out
}
/// The region a hover popup should be keyed to at `(line, ch)`: the word
/// range when one is under the point, otherwise the span of the first
/// diagnostic covering it (so a squiggle on punctuation, e.g. a `..`
/// syntax error, still anchors and dismisses a hover). `None` when neither
/// is present. Shared by `poll_hover`, `drain_lsp_hover`, and the
/// mouse-move dismissal so all three agree on what the popup belongs to.
pub fn hover_region_at(&self, line: usize, ch: usize) -> Option<(usize, usize, usize)> {
if let Some((start, end)) = self.word_at(line, ch) {
return Some((line, start, end));
}
if self.diagnostics_path.as_deref() != self.path.as_deref() {
return None;
}
for d in &self.diagnostics {
let start_line = d.start_line as usize;
let end_line = d.end_line as usize;
if line < start_line || line > end_line {
continue;
}
let text = self.lines.get(line)?;
let from = if line == start_line {
utf16_to_char_col(text, d.start_char)
} else {
0
};
let to = if line == end_line {
utf16_to_char_col(text, d.end_char)
} else {
text.chars().count()
};
let to = to.max(from + 1);
if ch >= from && ch < to {
return Some((line, from, to));
}
}
None
}
}
/// Convert a char index within `s` to a byte index, saturating at `s.len()`.
fn char_byte(s: &str, char_idx: usize) -> usize {
s.char_indices()
.nth(char_idx)
.map(|(i, _)| i)
.unwrap_or(s.len())
}
fn is_word_char(c: char) -> bool {
c.is_alphanumeric() || c == '_'
}
/// A `(row, char_col)` position of a bracket character, used by the bracket
/// matching helpers.
type BPos = (usize, usize);
/// The bracket pairs croft matches, mirroring VS Code's default language
/// brackets `()`, `[]`, `{}`.
const BRACKET_PAIRS: [(char, char); 3] = [('(', ')'), ('[', ']'), ('{', '}')];
fn is_open_bracket(c: char) -> bool {
BRACKET_PAIRS.iter().any(|&(o, _)| o == c)
}
fn is_close_bracket(c: char) -> bool {
BRACKET_PAIRS.iter().any(|&(_, cl)| cl == c)
}
fn matching_close(c: char) -> Option<char> {
BRACKET_PAIRS
.iter()
.find(|&&(o, _)| o == c)
.map(|&(_, cl)| cl)
}
fn matching_open(c: char) -> Option<char> {
BRACKET_PAIRS
.iter()
.find(|&&(_, cl)| cl == c)
.map(|&(o, _)| o)
}
/// Start char-indices of every whole-word, case-sensitive occurrence of
/// `word` within `chars`. "Whole-word" means the char immediately before and
/// after the match is not a word char, so `foo` does not match inside
/// `foobar`. Mirrors VS Code's "Change All Occurrences" matching.
fn find_word_occurrences(chars: &[char], word: &[char]) -> Vec<usize> {
let mut out = Vec::new();
if word.is_empty() || word.len() > chars.len() {
return out;
}
let last_start = chars.len() - word.len();
let mut i = 0;
while i <= last_start {
if chars[i..i + word.len()] == *word {
let before_ok = i == 0 || !is_word_char(chars[i - 1]);
let after_idx = i + word.len();
let after_ok = after_idx >= chars.len() || !is_word_char(chars[after_idx]);
if before_ok && after_ok {
out.push(i);
i += word.len();
continue;
}
}
i += 1;
}
out
}
/// The line-comment marker for a language (VS Code `lineComment`), or `None`
/// for languages with no line comment, in which case Toggle Line Comment is a
/// no-op.
fn line_comment_token(lang: Option<LangKind>) -> Option<&'static str> {
match lang {
Some(LangKind::Rust)
| Some(LangKind::JavaScript)
| Some(LangKind::TypeScript)
| Some(LangKind::Tsx)
| Some(LangKind::Go)
| Some(LangKind::C)
| Some(LangKind::Cpp) => Some("//"),
Some(LangKind::Python)
| Some(LangKind::Yaml)
| Some(LangKind::Toml)
| Some(LangKind::Bash) => Some("#"),
_ => None,
}
}
/// The block-comment delimiters for a language (VS Code `blockComment`), or
/// `None` for languages without one (e.g. YAML), in which case Toggle Block
/// Comment is a no-op. Python has no real block comment, but VS Code maps the
/// command onto a triple-quoted string, so it returns `""" """`.
fn block_comment_tokens(lang: Option<LangKind>) -> Option<(&'static str, &'static str)> {
match lang {
Some(LangKind::Rust)
| Some(LangKind::JavaScript)
| Some(LangKind::TypeScript)
| Some(LangKind::Tsx)
| Some(LangKind::Go)
| Some(LangKind::C)
| Some(LangKind::Cpp)
| Some(LangKind::Css) => Some(("/*", "*/")),
Some(LangKind::Html) | Some(LangKind::Markdown) => Some(("<!--", "-->")),
// Python has no true block comment; VS Code's language config maps
// Toggle Block Comment onto a triple-quoted string (`""" """`).
Some(LangKind::Python) => Some(("\"\"\"", "\"\"\"")),
_ => None,
}
}
/// Title-case a string: the first alphanumeric of each run is upper-cased,
/// the rest lower-cased, separators preserved. Used by Transform to Title
/// Case.
fn title_case(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut prev_alnum = false;
for c in s.chars() {
if c.is_alphanumeric() {
if prev_alnum {
out.extend(c.to_lowercase());
} else {
out.extend(c.to_uppercase());
}
prev_alnum = true;
} else {
out.push(c);
prev_alnum = false;
}
}
out
}
fn indent_unit_for(lang: Option<LangKind>) -> &'static str {
match lang {
Some(LangKind::Yaml) => " ",
_ => " ",
}
}
/// Display name for a language mode, matching VS Code's status-bar labels.
/// `None` is a buffer with no recognised grammar.
pub fn language_label(lang: Option<LangKind>) -> &'static str {
match lang {
None => "Plain Text",
Some(LangKind::Rust) => "Rust",
Some(LangKind::Python) => "Python",
Some(LangKind::JavaScript) => "JavaScript",
Some(LangKind::TypeScript) => "TypeScript",
Some(LangKind::Tsx) => "TypeScript JSX",
Some(LangKind::Json) => "JSON",
Some(LangKind::Toml) => "TOML",
Some(LangKind::Yaml) => "YAML",
Some(LangKind::Markdown) => "Markdown",
Some(LangKind::Go) => "Go",
Some(LangKind::Html) => "HTML",
Some(LangKind::Css) => "CSS",
Some(LangKind::Bash) => "Shell Script",
Some(LangKind::C) => "C",
Some(LangKind::Cpp) => "C++",
}
}
/// The VS Code language id for `lang`, used to scope user snippets (their
/// `scope` field names these). Lowercase, matching VS Code's identifiers so a
/// config copied from there just works.
pub fn language_scope_id(lang: Option<LangKind>) -> &'static str {
match lang {
None => "plaintext",
Some(LangKind::Rust) => "rust",
Some(LangKind::Python) => "python",
Some(LangKind::JavaScript) => "javascript",
Some(LangKind::TypeScript) => "typescript",
Some(LangKind::Tsx) => "typescriptreact",
Some(LangKind::Json) => "json",
Some(LangKind::Toml) => "toml",
Some(LangKind::Yaml) => "yaml",
Some(LangKind::Markdown) => "markdown",
Some(LangKind::Go) => "go",
Some(LangKind::Html) => "html",
Some(LangKind::Css) => "css",
Some(LangKind::Bash) => "shellscript",
Some(LangKind::C) => "c",
Some(LangKind::Cpp) => "cpp",
}
}
/// Every selectable language mode, in the order the status-bar picker lists
/// them. `None` (Plain Text) is offered separately by the picker.
pub const SELECTABLE_LANGUAGES: &[LangKind] = &[
LangKind::Rust,
LangKind::Python,
LangKind::JavaScript,
LangKind::TypeScript,
LangKind::Tsx,
LangKind::Json,
LangKind::Toml,
LangKind::Yaml,
LangKind::Markdown,
LangKind::Go,
LangKind::Html,
LangKind::Css,
LangKind::Bash,
LangKind::C,
LangKind::Cpp,
];
/// Number of leading whitespace bytes to strip for one outdent step, matching
/// VS Code's tab-stop-aligned `unshiftIndent`. A leading tab counts as one
/// level (strip it). For spaces, strip back to the previous tab stop: with a
/// width-4 unit, 4 -> remove 4, 6 -> remove 2, 3 -> remove 3, 1 -> remove 1.
/// All leading whitespace here is ASCII (`' '` / `'\t'`), so byte count equals
/// char count.
fn leading_outdent_chars(line: &str, unit_w: usize) -> usize {
if unit_w == 0 {
return 0;
}
if line.starts_with('\t') {
return 1;
}
let spaces = line.chars().take_while(|c| *c == ' ').count();
if spaces == 0 {
0
} else {
((spaces - 1) % unit_w) + 1
}
}
fn extra_indent_triggered(lang: Option<LangKind>, last_non_ws: Option<char>) -> bool {
let last = match last_non_ws {
Some(c) => c,
None => return false,
};
match lang {
Some(LangKind::Python) => matches!(last, ':' | '(' | '[' | '{'),
Some(LangKind::Rust)
| Some(LangKind::JavaScript)
| Some(LangKind::TypeScript)
| Some(LangKind::Tsx)
| Some(LangKind::Json)
| Some(LangKind::Go)
| Some(LangKind::Css) => matches!(last, '(' | '[' | '{'),
_ => false,
}
}
fn is_bracket_pair_split(lang: Option<LangKind>, prev: Option<char>, next: Option<char>) -> bool {
let bracket_aware = matches!(
lang,
Some(LangKind::Rust)
| Some(LangKind::Python)
| Some(LangKind::JavaScript)
| Some(LangKind::TypeScript)
| Some(LangKind::Tsx)
| Some(LangKind::Json)
| Some(LangKind::Go)
| Some(LangKind::Css)
);
if !bracket_aware {
return false;
}
matches!(
(prev, next),
(Some('{'), Some('}')) | (Some('['), Some(']')) | (Some('('), Some(')'))
)
}
fn is_binary(data: &[u8]) -> bool {
let sample = &data[..data.len().min(4096)];
if sample.contains(&0) {
return true;
}
if sample.is_empty() {
return false;
}
let nontext = sample
.iter()
.filter(|&&b| !(b >= 0x20 || matches!(b, b'\n' | b'\r' | b'\t' | 0x0c | 0x08)))
.count();
(nontext as f32 / sample.len() as f32) > 0.30
}
/// Build a Vec<Span> from a line and its byte-range highlight spans.
/// Byte index of the character at character index `chars_in`, or the line's
/// byte length when `chars_in` falls past the end. Used to slice a line at
/// a horizontal scroll offset measured in characters.
fn byte_index_of_char(line: &str, chars_in: usize) -> usize {
line.char_indices()
.nth(chars_in)
.map(|(b, _)| b)
.unwrap_or(line.len())
}
/// Character column for the LSP UTF-16 offset `u16_off` within `line`. LSP
/// positions count UTF-16 code units; the editor lays out by character, so a
/// line containing astral-plane characters (which take two UTF-16 units) needs
/// this conversion. An offset past the line clamps to the line's char length.
fn utf16_to_char_col(line: &str, u16_off: u32) -> usize {
let mut units: u32 = 0;
for (char_idx, ch) in line.chars().enumerate() {
if units >= u16_off {
return char_idx;
}
units += ch.len_utf16() as u32;
}
line.chars().count()
}
/// A character VS Code breaks a wrapped line *after* (a subset of its default
/// `editor.wordWrapBreakAfterCharacters` that matters for Latin/Markdown
/// prose): whitespace plus trailing punctuation, so the next word starts a
/// fresh visual row.
fn is_wrap_break_after(c: char) -> bool {
c.is_whitespace()
|| matches!(
c,
')' | ']' | '}' | '-' | '/' | ',' | '.' | ';' | ':' | '?' | '!'
)
}
/// Soft-wrap a logical line into visual segments for word-wrap mode.
///
/// Each segment is a half-open `(start_char, end_char)` range over `chars`;
/// the segments tile the line with no gaps or overlaps, so the inverse maps
/// (cursor -> screen, click -> cursor) stay exact. Breaks after the last
/// break-after character at or before `width` when one exists (word wrap), and
/// hard-breaks a single token longer than `width` at the column. A line that
/// already fits, an empty line, or `width == 0` yields one segment so the line
/// still occupies a row. Mirrors VS Code's monospace wrap: pick the rightmost
/// break opportunity within the column, else split the over-long token.
fn wrap_segments(chars: &[char], width: usize) -> Vec<(usize, usize)> {
if width == 0 || chars.len() <= width {
return vec![(0, chars.len())];
}
let mut segs = Vec::new();
let mut start = 0;
while start < chars.len() {
if chars.len() - start <= width {
segs.push((start, chars.len()));
break;
}
let limit = start + width;
// Scan back from the hard limit for the rightmost break-after char;
// `i` is the char count, so the char sits at `chars[i - 1]` and the
// segment becomes `[start, i)`. Stop at `start + 1` so a break right
// at the segment start can't produce a one-char row (or stall).
let mut brk = limit;
let mut i = limit;
while i > start + 1 {
if is_wrap_break_after(chars[i - 1]) {
brk = i;
break;
}
i -= 1;
}
segs.push((start, brk));
start = brk;
}
segs
}
/// Shift highlight spans left by `byte_start`, dropping spans that fall
/// entirely before the cut and clamping spans straddling the cut.
/// Split file text into buffer lines the way the LSP spec / VS Code / Zed do:
/// `\r\n`, a lone `\r`, and `\n` are all line terminators. Rust's `str::lines`
/// only breaks on `\n` (stripping a trailing `\r`), so a stray lone `\r` would
/// leave croft's line count one short of the language server's. Every LSP
/// position past that `\r` (semantic tokens, diagnostics, hover, definition)
/// would then resolve one row off. Normalizing first keeps the two in lockstep
/// and is a no-op for clean `\n`-only files.
fn split_into_lines(text: &str) -> Vec<String> {
normalize_newlines(text)
.lines()
.map(|s| s.to_string())
.collect()
}
/// Fold every line-ending convention to `\n` so callers can split on it
/// alone. Shared by the file loader and the find bar's replacement paths: a
/// replacement carrying `\r` (VS Code's escape, or pasted CRLF text) must
/// become a real line break, never a bare CR sitting inside a line — a CRLF
/// buffer would then save `\r\r\n` and every line count would disagree.
pub(crate) fn normalize_newlines(text: &str) -> String {
text.replace("\r\n", "\n").replace('\r', "\n")
}
fn shift_spans_for_view(spans: &[HiSpan], byte_start: usize) -> Vec<HiSpan> {
let mut out = Vec::with_capacity(spans.len());
for sp in spans {
if sp.end <= byte_start {
continue;
}
let new_start = sp.start.saturating_sub(byte_start);
let new_end = sp.end - byte_start;
out.push(HiSpan {
start: new_start,
end: new_end,
style: sp.style,
});
}
out
}
/// Overlay `over` onto `base` (both sorted, non-overlapping, byte offsets
/// within one line) and return a sorted, non-overlapping span list. Where
/// the two cover the same bytes, `over` wins; `base` shows through in the
/// gaps. This is the per-line realization of the VS Code / Zed "combined"
/// rule: semantic tokens repaint the bytes they resolve, tree-sitter
/// syntax fills the rest.
/// Resolve a span's foreground to RGB for the minimap; non-RGB colors
/// (named/indexed/reset) fall back to the editor's default text color.
fn span_rgb(style: ratatui::style::Style, default: (u8, u8, u8)) -> (u8, u8, u8) {
match style.fg {
Some(ratatui::style::Color::Rgb(r, g, b)) => (r, g, b),
_ => default,
}
}
fn merge_overlay(base: &[HiSpan], over: &[HiSpan]) -> Vec<HiSpan> {
if over.is_empty() {
return base.to_vec();
}
let mut out: Vec<HiSpan> = Vec::with_capacity(base.len() + over.len());
for b in base {
// Emit the parts of this base span not covered by any overlay span.
let mut cur = b.start;
for o in over.iter().filter(|o| o.end > b.start && o.start < b.end) {
if o.start > cur {
out.push(HiSpan {
start: cur,
end: o.start,
style: b.style,
});
}
cur = cur.max(o.end);
if cur >= b.end {
break;
}
}
if cur < b.end {
out.push(HiSpan {
start: cur,
end: b.end,
style: b.style,
});
}
}
out.extend_from_slice(over);
out.sort_by_key(|s| s.start);
out
}
fn build_line_spans<'a>(line: &'a str, spans: &[HiSpan]) -> Vec<Span<'a>> {
if spans.is_empty() {
return vec![Span::raw(line)];
}
let mut out: Vec<Span> = Vec::with_capacity(spans.len() * 2);
let mut cursor = 0usize;
for sp in spans {
if sp.start > cursor && sp.start <= line.len() {
let slice = &line[cursor..sp.start];
if !slice.is_empty() {
out.push(Span::raw(slice));
}
}
let s = sp.start.min(line.len());
let e = sp.end.min(line.len());
if e > s {
out.push(Span::styled(&line[s..e], sp.style));
cursor = e;
}
}
if cursor < line.len() {
out.push(Span::raw(&line[cursor..]));
}
out
}
impl Widget for &mut Editor {
fn render(self, area: Rect, buf: &mut Buffer) {
let block_style = if self.focused {
Style::default().fg(Color::Rgb(0x4e, 0x9a, 0xff))
} else {
Style::default().fg(Color::DarkGray)
};
let block = Block::default()
.borders(Borders::ALL)
.border_style(block_style);
let inner = block.inner(area);
block.render(area, buf);
// Black theme: replace the solid focus border with the orange→green
// gradient that matches the welcome activity box. The editor has no
// title, so nothing needs re-stamping over the gradient top edge.
if self.focused && self.focus_gradient {
crate::gradient::paint_gradient_box(buf, area);
}
self.last_area = area;
self.last_inner = inner;
self.last_scrollbar = Rect::default();
self.last_hscrollbar = Rect::default();
let height = inner.height as usize;
if height == 0 {
return;
}
let cbg = canvas_bg(
crate::iterm2_inline::detect_iterm2_inline_support(),
self.theme,
);
if let Some(image) = self.image.as_ref() {
let ibg = image_canvas_bg(
crate::iterm2_inline::detect_inline_image_protocol(),
self.theme,
);
render_image_placeholder(image, self.path.as_deref(), inner, buf, ibg);
return;
}
if let Some(view) = self.sheet.as_ref() {
render_sheet(view, self.path.as_deref(), inner, buf, cbg);
return;
}
if self.markdown_preview.is_some() {
self.render_markdown_preview(inner, buf);
return;
}
if let Some(diff) = self.diff.as_mut() {
let (prev_arrow, next_arrow) = render_diff(diff, inner, buf, cbg);
self.diff_prev_arrow = prev_arrow;
self.diff_next_arrow = next_arrow;
return;
}
// Non-diff tabs: clear the hit rects so a stale arrow click on a
// tab the user just switched away from can't fire.
self.diff_prev_arrow = Rect::default();
self.diff_next_arrow = Rect::default();
// Gutter = a left glyph margin (the breakpoint / stop column, VS Code
// style) + the right-aligned line number + its trailing space. Folding
// the margin into `gutter_width` shifts the numbers and code right
// together, so the number-to-code gap is unchanged and click/cursor
// mapping (which derives from `gutter_width`) stays correct for free.
const SIGN_MARGIN: u16 = 2;
let gutter_width = (self.lines.len() + 1).to_string().len() as u16 + 1 + SIGN_MARGIN;
self.last_gutter_width = gutter_width;
// Rebuild the git-gutter marks if the buffer moved since last frame
// (cheap no-op otherwise), so the bars below diff against HEAD.
self.refresh_git_marks();
// Rescan merge-conflict blocks on the same cadence so the region
// tints below always match the buffer.
self.conflicts();
// Fold headers are line indexes; an insert/delete anywhere shifts them.
// If the line count changed since folds were set, drop them wholesale
// rather than hide the wrong lines (see `fold_epoch_lines`).
if !self.folded.is_empty() && self.lines.len() != self.fold_epoch_lines {
self.folded.clear();
self.hidden_ranges.clear();
}
// A caret on a hidden line has no painted row, so nothing is drawn and
// the user cannot see where they are. Every direct `cursor_row` setter
// can land there — go-to-definition, search, Ctrl+G, a restored session
// — and enumerating them is how one gets missed, so normalise here,
// where all of them converge before anything is painted. One binary
// search against `hidden_ranges` when a fold exists, nothing when none
// does.
if !self.folded.is_empty() && self.is_line_hidden(self.cursor_row) {
self.reveal_cursor_fold();
}
let wrap = self.wrap_enabled();
if wrap {
// Wrapped text folds onto extra rows instead of scrolling sideways.
self.scroll_col = 0;
}
let text_x = inner.x + gutter_width + 1;
let content_cols = self.content_cols();
// Layout has a small cycle: the horizontal bar takes the bottom row
// only when a line overflows the text column, but the text column's
// width depends on whether the vertical bar takes a column. Break it
// in one pass: provisionally reserve the vertical bar's column from
// the FULL height to decide the horizontal bar, then recompute the
// vertical thumb against the real (possibly shortened) viewport.
// Reserving the horizontal row can only ever narrow the text column,
// never widen it, so a line that overflows under the provisional
// width still overflows under the final one - the decision is stable.
// The horizontal bar never appears in wrap mode (lines fold instead).
let provisional_text_width = inner
.width
.saturating_sub(gutter_width + 2 + u16::from(self.lines.len() > height))
as usize;
let hbar_present =
!wrap && provisional_text_width > 0 && content_cols > provisional_text_width;
let text_height = height - usize::from(hbar_present);
self.last_text_rows = text_height as u16;
let scrollbar_area = Rect {
x: inner.x + inner.width.saturating_sub(1),
y: inner.y,
width: u16::from(inner.width > 0),
height: text_height as u16,
};
// Decide the vertical scrollbar and final text width, then clamp the
// scroll so the cursor stays visible. In wrap mode the content extent
// and scroll position are measured in VISUAL rows (a logical line may
// span several); otherwise in logical lines, one per row.
let (text_width, scrollbar_metrics) = if wrap {
let wide = inner.width.saturating_sub(gutter_width + 2) as usize;
// Overflow at the wide width forces the vbar, which narrows the
// column and only ever adds rows, so the decision is stable.
let vbar = self.total_visual_rows(wide) > text_height;
let tw = if vbar { wide.saturating_sub(1) } else { wide };
let content_rows = self.total_visual_rows(tw);
// Keep the cursor's visual row inside the viewport.
if text_height > 0 {
let cursor_vrow = self.cursor_visual_row(tw);
let mut top = self.top_visual_row(tw);
if cursor_vrow < top {
top = cursor_vrow;
} else if cursor_vrow >= top + text_height {
top = cursor_vrow + 1 - text_height;
}
top = top.min(content_rows.saturating_sub(text_height));
self.set_top_to_visual_row(top, tw);
}
let metrics = scrollbar::vertical_metrics(
scrollbar_area,
content_rows,
text_height,
self.top_visual_row(tw),
);
(tw as u16, metrics)
} else {
// `scroll_sub` counts rows INTO the top line's comment box (set
// by the content-row scroller); clamp it to the group and drop
// it entirely when a cursor clamp below moves the top line.
if self.comment_boxes.is_empty() {
self.scroll_sub = 0;
} else {
let bw = inner.width.saturating_sub(gutter_width + 3) as usize;
let group = 1 + self.box_rows_between(self.scroll, self.scroll + 1, bw);
self.scroll_sub = self.scroll_sub.min(group - 1);
}
let scroll_before = self.scroll;
if text_height > 0 {
if self.cursor_row < self.scroll {
self.scroll = self.cursor_row;
} else if self.cursor_row >= self.scroll + text_height {
self.scroll = self.cursor_row + 1 - text_height;
}
// Comment boxes between the top and the cursor consume rows;
// scroll further until the cursor's painted row fits. Boxes
// are measured at the pre-scrollbar width, one column
// narrower than the final text column, which can only
// overestimate their height (the cursor stays visible).
if !self.comment_boxes.is_empty() {
let bw = inner.width.saturating_sub(gutter_width + 3) as usize;
while self.cursor_row + self.box_rows_between(self.scroll, self.cursor_row, bw)
>= self.scroll + text_height
{
self.scroll += 1;
}
}
}
if self.scroll != scroll_before {
self.scroll_sub = 0;
}
let bw = inner.width.saturating_sub(gutter_width + 3) as usize;
let box_rows = self.box_rows_between(0, self.lines.len(), bw);
let metrics = scrollbar::vertical_metrics(
scrollbar_area,
self.lines.len() + box_rows,
text_height,
// Content-row top: lines above plus box rows above plus the
// rows scrolled into the top line's own box.
self.scroll + self.box_rows_between(0, self.scroll, bw) + self.scroll_sub,
);
let sw = u16::from(metrics.is_some());
let tw = inner.width.saturating_sub(gutter_width + 2 + sw);
(tw, metrics)
};
if let Some(metrics) = scrollbar_metrics {
self.last_scrollbar = metrics.area;
}
// Clamp horizontal scroll (non-wrap only) so a wheel swipe can't
// strand the buffer off-screen, then lay out the horizontal bar.
let max_scroll_col = content_cols.saturating_sub(text_width as usize);
self.scroll_col = self.scroll_col.min(max_scroll_col);
let hbar_metrics = scrollbar::horizontal_metrics(
Rect {
x: text_x,
y: inner.y + text_height as u16,
width: text_width,
height: u16::from(hbar_present),
},
content_cols,
text_width as usize,
self.scroll_col,
);
self.last_hscrollbar = hbar_metrics.map(|m| m.area).unwrap_or_default();
// Build the visual-row layout the loop paints and the inverse maps
// (cursor/click) read back. Each entry is (logical_line, char_start,
// char_end). Non-wrap: one row per visible line, range [scroll_col,
// scroll_col + text_width). Wrap: the wrapped segments from
// (scroll, scroll_sub) down, capped at the viewport height.
let mut visual_rows: Vec<VisRow> = Vec::with_capacity(text_height);
// The boxes hanging under a line, emitted right after its last
// segment. `group_pos` is the row's index within the line's whole
// group (segments first, then box rows) so a wrap viewport top that
// decomposed into a box region resumes mid-box.
let push_box_rows = |rows: &mut Vec<VisRow>,
ed: &Editor,
line: usize,
skip: usize,
seg_count: usize,
tw: usize| {
if ed.comment_boxes.is_empty() {
return;
}
let mut group_pos = seg_count;
for idx in 0..ed.comment_boxes.len() {
if ed.comment_boxes[idx].line != line {
continue;
}
for box_row in 0..ed.comment_box_height(idx, tw) {
if group_pos >= skip && rows.len() < text_height {
rows.push(VisRow::Box {
box_idx: idx,
box_row,
});
}
group_pos += 1;
}
}
};
if wrap {
let tw = text_width as usize;
let mut line = self.scroll;
let mut skip = self.scroll_sub;
while line < self.lines.len() && visual_rows.len() < text_height {
if self.is_line_hidden(line) {
line += 1;
continue;
}
let segs = self.line_segments(line, tw);
for (i, &(s, e)) in segs.iter().enumerate() {
if i < skip {
continue;
}
if visual_rows.len() >= text_height {
break;
}
visual_rows.push(VisRow::Text {
line,
start: s,
end: e,
});
}
push_box_rows(&mut visual_rows, self, line, skip, segs.len(), tw);
skip = 0;
line += 1;
}
} else {
// Collect up to `text_height` VISIBLE logical lines from `scroll`,
// skipping any hidden inside a collapsed fold.
let tw = text_width as usize;
let mut line = self.scroll;
// Group-position skip on the top line: 0 = its text row, 1..
// = rows into its comment box, so the viewport can start
// mid-box exactly like the wrap path.
let mut skip = self.scroll_sub;
while line < self.lines.len() && visual_rows.len() < text_height {
if !self.is_line_hidden(line) {
if skip == 0 && visual_rows.len() < text_height {
visual_rows.push(VisRow::Text {
line,
start: self.scroll_col,
end: self.scroll_col + text_width as usize,
});
}
push_box_rows(&mut visual_rows, self, line, skip, 1, tw);
skip = 0;
}
line += 1;
}
}
self.last_wrap_rows = visual_rows.clone();
let sel_norm = self
.selection
.filter(|s| s.has_area())
.map(|s| s.normalised());
let occ_needle = self.selection_occurrence_needle();
// Bracket-match highlight (VS Code `editorBracketMatch`): only in the
// focused editor, and only when the caret is beside a bracket.
let bracket_pair = if self.focused {
self.bracket_match_pair()
} else {
None
};
for (row_idx, vis_row) in visual_rows.iter().enumerate() {
let y = inner.y + row_idx as u16;
let (line_idx, row_start, row_end) = match *vis_row {
VisRow::Box { box_idx, box_row } => {
self.paint_comment_box_row(
buf,
inner,
y,
gutter_width,
text_x,
text_width,
box_idx,
box_row,
);
continue;
}
VisRow::Text { line, start, end } => (line, start, end),
};
// The line number shows once per logical line - on its first visual
// row; wrapped continuation rows get a blank gutter, like VS Code.
if !wrap || row_start == 0 {
let line_no = format!(
"{:>width$} ",
line_idx + 1,
width = gutter_width as usize - 1
);
let gutter =
Line::from(Span::styled(line_no, Style::default().fg(Color::DarkGray)));
buf.set_line(inner.x, y, &gutter, gutter_width);
} else {
buf.set_line(
inner.x,
y,
&Line::from(" ".repeat(gutter_width as usize)),
gutter_width,
);
}
// Debugger gutter glyphs, on a logical line's first visual row only:
// a yellow stop arrow (▶) takes priority over a red breakpoint dot
// (●). Painted in the dedicated glyph margin at the far left
// (`inner.x`), VS Code-style, so it never crowds the line number or
// the code and the number-to-code gap is untouched.
let sign_x = inner.x;
let mut sign_taken = false;
if (!wrap || row_start == 0)
&& let Some(path) = self.path.as_deref()
{
let here = line_idx + 1; // gutter is 1-based
let is_stop = self
.stop_line
.as_ref()
.is_some_and(|(p, l)| p == path && *l == here);
let is_bp = self
.breakpoints
.get(path)
.is_some_and(|s| s.contains(&here));
let is_unverified = self
.unverified_breakpoints
.get(path)
.is_some_and(|s| s.contains(&here));
let is_conditional = self
.breakpoint_conditions
.get(path)
.is_some_and(|c| c.contains_key(&here));
let is_logpoint = self
.breakpoint_logs
.get(path)
.is_some_and(|m| m.contains_key(&here));
if is_stop {
buf.set_string(
sign_x,
y,
"▶",
Style::default().fg(Color::Rgb(0xff, 0xcc, 0x00)),
);
sign_taken = true;
} else if is_bp {
// Hollow dimmed ring when the adapter could not bind it; a
// red diamond for a conditional breakpoint; an amber one
// for a logpoint; a solid red dot for a plain, live one.
let (glyph, color) = if is_unverified {
("○", Color::Rgb(0x99, 0x99, 0x99))
} else if is_conditional {
("◆", Color::Rgb(0xe5, 0x1c, 0x23))
} else if is_logpoint {
("◆", Color::Rgb(0xe5, 0xc0, 0x7b))
} else {
("●", Color::Rgb(0xe5, 0x1c, 0x23))
};
buf.set_string(sign_x, y, glyph, Style::default().fg(color));
sign_taken = true;
}
}
// AI-stream stop button (croft pair): while a pilot streams into
// this file, the row under its caret wears a stop square in the
// badge's orange; clicking it (or Cmd+K X) cancels and reverts
// the stream. Outranks the test play glyph; the debugger's
// glyphs above still win the shared cell.
if (!wrap || row_start == 0) && !sign_taken && self.stream_stop_line == Some(line_idx) {
buf.set_string(
sign_x,
y,
"■",
Style::default().fg(Color::Rgb(0xff, 0x9d, 0x2f)),
);
sign_taken = true;
}
// Testing gutter glyph: the play button beside a test fn's
// definition (VS Code's run bead, in its testing green), sharing
// the sign cell with the debugger — whose stop arrow and
// breakpoint dot both outrank it.
if (!wrap || row_start == 0)
&& !sign_taken
&& crate::testing::locate::test_fn_on_line(
self.path.as_deref(),
&self.lines,
line_idx,
)
.is_some()
{
buf.set_string(
sign_x,
y,
"\u{eb2c}", // cod-play, the Testing panel's run glyph
Style::default().fg(Color::Rgb(0x73, 0xc9, 0x91)),
);
}
// Fold chevron: on a foldable header's first visual row, in the
// second sign-margin column (`sign_x + 1`) so it never collides with
// a breakpoint dot at `sign_x`. ▾ = expanded, ▸ = collapsed; the
// collapsed one is brighter so hidden content is noticeable. Matches
// the gutter's own DarkGray line numbers (which are theme-agnostic).
if (!wrap || row_start == 0) && self.is_foldable(line_idx) {
let collapsed = self.folded.contains(&line_idx);
let (glyph, color) = if collapsed {
("▸", Color::Gray)
} else {
("▾", Color::DarkGray)
};
buf.set_string(sign_x + 1, y, glyph, Style::default().fg(color));
}
// Git gutter: a thin coloured bar in the spacer cell between the
// line number and the code (VS Code's dirty-diff lane), on a
// logical line's first visual row only. Colour carries add/mod/del.
// ponytail: one heavy bar in three colours; the bar's column also
// shows deletions, which VS Code renders as a small triangle — fine
// as a first cut, the colour already disambiguates.
if (!wrap || row_start == 0)
&& let Some(mark) = self.git_marks.get(&line_idx)
{
let color = match mark {
GitMark::Added => self.theme.git_added(),
GitMark::Modified => self.theme.git_modified(),
GitMark::Deleted => self.theme.git_deleted(),
};
buf.set_string(
inner.x + gutter_width,
y,
"\u{2503}", // ┃ heavy vertical
Style::default().fg(color),
);
}
// Per-row window: the segment [row_start, row_end). For non-wrap
// this is exactly the horizontally-scrolled view; for wrap it is
// one folded segment. The overlay painters treat `row_start` as
// the scroll offset and `row_width` as the text width, so the
// same code paints both modes.
let raw = &self.lines[line_idx];
let line_len = self.line_char_len(line_idx);
let row_width = (row_end - row_start) as u16;
let byte_start = byte_index_of_char(raw, row_start);
let byte_end = byte_index_of_char(raw, row_end);
let visible_raw = &raw[byte_start..byte_end];
let seg_bytes = byte_end - byte_start;
let empty: Vec<HiSpan> = Vec::new();
let line_spans = self.highlights.get(line_idx).unwrap_or(&empty);
// Paint the LSP semantic overlay over the tree-sitter base so
// resolved symbols (parameters, etc.) win wherever the server
// has an opinion, syntax fills the gaps (the "combined" model).
let sem_spans = self.semantic_overlay.get(line_idx).unwrap_or(&empty);
let merged = merge_overlay(line_spans, sem_spans);
// Inlay hints anchored inside this row's window, as (anchor col,
// display cells) pairs. A hint's label is spliced into the row
// BEFORE the character at its anchor; every buffer column at or
// past an anchor therefore paints `inlay_cells_before` cells
// further right, and each overlay painter below translates its
// columns through that same map so highlights, underlines, and
// carets stay glued to their glyphs.
let hint_cap = row_end.min(line_len);
let hint_cells: Vec<(usize, usize)> = self
.row_inlay_spans(line_idx)
.iter()
.filter(|(hc, _)| *hc >= row_start && *hc <= hint_cap)
.map(|(hc, l)| (*hc, l.chars().count()))
.collect();
let ex = |c: usize| inlay_cells_before(&hint_cells, c);
// Caret placement uses the strictly-before rule instead: a
// caret AT a hint's anchor sits left of the hint (the
// `cursor_screen_pos` / VS Code convention), while a CHARACTER
// at the anchor paints right of it (`ex` above).
let ex_caret = |c: usize| inlay_cells_strictly_before(&hint_cells, c);
if hint_cells.is_empty() {
// Shift highlight spans to the row origin and clip them to the
// segment so build_line_spans never slices past `visible_raw`.
let shifted: Vec<HiSpan> = shift_spans_for_view(&merged, byte_start)
.into_iter()
.filter_map(|mut sp| {
if sp.start >= seg_bytes {
return None;
}
sp.end = sp.end.min(seg_bytes);
Some(sp)
})
.collect();
let spans = build_line_spans(visible_raw, &shifted);
buf.set_line(text_x, y, &Line::from(spans), row_width);
} else {
// Splice each hint label between the text segments it splits.
// Style A (Zed's look): dim italic text, one shade quieter
// than comments, no chip background.
let hint_style = Style::default()
.fg(self.theme.ignored_fg())
.add_modifier(Modifier::ITALIC);
let mut out: Vec<Span> = Vec::new();
let mut from = row_start;
for (hcol, label) in self
.row_inlay_spans(line_idx)
.iter()
.filter(|(hc, _)| *hc >= row_start && *hc <= hint_cap)
{
if *hcol > from {
out.extend(inlay_text_segment(raw, &merged, from, *hcol));
}
out.push(Span::styled(label.clone(), hint_style));
from = from.max(*hcol);
}
if row_end > from {
out.extend(inlay_text_segment(raw, &merged, from, row_end));
}
buf.set_line(text_x, y, &Line::from(out), row_width);
}
// Merge-conflict region tints (VS Code's current/incoming
// backgrounds), painted right after the text so diagnostics,
// search highlights, and the selection all win over them.
if let Some(tint) = conflict_row_tint(&self.conflicts, line_idx) {
paint_full_row_bg(buf, text_x, y, row_width, tint);
}
// LSP diagnostics: underline each problem span in its severity
// colour, clipped to the visible row window. VS Code draws a wavy
// underline; the crossterm backend exposes only a straight one, so
// a coloured straight underline carries the severity instead. The
// underline colour is independent of the glyph's foreground, so the
// syntax/semantic colour underneath is preserved.
let empty_diag: Vec<(usize, usize, crate::lsp::manager::DiagnosticSeverity)> =
Vec::new();
for &(sc, ec, severity) in self.diagnostic_spans.get(line_idx).unwrap_or(&empty_diag) {
let vs = (sc + ex(sc)).saturating_sub(row_start);
// The exclusive end shifts by the hints before its LAST char,
// so an underline never swallows a hint anchored right at it.
let ve = (ec + ex(ec.saturating_sub(1))).saturating_sub(row_start);
if ve > vs {
paint_diagnostic_underline(buf, text_x, y, row_width, vs, ve, severity);
}
}
// LSP occurrences of the symbol under the caret (word highlight):
// painted before the find layer, the selection band, and the
// bracket match so all three stay visible on top. The find layer
// in particular paints black-on-gold; an occurrence bg over it
// left black text on a dark grey (the caret parked on a find
// match makes the two layers cover the same cells).
for &(occ_row, occ_start, occ_end, occ_write) in &self.occurrences {
if occ_row != line_idx {
continue;
}
let bg = if occ_write {
self.theme.occurrence_write_bg()
} else {
self.theme.occurrence_bg()
};
for c in occ_start..occ_end {
if c < row_start {
continue;
}
let col = (c + inlay_cells_before(&hint_cells, c) - row_start) as u16;
if col >= row_width {
break;
}
let cell = &mut buf[(text_x + col, y)];
cell.set_style(cell.style().bg(bg));
}
}
if let Some(term) = self.search_highlight.as_deref() {
let active_on_line = self
.active_search_match
.filter(|(r, _, _)| *r == line_idx)
.map(|(_, c, l)| (c, l));
paint_search_highlight(
buf,
text_x,
y,
row_width,
raw,
term,
self.search_highlight_opts,
row_start,
active_on_line,
&hint_cells,
);
}
if let Some(needle) = occ_needle.as_deref() {
paint_selection_occurrences(
buf,
text_x,
y,
row_width,
raw,
needle,
row_start,
&hint_cells,
);
}
if let Some(((sr, sc), (er, ec))) = sel_norm
&& line_idx >= sr
&& line_idx <= er
{
let sel_start = if line_idx == sr { sc } else { 0 };
// For non-final selected rows, paint past the content by one
// cell to make the trailing newline visible.
let sel_end = if line_idx == er { ec } else { line_len + 1 };
let visible_start = (sel_start + ex(sel_start)).saturating_sub(row_start);
let visible_end =
(sel_end + ex(sel_end.saturating_sub(1))).saturating_sub(row_start);
if visible_end > visible_start {
paint_selection_band(buf, text_x, y, row_width, visible_start, visible_end);
}
}
// Bracket-match highlight: tint the two matched bracket cells that
// fall on this visual row, honouring horizontal scroll.
if let Some((open, close)) = bracket_pair {
for pos in [open, close] {
if pos.0 == line_idx {
paint_bracket_match(
buf,
text_x,
y,
row_width,
pos.1 + ex(pos.1),
row_start,
self.theme,
);
}
}
}
// Secondary multi-cursor carets ("Change All Occurrences"): paint
// each one's selection band and a software block cursor at its
// head. The primary cursor is the hardware caret; these blocks
// mark the extra cursors so the user sees every edit point.
for caret in &self.carets {
let ((cr0, cc0), (cr1, cc1)) = caret.normalised();
if line_idx >= cr0 && line_idx <= cr1 {
let cs = if line_idx == cr0 { cc0 } else { 0 };
let ce = if line_idx == cr1 { cc1 } else { line_len + 1 };
let vs = (cs + ex(cs)).saturating_sub(row_start);
let ve = (ce + ex(ce.saturating_sub(1))).saturating_sub(row_start);
if ve > vs {
paint_selection_band(buf, text_x, y, row_width, vs, ve);
}
}
// Only paint the block on the visual row that holds the caret.
let c = caret.head.1;
let on_row = caret.head.0 == line_idx
&& c >= row_start
&& (c < row_end || (c == row_end && row_end == line_len));
if on_row {
// Strictly-before, not `ex`: a caret AT a hint's anchor
// sits left of the hint like the primary caret
// (`cursor_screen_pos`), marking the cell its typed
// character will land on, inside its own band.
paint_block_cursor(buf, text_x, y, row_width, c + ex_caret(c), row_start);
}
}
// Collaborators' ghost carets (multiplayer): the same block shape
// as secondary carets, in each participant's color. The local
// hardware caret is painted by the terminal after drawing, so it
// always sits above these.
for &(gr, gc, color) in &self.ghost_carets {
let on_row = gr == line_idx
&& gc >= row_start
&& (gc < row_end || (gc == row_end && row_end == line_len));
if on_row {
paint_ghost_caret(
buf,
text_x,
y,
row_width,
gc + ex_caret(gc),
row_start,
color,
);
}
}
// GitLens-style inline blame: a dim italic annotation trailing the
// cursor's line, on its last visual segment, in the focused editor
// only. Painted last so no overlay covers it, and clipped to the
// pane's right edge.
if self.focused
&& line_idx == self.cursor_row
&& row_end >= line_len
&& let Some(note) = self.current_line_blame_annotation()
{
let text_cols = (line_len + ex(line_len)).saturating_sub(row_start);
let start_x = text_x + text_cols as u16 + 2;
let right = inner.x + inner.width;
if start_x < right {
let avail = (right - start_x) as usize;
let shown: String = note.chars().take(avail).collect();
buf.set_string(
start_x,
y,
&shown,
Style::default()
.fg(self.theme.ignored_fg())
.add_modifier(Modifier::ITALIC),
);
}
}
// The cursor itself is drawn by the host terminal as a hardware
// caret (DECSCUSR `BlinkingBar`); App calls
// `frame.set_cursor_position(...)` so the blink/overlay never
// hides the underlying character.
}
// Ghost caret name tags (VS Code Live Share): while a peer's caret is
// inside its fade window the App feeds a label here, painted on the
// visual row above the caret — below when the caret sits on the
// viewport's top row — at the caret's on-screen column. Painted after
// the row loop (a tag overlays a neighbouring row's content) but
// before sticky scroll, which floats above everything.
for (cl, cc, name, color) in &self.ghost_caret_labels {
let Some(caret_vidx) = visual_rows.iter().position(|r| {
matches!(r, VisRow::Text { line: li, start: s, end: e }
if *li == *cl && *cc >= *s
&& (*cc < *e || (*cc == *e && *e == self.line_char_len(*li))))
}) else {
continue;
};
let label_vidx = if caret_vidx > 0 {
caret_vidx - 1
} else {
caret_vidx + 1
};
if label_vidx >= visual_rows.len() {
continue;
}
// The caret's on-screen column: translate through the caret row's
// own inlay map, exactly as its ghost cell was painted.
let VisRow::Text {
start: caret_start,
end: caret_end,
..
} = visual_rows[caret_vidx]
else {
continue;
};
let hint_cap = caret_end.min(self.line_char_len(*cl));
let hint_cells: Vec<(usize, usize)> = self
.row_inlay_spans(*cl)
.iter()
.filter(|(hc, _)| *hc >= caret_start && *hc <= hint_cap)
.map(|(hc, l)| (*hc, l.chars().count()))
.collect();
let col_cells = cc + inlay_cells_before(&hint_cells, *cc);
if col_cells < caret_start {
continue;
}
let x0 = (col_cells - caret_start) as u16;
let y = inner.y + label_vidx as u16;
let style = Style::default()
.fg(Color::Black)
.bg(*color)
.add_modifier(Modifier::BOLD);
for (i, ch) in name.chars().enumerate() {
let x = x0 + i as u16;
if x >= text_width {
break;
}
buf[(text_x + x, y)].set_char(ch).set_style(style);
}
}
// Sticky scroll: pin the enclosing scope headers to the top of the
// viewport, overpainting the topmost content rows (VS Code's sticky
// widget floats over the content the same way). Non-wrap only; the app
// supplies the header lines outermost-first, already filtered to those
// scrolled above the viewport.
self.sticky_click_rows.clear();
if !wrap && !self.sticky_lines.is_empty() && inner.height > 1 {
let bg = self.theme.sticky_scroll_bg();
let text_w = (inner.x + inner.width).saturating_sub(text_x);
// Never paint over the caret's own row: a caret under the band
// cannot be seen at all. `caret_floor_row` keeps a scroll from
// parking it there, so this normally trims nothing; it still has to
// hold for the frames where the caret arrived some other way.
//
// The caret's PAINTED row, from the layout built above — counting
// buffer lines instead would overstate it whenever a collapsed fold
// or a comment box sits between the top of the viewport and the
// caret, and the band would paint straight over it. `None` means
// the caret's line is not painted at all (scrolled off, or hidden
// inside a fold), so nothing needs to be kept clear.
let rows_avail = (inner.height - 1) as usize;
let caret_row = self
.last_wrap_rows
.iter()
.position(|r| matches!(r, VisRow::Text { line, .. } if *line == self.cursor_row));
let max_rows = match caret_row {
Some(row) => rows_avail.min(row),
None => rows_avail,
};
let sticky = self.sticky_lines.clone();
for (i, &line) in sticky.iter().take(max_rows).enumerate() {
let y = inner.y + i as u16;
for x in inner.x..inner.x + inner.width {
buf[(x, y)].set_symbol(" ");
buf[(x, y)].set_style(Style::default().bg(bg));
}
let line_no = format!(
"{:>width$} ",
line + 1,
width = gutter_width.saturating_sub(1) as usize
);
buf.set_string(
inner.x,
y,
&line_no,
Style::default().fg(Color::DarkGray).bg(bg),
);
self.paint_highlighted_line(buf, text_x, y, text_w, line as usize);
self.sticky_click_rows.push((y, line));
}
}
if let Some(metrics) = scrollbar_metrics {
scrollbar::render_vertical(buf, metrics, self.focused, self.theme);
}
if let Some(metrics) = hbar_metrics {
scrollbar::render_horizontal(buf, metrics, self.focused, self.theme);
}
}
}
impl Editor {
/// Paint the rendered Markdown preview: the pre-built lines flow through
/// a wrapping `Paragraph` so paragraphs reflow with the pane, scrolled by
/// the preview's own offset, with a scrollbar over the wrapped height.
/// Rebuilds first when the buffer moved under the preview (a live edit in
/// a split, an external reload) so it always shows the current text.
fn render_markdown_preview(&mut self, inner: Rect, buf: &mut Buffer) {
let stale = self
.markdown_preview
.as_ref()
.is_some_and(|md| md.built_seq != self.edit_seq);
if stale {
let text = self.lines.join("\n");
let lines = crate::markdown::render_markdown(&text, self.theme, &mut self.registry);
if let Some(md) = self.markdown_preview.as_mut() {
md.lines = lines;
md.built_seq = self.edit_seq;
}
}
let Some(md) = self.markdown_preview.as_mut() else {
return;
};
// One-cell left margin; the right column stays free for the bar.
let text_area = Rect {
x: inner.x + 1,
width: inner.width.saturating_sub(3),
..inner
};
if text_area.width == 0 {
return;
}
let para = Paragraph::new(Text::from(md.lines.clone())).wrap(Wrap { trim: false });
let total = para.line_count(text_area.width);
let max_scroll = total.saturating_sub(inner.height as usize) as u16;
md.scroll = md.scroll.min(max_scroll);
para.scroll((md.scroll, 0)).render(text_area, buf);
if let Some(metrics) = scrollbar::vertical_metrics(
Rect {
x: inner.x + inner.width.saturating_sub(1),
y: inner.y,
width: 1,
height: inner.height,
},
total,
inner.height as usize,
md.scroll as usize,
) {
self.last_scrollbar = metrics.area;
scrollbar::render_vertical(buf, metrics, self.focused, self.theme);
}
}
/// Paint logical line `line_idx`'s syntax/semantic-highlighted text at row
/// `y` from column 0 (no horizontal scroll), clipped to `width`. Used by
/// the sticky-scroll bar to render pinned scope headers. Assumes the row
/// background is already filled by the caller.
fn paint_highlighted_line(
&self,
buf: &mut Buffer,
text_x: u16,
y: u16,
width: u16,
line_idx: usize,
) {
let Some(raw) = self.lines.get(line_idx) else {
return;
};
let empty: Vec<HiSpan> = Vec::new();
let line_spans = self.highlights.get(line_idx).unwrap_or(&empty);
let sem_spans = self.semantic_overlay.get(line_idx).unwrap_or(&empty);
let merged = merge_overlay(line_spans, sem_spans);
let raw_bytes = raw.len();
let clamped: Vec<HiSpan> = merged
.into_iter()
.filter_map(|mut sp| {
if sp.start >= raw_bytes {
return None;
}
sp.end = sp.end.min(raw_bytes);
Some(sp)
})
.collect();
let spans = build_line_spans(raw, &clamped);
buf.set_line(text_x, y, &Line::from(spans), width);
}
/// If `(col, row)` lands on a pinned sticky-scroll header, return the
/// logical line to jump to. `None` when the row is not a sticky header.
pub fn sticky_line_at(&self, col: u16, row: u16) -> Option<u32> {
if col < self.last_inner.x || col >= self.last_inner.x + self.last_inner.width {
return None;
}
self.sticky_click_rows
.iter()
.find(|&&(y, _)| y == row)
.map(|&(_, line)| line)
}
/// Absolute (column, row) of the editor's cursor in screen coordinates,
/// or `None` if the cursor is outside the visible viewport. Used by
/// `App::render` to position the host terminal's hardware caret.
pub fn cursor_screen_pos(&self) -> Option<(u16, u16)> {
if self.last_inner.height == 0 {
return None;
}
let text_x = self.last_inner.x + self.last_gutter_width + 1;
if self.wrap_enabled() {
// Find the visual row holding the cursor in the layout the last
// render captured (a logical line spans several wrapped rows). A
// column on a wrap boundary belongs to the next row's start.
let idx = self.last_wrap_rows.iter().position(|r| {
matches!(r, VisRow::Text { line, start, end }
if *line == self.cursor_row
&& self.cursor_col >= *start
&& (self.cursor_col < *end
|| (self.cursor_col == *end
&& *end == self.line_char_len(*line))))
})?;
let (_, start, end) = self.text_row(idx)?;
let visible_col = self.cursor_col - start;
if end == start || visible_col >= end - start {
return None;
}
return Some((text_x + visible_col as u16, self.last_inner.y + idx as u16));
}
// Non-wrap: one row per line, horizontally scrolled by `scroll_col`;
// comment boxes shift the rows below them, so map through the
// painted layout when one exists.
if self.cursor_row < self.scroll {
return None;
}
let row_in_view = if self.last_wrap_rows.is_empty() {
self.cursor_row - self.scroll
} else {
self.last_wrap_rows
.iter()
.position(|r| matches!(r, VisRow::Text { line, .. } if *line == self.cursor_row))?
};
if (row_in_view as u16) >= self.last_inner.height {
return None;
}
let text_width = self
.last_inner
.width
.saturating_sub(self.last_gutter_width + 2 + u16::from(self.last_scrollbar.width > 0));
if text_width == 0 || self.cursor_col < self.scroll_col {
return None;
}
// Inlay-hint cells before the caret shift it right. Strictly-before
// (`hc < cursor_col`, not `<=`): a caret AT a hint's anchor sits left
// of the hint, like VS Code, so typing there pushes the hint along.
let extra = self.inlay_cells_before_cursor(self.cursor_row, self.scroll_col);
let visible_col = self.cursor_col - self.scroll_col + extra;
if (visible_col as u16) >= text_width {
return None;
}
Some((
text_x + visible_col as u16,
self.last_inner.y + row_in_view as u16,
))
}
/// Hint cells spliced between `scroll_col` and the caret on `line`
/// (anchors strictly before `cursor_col`, at or past `scroll_col`).
fn inlay_cells_before_cursor(&self, line: usize, scroll_col: usize) -> usize {
self.row_inlay_spans(line)
.iter()
.filter(|(hc, _)| *hc >= scroll_col && *hc < self.cursor_col)
.map(|(_, l)| l.chars().count())
.sum()
}
/// Screen cell of the diff view's read-only caret (its selection head),
/// clamped to the viewport. The exact inverse of `diff_hit_test`: feed
/// it the same layout (`last_inner`, gutters, seam, scroll) so the
/// blinking caret lands on the cell a click there would map back to.
/// `None` when there's no diff, no caret, or the caret is scrolled out.
pub fn diff_caret_screen_pos(&self) -> Option<(u16, u16)> {
use crate::widgets::diff::DiffSide;
let diff = self.diff.as_ref()?;
if diff.unified {
return None;
}
let (side, row_idx, char_col) = diff.caret()?;
let inner = self.last_inner;
if inner.width < 16 || inner.height < 3 {
return None;
}
let body_top = inner.y + 1;
let body_height = inner.height.saturating_sub(2);
if body_height == 0 || row_idx < diff.scroll {
return None;
}
let vis_row = row_idx - diff.scroll;
if vis_row as u16 >= body_height {
return None;
}
let half = inner.width / 2;
if half < 8 {
return None;
}
let l_gutter = (diff.left_lines.len() + 1).to_string().len() as u16 + 1;
let r_gutter = (diff.right_lines.len() + 1).to_string().len() as u16 + 1;
let (text_x, text_w) = match side {
DiffSide::Left => (
inner.x + l_gutter + 2,
half.saturating_sub(l_gutter + 2 + 1),
),
DiffSide::Right => (
inner.x + half + 1 + r_gutter + 2,
(inner.width - (half + 1)).saturating_sub(r_gutter + 2),
),
};
if text_w == 0 || char_col < diff.scroll_x {
return None;
}
let vis_col = char_col - diff.scroll_x;
if vis_col as u16 >= text_w {
return None;
}
Some((text_x + vis_col as u16, body_top + vis_row as u16))
}
}
/// Overpaint every match of `needle` in `raw_line` with the search-match
/// style, honouring `opts` (case-sensitive / whole-word / regex). Delegates
/// to `split_for_highlight` so the highlight rule stays 1:1 with the
/// search-engine matcher; column conversion uses `chars().count()` over
/// the byte prefix to stay correct for Unicode.
/// Hint cells inserted at or before buffer column `c` on this row: the
/// display-column shift every overlay painter applies past inlay hints.
/// `hints` is the row's visible (anchor col, label cells) set.
fn inlay_cells_before(hints: &[(usize, usize)], c: usize) -> usize {
hints
.iter()
.filter(|(hc, _)| *hc <= c)
.map(|(_, n)| n)
.sum()
}
/// Hint cells strictly before buffer column `c`: the shift for a CARET at
/// `c`, which sits left of a hint anchored exactly there (a character at
/// `c` sits right of it — that is [`inlay_cells_before`]).
fn inlay_cells_strictly_before(hints: &[(usize, usize)], c: usize) -> usize {
hints.iter().filter(|(hc, _)| *hc < c).map(|(_, n)| n).sum()
}
/// The syntax-highlighted spans of `raw`'s character range `[from, to)`,
/// clipped out of the line's merged highlight spans. Paints the text
/// segments between spliced inlay-hint labels.
fn inlay_text_segment<'a>(
raw: &'a str,
merged: &[HiSpan],
from: usize,
to: usize,
) -> Vec<Span<'a>> {
let byte_from = byte_index_of_char(raw, from);
let byte_to = byte_index_of_char(raw, to);
let seg = &raw[byte_from..byte_to];
let seg_bytes = byte_to - byte_from;
let shifted: Vec<HiSpan> = shift_spans_for_view(merged, byte_from)
.into_iter()
.filter_map(|mut sp| {
if sp.start >= seg_bytes {
return None;
}
sp.end = sp.end.min(seg_bytes);
Some(sp)
})
.collect();
build_line_spans(seg, &shifted)
}
// Render helper: each argument is an independent painting input (buffer,
// geometry, text, styling); bundling them into a struct would add indirection
// without improving clarity.
#[allow(clippy::too_many_arguments)]
fn paint_search_highlight(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
raw_line: &str,
needle: &str,
opts: crate::widgets::search::SearchOpts,
scroll_col: usize,
active_match_on_line: Option<(usize, usize)>,
hints: &[(usize, usize)],
) {
if needle.is_empty() {
return;
}
let inactive_style = Style::default()
.fg(Color::Black)
.bg(Color::Rgb(0xff, 0xd7, 0x4a))
.add_modifier(Modifier::BOLD);
let active_style = Style::default()
.fg(Color::Black)
.bg(Color::Rgb(0xff, 0x8c, 0x2a))
.add_modifier(Modifier::BOLD);
let segments = crate::widgets::search::split_for_highlight(raw_line, needle, opts);
// `abs_col` tracks the absolute character index in the original line.
// Visible columns are `abs_col - scroll_col`, painted only when
// non-negative and inside `text_width`.
let mut abs_col: usize = 0;
for (chunk, is_match) in segments {
let chunk_cols = chunk.chars().count();
if is_match {
let is_active =
active_match_on_line.is_some_and(|(c, l)| c == abs_col && l == chunk_cols);
let style = if is_active {
active_style
} else {
inactive_style
};
for c in 0..chunk_cols {
let absolute = abs_col + c;
if absolute < scroll_col {
continue;
}
let col = (absolute + inlay_cells_before(hints, absolute) - scroll_col) as u16;
if col >= text_width {
break;
}
buf[(text_x + col, y)].set_style(style);
}
}
abs_col = abs_col.saturating_add(chunk_cols);
if abs_col >= scroll_col + text_width as usize {
break;
}
}
}
/// Paint VS Code-style "selection highlight": a muted blue box over every
/// occurrence of `needle` on this line. Matches the literal selected text
/// case-sensitively (like VS Code's `editor.selectionHighlight`). Only the
/// background is repainted, so syntax foreground colours show through. The
/// active selection itself is overpainted afterwards by
/// `paint_selection_band` with a brighter blue, giving the two-tone look.
// Render helper: same independent-inputs shape as paint_search_highlight.
#[allow(clippy::too_many_arguments)]
fn paint_selection_occurrences(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
raw_line: &str,
needle: &str,
scroll_col: usize,
hints: &[(usize, usize)],
) {
if needle.is_empty() {
return;
}
let bg = Color::Rgb(0x37, 0x61, 0x8e);
let needle_cols = needle.chars().count();
let mut search_from = 0usize;
while let Some(rel) = raw_line[search_from..].find(needle) {
let byte_start = search_from + rel;
let char_start = raw_line[..byte_start].chars().count();
for c in 0..needle_cols {
let absolute = char_start + c;
if absolute < scroll_col {
continue;
}
let col = (absolute + inlay_cells_before(hints, absolute) - scroll_col) as u16;
if col >= text_width {
break;
}
let cell = &mut buf[(text_x + col, y)];
cell.set_style(cell.style().bg(bg));
}
search_from = byte_start + needle.len();
}
}
/// Paint a software block cursor for a secondary multi-cursor caret at char
/// column `col_char` of row `y`. Uses a reversed bar so it reads as a caret
/// distinct from the host terminal's hardware caret on the primary cursor.
fn paint_block_cursor(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
col_char: usize,
scroll_col: usize,
) {
if col_char < scroll_col {
return;
}
let col = col_char - scroll_col;
if col as u16 >= text_width {
return;
}
let cell = &mut buf[(text_x + col as u16, y)];
cell.set_style(
Style::default()
.fg(Color::Black)
.bg(Color::Rgb(0xae, 0xc6, 0xff))
.add_modifier(Modifier::BOLD),
);
}
/// Paint one collaborator's caret cell in their color (multiplayer ghost
/// caret): same shape and clipping as `paint_block_cursor`, but tinted per
/// participant so everyone can see where the others are.
fn paint_ghost_caret(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
col_char: usize,
scroll_col: usize,
color: Color,
) {
if col_char < scroll_col {
return;
}
let col = col_char - scroll_col;
if col as u16 >= text_width {
return;
}
let cell = &mut buf[(text_x + col as u16, y)];
cell.set_style(
Style::default()
.fg(Color::Black)
.bg(color)
.add_modifier(Modifier::BOLD),
);
}
/// Tint the single bracket cell at char column `col_char` of row `y` with the
/// theme's bracket-match background, leaving the glyph's foreground intact so
/// the bracket stays readable. No-op when the cell is scrolled out of view.
fn paint_bracket_match(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
col_char: usize,
scroll_col: usize,
theme: crate::theme::Theme,
) {
if col_char < scroll_col {
return;
}
let col = (col_char - scroll_col) as u16;
if col >= text_width {
return;
}
let cell = &mut buf[(text_x + col, y)];
cell.set_style(cell.style().bg(theme.bracket_match_bg()));
}
/// Apply the selection background colour to columns `[start_char..end_char)`
/// of row `y`, where columns are character indices within the editor's text
/// area. Clamps to the visible width.
/// The background tint for a row inside a merge-conflict block, or `None`
/// outside one. Header / footer marker rows tint stronger than their
/// content, mirroring VS Code's current(green) / incoming(blue) scheme; the
/// diff3 base section and the `=======` separator sit on a neutral grey.
/// Fixed dark tints, like the selection band: both bundled themes are dark,
/// and the hues are semantic (green = yours, blue = theirs), not accents.
fn conflict_row_tint(blocks: &[crate::merge::ConflictBlock], row: usize) -> Option<Color> {
let block = blocks.iter().find(|b| b.contains(row))?;
let ours_end = block.base_start.unwrap_or(block.sep);
Some(if row == block.ours_start {
Color::Rgb(0x2a, 0x4f, 0x33)
} else if row < ours_end {
Color::Rgb(0x1b, 0x33, 0x22)
} else if row < block.sep {
Color::Rgb(0x2a, 0x2a, 0x2a)
} else if row == block.sep {
Color::Rgb(0x28, 0x28, 0x28)
} else if row == block.theirs_end {
Color::Rgb(0x1f, 0x41, 0x66)
} else {
Color::Rgb(0x16, 0x2b, 0x44)
})
}
/// Tint every cell of one text row, keeping the glyphs and their colours.
fn paint_full_row_bg(buf: &mut Buffer, text_x: u16, y: u16, text_width: u16, bg: Color) {
for col in 0..text_width {
let x = text_x + col;
let cell = &mut buf[(x, y)];
cell.set_style(cell.style().bg(bg));
}
}
fn paint_selection_band(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
start_char: usize,
end_char: usize,
) {
let bg = Color::Rgb(0x26, 0x4f, 0x78);
let s = start_char.min(text_width as usize);
let e = end_char.min(text_width as usize);
if e <= s {
return;
}
for col in s..e {
let x = text_x + col as u16;
let cell = &mut buf[(x, y)];
cell.set_style(cell.style().bg(bg));
}
}
/// Paint a coloured underline across `[start_char, end_char)` of a rendered
/// row to mark an LSP diagnostic. The colour encodes severity, matching VS
/// Code's palette (red error, yellow warning, teal info/hint). Uses the
/// terminal's separate underline colour so the glyph's foreground (its syntax
/// or semantic colour) is left untouched, the way VS Code's squiggle sits
/// under unchanged text.
fn paint_diagnostic_underline(
buf: &mut Buffer,
text_x: u16,
y: u16,
text_width: u16,
start_char: usize,
end_char: usize,
severity: crate::lsp::manager::DiagnosticSeverity,
) {
use crate::lsp::manager::DiagnosticSeverity;
let color = match severity {
DiagnosticSeverity::Error => Color::Rgb(0xf1, 0x4c, 0x4c),
DiagnosticSeverity::Warning => Color::Rgb(0xcc, 0xa7, 0x00),
DiagnosticSeverity::Information | DiagnosticSeverity::Hint => Color::Rgb(0x3b, 0x9e, 0xff),
};
let s = start_char.min(text_width as usize);
let e = end_char.min(text_width as usize);
if e <= s {
return;
}
for col in s..e {
let x = text_x + col as u16;
let cell = &mut buf[(x, y)];
cell.set_style(
cell.style()
.add_modifier(Modifier::UNDERLINED)
.underline_color(color),
);
}
}
/// Multi-buffer editor: a stack of `Editor` instances with a single active
/// one, plus a 1-row clickable tab strip rendered above the active editor.
/// `Deref`/`DerefMut` aim at the active editor so existing call sites that
/// were written for a single `Editor` continue to work without rewrites.
/// One segment of the breadcrumb bar (VS Code's editor breadcrumbs): a path
/// folder, the file name, or an enclosing symbol. `target` is the caret
/// position a click jumps to; `None` for the informational path/file crumbs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Crumb {
pub label: String,
pub target: Option<(u32, u32)>,
}
/// A rendered breadcrumb crumb's hit-test span: `(x_start, width, jump target)`.
type BreadcrumbRange = (u16, u16, Option<(u32, u32)>);
pub struct EditorTabs {
pub editors: Vec<Editor>,
active: usize,
/// Breadcrumb segments for the active file, set by `App` each frame from
/// the workspace-relative path and the outline scope chain at the caret.
/// Empty hides the bar (e.g. the welcome screen or an unsaved buffer).
pub breadcrumbs: Vec<Crumb>,
/// Per-crumb `(x_start, width, target)` recorded by the most recent render,
/// so `App::handle_mouse` can map a click on the breadcrumb row to a jump.
breadcrumb_ranges: Vec<BreadcrumbRange>,
/// Screen row the breadcrumb bar rendered on, or `None` when it was hidden.
breadcrumb_y: Option<u16>,
/// Per-tab on-screen `(x_start, width)` recorded by the most recent
/// render. `tab_at(col, row)` reads this to map mouse clicks to tab
/// indices.
tab_screen_ranges: Vec<(u16, u16)>,
/// Per-tab absolute column where the close `\u{2715}` glyph lives.
/// `0` means "no close button rendered for this tab" (e.g. when the
/// tab is the only one — closing it isn't allowed so we hide the X).
tab_close_x: Vec<u16>,
tab_strip_y: u16,
/// The full pane area (tab strip + body) from the most recent render.
/// Used by `App::handle_mouse` for hit-testing — the active editor's
/// own `last_area` only covers the body below the strip.
pub last_full_area: Rect,
/// Source of truth for the search-match term that's currently being
/// painted in every tab's body. Each `Editor.search_highlight` is a
/// copy kept in sync with this; storing it here lets a freshly-created
/// editor (e.g. after closing the previous tab and opening a new file
/// from a search hit) inherit the term without the App needing to
/// re-call `set_search_highlight` after every open.
search_highlight_term: Option<String>,
/// Toggle state matching `search_highlight_term`. Same propagation
/// strategy: every newly-created editor inherits these.
search_highlight_opts: crate::widgets::search::SearchOpts,
/// Last pointer cell, fed in by `App` before each render so the tab
/// strip can paint a hover lift on the tab body under the pointer and a
/// pill behind the close `\u{2715}` cell when the pointer rests on it.
/// `None` when the pointer is off-screen or unknown.
pub hover_pointer: Option<(u16, u16)>,
}
impl EditorTabs {
pub fn new() -> Self {
Self {
editors: vec![Editor::new()],
active: 0,
breadcrumbs: Vec::new(),
breadcrumb_ranges: Vec::new(),
breadcrumb_y: None,
tab_screen_ranges: Vec::new(),
tab_close_x: Vec::new(),
tab_strip_y: 0,
last_full_area: Rect::default(),
search_highlight_term: None,
search_highlight_opts: crate::widgets::search::SearchOpts::default(),
hover_pointer: None,
}
}
pub fn tab_count(&self) -> usize {
self.editors.len()
}
/// Set (or clear) the search-match highlight term + opts for every
/// open tab, so opening another file from search keeps the same query
/// lit, clearing the search box wipes the highlights, and toggling a
/// search mode (case / whole-word / regex) re-paints the file with
/// the matching rule. Also persists term + opts so editors created
/// after this call (e.g. after a close + reopen) inherit them.
pub fn set_search_highlight(
&mut self,
term: Option<String>,
opts: crate::widgets::search::SearchOpts,
) {
let normalised = term.filter(|s| !s.is_empty());
let cleared = normalised.is_none();
self.search_highlight_term = normalised.clone();
self.search_highlight_opts = opts;
for ed in &mut self.editors {
ed.search_highlight = normalised.clone();
ed.search_highlight_opts = opts;
if cleared {
ed.active_search_match = None;
}
}
}
pub fn active_index(&self) -> usize {
self.active
}
/// The tab strip label for the tab at `idx` (file name, diff pair, or
/// "untitled"), for status messages. Empty string for an out-of-range idx.
pub fn tab_display_label(&self, idx: usize) -> String {
self.editors.get(idx).map(tab_label).unwrap_or_default()
}
/// The on-disk path backing the tab at `idx`, if any. `None` for an
/// out-of-range index or a blank/untitled buffer. Used to seed the tab
/// context-menu's path-bearing actions (Reveal in Finder, Copy Path).
pub fn tab_path(&self, idx: usize) -> Option<PathBuf> {
self.editors.get(idx).and_then(|e| e.path.clone())
}
pub fn iter_tabs(&self) -> impl Iterator<Item = &Editor> {
self.editors.iter()
}
pub fn select(&mut self, idx: usize) -> bool {
if idx >= self.editors.len() {
return false;
}
self.editors[self.active].focused = false;
self.active = idx;
self.editors[self.active].focused = true;
true
}
/// Sweep EVERY open tab (not just the active one) for external on-disk
/// changes. Clean tabs are silently reloaded; dirty tabs are flagged as
/// conflicts. This is the core of the FS-sync invariant: a file open in
/// a background tab must reflect disk reality just like the focused one.
/// `skip` exempts paths from the sweep: a collab guest never reloads
/// shared files from disk — its replica is authoritative, and the
/// owner's own reload reaches it as ops (docs/MULTIPLAYER.md, Phase D).
pub fn reload_externally_changed_tabs(
&mut self,
skip: &dyn Fn(&Path) -> bool,
) -> ExternalReloadReport {
let mut report = ExternalReloadReport::default();
for ed in &mut self.editors {
// Diff/image/sheet views don't carry an editable text buffer to
// reload, and a path-less blank tab has nothing to sync.
if ed.path.is_none() || ed.diff.is_some() {
continue;
}
if ed.path.as_deref().is_some_and(skip) {
continue;
}
let path = ed.path.clone();
match ed.reload_or_flag_conflict() {
ExternalChange::Reloaded => {
if let Some(p) = path {
report.reloaded.push(p);
}
}
ExternalChange::Conflict => {
if let Some(p) = path {
report.conflicts.push(p);
}
}
ExternalChange::ReloadFailed => {
if let Some(p) = path {
report.failed.push(p);
}
}
ExternalChange::Unchanged => {}
}
}
report
}
/// Discard unsaved edits and reload the given paths from disk — the
/// "Reload" resolution of an external-change conflict. Returns the paths
/// actually reverted (a tab must still be open on each).
pub fn revert_paths_to_disk(&mut self, paths: &[PathBuf]) -> Vec<PathBuf> {
let mut reverted = Vec::new();
for ed in &mut self.editors {
let Some(p) = ed.path.clone() else { continue };
if paths.iter().any(|q| q == &p) && ed.revert_to_disk().is_ok() {
reverted.push(p);
}
}
reverted
}
/// If any tab currently points at `old`, repoint it to `new`. The on-
/// disk file has already been moved; this only updates the in-memory
/// path so subsequent saves and the tab label track the new name.
pub fn rename_open_path(&mut self, old: &Path, new: &Path) {
for e in &mut self.editors {
if e.path.as_deref() == Some(old) {
e.path = Some(new.to_path_buf());
// Re-anchor the disk stamp to the new path so the rename
// isn't mistaken for an external content change on the next
// FS-sync sweep.
e.mark_synced_with_disk();
}
}
}
/// Open `path` in a brand-new tab inserted directly after the active
/// one, then make that new tab active. Returns the result of the
/// underlying `Editor::open` so the caller can surface errors.
pub fn open_in_new_tab(&mut self, path: &Path) -> Result<()> {
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.open(path)?;
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Test-only / disk-less helper: insert a tab whose path is set but
/// whose contents are empty. Production code should call
/// `open_in_new_tab` so the file is actually loaded from disk.
pub fn add_tab_with_path(&mut self, path: PathBuf) {
let mut e = Editor::new();
e.path = Some(path);
e.focused = self.editors[self.active].focused;
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
}
/// Close the currently active tab. Refuses (returns false) when only one
/// tab remains — closing the last would leave the editor pane empty.
pub fn close_active(&mut self) -> bool {
self.close_tab(self.active)
}
/// Close the tab at `idx`. When more than one tab is open the tab is
/// removed and `self.active` is shifted so it still points at a valid
/// tab. When this is the last remaining tab it is reset to the blank
/// just-launched state instead of being removed (so the editor pane
/// always has at least one buffer to render). Returns false only on an
/// out-of-range index.
pub fn close_tab(&mut self, idx: usize) -> bool {
if idx >= self.editors.len() {
return false;
}
if self.editors.len() == 1 {
let was_focused = self.editors[0].focused;
let mut fresh = Editor::new();
fresh.focused = was_focused;
self.editors[0] = fresh;
self.active = 0;
return true;
}
self.editors.remove(idx);
if self.active > idx {
self.active -= 1;
} else if self.active >= self.editors.len() {
self.active = self.editors.len() - 1;
}
for (i, ed) in self.editors.iter_mut().enumerate() {
ed.focused = i == self.active;
}
true
}
/// Close every tab whose index ≠ `keep_idx`, except pinned tabs, which
/// always survive (VS Code "Close Others" never closes a pinned tab). The
/// kept tab stays active. Returns how many tabs were actually removed (0
/// when `keep_idx` is out of range or nothing else is closeable). Mirrors
/// VS Code's "Close Others" context-menu action.
pub fn close_others(&mut self, keep_idx: usize) -> usize {
if keep_idx >= self.editors.len() || self.editors.len() <= 1 {
return 0;
}
let before = self.editors.len();
let mut new_active = 0;
let mut kept: Vec<Editor> = Vec::with_capacity(before);
for (i, ed) in std::mem::take(&mut self.editors).into_iter().enumerate() {
if i == keep_idx {
new_active = kept.len();
kept.push(ed);
} else if ed.pinned {
kept.push(ed);
}
}
let removed = before - kept.len();
self.editors = kept;
self.active = new_active;
for (i, ed) in self.editors.iter_mut().enumerate() {
ed.focused = i == self.active;
}
removed
}
/// Close every tab whose index > `from_idx`, except pinned tabs, which
/// always survive (VS Code "Close to the Right" never closes a pinned
/// tab). The tab at `from_idx` stays active; tabs to the left are
/// untouched. Returns the number of tabs removed. Matches VS Code's
/// "Close to the Right".
pub fn close_to_right(&mut self, from_idx: usize) -> usize {
if from_idx >= self.editors.len() {
return 0;
}
let before = self.editors.len();
let old_active = self.active;
let mut new_active: Option<usize> = None;
let mut pivot_pos = 0;
let mut kept: Vec<Editor> = Vec::with_capacity(before);
for (i, ed) in std::mem::take(&mut self.editors).into_iter().enumerate() {
if i <= from_idx || ed.pinned {
if i == from_idx {
pivot_pos = kept.len();
}
if i == old_active {
new_active = Some(kept.len());
}
kept.push(ed);
}
}
let removed = before - kept.len();
self.editors = kept;
// Keep the previously-active tab active when it survived; otherwise
// fall back to the pivot tab's new position.
self.active = new_active.unwrap_or(pivot_pos);
for (i, ed) in self.editors.iter_mut().enumerate() {
ed.focused = i == self.active;
}
removed
}
/// Close every tab, resetting the editor pane to the single blank
/// just-launched state — mirrors `close_tab` on the last remaining
/// tab. Returns how many tabs were collapsed away (always ≥ 1 when
/// the editor had any content). Matches VS Code's "Close All".
pub fn close_all(&mut self) -> usize {
let n = self.editors.len();
let was_focused = self.editors[self.active].focused;
let mut fresh = Editor::new();
fresh.focused = was_focused;
self.editors = vec![fresh];
self.active = 0;
n
}
/// Close every saved (non-dirty) tab, keeping any with unsaved changes.
/// When no dirty tab remains the pane resets to a single blank buffer,
/// mirroring `close_all`; otherwise the surviving dirty tabs are kept in
/// order and the first becomes active. Returns how many tabs were removed
/// (0 when every open tab is dirty). Matches VS Code's "Close Saved"
/// (`workbench.action.closeUnmodifiedEditors`).
pub fn close_saved(&mut self) -> usize {
let before = self.editors.len();
let was_focused = self.editors[self.active].focused;
let mut kept: Vec<Editor> = std::mem::take(&mut self.editors)
.into_iter()
.filter(|e| e.dirty)
.collect();
let removed = before - kept.len();
if kept.is_empty() {
let mut fresh = Editor::new();
fresh.focused = was_focused;
kept.push(fresh);
}
self.editors = kept;
self.active = 0;
for (i, ed) in self.editors.iter_mut().enumerate() {
ed.focused = i == 0;
}
removed
}
/// Map a mouse cell `(col, row)` to a tab index, or `None` if the click
/// missed every tab. Uses the on-screen ranges captured during the most
/// recent render.
pub fn tab_at(&self, col: u16, row: u16) -> Option<usize> {
if row != self.tab_strip_y {
return None;
}
for (i, &(x, w)) in self.tab_screen_ranges.iter().enumerate() {
if col >= x && col < x.saturating_add(w) {
return Some(i);
}
}
None
}
/// Full filesystem path for a tab, used as the hover tooltip that
/// disambiguates same-named files (the tab label only shows the bare
/// `file_name()`). Returns `None` for an unsaved "untitled" buffer
/// since there is nothing to disambiguate. Diff tabs report both
/// sides so the viewer can tell which revisions are being compared.
pub fn tab_full_path(&self, idx: usize) -> Option<String> {
let e = self.editors.get(idx)?;
if let Some(diff) = e.diff.as_ref() {
let l = diff.left_path.to_string_lossy().into_owned();
let r_is_real = diff.right_path != Path::new("/dev/null")
&& !diff.right_path.as_os_str().is_empty();
if r_is_real {
let r = diff.right_path.to_string_lossy().into_owned();
return Some(format!("{l} \u{2194} {r}"));
}
return Some(l);
}
e.path.as_ref().map(|p| p.to_string_lossy().into_owned())
}
pub fn tab_screen_x(&self, idx: usize) -> Option<(u16, u16)> {
self.tab_screen_ranges.get(idx).copied()
}
pub fn close_screen_x(&self, idx: usize) -> Option<u16> {
self.tab_close_x.get(idx).copied().filter(|&x| x != 0)
}
/// Map a mouse cell to the tab whose close `\u{2715}` glyph occupies it,
/// or `None` if the click missed every close button. Used by the App's
/// mouse handler to short-circuit ahead of `tab_at` so a click on the X
/// closes the tab instead of selecting it.
pub fn close_at(&self, col: u16, row: u16) -> Option<usize> {
if row != self.tab_strip_y {
return None;
}
self.tab_close_x.iter().position(|&x| x != 0 && x == col)
}
/// Index of the first tab whose `path` matches `target` either by
/// literal equality or by canonicalised equality (so symlink + relative
/// path aliases dedupe to the same tab). Returns `None` if no tab is
/// currently holding that file.
pub fn find_tab_with_path(&self, target: &Path) -> Option<usize> {
self.find_tab_matching(target, |_| true)
}
/// If `path` is open in a tab, apply LSP rename `edits` to that buffer
/// in-memory (one undo step, marked dirty) and return the count applied.
/// `None` when no tab holds the file, so the caller can edit it on disk.
pub fn apply_rename_to_open_tab(
&mut self,
path: &Path,
edits: &[TextSpanEdit],
) -> Option<usize> {
let idx = self.find_tab_with_path(path)?;
Some(self.editors[idx].apply_span_edits(edits))
}
fn find_tab_matching(&self, target: &Path, extra: impl Fn(&Editor) -> bool) -> Option<usize> {
let canon_target = target.canonicalize().ok();
self.editors.iter().position(|e| {
if !extra(e) {
return false;
}
let Some(p) = e.path.as_ref() else {
return false;
};
if p == target {
return true;
}
match (canon_target.as_ref(), p.canonicalize().ok()) {
(Some(a), Some(b)) => *a == b,
_ => false,
}
})
}
pub fn preview_index(&self) -> Option<usize> {
self.editors.iter().position(|e| e.preview)
}
/// Mark the active tab as pinned (no longer the preview slot). Called
/// when the user double-clicks a file, hits Ctrl+Enter, or starts typing
/// inside a preview tab.
pub fn pin_active(&mut self) {
self.editors[self.active].preview = false;
}
/// True when the tab at `idx` is the replaceable preview slot. Drives the
/// tab menu's "Keep Open" entry, which only makes sense for a preview tab.
pub fn is_preview(&self, idx: usize) -> bool {
self.editors.get(idx).is_some_and(|e| e.preview)
}
/// VS Code "Keep Open": promote the tab at `idx` out of the preview slot
/// so a subsequent single-click open no longer replaces it. Returns true
/// when a preview tab was actually promoted (false if it was already
/// permanent or the index is out of range).
pub fn keep_open(&mut self, idx: usize) -> bool {
match self.editors.get_mut(idx) {
Some(e) if e.preview => {
e.preview = false;
true
}
_ => false,
}
}
/// True when the tab at `idx` is pinned. Drives the tab menu's
/// "Pin"/"Unpin" label and the close-cell glyph (pin vs `\u{2715}`).
pub fn is_pinned(&self, idx: usize) -> bool {
self.editors.get(idx).is_some_and(|e| e.pinned)
}
/// VS Code "Pin"/"Unpin" the tab at `idx`. Returns the tab's new pinned
/// state. Pinning clears the preview flag (a pinned tab is never the
/// replaceable preview slot) and reorders the strip so pinned tabs stay
/// leftmost: a stable partition lands a newly-pinned tab at the end of the
/// pinned block and a newly-unpinned tab at the front of the unpinned
/// block, matching VS Code. The active tab follows its editor across the
/// reorder.
pub fn toggle_pin(&mut self, idx: usize) -> bool {
if idx >= self.editors.len() {
return false;
}
let now_pinned = !self.editors[idx].pinned;
self.editors[idx].pinned = now_pinned;
if now_pinned {
self.editors[idx].preview = false;
}
// Stable partition: pinned first (keeping their relative order), then
// unpinned (keeping theirs). `sort_by_key` is stable, so the two blocks
// preserve order and only the toggled tab crosses the boundary.
let n = self.editors.len();
let mut order: Vec<usize> = (0..n).collect();
order.sort_by_key(|&i| !self.editors[i].pinned);
let new_active = order.iter().position(|&i| i == self.active).unwrap_or(0);
let mut slots: Vec<Option<Editor>> = self.editors.drain(..).map(Some).collect();
self.editors = order.iter().map(|&i| slots[i].take().unwrap()).collect();
self.active = new_active;
for (i, ed) in self.editors.iter_mut().enumerate() {
ed.focused = i == self.active;
}
now_pinned
}
/// Remove and return the active editor (for a Move to another group). When
/// it was the only tab the group falls back to a single blank tab (like
/// closing the last tab), so the group stays renderable; the caller can
/// detect that empty state via [`Self::is_blank_initial`] and prune it.
pub fn take_active_editor(&mut self) -> Editor {
let ed = self.editors.remove(self.active);
if self.editors.is_empty() {
self.editors.push(Editor::new());
self.active = 0;
} else if self.active >= self.editors.len() {
self.active = self.editors.len() - 1;
}
for (i, e) in self.editors.iter_mut().enumerate() {
e.focused = i == self.active;
}
ed
}
/// Add `ed` (moved from another group) and make it active. A blank-initial
/// group is replaced in place so a Move never leaves a stray blank tab
/// beside the moved editor.
pub fn push_editor(&mut self, mut ed: Editor) {
if self.is_blank_initial() {
ed.focused = true;
self.editors[0] = ed;
self.active = 0;
return;
}
self.editors.push(ed);
self.active = self.editors.len() - 1;
for (i, e) in self.editors.iter_mut().enumerate() {
e.focused = i == self.active;
}
}
/// VS Code "preview tab" semantics: open `path` in the single
/// replaceable preview slot. If the file is already in some tab, just
/// switch to it. Otherwise reuse the existing preview slot, or create a
/// fresh preview tab next to the active one when none exists.
pub fn open_preview(&mut self, path: &Path) -> Result<()> {
if let Some(idx) = self.find_tab_with_path(path) {
// Switching to an already-open file: if a stale preview tab
// exists for some OTHER file, drop it so the user doesn't
// accumulate ghost tabs from quick "peek" navigations.
if let Some(prev) = self.preview_index()
&& prev != idx
{
self.editors.remove(prev);
let new_idx = if idx > prev { idx - 1 } else { idx };
// Bypass `select` here because the removal may have left
// `self.active` pointing past the end (when the active
// tab was the preview we just dropped).
self.active = new_idx;
for (i, ed) in self.editors.iter_mut().enumerate() {
ed.focused = i == new_idx;
}
return Ok(());
}
self.select(idx);
return Ok(());
}
if let Some(idx) = self.preview_index() {
self.select(idx);
self.editors[idx].open(path)?;
self.editors[idx].preview = true;
return Ok(());
}
if self.is_blank_initial() {
let active = self.active;
self.editors[active].open(path)?;
self.editors[active].preview = true;
self.editors[active].search_highlight = self.search_highlight_term.clone();
self.editors[active].search_highlight_opts = self.search_highlight_opts;
return Ok(());
}
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.open(path)?;
e.preview = true;
e.search_highlight = self.search_highlight_term.clone();
e.search_highlight_opts = self.search_highlight_opts;
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Open a side-by-side diff of two files in a fresh pinned tab next
/// to the active one. The new tab is read-only: edits, save, and the
/// text-rendering path are all bypassed via the `diff: Some(...)` flag
/// on the underlying Editor.
pub fn open_diff(&mut self, left: &Path, right: &Path) -> Result<()> {
let left_text =
std::fs::read_to_string(left).with_context(|| format!("reading {}", left.display()))?;
let right_text = std::fs::read_to_string(right)
.with_context(|| format!("reading {}", right.display()))?;
let left_lines: Vec<String> = left_text.lines().map(str::to_string).collect();
let right_lines: Vec<String> = right_text.lines().map(str::to_string).collect();
let mut data = crate::widgets::diff::DiffData::build(
left.to_path_buf(),
right.to_path_buf(),
left_lines,
right_lines,
);
data.left_is_real_file = true;
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.preview = false;
// The path is set so close-by-path lookups work; `diff` being Some
// is what diverts the renderer onto the side-by-side path.
e.path = Some(left.to_path_buf());
e.diff = Some(data);
// Reuse the blank-initial slot if the editor pane was empty;
// otherwise insert a new tab next to the active one.
if self.is_blank_initial() {
self.editors[self.active] = e;
return Ok(());
}
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Open a side-by-side diff between an in-memory `left_text` (e.g.
/// the HEAD version of a file) and the working-tree file at `right`.
/// The left "path" is purely a label; nothing reads from it on disk.
/// Used by the Source Control panel to show working-tree-vs-HEAD
/// diffs when the user clicks a Modified entry.
pub fn open_head_diff_with_text(
&mut self,
left_label: PathBuf,
left_text: &str,
right: &Path,
left_is_git_head: bool,
) -> Result<()> {
let right_text = std::fs::read_to_string(right)
.with_context(|| format!("reading {}", right.display()))?;
let left_lines: Vec<String> = left_text.lines().map(str::to_string).collect();
let right_lines: Vec<String> = right_text.lines().map(str::to_string).collect();
let mut data = crate::widgets::diff::DiffData::build_with_byte_check(
left_label,
right.to_path_buf(),
left_lines,
right_lines,
Some(left_text),
Some(&right_text),
);
// Park the viewport on the first change hunk so the user lands on
// the first edit instead of reading through unchanged leading
// lines. Identical files stay at scroll 0.
if let Some(row) = data.first_change_row() {
data.scroll_to_row(row);
}
data.left_is_git_head = left_is_git_head;
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.preview = false;
e.path = Some(right.to_path_buf());
e.diff = Some(data);
if self.is_blank_initial() {
self.editors[self.active] = e;
return Ok(());
}
// If a tab is already showing this path (as a plain file or a
// diff), reuse it so we don't pile up duplicate tabs as the
// user clicks through the change list — unless it holds unsaved
// edits, which replacing wholesale would silently destroy.
if let Some(idx) = self.find_tab_with_path(right)
&& !self.editors[idx].dirty
{
self.editors[idx] = e;
self.select(idx);
return Ok(());
}
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Open a single-pane unified diff that paints every line of
/// `head_text` as a removed line. `path` is the workspace-relative
/// path that no longer exists on disk; it doubles as the display
/// label in the diff header and as the tab dedup key so repeated
/// clicks on the same Source-Control row reuse the tab rather than
/// stacking new ones.
pub fn open_deleted_diff_with_text(&mut self, path: &Path, head_text: &str) -> Result<()> {
let data =
crate::widgets::diff::DiffData::build_unified_deletion(path.to_path_buf(), head_text);
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.preview = false;
e.path = Some(path.to_path_buf());
e.diff = Some(data);
if self.is_blank_initial() {
self.editors[self.active] = e;
return Ok(());
}
// Same dirty guard as `open_head_diff_with_text`: a deleted-on-disk
// file can still hold unsaved edits in its tab.
if let Some(idx) = self.find_tab_with_path(path)
&& !self.editors[idx].dirty
{
self.editors[idx] = e;
self.select(idx);
return Ok(());
}
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Open a side-by-side view of raw `git diff` text (e.g. the stdout
/// of `git diff --staged` or `git diff <branch>`). `label` is a
/// synthetic path used as the tab title and as the dedup key so a
/// repeat invocation reuses the existing tab instead of stacking new
/// ones. The text is parsed into separate left/right streams so the
/// standard two-column renderer takes over — every `+`/`-` pair in a
/// hunk lines up horizontally instead of zigzagging vertically.
pub fn open_git_diff_side_by_side(&mut self, label: &Path, raw_diff: &str) -> Result<()> {
let data = crate::widgets::diff::DiffData::build_side_by_side_from_git_text(
label.to_path_buf(),
raw_diff,
);
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.preview = false;
e.path = Some(label.to_path_buf());
e.diff = Some(data);
if self.is_blank_initial() {
self.editors[self.active] = e;
return Ok(());
}
if let Some(idx) = self.find_tab_with_path(label) {
self.editors[idx] = e;
self.select(idx);
return Ok(());
}
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Open arbitrary text in a scratch tab labelled `label` (no file on
/// disk). Used by "Show Git Output" to surface the git command log. The
/// tab has no `disk_stamp`, so the FS-sync layer never tries to reload
/// or overwrite it.
pub fn open_text_buffer(&mut self, label: &Path, text: &str) -> Result<()> {
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.preview = false;
e.path = Some(label.to_path_buf());
e.lines = split_into_lines(text);
if e.lines.is_empty() {
e.lines.push(String::new());
}
if self.is_blank_initial() {
self.editors[self.active] = e;
return Ok(());
}
if let Some(idx) = self.find_tab_with_path(label) {
self.editors[idx] = e;
self.select(idx);
return Ok(());
}
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// Pinned-tab open: if the file is already in some tab, pin and switch
/// to it. Otherwise create a fresh pinned tab next to the active one.
/// Used by double-click in the explorer and Ctrl+Enter on a tree row.
pub fn open_pinned(&mut self, path: &Path) -> Result<()> {
// A diff tab can carry the working file's `path` yet is not an
// editable view of it, so skip diffs here: Enter on a diff (or a
// double-click) opens the real file beside the diff rather than just
// re-selecting the diff tab.
if let Some(idx) = self.find_tab_matching(path, |e| e.diff.is_none()) {
self.editors[idx].preview = false;
self.select(idx);
return Ok(());
}
if self.is_blank_initial() {
let active = self.active;
self.editors[active].open(path)?;
self.editors[active].preview = false;
self.editors[active].search_highlight = self.search_highlight_term.clone();
self.editors[active].search_highlight_opts = self.search_highlight_opts;
return Ok(());
}
let mut e = Editor::new();
e.focused = self.editors[self.active].focused;
e.open(path)?;
e.preview = false;
e.search_highlight = self.search_highlight_term.clone();
e.search_highlight_opts = self.search_highlight_opts;
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
Ok(())
}
/// True iff the editor is in its just-launched state: a single tab with
/// no file loaded and no edits. `App::render` uses this to swap the
/// editor pane for the welcome screen, and `open_preview` /
/// `open_pinned` use it to reuse the placeholder tab rather than stack
/// a new one on top.
pub fn is_blank_initial(&self) -> bool {
self.editors.len() == 1
&& self.editors[0].path.is_none()
&& !self.editors[0].dirty
&& self.editors[0].lines.iter().all(|l| l.is_empty())
}
/// Test-only helper that mirrors `open_preview` without going through
/// the disk: insert a preview tab whose path is set but whose buffer is
/// empty. Used by unit tests to exercise preview-slot bookkeeping in
/// isolation from filesystem I/O.
pub fn add_preview_tab_with_path(&mut self, path: PathBuf) {
let mut e = Editor::new();
e.path = Some(path);
e.preview = true;
e.focused = self.editors[self.active].focused;
let pos = self.active + 1;
self.editors.insert(pos, e);
self.editors[self.active].focused = false;
self.active = pos;
}
/// Test-only helper for the "single-click on a different file when a
/// preview tab already exists" path: rewrite the existing preview tab's
/// path without changing tab count.
pub fn repoint_preview_to(&mut self, path: PathBuf) {
if let Some(idx) = self.preview_index() {
self.editors[idx].path = Some(path);
}
}
}
impl Default for EditorTabs {
fn default() -> Self {
Self::new()
}
}
impl Deref for EditorTabs {
type Target = Editor;
fn deref(&self) -> &Editor {
&self.editors[self.active]
}
}
impl DerefMut for EditorTabs {
fn deref_mut(&mut self) -> &mut Editor {
&mut self.editors[self.active]
}
}
// Tab-strip chrome (strip, tab bodies, hover lift, close pill) is per-theme
// data: `Theme::tab_*` carries the two built-ins' historical constants and
// derives colors for every manifest theme, so the strip follows the selected
// theme instead of staying VS-Code navy. Only the label foregrounds stay
// fixed: this muted grey and white are legible on all dark themes.
const TAB_INACTIVE_FG: Color = Color::Rgb(0x9d, 0xa5, 0xb4);
const TAB_ACTIVE_FG: Color = Color::White;
impl Widget for &mut EditorTabs {
fn render(self, area: Rect, buf: &mut Buffer) {
self.last_full_area = area;
if area.height == 0 || area.width == 0 {
// Ctrl+Shift+J maximises the terminal pane, which collapses
// the editor area to height 0. The inner editor's `render`
// is never reached on this branch, so its `last_area` would
// otherwise keep the rectangle from the pre-maximise frame
// and `App::handle_mouse`'s `in_editor` hit-test
// (rect_contains(self.editor.last_area, ...)) would still
// win against the terminal in the dispatch chain at
// app.rs:7229 / 7336 — clicks meant to begin a terminal
// selection get routed to the editor's mouse_down and the
// file caret jumps instead. Zero the inner editor's
// hit-test rectangle here so the terminal pane wins
// unambiguously while the editor is collapsed.
for ed in self.editors.iter_mut() {
ed.last_area = Rect {
x: area.x,
y: area.y,
width: area.width,
height: 0,
};
ed.last_inner = ed.last_area;
ed.last_scrollbar = Rect::default();
}
return;
}
let strip_h: u16 = 1;
let strip = Rect {
x: area.x,
y: area.y,
width: area.width,
height: strip_h,
};
let mut body = Rect {
x: area.x,
y: area.y + strip_h,
width: area.width,
height: area.height - strip_h,
};
// The tab chrome follows the theme. Like the brand flag below, the
// theme propagates from a synced sibling: a tab opened since the last
// sync still carries the default theme and must not flash the wrong
// chrome for a frame.
let theme = self
.editors
.iter()
.map(|e| e.theme)
.find(|t| *t != crate::theme::Theme::default())
.unwrap_or_default();
// Paint strip background first so the gap to the right of the last
// tab still reads as the tab-strip colour rather than terminal default.
let strip_bg_style = Style::default().bg(theme.tab_strip_bg());
for x in strip.x..strip.x + strip.width {
buf[(x, strip.y)].set_style(strip_bg_style);
buf[(x, strip.y)].set_symbol(" ");
}
self.tab_strip_y = strip.y;
self.tab_screen_ranges.clear();
self.tab_close_x.clear();
let mut cursor_x = strip.x;
let active = self.active;
// Black theme (`focus_gradient` doubles as the theme flag): the
// active tab wears the muted teal selection fill instead of the
// legacy navy chip; Croft Dark keeps the navy. Sync pushes the flag
// to every editor, but one freshly opened since the last sync still
// defaults to false — propagate from its siblings so it can't flash
// the navy chip (or a blue body border) for a frame.
let brand = self.editors.iter().any(|e| e.focus_gradient);
if brand {
for ed in self.editors.iter_mut() {
ed.focus_gradient = true;
}
}
let active_tab_bg = theme.tab_active_bg();
let pointer = self.hover_pointer;
for (i, ed) in self.editors.iter().enumerate() {
let label_text = tab_label(ed);
let label_chars = label_text.chars().count() as u16;
let pad: u16 = 1;
let close_pad: u16 = 2;
let width = label_chars
.saturating_add(pad * 2)
.saturating_add(close_pad);
if cursor_x.saturating_add(width) > strip.x + strip.width {
self.tab_screen_ranges.push((cursor_x, 0));
self.tab_close_x.push(0);
continue;
}
let is_active = i == active;
let close_x = cursor_x + 1 + label_chars + 1;
// Treatment C: the pointer resting anywhere on a tab lifts an
// inactive tab's body (the active tab is already prominent), and
// resting on the close cross gives that single cell a pill.
let tab_hovered = pointer.is_some_and(|(px, py)| {
py == strip.y && px >= cursor_x && px < cursor_x.saturating_add(width)
});
let on_close = pointer == Some((close_x, strip.y));
let hover_lift = tab_hovered && !is_active;
let bg = if hover_lift {
theme.tab_hover_bg()
} else if is_active {
active_tab_bg
} else {
theme.tab_inactive_bg()
};
let fg = if is_active || hover_lift {
TAB_ACTIVE_FG
} else {
TAB_INACTIVE_FG
};
let mut modifiers = Modifier::empty();
if is_active {
modifiers |= Modifier::BOLD;
}
if ed.preview {
modifiers |= Modifier::ITALIC;
}
let style = Style::default().fg(fg).bg(bg).add_modifier(modifiers);
// The close cell shows a thumb-tack on a pinned tab (clicking it
// unpins) and the close cross otherwise (clicking it closes).
let close_glyph = if ed.pinned { "\u{f08d}" } else { "\u{2715}" };
// Layout: " " + label + " " + ✕/pin + " "
let padded = format!(" {label_text} {close_glyph} ");
buf.set_string(cursor_x, strip.y, &padded, style);
// Overpaint the close/pin cell with its hover pill so the user sees
// exactly which glyph their click will land on.
if on_close {
let pill_bg = theme.tab_close_pill_bg();
buf.set_string(
close_x,
strip.y,
close_glyph,
Style::default()
.fg(Color::White)
.bg(pill_bg)
.add_modifier(Modifier::BOLD),
);
}
self.tab_screen_ranges.push((cursor_x, width));
self.tab_close_x.push(close_x);
cursor_x = cursor_x.saturating_add(width);
}
// Breadcrumb bar (VS Code editor breadcrumbs): one row below the tab
// strip showing the file path + enclosing symbol chain. Carve it off
// the body so the editor's own coordinate math shrinks with it.
self.breadcrumb_ranges.clear();
self.breadcrumb_y = None;
if !self.breadcrumbs.is_empty() && body.height > 1 {
let crumb_y = body.y;
self.breadcrumb_y = Some(crumb_y);
let theme = self.editors[active].theme;
let bg = theme.editor_bg();
for x in area.x..area.x + area.width {
buf[(x, crumb_y)].set_symbol(" ");
buf[(x, crumb_y)].set_style(Style::default().bg(bg));
}
let max_x = area.x + area.width;
let mut x = area.x + 1;
for (i, crumb) in self.breadcrumbs.iter().enumerate() {
if i > 0 {
let sep = " \u{203A} "; // " › "
let sw = sep.chars().count() as u16;
if x + sw >= max_x {
break;
}
buf.set_string(x, crumb_y, sep, Style::default().fg(Color::DarkGray).bg(bg));
x += sw;
}
if x >= max_x {
break;
}
let avail = (max_x - x) as usize;
// Symbol crumbs (clickable) read brighter than the informational
// path crumbs, mirroring VS Code's active/inactive breadcrumbs.
let fg = if crumb.target.is_some() {
Color::Gray
} else {
Color::DarkGray
};
// Budget and advance in display CELLS: `set_stringn` clips by
// grapheme width and reports where it stopped, so a CJK or
// emoji crumb neither overruns the bar nor gets painted over by
// the next crumb, and its hit rect covers what was drawn.
let (end_x, _) = buf.set_stringn(
x,
crumb_y,
&crumb.label,
avail,
Style::default().fg(fg).bg(bg),
);
let sw = end_x.saturating_sub(x);
self.breadcrumb_ranges.push((x, sw, crumb.target));
x = end_x;
}
body = Rect {
y: body.y + 1,
height: body.height - 1,
..body
};
}
let active_editor = &mut self.editors[active];
Widget::render(active_editor, body, buf);
}
}
impl EditorTabs {
/// If `(col, row)` lands on a breadcrumb crumb with a jump target, return
/// the caret position to navigate to. Path/file crumbs return `None`.
pub fn breadcrumb_target_at(&self, col: u16, row: u16) -> Option<(u32, u32)> {
if self.breadcrumb_y != Some(row) {
return None;
}
self.breadcrumb_ranges
.iter()
.find(|&&(x, w, _)| col >= x && col < x + w)
.and_then(|&(_, _, target)| target)
}
}
fn tab_label(e: &Editor) -> String {
if let Some(diff) = e.diff.as_ref() {
let l = diff
.left_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| diff.left_path.to_string_lossy().into_owned());
// Synthetic single-sided diffs (deletion view, raw `git diff`
// text view) leave `right_path` empty / `/dev/null` so the tab
// label collapses to just the left label instead of trailing a
// misleading "↔ null".
let r_is_real = diff.right_path != std::path::Path::new("/dev/null")
&& !diff.right_path.as_os_str().is_empty();
if r_is_real {
let r = diff
.right_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| diff.right_path.to_string_lossy().into_owned());
return format!("{l} \u{2194} {r}");
}
return l;
}
let name = match &e.path {
Some(p) => p
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| String::from("untitled")),
None => String::from("untitled"),
};
if e.dirty {
format!("\u{25cf} {name}")
} else {
name
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
fn blame_line(
author: &str,
summary: &str,
age: i64,
uncommitted: bool,
) -> crate::git::BlameLine {
crate::git::BlameLine {
short_hash: "abc12345".into(),
summary: summary.into(),
author: author.into(),
age_secs: age,
uncommitted,
}
}
#[test]
fn snippet_expansion_places_caret_and_tab_advances_with_drift() {
let mut e = editor_with("");
e.insert_str("for");
e.expand_snippet("for ${1:i} in ${2:xs}:", 3);
// Prefix removed, body inserted, caret on the first placeholder with it
// selected so the next keystroke replaces it.
assert_eq!(e.lines[0], "for i in xs:");
assert_eq!((e.cursor_row, e.cursor_col), (0, 5));
assert!(e.snippet_active());
assert_eq!(e.selection_text(), "i");
// Replacing the 1-char placeholder with a 3-char name shifts $2 right.
e.insert_str("idx");
assert_eq!(e.lines[0], "for idx in xs:");
assert!(e.snippet_next());
assert_eq!(
e.selection_text(),
"xs",
"$2 must track the drift from the longer $1"
);
// The last stop ends the session.
assert!(!e.snippet_active());
}
/// A failed revert must not launder the buffer clean: clearing `dirty`
/// before the fallible reload meant a file deleted between the conflict
/// popup and the Enter left unsaved edits flagged clean — the tab lost
/// its unsaved marker, auto-save ignored it, and the next FS sweep
/// silently auto-reverted the edits when the file reappeared.
#[test]
fn a_failed_revert_keeps_the_buffer_dirty() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("gone.txt");
std::fs::write(&path, "disk\n").unwrap();
let mut e = Editor::new();
e.open(&path).unwrap();
e.insert_char('x');
assert!(e.dirty, "staging: unsaved edits");
std::fs::remove_file(&path).unwrap();
assert!(e.revert_to_disk().is_err(), "staging: the reload fails");
assert!(
e.dirty,
"a failed reload must keep the unsaved edits marked dirty"
);
}
/// A snippet stop can outlive its row: rows deleted mid-session (the
/// backspace join) leave later stops pointing past the end of the
/// buffer, and Tab then planted a selection on the vanished row — the
/// next edit indexed `lines[row]` out of bounds and panicked the TUI.
#[test]
fn a_snippet_stop_on_a_deleted_row_cannot_plant_a_selection_out_of_bounds() {
let mut e = editor_with("");
e.insert_str("try");
e.expand_snippet(
"try:\n ${1:pass}\nexcept ${2:Exception}:\n ${3:raise}",
3,
);
assert!(e.snippet_active());
assert_eq!(e.selection_text(), "pass");
// Delete the placeholder, its indent, and the row join — the buffer
// shrinks below stop $3's recorded row, and backspace keeps the
// session alive by design.
for _ in 0..6 {
e.backspace();
}
assert!(e.lines.len() < 4, "staging: a row was joined away");
e.snippet_next();
e.snippet_next();
// The next edit must not panic on a selection past the buffer end.
e.insert_char('x');
}
/// A reused tab (the preview tab navigating file to file) must drop its
/// collab attachment when it loads a DIFFERENT path: the old generation
/// belongs to another file's doc, and keeping it lets the next collab
/// tick broadcast this file's disk text as edits to that doc. A
/// same-path reload keeps the attachment on purpose — that is how an
/// owner's reload-diff (external change, Replace All) converges as ops.
#[test]
fn open_resets_collab_attachment_only_on_a_path_change() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
let b = tmp.path().join("b.txt");
std::fs::write(&a, "aaa").unwrap();
std::fs::write(&b, "bbb").unwrap();
let mut e = Editor::new();
e.open(&a).unwrap();
e.collab_doc_gen = 7;
e.collab_synced_seq = 3;
e.open(&b).unwrap();
assert_eq!(
e.collab_doc_gen, 0,
"opening another path must detach the buffer from the old doc"
);
assert_eq!(
e.collab_synced_seq, e.edit_seq,
"a fresh open has nothing pending to extract"
);
e.collab_doc_gen = 7;
e.open(&b).unwrap();
assert_eq!(
e.collab_doc_gen, 7,
"a same-path reload must keep its attachment so the reload broadcasts as edits"
);
}
#[test]
fn snippet_indents_continuation_lines_to_the_caret() {
let mut e = editor_with(" ");
e.cursor_row = 0;
e.cursor_col = 4;
e.expand_snippet("if x:\n $0", 0);
assert_eq!(e.lines[0], " if x:");
assert_eq!(
e.lines[1], " ",
"continuation line keeps the 4-space block indent plus its own"
);
}
#[test]
fn current_line_blame_annotation_reads_author_age_and_summary() {
let mut e = editor_with("one\ntwo\n");
e.path = Some(PathBuf::from("f.rs"));
e.set_blame(
PathBuf::from("f.rs"),
Some(vec![
blame_line("Vitali", "fix: the thing", 90, false),
blame_line("Alice", "feat: two", 7200, false),
]),
);
e.cursor_row = 0;
assert_eq!(
e.current_line_blame_annotation().as_deref(),
Some("Vitali, 1 minute ago • fix: the thing")
);
e.cursor_row = 1;
assert_eq!(
e.current_line_blame_annotation().as_deref(),
Some("Alice, 2 hours ago • feat: two")
);
}
#[test]
fn current_line_blame_annotation_marks_uncommitted_lines() {
let mut e = editor_with("edited\n");
e.path = Some(PathBuf::from("f.rs"));
e.set_blame(
PathBuf::from("f.rs"),
Some(vec![blame_line("Not Committed Yet", "x", 0, true)]),
);
e.cursor_row = 0;
assert_eq!(
e.current_line_blame_annotation().as_deref(),
Some("Uncommitted changes")
);
}
#[test]
fn current_line_blame_annotation_is_none_when_disabled_or_unavailable() {
let mut e = editor_with("one\n");
e.path = Some(PathBuf::from("f.rs"));
// No blame fetched yet.
assert!(e.current_line_blame_annotation().is_none());
e.set_blame(
PathBuf::from("f.rs"),
Some(vec![blame_line("V", "s", 5, false)]),
);
e.blame_enabled = false;
assert!(
e.current_line_blame_annotation().is_none(),
"the toggle suppresses the annotation without dropping the data"
);
// A cursor past the blamed range (line added after the fetch) is safe.
e.blame_enabled = true;
e.cursor_row = 9;
assert!(e.current_line_blame_annotation().is_none());
}
/// A blame reply fetched for one file must never paint on another: the
/// preview tab is reused across files, and the async refetch takes frames
/// to land, so a same-line-count neighbour would wear the old blame.
#[test]
fn blame_for_another_file_never_paints_on_this_one() {
let mut e = editor_with("one\ntwo\n");
e.path = Some(PathBuf::from("a.rs"));
e.set_blame(
PathBuf::from("a.rs"),
Some(vec![
blame_line("Alice", "s", 5, false),
blame_line("Alice", "s", 5, false),
]),
);
e.path = Some(PathBuf::from("b.rs"));
assert!(
e.current_line_blame_annotation().is_none(),
"a.rs blame painted on b.rs while its own fetch was still in flight"
);
}
/// Opening a real file into an editor showing a diff must drop the diff
/// view, or a restore-then-reload keeps rendering the stale side-by-side.
#[test]
fn opening_a_real_file_clears_a_stale_diff_view() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("a.txt");
std::fs::write(&path, "disk\n").unwrap();
let mut e = Editor::new();
e.diff = Some(crate::widgets::diff::DiffData::build_unified_deletion(
path.clone(),
"old\n",
));
e.open(&path).unwrap();
assert!(
e.diff.is_none(),
"the stale diff kept rendering over the freshly opened file"
);
}
/// Deduping a snapshot/HEAD diff onto an existing tab must not destroy a
/// dirty buffer: the TIMELINE always targets the file being edited, so
/// replacing the tab wholesale silently discards unsaved edits.
#[test]
fn a_head_diff_never_replaces_a_dirty_tab_in_place() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("a.txt");
std::fs::write(&path, "disk\n").unwrap();
let mut tabs = EditorTabs::new();
tabs.open(&path).unwrap();
tabs.lines[0].push('x');
tabs.dirty = true;
tabs.open_head_diff_with_text(
PathBuf::from("a.txt (local snapshot)"),
"old\n",
&path,
false,
)
.unwrap();
assert!(
tabs.editors
.iter()
.any(|e| e.dirty && e.diff.is_none() && e.lines[0] == "diskx"),
"the dirty buffer was destroyed by the snapshot diff"
);
assert!(
tabs.editors.iter().any(|e| e.diff.is_some()),
"staging: the diff itself still opened in some tab"
);
}
#[test]
fn disabling_the_csv_viewer_opens_the_file_as_plain_text() {
// A .csv opens in the inline sheet viewer by default, but when the CSV
// extension is disabled in the Extensions panel the same file must fall
// through to a normal text buffer (no sheet, raw rows as lines).
let f = tempfile::Builder::new()
.suffix(".csv")
.tempfile()
.expect("temp csv");
std::fs::write(f.path(), "a,b\n1,2\n").expect("write csv");
let mut on = Editor::new();
on.open(f.path()).expect("open with viewer enabled");
assert!(on.sheet.is_some(), "enabled CSV viewer renders the sheet");
let mut off = Editor::new();
off.csv_viewer_enabled = false;
off.open(f.path()).expect("open with viewer disabled");
assert!(
off.sheet.is_none(),
"disabled CSV viewer must not build a sheet"
);
assert_eq!(
off.lines,
vec!["a,b".to_string(), "1,2".to_string()],
"disabled CSV viewer opens the raw rows as text lines"
);
}
#[test]
fn toggle_breakpoint_adds_then_removes_on_cursor_line() {
let mut e = Editor::new();
e.path = Some(PathBuf::from("/x/a.py"));
e.lines = vec!["a".into(), "b".into(), "c".into()];
e.cursor_row = 1; // 0-based row 1 => 1-based line 2
// First toggle adds it (now_set = true).
assert_eq!(
e.toggle_breakpoint(),
Some((PathBuf::from("/x/a.py"), 2, true))
);
assert_eq!(e.breakpoint_lines(Path::new("/x/a.py")), vec![2u32]);
// Second toggle removes it (now_set = false).
assert_eq!(
e.toggle_breakpoint(),
Some((PathBuf::from("/x/a.py"), 2, false))
);
assert!(e.breakpoint_lines(Path::new("/x/a.py")).is_empty());
}
#[test]
fn toggle_breakpoint_without_open_file_is_noop() {
let mut e = Editor::new();
assert_eq!(e.toggle_breakpoint(), None);
}
#[test]
fn removing_a_breakpoint_discards_its_logpoint_and_condition() {
// F9 twice on a logpoint line reads as "remove, then set a plain
// breakpoint" — but the orphaned message survived in
// `breakpoint_logs` and re-attached on the next set, resurrecting a
// logpoint that never pauses. Same leak for conditions.
let path = PathBuf::from("/x/a.py");
let mut e = Editor::new();
e.path = Some(path.clone());
e.lines = vec!["a".into(), "b".into(), "c".into()];
e.toggle_breakpoint_line(2);
e.breakpoint_logs
.entry(path.clone())
.or_default()
.insert(2, String::from("x is {x}"));
e.breakpoint_conditions
.entry(path.clone())
.or_default()
.insert(2, String::from("x > 1"));
e.toggle_breakpoint_line(2); // remove
e.toggle_breakpoint_line(2); // re-add: a PLAIN breakpoint
let lines = e.breakpoints.get(&path).cloned().unwrap();
let specs = e.source_breakpoints(&path, &lines);
assert_eq!(specs.len(), 1);
assert_eq!(
specs[0].log_message, None,
"a re-added breakpoint must pause, not print"
);
assert_eq!(
specs[0].condition, None,
"a re-added breakpoint must be unconditional"
);
}
#[test]
fn toggle_breakpoint_line_targets_an_explicit_line_not_the_cursor() {
let mut e = Editor::new();
e.path = Some(PathBuf::from("/x/a.py"));
e.lines = vec!["a".into(), "b".into(), "c".into()];
e.cursor_row = 0; // cursor on line 1, but we target line 3
assert_eq!(
e.toggle_breakpoint_line(3),
Some((PathBuf::from("/x/a.py"), 3, true))
);
assert_eq!(e.breakpoint_lines(Path::new("/x/a.py")), vec![3u32]);
// Cursor never moved.
assert_eq!(e.cursor_row, 0);
// Toggling the same line removes it again.
assert_eq!(
e.toggle_breakpoint_line(3),
Some((PathBuf::from("/x/a.py"), 3, false))
);
assert!(e.breakpoint_lines(Path::new("/x/a.py")).is_empty());
}
#[test]
fn gutter_line_at_maps_gutter_clicks_and_ignores_the_body() {
let mut e = editor_with("a\nbb\nccc\ndddd\neeeee");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2; // text_x = 0 + 2 + 1 = 3
// A click in the glyph margin / line-number columns maps to the line.
assert_eq!(e.gutter_line_at(0, 0), Some(0), "col 0 is the glyph margin");
assert_eq!(e.gutter_line_at(2, 2), Some(2), "col 2 is the line number");
// The body (col >= text_x) is NOT the gutter.
assert_eq!(e.gutter_line_at(3, 0), None, "col 3 is the text body");
assert_eq!(e.gutter_line_at(10, 1), None, "deep in the body");
// Past the last line there is no gutter target.
assert_eq!(e.gutter_line_at(0, 20), None, "row past content");
}
#[test]
fn gutter_line_at_follows_vertical_scroll() {
let mut e = editor_with("a\nbb\nccc\ndddd\neeeee");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
e.scroll = 2;
assert_eq!(
e.gutter_line_at(1, 0),
Some(2),
"the top visible gutter row is buffer line scroll = 2"
);
}
#[test]
fn breakpoint_glyph_sits_in_left_margin_not_over_the_line_number() {
let f = NamedTempFile::new().unwrap();
std::fs::write(f.path(), "aaa\nbbb\nccc\n").unwrap();
let mut ed = Editor::new();
ed.open(f.path()).unwrap();
ed.breakpoints
.entry(f.path().to_path_buf())
.or_default()
.insert(2); // breakpoint on line 2
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut ed, area, &mut buf);
// The editor insets by a 1-col border, so inner.x == 1 and content rows
// start at y == 1; line 2 is therefore at y == 2.
let inner_x = ed.last_inner.x;
let gutter = ed.last_gutter_width;
let row = ed.last_inner.y + 1; // line 2 is the 2nd content row
let mut line = String::new();
for x in 0..area.width {
line.push_str(buf[(x, row)].symbol());
}
// The dot lives in the far-left glyph margin...
assert_eq!(
buf[(inner_x, row)].symbol(),
"●",
"breakpoint dot must be in the left glyph margin; row was {line:?}"
);
// ...the line number is right-aligned after the margin (its last digit
// sits one cell before the gutter's trailing space) and is intact...
assert_eq!(
buf[(inner_x + gutter - 2, row)].symbol(),
"2",
"line number must survive after the margin; row was {line:?}"
);
// ...and the margin never overwrites a digit (the cell after the dot is
// a blank, not a number).
assert_eq!(
buf[(inner_x + 1, row)].symbol(),
" ",
"a space separates the dot from the line number; row was {line:?}"
);
}
#[test]
fn source_breakpoints_attach_conditions() {
let mut e = Editor::new();
let p = PathBuf::from("/x/a.py");
let mut lines = std::collections::BTreeSet::new();
lines.insert(3usize);
lines.insert(5usize);
e.breakpoint_conditions
.entry(p.clone())
.or_default()
.insert(5, String::from("i == 10"));
let specs = e.source_breakpoints(&p, &lines);
assert_eq!(specs.len(), 2);
assert_eq!(specs[0].line, 3);
assert_eq!(specs[0].condition, None);
assert_eq!(specs[1].line, 5);
assert_eq!(specs[1].condition.as_deref(), Some("i == 10"));
}
#[test]
fn source_breakpoints_attach_log_messages() {
let mut e = Editor::new();
let p = PathBuf::from("/x/a.py");
let mut lines = std::collections::BTreeSet::new();
lines.insert(3usize);
lines.insert(5usize);
e.breakpoint_logs
.entry(p.clone())
.or_default()
.insert(5, String::from("value is {v}"));
let specs = e.source_breakpoints(&p, &lines);
assert_eq!(specs[0].log_message, None);
assert_eq!(specs[1].log_message.as_deref(), Some("value is {v}"));
}
#[test]
fn logpoint_wears_an_amber_diamond_in_the_gutter() {
let mut e = editor_with("a\nb\nc");
let p = PathBuf::from("/x/lp.py");
e.path = Some(p.clone());
e.breakpoints.entry(p.clone()).or_default().insert(2);
e.breakpoint_logs
.entry(p)
.or_default()
.insert(2, String::from("hit {x}"));
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let cell = &buf[(e.last_inner.x, e.last_inner.y + 1)];
assert_eq!(
cell.symbol(),
"◆",
"a logpoint paints the diamond in the glyph margin"
);
assert_eq!(
cell.fg,
Color::Rgb(0xe5, 0xc0, 0x7b),
"the logpoint diamond is amber, distinct from the conditional's red"
);
}
#[test]
fn word_string_at_reads_identifier_under_cursor() {
let mut e = Editor::new();
e.lines = vec![" total = count + 1".into()];
// cursor inside "total" (chars 4..9)
assert_eq!(e.word_string_at(0, 6).as_deref(), Some("total"));
// cursor inside "count"
assert_eq!(e.word_string_at(0, 13).as_deref(), Some("count"));
// over the '=' (non-word) => None
assert_eq!(e.word_string_at(0, 10), None);
}
fn editor_with(text: &str) -> Editor {
let mut e = Editor::new();
e.lines = if text.is_empty() {
vec![String::new()]
} else {
text.lines().map(|s| s.to_string()).collect()
};
if e.lines.is_empty() {
e.lines.push(String::new());
}
e
}
fn head(lines: &[&str]) -> Option<Vec<String>> {
Some(lines.iter().map(|s| s.to_string()).collect())
}
#[test]
fn git_gutter_marks_added_modified_and_deleted_lines() {
let mut e = editor_with("a\nB2\nINSERTED\nc");
// HEAD: a / b / c / d → b became B2 (modified), INSERTED is new (added),
// and d was removed before EOF.
e.set_git_head_lines(PathBuf::from("f.rs"), head(&["a", "b", "c", "d"]));
e.refresh_git_marks();
assert_eq!(e.git_mark_at(0), None, "unchanged line carries no mark");
assert_eq!(e.git_mark_at(1), Some(GitMark::Modified));
assert_eq!(e.git_mark_at(2), Some(GitMark::Added));
// `c` survives at line 3 with the deletion of `d` just below it.
assert_eq!(e.git_mark_at(3), Some(GitMark::Deleted));
}
#[test]
fn git_gutter_no_baseline_means_no_marks() {
let mut e = editor_with("a\nb\nc");
e.refresh_git_marks();
assert_eq!(e.git_mark_at(0), None);
assert_eq!(e.git_mark_at(1), None);
}
#[test]
fn git_gutter_recomputes_after_an_edit() {
let mut e = editor_with("a\nb");
e.set_git_head_lines(PathBuf::from("f.rs"), head(&["a", "b"]));
e.refresh_git_marks();
assert_eq!(e.git_mark_at(1), None, "buffer matches HEAD: clean");
// Edit line 1, then a fresh render-time refresh must light it up.
e.cursor_row = 1;
e.cursor_col = 1;
e.insert_char('X');
e.refresh_git_marks();
assert_eq!(e.git_mark_at(1), Some(GitMark::Modified));
}
#[test]
fn git_gutter_render_paints_bar_in_the_spacer_cell() {
use ratatui::buffer::Buffer;
let mut e = editor_with("a\nb");
e.set_git_head_lines(PathBuf::from("f.rs"), head(&["a", "DIFFERENT"]));
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 5,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
// Line 1 (`b` vs `DIFFERENT`) is modified; the bar sits in the spacer
// cell at `inner.x + gutter_width`, one cell left of the text.
let bar_x = e.last_inner.x + e.last_gutter_width;
let cell = &buf[(bar_x, e.last_inner.y + 1)];
assert_eq!(
cell.symbol(),
"\u{2503}",
"modified line shows a gutter bar"
);
}
#[test]
fn is_binary_detects_nul() {
assert!(is_binary(b"hello\0world"));
assert!(!is_binary(b"hello world"));
assert!(!is_binary(b""));
}
fn diag(
start_line: u32,
start_char: u32,
end_line: u32,
end_char: u32,
severity: crate::lsp::manager::DiagnosticSeverity,
) -> crate::lsp::manager::Diagnostic {
crate::lsp::manager::Diagnostic {
start_line,
start_char,
end_line,
end_char,
severity,
message: String::from("test diagnostic"),
}
}
#[test]
fn apply_diagnostics_decodes_into_per_line_char_spans() {
use crate::lsp::manager::DiagnosticSeverity;
let mut e = editor_with("const x = 1;");
let p = std::path::PathBuf::from("/tmp/diag.ts");
e.path = Some(p.clone());
// Underline the `x` (chars 6..7).
e.apply_diagnostics(p, vec![diag(0, 6, 0, 7, DiagnosticSeverity::Error)]);
let spans = e.diagnostic_spans_for_test();
assert_eq!(
spans[0],
vec![(6, 7, DiagnosticSeverity::Error)],
"the diagnostic must decode to a single-char run over `x`"
);
}
#[test]
fn zero_width_diagnostic_is_widened_to_one_cell() {
use crate::lsp::manager::DiagnosticSeverity;
let mut e = editor_with("const x = 1;");
let p = std::path::PathBuf::from("/tmp/diag.ts");
e.path = Some(p.clone());
// A point diagnostic (start == end) must still show one cell.
e.apply_diagnostics(p, vec![diag(0, 6, 0, 6, DiagnosticSeverity::Warning)]);
assert_eq!(
e.diagnostic_spans_for_test()[0],
vec![(6, 7, DiagnosticSeverity::Warning)],
"a zero-width diagnostic must widen to one cell so it stays visible"
);
}
#[test]
fn diagnostics_for_another_file_do_not_decode() {
use crate::lsp::manager::DiagnosticSeverity;
let mut e = editor_with("const x = 1;");
e.path = Some(std::path::PathBuf::from("/tmp/open.ts"));
// Batch is for a different file than the one loaded.
e.apply_diagnostics(
std::path::PathBuf::from("/tmp/other.ts"),
vec![diag(0, 0, 0, 5, DiagnosticSeverity::Error)],
);
assert!(
e.diagnostic_spans_for_test().iter().all(|l| l.is_empty()),
"a batch for a different file must not underline the loaded one"
);
}
#[test]
fn empty_diagnostics_batch_clears_the_underlines() {
use crate::lsp::manager::DiagnosticSeverity;
let mut e = editor_with("const x = 1;");
let p = std::path::PathBuf::from("/tmp/diag.ts");
e.path = Some(p.clone());
e.apply_diagnostics(p.clone(), vec![diag(0, 6, 0, 7, DiagnosticSeverity::Error)]);
assert!(!e.diagnostic_spans_for_test()[0].is_empty());
// An "all clear" republish carries an empty list.
e.apply_diagnostics(p, Vec::new());
assert!(
e.diagnostic_spans_for_test().iter().all(|l| l.is_empty()),
"an empty batch must erase the squiggles"
);
}
#[test]
fn diagnostics_at_returns_messages_covering_the_point() {
use crate::lsp::manager::{Diagnostic, DiagnosticSeverity};
let mut e = editor_with("const x = 1;");
let p = std::path::PathBuf::from("/tmp/diag.ts");
e.path = Some(p.clone());
// Underline the `x` (chars 6..7) with a real message.
e.apply_diagnostics(
p,
vec![Diagnostic {
start_line: 0,
start_char: 6,
end_line: 0,
end_char: 7,
severity: DiagnosticSeverity::Error,
message: String::from("Type 'number' is not assignable to type 'string'."),
}],
);
assert_eq!(
e.diagnostics_at(0, 6),
vec![(
DiagnosticSeverity::Error,
String::from("Type 'number' is not assignable to type 'string'.")
)],
"a point inside the diagnostic range must surface its message"
);
assert!(
e.diagnostics_at(0, 0).is_empty(),
"a point outside every diagnostic range must surface nothing"
);
}
#[test]
fn diagnostics_at_widens_a_zero_width_diagnostic_to_one_cell() {
use crate::lsp::manager::{Diagnostic, DiagnosticSeverity};
let mut e = editor_with("const x = 1;");
let p = std::path::PathBuf::from("/tmp/diag.ts");
e.path = Some(p.clone());
e.apply_diagnostics(
p,
vec![Diagnostic {
start_line: 0,
start_char: 6,
end_line: 0,
end_char: 6,
severity: DiagnosticSeverity::Warning,
message: String::from("missing semicolon"),
}],
);
assert_eq!(
e.diagnostics_at(0, 6),
vec![(
DiagnosticSeverity::Warning,
String::from("missing semicolon")
)],
"a zero-width diagnostic must still be hoverable over its one widened cell"
);
}
#[test]
fn hover_region_at_prefers_the_word_then_falls_back_to_the_diagnostic_span() {
use crate::lsp::manager::{Diagnostic, DiagnosticSeverity};
// `a.b` — the `.` at char 1 is not a word char, but a diagnostic covers it.
let mut e = editor_with("a.b");
let p = std::path::PathBuf::from("/tmp/dot.ts");
e.path = Some(p.clone());
e.apply_diagnostics(
p,
vec![Diagnostic {
start_line: 0,
start_char: 1,
end_line: 0,
end_char: 2,
severity: DiagnosticSeverity::Error,
message: String::from("unexpected token"),
}],
);
assert_eq!(
e.hover_region_at(0, 0),
Some((0, 0, 1)),
"over the `a` identifier the region is the word range"
);
assert_eq!(
e.hover_region_at(0, 1),
Some((0, 1, 2)),
"over the punctuation `.` the region falls back to the covering diagnostic span"
);
}
#[test]
fn render_paints_severity_coloured_underline_over_the_diagnostic_span() {
use crate::lsp::manager::DiagnosticSeverity;
let mut e = editor_with("const x = 1;");
let p = std::path::PathBuf::from("/tmp/diag.ts");
e.path = Some(p.clone());
e.apply_diagnostics(p, vec![diag(0, 6, 0, 7, DiagnosticSeverity::Error)]);
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let red = Color::Rgb(0xf1, 0x4c, 0x4c);
let mut underlined: Vec<(u16, u16, String)> = Vec::new();
for y in 0..area.height {
for x in 0..area.width {
let cell = &buf[(x, y)];
if cell.modifier.contains(Modifier::UNDERLINED) && cell.underline_color == red {
underlined.push((x, y, cell.symbol().to_string()));
}
}
}
assert_eq!(
underlined.len(),
1,
"exactly one cell (the `x`) must carry a red error underline; got {underlined:?}"
);
assert_eq!(
underlined[0].2, "x",
"the underline must sit under the offending `x` glyph"
);
}
#[test]
fn focused_editor_gradient_border_is_gated_on_focus_gradient() {
use crate::gradient::{GRAD_TL, rgb_color};
let mut e = editor_with("hi");
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 5,
};
// Black theme: rounded gradient corner in the gradient's TL colour.
e.focus_gradient = true;
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert_eq!(buf[(0, 0)].symbol(), "\u{256d}");
assert_eq!(buf[(0, 0)].fg, rgb_color(GRAD_TL));
// Croft Dark: square corner, solid blue (the historical highlight).
e.focus_gradient = false;
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert_eq!(buf[(0, 0)].symbol(), "\u{250c}");
assert_eq!(buf[(0, 0)].fg, Color::Rgb(0x4e, 0x9a, 0xff));
}
fn inlay(line: u32, character: u32, label: &str) -> crate::lsp::manager::InlayHintItem {
crate::lsp::manager::InlayHintItem {
line,
character,
label: label.to_string(),
}
}
/// Render `e` into a fresh 60x5 buffer and return the full text of the
/// first content row (y = 1, inside the border).
fn first_row_text(e: &mut Editor) -> String {
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(e as &mut Editor).render(area, &mut buf);
(0..area.width)
.map(|x| buf[(x, 1)].symbol().to_string())
.collect()
}
#[test]
fn inlay_hint_splices_dim_italic_label_into_the_row() {
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32"), inlay(0, 10, "n: ")]);
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let row: String = (0..area.width)
.map(|x| buf[(x, 1)].symbol().to_string())
.collect();
assert!(
row.contains("let x: i32 = f(n: y);"),
"hints must splice into the rendered row; got {row:?}"
);
// The hint cells are dim italic; the real code cells are not.
let hint_fg = e.theme.ignored_fg();
let hint_x = (0..area.width)
.find(|&x| {
buf[(x, 1)].symbol() == "i"
&& buf[(x + 1, 1)].symbol() == "3"
&& buf[(x + 2, 1)].symbol() == "2"
})
.expect("the i32 hint must be on the row");
for dx in 0..3 {
let cell = &buf[(hint_x + dx, 1)];
assert!(
cell.modifier.contains(Modifier::ITALIC),
"hint cells must be italic"
);
assert_eq!(cell.fg, hint_fg, "hint cells must use the muted grey");
}
let code_x = (0..area.width)
.find(|&x| buf[(x, 1)].symbol() == "x")
.expect("the x binding must be on the row");
assert!(
!buf[(code_x, 1)].modifier.contains(Modifier::ITALIC),
"real code cells must stay non-italic"
);
}
#[test]
fn gutter_play_glyph_marks_test_fns_and_maps_clicks_to_the_name() {
let mut e = editor_with("#[test]\nfn my_case() {}\nfn helper() {}");
// Beads need a saved .rs file: an unsaved buffer has nothing a
// runner could target.
e.path = Some(PathBuf::from("/x/a.rs"));
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let sign_x = e.last_inner.x;
// Buffer line 1 (`fn my_case`) paints on content row y = 2.
assert_eq!(
buf[(sign_x, 2)].symbol(),
"\u{eb2c}",
"a test fn's first row must wear the play glyph in the sign margin"
);
assert_ne!(
buf[(sign_x, 3)].symbol(),
"\u{eb2c}",
"a plain fn must not wear the glyph"
);
assert_eq!(
e.test_glyph_at(sign_x, 2).as_deref(),
Some("my_case"),
"clicking the glyph must resolve the test's name"
);
assert_eq!(
e.test_glyph_at(sign_x, 3),
None,
"clicking the sign margin of a non-test line resolves nothing"
);
assert_eq!(
e.test_glyph_at(sign_x + 1, 2),
None,
"the fold-chevron column is not the play glyph"
);
}
/// The AI-stream stop button: while `stream_stop_line` is set, that
/// line's sign cell wears the stop square (outranking the test play
/// glyph), and `stream_stop_at` maps clicks on exactly that cell.
#[test]
fn gutter_stream_stop_glyph_renders_maps_clicks_and_outranks_the_play_glyph() {
let mut e = editor_with("#[test]\nfn my_case() {}\nfn helper() {}");
e.path = Some(PathBuf::from("/x/a.rs")); // beads need a saved .rs file
e.stream_stop_line = Some(1); // 0-based: the test fn's line
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let sign_x = e.last_inner.x;
// Buffer line 1 paints on content row y = 2 (breadcrumb row above).
assert_eq!(
buf[(sign_x, 2)].symbol(),
"■",
"the streamed row's sign cell wears the stop square, not the play glyph"
);
assert!(e.stream_stop_at(sign_x, 2), "the stop cell is clickable");
assert!(
!e.stream_stop_at(sign_x + 1, 2),
"the fold-chevron column is not the button"
);
assert!(
!e.stream_stop_at(sign_x, 3),
"other rows are not the button"
);
// Stream over: the glyph clears and the play glyph returns.
e.stream_stop_line = None;
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert_eq!(
buf[(sign_x, 2)].symbol(),
"\u{eb2c}",
"without a stream the test play glyph owns the cell again"
);
assert!(!e.stream_stop_at(sign_x, 2));
}
/// A comment box anchored to line 0 renders as an unnumbered block
/// between line 0 and line 1: title row naming the author, body, and a
/// footer with the reply field and Ignore button. The gutter's number
/// column stays blank on box rows, and every following line paints
/// (and reports its screen row) shifted down by the box height.
#[test]
fn comment_box_renders_unnumbered_rows_between_lines() {
let mut e = editor_with("first\nsecond\nthird");
e.comment_boxes = vec![CommentBox {
id: 7,
line: 0,
author: "navigator".into(),
body: "tighten this".into(),
}];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let row_text = |y: u16| -> String {
(0..area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect()
};
// Breadcrumb row at y=0; line 0 at y=1 with its number.
assert!(row_text(1).contains("first"));
assert!(row_text(1).contains('1'), "line 0 keeps its number");
// Box: title (author), body, footer (reply + Ignore) at y=2..=4.
assert!(row_text(2).contains("navigator"), "title names the author");
assert!(row_text(3).contains("tighten this"), "body renders");
assert!(row_text(4).contains("Ignore"), "footer has the button");
let gutter = e.last_gutter_width;
for y in 2..=4u16 {
let g: String = (0..gutter)
.map(|x| buf[(x, y)].symbol().to_string())
.collect();
assert!(
g.chars().all(|c| !c.is_ascii_digit()),
"box rows are unnumbered, got gutter {g:?} at y={y}"
);
}
// Line 1 shifted below the box, with its own number.
assert!(row_text(5).contains("second"));
assert!(row_text(5).contains('2'));
assert_eq!(
e.screen_row_of_line(1),
Some(5),
"screen row mapping shifts by the box height"
);
}
/// Box rows belong to no buffer line: a click inside the box maps to no
/// buffer position, and a click on the line below the box maps to that
/// line at its shifted screen row.
#[test]
fn clicks_map_through_comment_box_rows() {
let mut e = editor_with("first\nsecond\nthird");
e.comment_boxes = vec![CommentBox {
id: 7,
line: 0,
author: "navigator".into(),
body: "tighten this".into(),
}];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
assert_eq!(
e.buffer_pos_at(text_x, 3),
None,
"a box row is not a buffer cell"
);
assert_eq!(
e.buffer_pos_at(text_x, 5),
Some((1, 0)),
"the line below the box maps at its shifted row"
);
}
/// The box's mouse surface: the footer's ✕ Ignore cells hit Ignore, the
/// rest of the footer hits Reply, body rows hit Body, and buffer-text
/// rows hit nothing.
#[test]
fn comment_box_hit_finds_reply_ignore_and_body() {
let mut e = editor_with("first\nsecond\nthird");
e.comment_boxes = vec![CommentBox {
id: 7,
line: 0,
author: "navigator".into(),
body: "tighten this".into(),
}];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
// Find the ✕ on the footer row rather than hard-coding geometry.
let ignore_x = (0..area.width)
.find(|&x| buf[(x, 4)].symbol() == "\u{2715}")
.expect("the footer renders the ✕ Ignore button");
assert_eq!(
e.comment_box_hit(ignore_x, 4),
Some((7, CommentHit::Ignore))
);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
assert_eq!(
e.comment_box_hit(text_x + 2, 4),
Some((7, CommentHit::Reply)),
"the rest of the footer is the reply field"
);
assert_eq!(
e.comment_box_hit(text_x + 2, 3),
Some((7, CommentHit::Body))
);
assert_eq!(e.comment_box_hit(text_x + 2, 1), None, "text rows miss");
assert_eq!(e.comment_box_hit(text_x + 2, 5), None);
}
/// In wrap mode (visual-row cursor motion) the caret steps over a box:
/// down from line 0 lands on line 1, never inside the block.
#[test]
fn cursor_skips_comment_box_rows_in_wrap_mode() {
let mut e = editor_with("first\nsecond\nthird");
e.wrap_override = Some(true);
e.comment_boxes = vec![CommentBox {
id: 7,
line: 0,
author: "navigator".into(),
body: "tighten this".into(),
}];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
e.cursor_row = 0;
e.cursor_col = 0;
e.move_down();
assert_eq!(e.cursor_row, 1, "the caret skips the box");
e.move_up();
assert_eq!(e.cursor_row, 0);
}
/// A comment box on a line hidden inside a collapsed fold is never
/// painted, so it must not count toward the scroll extent either: it
/// used to inflate the content length, keeping a scrollbar alive for
/// invisible content and letting the wheel scroll into blank space.
#[test]
fn a_box_hidden_by_a_fold_does_not_extend_the_scroll_geometry() {
let mut e = editor_with("fn a() {\n body();\n}\nlast");
e.comment_boxes = vec![CommentBox {
id: 1,
line: 1,
author: "navigator".into(),
body: (0..20)
.map(|i| format!("p{i}"))
.collect::<Vec<_>>()
.join("\n"),
}];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
e.toggle_fold(0);
assert!(e.is_line_hidden(1), "the box's line folds away");
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert_eq!(
e.last_scrollbar.width, 0,
"4 lines minus a fold fit the viewport; nothing left to scroll"
);
e.scroll_down(3);
assert_eq!(
(e.scroll, e.scroll_sub),
(0, 0),
"scrolling must not walk into folded-away box rows"
);
}
/// A comment box taller than the viewport must be reachable in non-wrap
/// mode: scrolling used to be line-granular (a 3-line file cannot
/// scroll at all), so a tall box was truncated at the viewport edge and
/// its footer - the Reply field and ✕ Ignore - could never be seen.
#[test]
fn nonwrap_scrolling_reaches_a_tall_comment_boxes_footer() {
let mut e = editor_with("first\nsecond\nthird");
e.comment_boxes = vec![CommentBox {
id: 1,
line: 0,
author: "navigator".into(),
body: (0..30)
.map(|i| format!("point {i}"))
.collect::<Vec<_>>()
.join("\n"),
}];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let screen = |buf: &ratatui::buffer::Buffer| -> String {
(0..area.height)
.map(|y| {
(0..area.width)
.map(|x| buf[(x, y)].symbol().to_string())
.collect::<String>()
})
.collect::<Vec<_>>()
.join("\n")
};
assert!(
!screen(&buf).contains("Ignore"),
"the tall box overflows: its footer starts off-screen"
);
// Scroll far enough that the footer row must be inside the viewport.
for _ in 0..12 {
e.scroll_down(3);
}
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert!(
screen(&buf).contains("Ignore"),
"scrolling must reach the box footer; got:\n{}",
screen(&buf)
);
}
/// A reply longer than the footer field must window around the caret:
/// the frozen first-N-chars rendering meant typing past the field width
/// went blind - no caret anywhere and the new text never appearing.
#[test]
fn a_long_reply_windows_so_the_caret_and_tail_stay_visible() {
let mut e = editor_with("first\nsecond\nthird");
e.comment_boxes = vec![CommentBox {
id: 7,
line: 0,
author: "navigator".into(),
body: "tighten this".into(),
}];
let reply = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_TAIL";
e.comment_focus = Some(CommentFocus {
id: 7,
reply: reply.into(),
cursor: reply.chars().count(),
});
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let footer: String = (0..area.width)
.map(|x| buf[(x, 4)].symbol().to_string())
.collect();
assert!(
footer.contains("_TAIL"),
"the field must window to keep the tail under the caret visible: {footer:?}"
);
let caret_cells = (0..area.width)
.filter(|&x| {
buf[(x, 4)]
.style()
.add_modifier
.contains(ratatui::style::Modifier::REVERSED)
})
.count();
assert_eq!(caret_cells, 1, "exactly one caret cell renders: {footer:?}");
}
/// The focused box renders the typed reply in its footer field.
#[test]
fn focused_comment_box_renders_the_reply_draft() {
let mut e = editor_with("first\nsecond\nthird");
e.comment_boxes = vec![CommentBox {
id: 7,
line: 0,
author: "navigator".into(),
body: "tighten this".into(),
}];
e.comment_focus = Some(CommentFocus {
id: 7,
reply: String::from("why here?"),
cursor: 9,
});
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let footer: String = (0..area.width)
.map(|x| buf[(x, 4)].symbol().to_string())
.collect();
assert!(
footer.contains("why here?"),
"the reply draft renders in the footer: {footer:?}"
);
}
#[test]
fn find_highlights_stay_readable_over_occurrence_tints() {
// Cmd+F then Enter parks the caret ON a match; the idle-caret
// documentHighlight then returns every occurrence of that word —
// exactly the cells the find layer just painted black-on-gold. The
// find layer must stay on top: an occurrence bg painted over it
// keeps the BLACK foreground on a dark grey, which is unreadable,
// and kills the orange active-match cue.
let mut e = editor_with("config here");
e.path = Some(PathBuf::from("/x/a.rs"));
e.set_search_highlight(Some(String::from("config")));
e.occurrences = vec![(0, 0, 6, false)];
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let gold = Color::Rgb(0xff, 0xd7, 0x4a);
let occ = crate::theme::Theme::BLACK.occurrence_bg();
let mut gold_cells = 0;
for y in 0..area.height {
for x in 0..area.width {
let cell = &buf[(x, y)];
if cell.bg == gold {
gold_cells += 1;
}
assert!(
!(cell.fg == Color::Black && cell.bg == occ),
"cell ({x},{y}) '{}' wears the find layer's black fg on the occurrence bg — unreadable",
cell.symbol()
);
}
}
assert!(
gold_cells >= 6,
"the find highlight must survive the occurrence pass (got {gold_cells} gold cells)"
);
}
#[test]
fn a_claimed_sign_cell_does_not_hit_test_as_the_play_bead() {
// Render gives the sign cell to the stop arrow / breakpoint glyph /
// AI-stream square before the play bead; the CLICK hit-test must
// agree, or the natural gesture of clicking a red ● (VS Code toggles
// a breakpoint there) silently starts a test run instead.
let mut e = editor_with("#[test]\nfn my_case() {}");
e.path = Some(PathBuf::from("/x/a.rs"));
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let sign_x = e.last_inner.x;
assert_eq!(e.test_glyph_at(sign_x, 2).as_deref(), Some("my_case"));
e.toggle_breakpoint_line(2);
assert_eq!(
e.test_glyph_at(sign_x, 2),
None,
"the breakpoint dot's cell must not run tests on click"
);
e.toggle_breakpoint_line(2);
assert_eq!(
e.test_glyph_at(sign_x, 2).as_deref(),
Some("my_case"),
"removing the dot hands the cell back to the bead"
);
e.stop_line = Some((e.path.clone().unwrap(), 2));
assert_eq!(
e.test_glyph_at(sign_x, 2),
None,
"the paused stop arrow owns its cell"
);
e.stop_line = None;
e.stream_stop_line = Some(1);
assert_eq!(
e.test_glyph_at(sign_x, 2),
None,
"the AI-stream stop square owns its cell"
);
}
#[test]
fn gutter_play_glyph_yields_to_a_breakpoint_dot() {
let mut e = editor_with("#[test]\nfn my_case() {}");
let p = std::path::PathBuf::from("/tmp/bp.rs");
e.path = Some(p.clone());
e.breakpoints.entry(p).or_default().insert(2); // 1-based: the fn line
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert_eq!(
buf[(e.last_inner.x, 2)].symbol(),
"●",
"a breakpoint on the fn line owns the shared sign cell"
);
}
fn occ(
start_line: u32,
start_char: u32,
end_line: u32,
end_char: u32,
write: bool,
) -> crate::lsp::manager::OccurrenceItem {
crate::lsp::manager::OccurrenceItem {
start_line,
start_char,
end_line,
end_char,
write,
}
}
#[test]
fn lsp_occurrences_paint_theme_tints_with_utf16_columns_and_clear_on_edit() {
// "a𐐀x = x;" — 𐐀 is one char but two UTF-16 code units, so the
// server's UTF-16 columns for the second `x` (units 7..8) land on
// char column 6, one LESS than a naive unit-as-char mapping.
let mut e = editor_with("a𐐀x = x;");
let p = std::path::PathBuf::from("/tmp/occ.rs");
e.path = Some(p.clone());
e.apply_occurrences(vec![occ(0, 0, 0, 4, true), occ(0, 7, 0, 8, false)]);
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let write_bg = e.theme.occurrence_write_bg();
let read_bg = e.theme.occurrence_bg();
let a_x = (0..area.width)
.find(|&x| buf[(x, 1)].symbol() == "a")
.expect("the identifier must be on the row");
for dx in 0..3 {
assert_eq!(
buf[(a_x + dx, 1)].bg,
write_bg,
"the write occurrence must tint all three identifier chars (dx={dx})"
);
}
let second_x = (a_x + 3..area.width)
.find(|&x| buf[(x, 1)].symbol() == "x")
.expect("the second x must be on the row");
assert_eq!(
buf[(second_x, 1)].bg,
read_bg,
"the read occurrence must tint exactly the second x"
);
assert_ne!(
buf[(second_x - 1, 1)].bg,
read_bg,
"the cell before the second x must stay untinted"
);
// Any edit invalidates the server's columns: the tints must vanish
// until the app's next idle-cursor request answers.
e.mark_buffer_changed();
let mut buf2 = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf2);
assert_ne!(
buf2[(second_x, 1)].bg,
read_bg,
"an edit must clear the occurrence tints"
);
}
#[test]
fn inlay_hints_for_another_file_do_not_paint() {
let mut e = editor_with("let x = f(y);");
e.path = Some(std::path::PathBuf::from("/tmp/current.rs"));
e.apply_inlay_hints(
std::path::PathBuf::from("/tmp/other.rs"),
vec![inlay(0, 5, ": i32")],
);
let row = first_row_text(&mut e);
assert!(
row.contains("let x = f(y);") && !row.contains("i32"),
"hints for a different file must not paint; got {row:?}"
);
}
#[test]
fn wrap_mode_suppresses_inlay_hints() {
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32")]);
e.wrap_override = Some(true);
let row = first_row_text(&mut e);
assert!(
row.contains("let x = f(y);") && !row.contains("i32"),
"wrap mode must render the raw text unshifted; got {row:?}"
);
}
#[test]
fn cursor_screen_pos_shifts_past_inlay_hints() {
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32")]);
e.focused = true;
let _ = first_row_text(&mut e);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
// Caret on the `=` (buffer col 6): five hint cells sit before it.
e.cursor_row = 0;
e.cursor_col = 6;
assert_eq!(
e.cursor_screen_pos(),
Some((text_x + 11, e.last_inner.y)),
"the caret must account for hint cells before it"
);
// Caret exactly at the hint's anchor (buffer col 5) stays LEFT of the
// hint, like VS Code: typing there pushes the hint right.
e.cursor_col = 5;
assert_eq!(
e.cursor_screen_pos(),
Some((text_x + 5, e.last_inner.y)),
"the caret at the anchor must sit before the hint"
);
}
#[test]
fn secondary_and_ghost_carets_sit_left_of_an_inlay_hint_like_the_primary() {
// Change All Occurrences puts every extra caret at a word end —
// exactly where rust-analyzer anchors a binding's type hint. The
// primary caret deliberately sits LEFT of such a hint (see
// cursor_screen_pos_shifts_past_inlay_hints); the block painted for
// a secondary caret, and a collaborator's ghost caret, must agree
// instead of landing a hint-width to the right, outside their own
// selection band.
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32")]);
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 5,
};
// Secondary caret at the hint's anchor (buffer col 5).
e.carets = vec![EditorSelection::new(0, 5)];
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let block_x = (0..area.width).find(|&x| buf[(x, 1)].bg == Color::Rgb(0xae, 0xc6, 0xff));
assert_eq!(
block_x,
Some(text_x + 5),
"the secondary caret's block must sit where the primary would"
);
// Ghost caret at the same anchor, painted in the participant color.
e.carets.clear();
e.ghost_carets = vec![(0, 5, Color::Red)];
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
let ghost_x = (0..area.width).find(|&x| buf[(x, 1)].bg == Color::Red);
assert_eq!(
ghost_x,
Some(text_x + 5),
"a collaborator's ghost caret must sit where the primary would"
);
}
#[test]
fn click_and_word_select_map_through_inlay_hints() {
// Repro: with a type hint spliced into the row, the caret / selection
// landed `hint` cells right of the pointer while hover (which goes
// through `buffer_pos_at`) resolved the symbol under it correctly.
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32")]);
let _ = first_row_text(&mut e);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
// `f` displays at col 13 (buffer col 8 plus the five hint cells).
e.click(text_x + 13, y);
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 8),
"the caret must land where the pointer is, not past the hint"
);
// Double-click on the shifted `y` selects `y`, not a neighbour.
e.select_word_at(text_x + 15, y);
assert_eq!(
e.selection.map(|s| (s.anchor, s.head)),
Some(((0, 10), (0, 11))),
"word select must resolve the word under the pointer"
);
}
#[test]
fn buffer_pos_at_maps_clicks_through_inlay_hints() {
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32")]);
let _ = first_row_text(&mut e);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
// A click on the shifted `=` (display col 11) lands on buffer col 6.
assert_eq!(e.buffer_pos_at(text_x + 11, y), Some((0, 6)));
// A click inside the hint's cells snaps to its anchor column.
assert_eq!(e.buffer_pos_at(text_x + 7, y), Some((0, 5)));
// Cells before the hint are unaffected.
assert_eq!(e.buffer_pos_at(text_x + 2, y), Some((0, 2)));
}
#[test]
fn opening_a_file_drops_the_previous_files_inlay_hints() {
let mut e = editor_with("let x = f(y);");
let p = std::path::PathBuf::from("/tmp/hints.rs");
e.path = Some(p.clone());
e.apply_inlay_hints(p, vec![inlay(0, 5, ": i32")]);
let dir = std::env::temp_dir().join("croft_inlay_open_test");
std::fs::create_dir_all(&dir).unwrap();
let other = dir.join("other.rs");
std::fs::write(&other, "fn g() {}\n").unwrap();
e.open(&other).unwrap();
let row = first_row_text(&mut e);
assert!(
!row.contains("i32"),
"a newly opened file must not inherit stale hints; got {row:?}"
);
}
#[test]
fn markdown_preview_toggles_renders_and_returns_to_source() {
let mut e = editor_with("# Title\n\nSome body text.");
e.lang = Some(LangKind::Markdown);
assert!(e.toggle_markdown_preview(), "a Markdown tab must toggle");
let text = first_row_screen(&mut e, 60, 8);
assert!(
text.contains("Title") && !text.contains("# Title"),
"the preview must render the heading without its # marker; got:\n{text}"
);
assert!(text.contains("Some body text."), "got:\n{text}");
assert!(e.toggle_markdown_preview(), "toggling again returns");
assert!(e.markdown_preview.is_none());
let text = first_row_screen(&mut e, 60, 8);
assert!(
text.contains("# Title"),
"the source view must show the raw markdown again; got:\n{text}"
);
}
#[test]
fn markdown_preview_refuses_non_markdown_tabs() {
let mut e = editor_with("fn main() {}");
e.lang = Some(LangKind::Rust);
assert!(!e.toggle_markdown_preview());
assert!(e.markdown_preview.is_none());
}
#[test]
fn markdown_preview_rebuilds_when_the_buffer_moves() {
let mut e = editor_with("# Old heading");
e.lang = Some(LangKind::Markdown);
assert!(e.toggle_markdown_preview());
e.lines[0] = String::from("# New heading");
e.edit_seq = e.edit_seq.wrapping_add(1);
let text = first_row_screen(&mut e, 60, 8);
assert!(
text.contains("New heading") && !text.contains("Old heading"),
"a stale preview must rebuild against the edited buffer; got:\n{text}"
);
}
/// Render into a fresh buffer and return the whole screen as one string.
fn first_row_screen(e: &mut Editor, width: u16, height: u16) -> String {
let area = Rect {
x: 0,
y: 0,
width,
height,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(e as &mut Editor).render(area, &mut buf);
let mut out = String::new();
for y in 0..height {
for x in 0..width {
out.push_str(buf[(x, y)].symbol());
}
out.push('\n');
}
out
}
#[test]
fn range_batch_does_not_overwrite_a_full_batch_for_the_same_file() {
// The viewport range request races the whole-document request on open.
// If a range reply lands AFTER the full reply, it must be dropped so it
// cannot blank the off-screen colour the full reply painted.
let mut e = editor_with("def f(x):\n return x\n");
let p = std::path::PathBuf::from("/tmp/sem_guard.py");
e.path = Some(p.clone());
let legend = std::sync::Arc::new(vec!["parameter".to_string()]);
// Full batch: signature param (line 0) AND body reference (line 1).
let full = vec![0, 6, 1, 0, 0, 1, 11, 1, 0, 0];
e.apply_semantic_tokens(p.clone(), full, legend.clone(), true);
let body_spans = e.semantic_overlay_for_test()[1].len();
assert!(body_spans > 0, "full batch must colour the body reference");
// A late range batch covering only line 0 must be rejected.
let range_only_line0 = vec![0, 6, 1, 0, 0];
e.apply_semantic_tokens(p.clone(), range_only_line0, legend, false);
assert_eq!(
e.semantic_overlay_for_test()[1].len(),
body_spans,
"a range batch must not erase off-screen colour from the full batch"
);
}
#[test]
fn range_batch_paints_first_then_full_replaces() {
// The intended first-paint order: range colours the viewport, then the
// full reply arrives and extends colour to off-screen lines.
let mut e = editor_with("def f(x):\n return x\n");
let p = std::path::PathBuf::from("/tmp/sem_first.py");
e.path = Some(p.clone());
let legend = std::sync::Arc::new(vec!["parameter".to_string()]);
// Range first: only line 0 (the viewport) is coloured.
e.apply_semantic_tokens(p.clone(), vec![0, 6, 1, 0, 0], legend.clone(), false);
let line1_before = e
.semantic_overlay_for_test()
.get(1)
.map(|s| s.len())
.unwrap_or(0);
assert_eq!(
line1_before, 0,
"range batch leaves the off-screen line bare"
);
// Full reply lands and colours the body reference on line 1.
e.apply_semantic_tokens(p.clone(), vec![0, 6, 1, 0, 0, 1, 11, 1, 0, 0], legend, true);
assert!(
!e.semantic_overlay_for_test()[1].is_empty(),
"full reply must colour the previously-bare off-screen line"
);
}
#[test]
fn find_word_occurrences_matches_whole_words_only() {
let chars: Vec<char> = "foo foobar foo_ foo".chars().collect();
let word: Vec<char> = "foo".chars().collect();
// Only the standalone "foo" at col 0 and the final "foo" qualify;
// "foobar" and "foo_" are larger identifiers.
assert_eq!(find_word_occurrences(&chars, &word), vec![0, 16]);
}
#[test]
fn select_all_occurrences_picks_cursor_match_as_primary() {
let mut e = editor_with("foo bar foo\nfoo");
e.cursor_row = 0;
e.cursor_col = 8; // second "foo" on line 0
let n = e.select_all_occurrences_of_word_at_cursor();
assert_eq!(n, 3);
// Primary selection covers the occurrence under the cursor.
assert_eq!(e.selection.unwrap().normalised(), ((0, 8), (0, 11)));
// The other two become secondary carets.
assert_eq!(e.carets.len(), 2);
assert!(e.has_multi_cursor());
}
#[test]
fn multi_insert_replaces_every_occurrence_in_one_undo_step() {
let mut e = editor_with("foo bar foo\nfoo");
e.cursor_row = 0;
e.cursor_col = 0;
assert_eq!(e.select_all_occurrences_of_word_at_cursor(), 3);
e.multi_insert_char('X');
assert_eq!(e.lines, vec!["X bar X".to_string(), "X".to_string()]);
// One undo restores the whole batch at once.
assert!(e.undo());
assert_eq!(e.lines, vec!["foo bar foo".to_string(), "foo".to_string()]);
}
#[test]
fn multi_backspace_deletes_at_every_caret() {
let mut e = editor_with("ab ab ab");
e.cursor_row = 0;
e.cursor_col = 0;
assert_eq!(e.select_all_occurrences_of_word_at_cursor(), 3);
// Selections are active; backspace deletes each selected "ab".
e.multi_backspace();
assert_eq!(e.lines, vec![" ".to_string()]);
}
#[test]
fn collapse_carets_drops_secondary_cursors() {
let mut e = editor_with("x x x");
e.cursor_col = 0;
e.select_all_occurrences_of_word_at_cursor();
assert!(e.has_multi_cursor());
e.collapse_carets();
assert!(!e.has_multi_cursor());
assert!(e.carets.is_empty());
}
#[test]
fn apply_span_edits_applies_bottom_to_top() {
let mut lines = vec!["foo bar foo".to_string()];
let edits = vec![
TextSpanEdit {
start: (0, 0),
end: (0, 3),
new_text: "baz".to_string(),
},
TextSpanEdit {
start: (0, 8),
end: (0, 11),
new_text: "baz".to_string(),
},
];
assert_eq!(apply_span_edits_to_lines(&mut lines, &edits), 2);
assert_eq!(lines, vec!["baz bar baz".to_string()]);
}
#[test]
fn apply_span_edits_splits_multiline_new_text_into_separate_lines() {
// Ruff's "Organize Imports" returns ONE edit whose new_text spans several
// lines. The result must become several Vec entries, not one line with
// embedded '\n' (the bug that mashed the imports onto a single line).
let mut lines = vec![
"import typing".to_string(),
"import pandas".to_string(),
"import logging".to_string(),
"import re".to_string(),
];
let edits = vec![TextSpanEdit {
start: (0, 0),
end: (3, 9),
new_text: "import logging\nimport re\nimport typing\n\nimport pandas".to_string(),
}];
assert_eq!(apply_span_edits_to_lines(&mut lines, &edits), 1);
assert_eq!(
lines,
vec![
"import logging".to_string(),
"import re".to_string(),
"import typing".to_string(),
String::new(),
"import pandas".to_string(),
]
);
}
#[test]
fn apply_span_edits_single_line_range_with_multiline_new_text() {
// A same-line range whose new_text introduces a newline must split the
// line in two, keeping the untouched prefix and suffix.
let mut lines = vec!["abXYef".to_string()];
let edits = vec![TextSpanEdit {
start: (0, 2),
end: (0, 4),
new_text: "C\nD".to_string(),
}];
assert_eq!(apply_span_edits_to_lines(&mut lines, &edits), 1);
assert_eq!(lines, vec!["abC".to_string(), "Def".to_string()]);
}
#[test]
fn editor_apply_span_edits_marks_dirty_and_one_undo() {
let mut e = editor_with("name = 1\nprint(name)");
let edits = vec![
TextSpanEdit {
start: (0, 0),
end: (0, 4),
new_text: "label".to_string(),
},
TextSpanEdit {
start: (1, 6),
end: (1, 10),
new_text: "label".to_string(),
},
];
assert_eq!(e.apply_span_edits(&edits), 2);
assert!(e.dirty);
assert_eq!(
e.lines,
vec!["label = 1".to_string(), "print(label)".to_string()]
);
assert!(e.undo());
assert_eq!(
e.lines,
vec!["name = 1".to_string(), "print(name)".to_string()]
);
}
#[test]
fn is_binary_detects_high_nontext_ratio() {
let mut data = vec![0x01u8; 100];
data.extend_from_slice(b"abc");
assert!(is_binary(&data));
}
#[test]
fn is_binary_accepts_normal_text() {
let txt = "fn main() { println!(\"hello\"); }\n// this is fine\n";
assert!(!is_binary(txt.as_bytes()));
}
#[test]
fn open_loads_a_real_world_sized_log_file_above_the_old_5mb_cap() {
// Regression for the user's "I can't open .croft/lsp.log" report:
// the LSP log grew past 5MB during normal use (their copy was
// 7.4MB) and the old cap silently bailed with "File too large".
// The current cap MUST accommodate at least 8MB of ASCII text so
// a typical LSP log session round-trips.
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("big.log");
let line =
"1778548312.915 lsp[ruff] stderr: 2026-05-12 02:11:52 INFO some workspace setting\n";
let line_bytes = line.len();
let target_bytes = 8 * 1024 * 1024;
let line_count = target_bytes / line_bytes + 1;
let mut buf = String::with_capacity(line_count * line_bytes);
for _ in 0..line_count {
buf.push_str(line);
}
std::fs::write(&path, &buf).unwrap();
assert!(
std::fs::metadata(&path).unwrap().len() > 5 * 1024 * 1024,
"test setup must produce a file larger than the historical 5MB cap"
);
let mut e = Editor::new();
e.open(&path)
.expect("editor.open must accept an 8MB plain-text log; the user reported lsp.log (7.4MB) being unopenable");
assert_eq!(e.lines.len(), line_count);
}
#[test]
fn line_char_len_counts_chars_not_bytes() {
let mut e = editor_with("héllo");
assert_eq!(e.line_char_len(0), 5);
e.lines[0] = String::from("日本語");
assert_eq!(e.line_char_len(0), 3);
}
#[test]
fn byte_index_ascii() {
let e = editor_with("abcdef");
assert_eq!(e.byte_index(0, 0), 0);
assert_eq!(e.byte_index(0, 3), 3);
assert_eq!(e.byte_index(0, 6), 6);
assert_eq!(e.byte_index(0, 99), 6); // saturates at end
}
#[test]
fn byte_index_multibyte() {
let e = editor_with("héllo");
// 'h'=1 byte, 'é'=2 bytes, 'l'=1 byte
assert_eq!(e.byte_index(0, 0), 0);
assert_eq!(e.byte_index(0, 1), 1); // before 'é'
assert_eq!(e.byte_index(0, 2), 3); // after 'é'
assert_eq!(e.byte_index(0, 3), 4);
}
#[test]
fn insert_char_at_end() {
let mut e = editor_with("abc");
e.cursor_col = 3;
e.insert_char('d');
assert_eq!(e.lines[0], "abcd");
assert_eq!(e.cursor_col, 4);
assert!(e.dirty);
}
#[test]
fn insert_char_at_start() {
let mut e = editor_with("bc");
e.cursor_col = 0;
e.insert_char('a');
assert_eq!(e.lines[0], "abc");
assert_eq!(e.cursor_col, 1);
}
#[test]
fn insert_char_in_middle() {
let mut e = editor_with("ac");
e.cursor_col = 1;
e.insert_char('b');
assert_eq!(e.lines[0], "abc");
assert_eq!(e.cursor_col, 2);
}
#[test]
fn insert_char_multibyte_position() {
let mut e = editor_with("aé");
e.cursor_col = 2; // after 'é'
e.insert_char('z');
assert_eq!(e.lines[0], "aéz");
}
#[test]
fn insert_newline_splits_line() {
let mut e = editor_with("hello world");
e.cursor_col = 5;
e.insert_newline();
assert_eq!(e.lines, vec!["hello".to_string(), " world".to_string()]);
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 0);
}
#[test]
fn insert_newline_at_end_creates_blank_line() {
let mut e = editor_with("abc");
e.cursor_col = 3;
e.insert_newline();
assert_eq!(e.lines, vec!["abc".to_string(), String::new()]);
assert_eq!(e.cursor_row, 1);
}
#[test]
fn insert_newline_copies_previous_indent_for_unknown_language() {
let mut e = editor_with(" abc");
e.cursor_col = 7;
e.insert_newline();
assert_eq!(e.lines, vec![" abc".to_string(), " ".to_string()]);
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_python_indents_one_step_after_colon() {
let mut e = editor_with("def hello():");
e.lang = Some(LangKind::Python);
e.cursor_col = e.line_char_len(0);
e.insert_newline();
assert_eq!(
e.lines,
vec!["def hello():".to_string(), " ".to_string()]
);
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_python_no_extra_indent_without_colon() {
let mut e = editor_with(" print(x)");
e.lang = Some(LangKind::Python);
e.cursor_col = e.line_char_len(0);
e.insert_newline();
assert_eq!(
e.lines,
vec![" print(x)".to_string(), " ".to_string()]
);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_python_stacks_indent_on_nested_colon() {
let mut e = editor_with(" if x:");
e.lang = Some(LangKind::Python);
e.cursor_col = e.line_char_len(0);
e.insert_newline();
assert_eq!(
e.lines,
vec![" if x:".to_string(), " ".to_string()]
);
assert_eq!(e.cursor_col, 8);
}
#[test]
fn shift_tab_dedents_current_line_when_no_selection() {
let mut e = editor_with(" pass");
e.lang = Some(LangKind::Python);
e.cursor_row = 0;
e.cursor_col = 8;
e.dedent_lines();
assert_eq!(e.lines, vec![" pass".to_string()]);
assert_eq!(e.cursor_col, 4, "cursor follows the four stripped spaces");
}
#[test]
fn shift_tab_dedent_aligns_to_previous_tab_stop() {
// Six leading spaces drop to four (the previous width-4 tab stop),
// removing two, not a flat four.
let mut e = editor_with(" x = 1");
e.lang = Some(LangKind::Python);
e.cursor_col = 6;
e.dedent_lines();
assert_eq!(e.lines, vec![" x = 1".to_string()]);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn shift_tab_dedent_partial_indent_removes_all_leading_spaces() {
let mut e = editor_with(" y");
e.lang = Some(LangKind::Python);
e.cursor_col = 3;
e.dedent_lines();
assert_eq!(e.lines, vec!["y".to_string()]);
assert_eq!(e.cursor_col, 0);
}
#[test]
fn shift_tab_dedent_on_flush_left_line_is_a_noop() {
let mut e = editor_with("x");
e.lang = Some(LangKind::Python);
e.cursor_col = 1;
e.dedent_lines();
assert_eq!(e.lines, vec!["x".to_string()]);
assert_eq!(e.cursor_col, 1);
}
#[test]
fn shift_tab_dedent_strips_a_single_leading_tab() {
let mut e = editor_with("\tpass");
e.lang = Some(LangKind::Python);
e.cursor_col = 1;
e.dedent_lines();
assert_eq!(e.lines, vec!["pass".to_string()]);
assert_eq!(e.cursor_col, 0);
}
#[test]
fn shift_tab_dedents_every_line_a_selection_touches() {
let mut e = editor_with(" a\n b\n c");
e.lang = Some(LangKind::Python);
e.cursor_row = 2;
e.cursor_col = 5;
e.selection = Some(EditorSelection {
anchor: (0, 4),
head: (2, 5),
});
e.dedent_lines();
assert_eq!(
e.lines,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
assert_eq!(
e.selection.unwrap(),
EditorSelection {
anchor: (0, 0),
head: (2, 1),
}
);
}
#[test]
fn tab_indents_every_line_a_multiline_selection_touches() {
let mut e = editor_with("a\nb");
e.lang = Some(LangKind::Python);
e.cursor_row = 1;
e.cursor_col = 1;
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (1, 1),
});
e.indent_lines();
assert_eq!(e.lines, vec![" a".to_string(), " b".to_string()]);
assert_eq!(
e.selection.unwrap(),
EditorSelection {
anchor: (0, 4),
head: (1, 5),
}
);
}
#[test]
fn tab_indent_block_leaves_empty_lines_untouched() {
let mut e = editor_with("a\n\nb");
e.lang = Some(LangKind::Python);
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (2, 1),
});
e.indent_lines();
assert_eq!(
e.lines,
vec![" a".to_string(), String::new(), " b".to_string()]
);
}
#[test]
fn tab_at_cursor_pads_to_the_next_tab_stop() {
// Cursor at column 2 inserts two spaces to reach column 4, not four.
let mut e = editor_with("ab");
e.lang = Some(LangKind::Python);
e.cursor_col = 2;
e.indent_at_cursor();
assert_eq!(e.lines, vec!["ab ".to_string()]);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn dedent_then_undo_restores_the_buffer_in_one_step() {
let mut e = editor_with(" pass");
e.lang = Some(LangKind::Python);
e.cursor_col = 8;
e.dedent_lines();
assert_eq!(e.lines, vec![" pass".to_string()]);
assert!(e.undo());
assert_eq!(e.lines, vec![" pass".to_string()]);
}
#[test]
fn redo_reapplies_an_undone_edit() {
let mut e = editor_with("");
e.insert_char('a');
e.insert_char('b');
assert_eq!(e.lines, vec!["ab".to_string()]);
assert!(e.undo());
assert_eq!(e.lines, vec![String::new()]);
assert!(e.redo());
assert_eq!(e.lines, vec!["ab".to_string()]);
assert_eq!(e.cursor_col, 2);
}
#[test]
fn redo_is_a_noop_on_an_empty_redo_stack() {
let mut e = editor_with("x");
assert!(!e.redo());
}
#[test]
fn undo_and_redo_bump_edit_seq_so_the_app_resyncs() {
// The app drives LSP did_change / git-gutter refresh off edit_seq; a
// buffer restore must bump it or diagnostics go stale until the next
// keystroke.
let mut e = editor_with("");
e.insert_char('a');
let after_edit = e.edit_seq;
assert!(e.undo());
assert!(e.edit_seq > after_edit, "undo must bump edit_seq");
let after_undo = e.edit_seq;
assert!(e.redo());
assert!(e.edit_seq > after_undo, "redo must bump edit_seq");
}
/// "Reopen with Encoding" swaps the whole buffer for a re-decode of the
/// file on disk, like every other whole-buffer replacement — and like
/// them it must clear BOTH history stacks: a redo popped after the swap
/// used to reinstate the entire buffer as decoded under the OLD
/// encoding (plus its dirty flag), silently discarding the re-decode.
#[test]
fn reopening_with_an_encoding_clears_both_history_stacks() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("enc.txt");
// 0xC2 0xA2 decodes to one char as UTF-8 ("\u{a2}") and two as
// Windows-1252 ("\u{c2}\u{a2}"), so the assertions below prove the
// buffer really was re-decoded, not merely re-read.
std::fs::write(&path, [0xC2_u8, 0xA2, b'\n']).unwrap();
let mut e = Editor::new();
e.open(&path).unwrap();
assert_eq!(e.lines, vec!["\u{a2}".to_string()], "staging: UTF-8 decode");
e.insert_char('x');
assert!(e.undo());
assert_eq!(e.lines, vec!["\u{a2}".to_string()], "staging: edit undone");
e.reopen_with_encoding(encoding_rs::WINDOWS_1252).unwrap();
assert_eq!(
e.lines,
vec!["\u{c2}\u{a2}".to_string()],
"the buffer holds the Windows-1252 decode"
);
assert!(
!e.redo(),
"redo must not resurrect the buffer decoded under the old encoding"
);
assert!(
!e.undo(),
"undo history from before the re-decode is equally stale"
);
assert_eq!(e.lines, vec!["\u{c2}\u{a2}".to_string()]);
}
#[test]
fn a_new_edit_after_undo_discards_the_redo_branch() {
let mut e = editor_with("");
e.insert_char('a');
assert!(e.undo());
assert_eq!(e.lines, vec![String::new()]);
// A fresh edit branches history: the redo of "a" must be discarded.
e.insert_char('b');
assert!(!e.redo(), "a fresh edit must clear the redo stack");
assert_eq!(e.lines, vec!["b".to_string()]);
}
#[test]
fn insert_newline_rust_indents_after_open_brace() {
let mut e = editor_with("fn main() {");
e.lang = Some(LangKind::Rust);
e.cursor_col = e.line_char_len(0);
e.insert_newline();
assert_eq!(e.lines, vec!["fn main() {".to_string(), " ".to_string()]);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_typescript_indents_after_open_paren() {
let mut e = editor_with("foo(");
e.lang = Some(LangKind::TypeScript);
e.cursor_col = e.line_char_len(0);
e.insert_newline();
assert_eq!(e.lines, vec!["foo(".to_string(), " ".to_string()]);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_rust_bracket_pair_split_places_close_on_own_line() {
let mut e = editor_with("fn main() {}");
e.lang = Some(LangKind::Rust);
e.cursor_col = 11;
e.insert_newline();
assert_eq!(
e.lines,
vec![
"fn main() {".to_string(),
" ".to_string(),
"}".to_string()
]
);
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_rust_bracket_pair_split_preserves_outer_indent() {
let mut e = editor_with(" let v = vec![];");
e.lang = Some(LangKind::Rust);
e.cursor_col = 17;
e.insert_newline();
assert_eq!(
e.lines,
vec![
" let v = vec![".to_string(),
" ".to_string(),
" ];".to_string(),
]
);
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 8);
}
#[test]
fn insert_newline_python_bracket_pair_split_places_close_on_own_line() {
let mut e = editor_with("foo()");
e.lang = Some(LangKind::Python);
e.cursor_col = 4;
e.insert_newline();
assert_eq!(
e.lines,
vec!["foo(".to_string(), " ".to_string(), ")".to_string()]
);
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 4);
}
#[test]
fn insert_newline_yaml_uses_two_space_indent() {
let mut e = editor_with(" key:");
e.lang = Some(LangKind::Yaml);
e.cursor_col = e.line_char_len(0);
e.insert_newline();
assert_eq!(e.lines, vec![" key:".to_string(), " ".to_string()]);
assert_eq!(e.cursor_col, 2);
}
#[test]
fn backspace_mid_line() {
let mut e = editor_with("abcd");
e.cursor_col = 3;
e.backspace();
assert_eq!(e.lines[0], "abd");
assert_eq!(e.cursor_col, 2);
}
#[test]
fn backspace_at_col_zero_joins_with_previous_line() {
let mut e = editor_with("hello\nworld");
e.cursor_row = 1;
e.cursor_col = 0;
e.backspace();
assert_eq!(e.lines, vec!["helloworld".to_string()]);
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 5);
}
#[test]
fn backspace_at_origin_does_nothing_destructive() {
let mut e = editor_with("abc");
e.cursor_row = 0;
e.cursor_col = 0;
e.backspace();
assert_eq!(e.lines, vec!["abc".to_string()]);
}
#[test]
fn delete_forward_mid_line() {
let mut e = editor_with("abcd");
e.cursor_col = 1;
e.delete_forward();
assert_eq!(e.lines[0], "acd");
assert_eq!(e.cursor_col, 1);
}
#[test]
fn delete_forward_at_end_joins_with_next_line() {
let mut e = editor_with("hello\nworld");
e.cursor_row = 0;
e.cursor_col = 5;
e.delete_forward();
assert_eq!(e.lines, vec!["helloworld".to_string()]);
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 5);
}
#[test]
fn buffer_pos_at_maps_cells_to_line_and_char() {
let mut e = editor_with("fn main() {}\nlet x = 1;");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
assert_eq!(
e.buffer_pos_at(3, 0),
Some((0, 0)),
"first text cell (text_x = x + gutter + 1 = 3) is char 0 of line 0"
);
assert_eq!(
e.buffer_pos_at(6, 0),
Some((0, 3)),
"three cells into the text is char 3"
);
assert_eq!(
e.buffer_pos_at(3, 1),
Some((1, 0)),
"the second screen row is the second buffer line"
);
}
#[test]
fn buffer_pos_at_returns_none_over_the_gutter() {
let mut e = editor_with("hello");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
assert_eq!(e.buffer_pos_at(0, 0), None, "column 0 is in the gutter");
assert_eq!(e.buffer_pos_at(2, 0), None, "still left of text_x");
}
#[test]
fn buffer_pos_at_accounts_for_vertical_scroll() {
let mut e = editor_with("a\nbb\nccc\ndddd\neeeee");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
e.scroll = 2;
assert_eq!(
e.buffer_pos_at(3, 0),
Some((2, 0)),
"the top visible row is buffer line scroll = 2"
);
}
#[test]
fn buffer_pos_at_accounts_for_horizontal_scroll() {
let mut e = editor_with("abcdefghijklmnop");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
e.scroll_col = 5;
assert_eq!(
e.buffer_pos_at(3, 0),
Some((0, 5)),
"the leftmost text cell maps to char scroll_col, not char 0"
);
assert_eq!(
e.buffer_pos_at(5, 0),
Some((0, 7)),
"two cells right of the left edge is char scroll_col + 2"
);
}
#[test]
fn buffer_pos_at_honours_pane_offset() {
let mut e = editor_with("hello");
e.last_inner = Rect {
x: 10,
y: 4,
width: 40,
height: 10,
};
e.last_gutter_width = 3;
assert_eq!(e.buffer_pos_at(14, 4), Some((0, 0)), "text_x = 10 + 3 + 1");
assert_eq!(e.buffer_pos_at(16, 4), Some((0, 2)));
assert_eq!(e.buffer_pos_at(13, 4), None, "one cell left of text_x");
}
#[test]
fn buffer_pos_at_returns_none_outside_viewport_and_past_content() {
let mut e = editor_with("one\ntwo");
e.last_inner = Rect {
x: 0,
y: 2,
width: 80,
height: 4,
};
e.last_gutter_width = 2;
assert_eq!(e.buffer_pos_at(3, 1), None, "row above the pane");
assert_eq!(e.buffer_pos_at(3, 6), None, "row below the pane");
assert_eq!(
e.buffer_pos_at(3, 4),
None,
"row maps past the last line of content"
);
}
#[test]
fn buffer_pos_at_clamps_past_end_of_line() {
let mut e = editor_with("hi");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
assert_eq!(
e.buffer_pos_at(40, 0),
Some((0, 2)),
"a cell past the line's text clamps to the line length"
);
}
#[test]
fn word_at_returns_identifier_bounds() {
let e = editor_with("fn main() {}");
assert_eq!(e.word_at(0, 0), Some((0, 2)), "fn");
assert_eq!(e.word_at(0, 1), Some((0, 2)), "still within fn");
assert_eq!(e.word_at(0, 3), Some((3, 7)), "main starts at char 3");
assert_eq!(e.word_at(0, 6), Some((3, 7)), "last char of main");
}
#[test]
fn word_at_returns_none_off_a_word() {
let e = editor_with("fn main() {}");
assert_eq!(e.word_at(0, 2), None, "the space between fn and main");
assert_eq!(e.word_at(0, 7), None, "the open paren");
assert_eq!(e.word_at(0, 99), None, "past the end of the line");
}
#[test]
fn word_at_includes_underscores_and_digits() {
let e = editor_with("let foo_bar2 = 1");
assert_eq!(
e.word_at(0, 4),
Some((4, 12)),
"foo_bar2 is a single identifier"
);
assert_eq!(
e.word_at(0, 11),
Some((4, 12)),
"a trailing digit is part of the identifier"
);
}
#[test]
fn word_at_handles_missing_line() {
let e = editor_with("only one line");
assert_eq!(e.word_at(5, 0), None, "no such line");
}
#[test]
fn page_down_advances_one_full_viewport_and_puts_first_unseen_line_at_top() {
// Simulate a 100-line file with the editor's viewport rendering 25
// lines. After PageDown the cursor should land on row 25 (line 26 in
// 1-indexed terms) and that row should be the new top of the view.
let mut e = editor_with_lines(100);
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
assert_eq!(e.scroll, 0);
assert_eq!(e.cursor_row, 0);
e.page_down_one_screen();
assert_eq!(
e.cursor_row, 25,
"cursor should jump to first previously-unseen row"
);
assert_eq!(
e.scroll, 25,
"scroll should align with new cursor at top of viewport"
);
}
#[test]
fn page_down_repeats_advance_one_viewport_at_a_time() {
let mut e = editor_with_lines(100);
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 20,
};
e.page_down_one_screen();
e.page_down_one_screen();
assert_eq!(e.cursor_row, 40);
assert_eq!(e.scroll, 40);
}
#[test]
fn page_down_clamps_at_end_of_file() {
let mut e = editor_with_lines(30);
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
// Realistic state: scroll = 4 means rows 4..=28 are on screen, with
// line 29 (cursor_row 28) visible at the bottom.
e.scroll = 4;
e.cursor_row = 28;
e.page_down_one_screen();
// 4 + 25 = 29 → last row. Cursor and scroll land there.
assert_eq!(e.cursor_row, 29);
assert_eq!(e.scroll, 29);
}
#[test]
fn page_up_rewinds_one_full_viewport() {
let mut e = editor_with_lines(200);
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.scroll = 100;
e.cursor_row = 100;
e.page_up_one_screen();
assert_eq!(e.cursor_row, 75);
assert_eq!(e.scroll, 75);
}
#[test]
fn page_up_clamps_at_top_of_file() {
let mut e = editor_with_lines(50);
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.scroll = 5;
e.cursor_row = 5;
e.page_up_one_screen();
assert_eq!(e.cursor_row, 0);
assert_eq!(e.scroll, 0);
}
#[test]
fn page_size_falls_back_when_viewport_is_unknown() {
// Before the first render last_inner is zero-sized; PageDown should
// still advance by some sensible default rather than no-op.
let mut e = editor_with_lines(100);
e.last_inner = Rect::default();
e.page_down_one_screen();
assert!(e.cursor_row > 0, "should advance even with zero last_inner");
}
fn editor_with_lines(n: usize) -> Editor {
let mut e = Editor::new();
e.lines = (0..n).map(|i| format!("line {i}")).collect();
e
}
#[test]
fn move_word_right_jumps_to_end_of_current_word() {
let mut e = editor_with("hello world");
e.cursor_row = 0;
e.cursor_col = 0;
e.move_word_right();
assert_eq!((e.cursor_row, e.cursor_col), (0, 5));
}
#[test]
fn move_word_right_skips_intervening_whitespace_and_lands_at_end_of_next_word() {
let mut e = editor_with("hello world!!!end");
e.cursor_row = 0;
e.cursor_col = 5;
e.move_word_right();
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 13),
"from end of 'hello' the next forward-word skips the spaces and stops at end of 'world'"
);
e.move_word_right();
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 19),
"second forward-word skips '!!!' and lands at end of 'end'"
);
}
#[test]
fn move_word_right_at_eol_advances_to_first_word_boundary_on_next_line() {
let mut e = editor_with("foo\n bar baz");
e.cursor_row = 0;
e.cursor_col = 3;
e.move_word_right();
assert_eq!((e.cursor_row, e.cursor_col), (1, 5));
}
#[test]
fn move_word_right_at_eof_is_a_noop() {
let mut e = editor_with("hi");
e.cursor_row = 0;
e.cursor_col = 2;
e.move_word_right();
assert_eq!((e.cursor_row, e.cursor_col), (0, 2));
}
#[test]
fn move_word_right_treats_underscore_as_part_of_the_word() {
let mut e = editor_with("snake_case rest");
e.cursor_row = 0;
e.cursor_col = 0;
e.move_word_right();
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 10),
"underscore is a word char so the whole identifier is one word"
);
}
#[test]
fn move_word_left_jumps_to_start_of_current_word() {
let mut e = editor_with("hello world");
e.cursor_row = 0;
e.cursor_col = 11;
e.move_word_left();
assert_eq!((e.cursor_row, e.cursor_col), (0, 6));
}
#[test]
fn move_word_left_skips_back_over_whitespace_and_punctuation() {
let mut e = editor_with("hello world!!!end");
e.cursor_row = 0;
e.cursor_col = 19;
e.move_word_left();
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 16),
"from end of 'end' the backward-word lands at start of 'end'"
);
e.move_word_left();
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 8),
"next backward-word skips '!!!' and lands at start of 'world'"
);
}
#[test]
fn move_word_left_at_bol_jumps_to_end_of_previous_line() {
let mut e = editor_with("foo bar\nbaz");
e.cursor_row = 1;
e.cursor_col = 0;
e.move_word_left();
assert_eq!(
(e.cursor_row, e.cursor_col),
(0, 4),
"backward-word from start of line 1 should cross the boundary and land at start of 'bar' on line 0"
);
}
#[test]
fn move_word_left_at_bof_is_a_noop() {
let mut e = editor_with("hi");
e.cursor_row = 0;
e.cursor_col = 0;
e.move_word_left();
assert_eq!((e.cursor_row, e.cursor_col), (0, 0));
}
#[test]
fn duplicate_lines_down_with_no_selection_copies_the_current_line_below_and_moves_cursor_to_it()
{
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
e.cursor_col = 2;
e.duplicate_lines_down();
assert_eq!(e.lines, vec!["alpha", "beta", "beta", "gamma"]);
assert_eq!((e.cursor_row, e.cursor_col), (2, 2));
assert!(e.dirty);
}
#[test]
fn duplicate_lines_up_with_no_selection_copies_the_current_line_above_and_keeps_cursor_on_the_copy()
{
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
e.cursor_col = 3;
e.duplicate_lines_up();
assert_eq!(e.lines, vec!["alpha", "beta", "beta", "gamma"]);
assert_eq!(
(e.cursor_row, e.cursor_col),
(1, 3),
"cursor stays at the same row index — that row is now the upper copy, the original was pushed to row 2"
);
}
#[test]
fn duplicate_lines_down_with_multiline_selection_duplicates_the_whole_block() {
let mut e = editor_with("a\nb\nc\nd");
e.cursor_row = 2;
e.cursor_col = 1;
e.selection = Some(EditorSelection {
anchor: (1, 0),
head: (2, 1),
});
e.duplicate_lines_down();
assert_eq!(e.lines, vec!["a", "b", "c", "b", "c", "d"]);
assert_eq!(
(e.cursor_row, e.cursor_col),
(4, 1),
"cursor follows the duplicated block — it was at (2,1) over the original 'c', now it's at (4,1) over the duplicate"
);
assert_eq!(
e.selection.unwrap(),
EditorSelection {
anchor: (3, 0),
head: (4, 1),
},
"the selection migrates onto the duplicated block too so the next gesture acts on the copy"
);
}
#[test]
fn duplicate_lines_up_with_multiline_selection_keeps_the_cursor_on_the_upper_copy() {
let mut e = editor_with("a\nb\nc\nd");
e.cursor_row = 2;
e.cursor_col = 0;
e.selection = Some(EditorSelection {
anchor: (1, 0),
head: (2, 1),
});
e.duplicate_lines_up();
assert_eq!(e.lines, vec!["a", "b", "c", "b", "c", "d"]);
assert_eq!(
(e.cursor_row, e.cursor_col),
(2, 0),
"the duplicate sits at rows 1-2 and the original was pushed down to rows 3-4; cursor at (2,0) is on the upper copy"
);
}
#[test]
fn duplicate_lines_down_then_undo_restores_the_buffer_in_one_step() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
e.cursor_col = 0;
e.duplicate_lines_down();
assert_eq!(e.lines.len(), 4);
assert!(e.undo());
assert_eq!(e.lines, vec!["alpha", "beta", "gamma"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 0));
}
#[test]
fn duplicate_lines_up_at_first_row_inserts_the_copy_at_row_zero() {
let mut e = editor_with("alpha\nbeta");
e.cursor_row = 0;
e.cursor_col = 4;
e.duplicate_lines_up();
assert_eq!(e.lines, vec!["alpha", "alpha", "beta"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 4));
}
#[test]
fn goto_top_lands_on_row_zero_col_zero() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 2;
e.cursor_col = 4;
e.goto_top();
assert_eq!((e.cursor_row, e.cursor_col), (0, 0));
}
#[test]
fn goto_bottom_lands_on_last_row_col_zero() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 0;
e.cursor_col = 3;
e.goto_bottom();
assert_eq!((e.cursor_row, e.cursor_col), (2, 0));
}
#[test]
fn goto_line_uses_one_based_indexing_and_clamps_to_last_row() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.goto_line(2);
assert_eq!((e.cursor_row, e.cursor_col), (1, 0));
e.goto_line(999);
assert_eq!((e.cursor_row, e.cursor_col), (2, 0));
e.goto_line(0);
assert_eq!((e.cursor_row, e.cursor_col), (0, 0));
}
#[test]
fn yank_lines_one_returns_current_line_with_trailing_newline() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
let yanked = e.yank_lines(1);
assert_eq!(yanked, "beta\n");
assert_eq!(e.lines, vec!["alpha", "beta", "gamma"]);
}
#[test]
fn yank_lines_n_grabs_current_plus_below() {
let mut e = editor_with("alpha\nbeta\ngamma\ndelta");
e.cursor_row = 1;
let yanked = e.yank_lines(3);
assert_eq!(yanked, "beta\ngamma\ndelta\n");
}
#[test]
fn delete_lines_one_drops_current_row() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
let yanked = e.delete_lines(1);
assert_eq!(yanked, "beta\n");
assert_eq!(e.lines, vec!["alpha", "gamma"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 0));
}
#[test]
fn delete_lines_n_clamps_at_end_of_buffer() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
let yanked = e.delete_lines(99);
assert_eq!(yanked, "beta\ngamma\n");
assert_eq!(e.lines, vec!["alpha"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 0));
}
#[test]
fn delete_lines_leaves_an_empty_line_when_buffer_emptied() {
let mut e = editor_with("alpha\nbeta");
e.cursor_row = 0;
e.delete_lines(2);
assert_eq!(e.lines, vec![""]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 0));
}
#[test]
fn delete_lines_is_undoable_in_one_step() {
let mut e = editor_with("alpha\nbeta\ngamma");
e.cursor_row = 1;
e.delete_lines(1);
assert!(e.undo());
assert_eq!(e.lines, vec!["alpha", "beta", "gamma"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 0));
}
#[test]
fn kill_to_bol_removes_head_of_current_line() {
let mut e = editor_with("hello world\nnext");
e.cursor_row = 0;
e.cursor_col = 6;
let killed = e.kill_to_bol();
assert_eq!(killed, "hello ");
assert_eq!(e.lines, vec!["world", "next"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 0));
}
#[test]
fn kill_to_bol_at_col_zero_is_noop() {
let mut e = editor_with("hello\nnext");
e.cursor_row = 0;
e.cursor_col = 0;
let killed = e.kill_to_bol();
assert_eq!(killed, "");
assert_eq!(e.lines, vec!["hello", "next"]);
}
#[test]
fn open_line_below_inserts_empty_line_after_current() {
let mut e = editor_with("alpha\nbeta");
e.cursor_row = 0;
e.cursor_col = 3;
e.open_line_below();
assert_eq!(e.lines, vec!["alpha", "", "beta"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 0));
}
#[test]
fn open_line_below_inherits_indent_from_current_row() {
let mut e = editor_with(" alpha\nbeta");
e.cursor_row = 0;
e.cursor_col = 6;
e.open_line_below();
assert_eq!(e.lines, vec![" alpha", " ", "beta"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 4));
}
#[test]
fn open_line_above_inserts_empty_line_before_current() {
let mut e = editor_with("alpha\nbeta");
e.cursor_row = 1;
e.cursor_col = 2;
e.open_line_above();
assert_eq!(e.lines, vec!["alpha", "", "beta"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 0));
}
#[test]
fn open_line_above_inherits_indent_from_current_row() {
let mut e = editor_with("alpha\n beta");
e.cursor_row = 1;
e.cursor_col = 10;
e.open_line_above();
assert_eq!(e.lines, vec!["alpha", " ", " beta"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 8));
}
#[test]
fn kill_to_eol_removes_tail_of_current_line() {
let mut e = editor_with("hello world\nnext");
e.cursor_row = 0;
e.cursor_col = 5;
let killed = e.kill_to_eol();
assert_eq!(killed, " world");
assert_eq!(e.lines, vec!["hello", "next"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 5));
}
#[test]
fn kill_to_eol_at_eol_is_noop() {
let mut e = editor_with("hello\nnext");
e.cursor_row = 0;
e.cursor_col = 5;
let killed = e.kill_to_eol();
assert_eq!(killed, "");
assert_eq!(e.lines, vec!["hello", "next"]);
}
#[test]
fn move_left_crosses_line_boundary() {
let mut e = editor_with("abc\ndef");
e.cursor_row = 1;
e.cursor_col = 0;
e.move_left();
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 3);
}
#[test]
fn move_right_crosses_line_boundary() {
let mut e = editor_with("abc\ndef");
e.cursor_row = 0;
e.cursor_col = 3;
e.move_right();
assert_eq!(e.cursor_row, 1);
assert_eq!(e.cursor_col, 0);
}
#[test]
fn move_up_clamps_column() {
let mut e = editor_with("ab\nlongline");
e.cursor_row = 1;
e.cursor_col = 7;
e.move_up();
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 2);
}
#[test]
fn home_and_end() {
let mut e = editor_with("hello world");
e.cursor_col = 5;
e.home_line();
assert_eq!(e.cursor_col, 0);
e.end_line();
assert_eq!(e.cursor_col, 11);
}
#[test]
fn open_reads_file_and_splits_lines() {
let mut tmp = NamedTempFile::new().unwrap();
writeln!(tmp, "alpha").unwrap();
writeln!(tmp, "beta").unwrap();
write!(tmp, "gamma").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(
e.lines,
vec!["alpha".to_string(), "beta".to_string(), "gamma".to_string()]
);
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 0);
assert!(!e.dirty);
}
#[test]
fn open_detects_crlf_and_save_preserves_it() {
use std::io::Read;
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(b"alpha\r\nbeta\r\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(e.eol, LineEnding::Crlf, "CRLF file detected");
assert_eq!(e.lines, vec!["alpha".to_string(), "beta".to_string()]);
e.save_to_disk().unwrap();
let mut raw = Vec::new();
std::fs::File::open(tmp.path())
.unwrap()
.read_to_end(&mut raw)
.unwrap();
assert_eq!(
raw, b"alpha\r\nbeta",
"save re-applies CRLF, no trailing EOL"
);
}
#[test]
fn open_defaults_to_lf_for_unix_files() {
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(b"a\nb\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(e.eol, LineEnding::Lf);
}
#[test]
fn reopen_with_encoding_decodes_windows_1252() {
use std::io::Write as _;
// 0xE9 is 'é' in Windows-1252 but invalid UTF-8 (renders as the
// replacement char when first opened as UTF-8).
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(&[b'c', b'a', b'f', 0xE9]).unwrap();
tmp.flush().unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_ne!(e.lines[0], "café", "UTF-8 decode mangles the 0xE9 byte");
e.reopen_with_encoding(encoding_rs::WINDOWS_1252).unwrap();
assert_eq!(e.lines[0], "café", "Windows-1252 decodes 0xE9 as é");
assert_eq!(e.encoding, encoding_rs::WINDOWS_1252);
}
/// A same-path reload (the FS-sync sweep on a clean buffer, or an explicit
/// revert) re-enters `open`, which auto-detects. Without a guard that
/// throws away the encoding the user picked through "Reopen with
/// Encoding", and the bytes get decoded as UTF-8 — so the buffer fills
/// with replacement characters and the next save writes them over the
/// original file.
#[test]
fn a_reload_keeps_the_encoding_the_user_reopened_with() {
use std::io::Write as _;
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(&[b'c', b'a', b'f', 0xE9]).unwrap();
tmp.flush().unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.reopen_with_encoding(encoding_rs::WINDOWS_1252).unwrap();
// The file changes on disk, still Windows-1252 and still BOM-less.
std::fs::write(tmp.path(), [b't', b'h', 0xE9]).unwrap();
e.reload_if_clean().unwrap().unwrap();
assert_eq!(
e.encoding,
encoding_rs::WINDOWS_1252,
"a same-path reload must keep the encoding the user chose"
);
assert_eq!(e.lines[0], "thé", "and decode the new bytes with it");
}
/// A UTF-16 file must survive the whole round trip: open, edit, save,
/// open again. Two things blocked it. `Encoding::encode` is decode-only
/// for UTF-16 per the WHATWG spec, so it silently emitted UTF-8; and
/// `is_binary` runs BEFORE the BOM sniff, so UTF-16's NUL bytes were
/// rejected as binary — meaning croft could offer UTF-16 in the encoding
/// picker and then never read back what it wrote.
#[test]
fn a_utf16_file_survives_open_edit_save_open() {
let tmp = NamedTempFile::new().unwrap();
let mut bytes = vec![0xFF, 0xFE]; // UTF-16LE BOM
for u in "hi\nthere\n".encode_utf16() {
bytes.extend_from_slice(&u.to_le_bytes());
}
std::fs::write(tmp.path(), &bytes).unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(e.encoding, encoding_rs::UTF_16LE, "opened as UTF-16LE");
assert_eq!(e.lines[0], "hi");
assert_eq!(e.lines[1], "there");
e.cursor_col = 2;
e.insert_char('!');
e.save_to_disk().unwrap();
let out = std::fs::read(tmp.path()).unwrap();
assert_eq!(&out[..2], &[0xFF, 0xFE], "the BOM must survive");
let mut e2 = Editor::new();
e2.open(tmp.path())
.expect("croft must be able to reopen what it just wrote");
assert_eq!(e2.encoding, encoding_rs::UTF_16LE);
assert_eq!(e2.lines[0], "hi!", "the edit round-tripped");
assert_eq!(e2.lines[1], "there");
}
/// UTF-16 is self-describing only via its BOM: without one, the bytes are
/// NUL-laden and no tool (croft included) can tell them from binary. VS
/// Code always writes it for its UTF-16 entries, so a file that never had
/// one still gets one when saved as UTF-16.
#[test]
fn saving_as_utf16_always_writes_a_bom() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "plain\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert!(!e.bom, "the source file had no BOM");
e.encoding = encoding_rs::UTF_16BE;
e.dirty = true;
e.save_to_disk().unwrap();
let out = std::fs::read(tmp.path()).unwrap();
assert_eq!(&out[..2], &[0xFE, 0xFF], "UTF-16BE BOM written anyway");
let mut e2 = Editor::new();
e2.open(tmp.path()).expect("and it reopens");
assert_eq!(e2.lines[0], "plain");
}
/// A UTF-8 BOM is stripped by `decode` and nothing re-emitted it, so every
/// save quietly dropped it.
#[test]
fn saving_keeps_a_utf8_bom() {
let tmp = NamedTempFile::new().unwrap();
let mut bytes = vec![0xEF, 0xBB, 0xBF];
bytes.extend_from_slice(b"hi\n");
std::fs::write(tmp.path(), &bytes).unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(e.lines[0], "hi");
e.cursor_col = 2;
e.insert_char('!');
e.save_to_disk().unwrap();
let out = std::fs::read(tmp.path()).unwrap();
assert_eq!(&out[..3], &[0xEF, 0xBB, 0xBF], "the UTF-8 BOM must survive");
assert_eq!(&out[3..6], b"hi!");
}
#[test]
fn set_language_updates_the_label() {
let mut e = editor_with("x = 1");
e.set_language(Some(crate::highlight::LangKind::Python));
assert_eq!(e.language_label(), "Python");
e.set_language(None);
assert_eq!(e.language_label(), "Plain Text");
}
#[test]
fn open_splits_on_lone_cr_to_match_lsp_line_numbering() {
// A file with a stray lone `\r` (mixed line endings). The LSP/VS Code
// treat `\r`, `\r\n`, and `\n` all as line breaks, so its token
// positions count this as a line boundary. Rust's `str::lines` only
// breaks on `\n`, so croft must split the same way or every LSP
// position past the stray CR lands one row off.
let mut tmp = NamedTempFile::new().unwrap();
// "a" <LF> "" (the lone CR) "b" <CRLF> "c"
write!(tmp, "a\n\rb\r\nc").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(
e.lines,
vec![
"a".to_string(),
String::new(),
"b".to_string(),
"c".to_string()
],
"lone CR and CRLF must each split a line, matching the LSP"
);
}
#[test]
fn open_png_populates_image_view_and_skips_text_buffer() {
// 1×1 transparent PNG, hand-crafted via the image crate.
let img: image::RgbaImage = image::ImageBuffer::from_pixel(1, 1, image::Rgba([0, 0, 0, 0]));
let mut buf: Vec<u8> = Vec::new();
image::DynamicImage::ImageRgba8(img)
.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
.unwrap();
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("pic.png");
std::fs::write(&path, &buf).unwrap();
let mut e = Editor::new();
e.open(&path).unwrap();
let img = e.image.as_ref().expect("image-mode tab");
assert_eq!(img.pixel_w, 1);
assert_eq!(img.pixel_h, 1);
assert_eq!(img.format_label, "PNG");
// Text scaffolding should be inert.
assert_eq!(e.lines, vec![String::new()]);
assert!(e.path.is_some());
assert!(e.lang.is_none());
assert!(!e.dirty);
}
/// A file rebuilt on disk reloads to the same path (and, for a PDF, the
/// same page and rect) - the overlay's re-emit key tells the loads apart
/// only through the content generation, so every reload must stamp a
/// fresh one even when the bytes happen to be identical.
#[test]
fn a_reloaded_image_carries_a_fresh_generation() {
let img: image::RgbaImage = image::ImageBuffer::from_pixel(1, 1, image::Rgba([0, 0, 0, 0]));
let mut buf: Vec<u8> = Vec::new();
image::DynamicImage::ImageRgba8(img)
.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
.unwrap();
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("pic.png");
std::fs::write(&path, &buf).unwrap();
let mut e = Editor::new();
e.open(&path).unwrap();
let first = e.image.as_ref().unwrap().generation;
e.open(&path).unwrap();
let second = e.image.as_ref().unwrap().generation;
assert_ne!(
first, second,
"a reload must stamp fresh content, or the baked overlay stays stale"
);
}
/// The scrollbar is drawn against `lines + comment-box rows`, so the
/// drag must map through the same content length. Mapping through bare
/// `lines.len()` made the bar of a short file with a tall navigator
/// comment completely dead (metrics said "no overflow"), and made a
/// long file's thumb run away from the pointer.
#[test]
fn dragging_the_scrollbar_of_a_file_with_a_comment_box_scrolls() {
let mut e = editor_with("a\nb\nc\nd\ne");
e.comment_boxes.push(CommentBox {
id: 1,
line: 1,
author: String::from("navigator"),
body: (0..20)
.map(|i| format!("line {i}"))
.collect::<Vec<_>>()
.join("\n"),
});
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e as &mut Editor).render(area, &mut buf);
assert!(
e.last_scrollbar.width > 0,
"the box overflows the viewport, so a bar must be drawn"
);
assert!(
e.scroll_to_bar_y(e.last_scrollbar.y + e.last_scrollbar.height / 2),
"a drag on the drawn bar must scroll, not be refused as no-overflow"
);
}
/// End means "last page", which is unanswerable when the page count is
/// unknown (sips-only Macs where mdls reports nothing): the sentinel
/// must not be rendered literally, which sprayed
/// "PDF page 4294967295 failed" into the status bar.
#[test]
fn end_with_an_unknown_page_count_reports_instead_of_rendering_the_sentinel() {
let mut e = Editor::new();
e.image = Some(ImageView {
bytes: Vec::new(),
format_label: String::from("PDF"),
pixel_w: 1,
pixel_h: 1,
byte_size: 0,
generation: 0,
pdf: Some(PdfState {
source_path: PathBuf::from("/nonexistent/doc.pdf"),
current_page: 1,
page_count: None,
backend: crate::pdf::PdfBackend::SipsCli,
source_byte_size: 0,
links: None,
}),
});
assert!(!e.set_pdf_page(u32::MAX));
assert!(
!e.status.contains("4294967295"),
"the sentinel page must not leak into the status: {}",
e.status
);
assert_eq!(e.pdf_page(), Some(1), "the page must not move");
}
#[test]
fn extension_is_image_recognises_common_formats() {
for ext in ["png", "PNG", "jpg", "jpeg", "JPEG", "gif", "bmp", "webp"] {
assert!(extension_is_image(ext), "should recognise: {ext}");
}
for ext in ["txt", "rs", "md", "py", ""] {
assert!(!extension_is_image(ext), "should not recognise: {ext}");
}
}
#[test]
fn open_unrecognised_file_after_image_clears_image_view() {
// Open an image, then a text file in the same Editor — `image`
// must reset so the text-rendering path comes back.
let img: image::RgbaImage = image::ImageBuffer::from_pixel(1, 1, image::Rgba([0, 0, 0, 0]));
let mut buf: Vec<u8> = Vec::new();
image::DynamicImage::ImageRgba8(img)
.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
.unwrap();
let dir = tempfile::tempdir().unwrap();
let img_path = dir.path().join("pic.png");
std::fs::write(&img_path, &buf).unwrap();
let txt_path = dir.path().join("hi.txt");
std::fs::write(&txt_path, "hi\n").unwrap();
let mut e = Editor::new();
e.open(&img_path).unwrap();
assert!(e.image.is_some());
e.open(&txt_path).unwrap();
assert!(e.image.is_none());
assert_eq!(e.lines, vec!["hi".to_string()]);
}
#[test]
fn open_rejects_binary_files() {
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(b"\x00\x01\x02binary garbage").unwrap();
let mut e = Editor::new();
let err = e.open(tmp.path()).unwrap_err();
assert!(err.to_string().to_lowercase().contains("binary"));
}
#[test]
fn matches_open_path_handles_canonical_difference() {
let mut tmp = NamedTempFile::new().unwrap();
writeln!(tmp, "x").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert!(e.matches_open_path(tmp.path()));
let bogus = std::path::Path::new("/definitely/not/the/same/path.txt");
assert!(!e.matches_open_path(bogus));
}
#[test]
fn reload_if_clean_picks_up_external_changes() {
let mut tmp = NamedTempFile::new().unwrap();
writeln!(tmp, "def hello():").unwrap();
writeln!(tmp, " print(\"hi\")").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert!(e.lines[0].contains("hello"));
// Simulate an external edit (vim, git pull, etc.).
std::fs::write(tmp.path(), "def hi():\n print(\"hi\")\n").unwrap();
let outcome = e.reload_if_clean();
assert!(matches!(outcome, Some(Ok(()))));
assert_eq!(e.lines[0], "def hi():");
assert!(!e.dirty);
}
#[test]
fn external_reload_bumps_edit_seq_and_drops_stale_semantic_overlay() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "def hello():\n pass\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
let seq_before = e.edit_seq;
let legend = std::sync::Arc::new(vec![String::from("function")]);
e.apply_semantic_tokens(tmp.path().to_path_buf(), vec![0, 4, 5, 0, 0], legend, true);
assert!(e.semantic_overlay_for_test().iter().any(|l| !l.is_empty()));
std::fs::write(tmp.path(), "x = 1\n").unwrap();
assert!(matches!(
e.reload_or_flag_conflict(),
ExternalChange::Reloaded
));
// The LSP doc resync is keyed on edit_seq; if the reload doesn't move
// it, the server never hears about the new content and never sends
// fresh semantic tokens.
assert_ne!(e.edit_seq, seq_before);
// The old token batch was measured against the old text; decoding it
// over the new lines paints wrong colors, so it must be dropped.
assert!(e.semantic_overlay_for_test().iter().all(|l| l.is_empty()));
}
#[test]
fn reload_if_clean_refuses_when_dirty() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "original\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_str("local edit");
assert!(e.dirty);
std::fs::write(tmp.path(), "external change\n").unwrap();
let outcome = e.reload_if_clean();
assert!(
outcome.is_none(),
"should refuse to reload over dirty buffer"
);
assert!(e.lines[0].contains("local edit"));
}
#[test]
fn save_round_trips_content() {
let tmp = NamedTempFile::new().unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_str("hello\nworld");
assert!(e.dirty);
e.save_to_disk().unwrap();
assert!(!e.dirty);
let written = std::fs::read_to_string(tmp.path()).unwrap();
assert_eq!(written, "hello\nworld");
}
#[test]
fn dirty_flag_lifecycle() {
let mut e = editor_with("abc");
assert!(!e.dirty);
e.insert_char('z');
assert!(e.dirty);
}
#[test]
fn switching_a_preview_tab_to_an_image_drops_the_stale_conflict_cache() {
// open_image swaps the whole buffer; a conflict list memoised on
// edit_seq from the previous text file must not survive, or a
// palette merge-resolve slices a one-line buffer with stale
// indices and panics.
let tmp = tempfile::tempdir().unwrap();
let mut e = Editor::new();
e.lines = vec![
"<<<<<<< HEAD".to_string(),
"ours".to_string(),
"=======".to_string(),
"theirs".to_string(),
">>>>>>> branch".to_string(),
];
e.mark_buffer_changed();
assert_eq!(e.conflicts().len(), 1, "the conflict parses");
let png = tmp.path().join("p.png");
image::RgbaImage::new(1, 1).save(&png).unwrap();
e.open_image(&png).unwrap();
assert!(
e.conflicts().is_empty(),
"an image tab has no merge conflicts; a stale cache here is a panic waiting in resolution_lines"
);
}
#[test]
fn undo_past_a_save_re_dirties_the_buffer() {
// Auto save writes one second after a keystroke; Cmd+Z then restores
// the pre-edit snapshot with dirty:false while disk keeps the edit.
// dirty means "buffer differs from disk", so crossing a save point
// backwards must re-dirty or the divergence is permanent and silent.
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "hello\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_char('x');
assert!(matches!(e.save_to_disk(), Ok(SaveOutcome::Saved)));
assert!(!e.dirty);
assert!(e.undo());
assert!(
e.dirty,
"the undone buffer no longer matches disk and must say so"
);
}
#[test]
fn a_save_breaks_insert_coalescing() {
// Typing after a save must open a new undo step: coalescing across
// the save point makes one Cmd+Z discard work from both sides of it.
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_char('x');
assert!(matches!(e.save_to_disk(), Ok(SaveOutcome::Saved)));
e.insert_char('y');
assert!(e.undo());
assert_eq!(
e.lines[0], "x",
"undo steps back to the save point, not past it"
);
}
#[test]
fn a_replacement_carriage_return_does_not_survive_in_a_line() {
let mut e = Editor::new();
e.lines = vec!["abc".to_string()];
e.replace_find_match(0, 1, 1, "x\r\ny");
assert_eq!(e.lines, vec!["ax".to_string(), "yc".to_string()]);
assert!(e.lines.iter().all(|l| !l.contains('\r')));
}
#[test]
fn a_multi_line_replacement_splices_into_separate_lines() {
// A regex replacement containing a real newline (VS Code's \n) must
// become two buffer lines, never an embedded control char in one.
let mut e = Editor::new();
e.lines = vec!["abc".to_string()];
e.replace_find_match(0, 1, 1, "x\ny");
assert_eq!(e.lines, vec!["ax".to_string(), "yc".to_string()]);
assert_eq!(
(e.cursor_row, e.cursor_col),
(1, 1),
"caret after the inserted text"
);
}
#[test]
fn an_external_reload_moves_the_edit_seq_past_an_in_flight_token_request() {
// Semantic-token replies now echo the `edit_seq` their request was
// fired for, and the app drops a reply whose seq no longer matches.
// That guard is only meaningful while a reload actually MOVES the
// seq: drop the bump and a batch computed against the old text
// matches again, paints the new buffer at the old offsets, and gets
// persisted to the on-disk cache under the new content's key.
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "fn a() {}\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.apply_semantic_tokens(
tmp.path().to_path_buf(),
vec![0, 0, 2, 0, 0],
std::sync::Arc::new(vec!["keyword".to_string()]),
true,
);
let in_flight = e.edit_seq;
std::fs::write(tmp.path(), "fn bbbb() {}\nfn c() {}\n").unwrap();
e.reload_from_disk().unwrap();
assert_ne!(
e.edit_seq, in_flight,
"a reload must invalidate every in-flight token request"
);
assert!(
e.semantic_data.is_empty(),
"and drop the batch measured against the old text"
);
}
/// `open` also serves the same-path reload, so clearing the fold set there
/// unconditionally made every FS-sync sweep pop the user's blocks open:
/// a `git checkout`, an external formatter, or another split saving the
/// file. Only a DIFFERENT file arriving in this tab invalidates them.
#[test]
fn an_external_reload_of_the_same_file_keeps_its_folds() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "def f():\n a\n b\ntail\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.toggle_fold(0);
assert!(e.is_line_hidden(1), "the block starts collapsed");
// An external rewrite of the same shape: same line count, same length,
// so only the mtime moves.
std::fs::write(tmp.path(), "def f():\n A\n B\ntail\n").unwrap();
let newer = std::time::SystemTime::now() + std::time::Duration::from_secs(2);
std::fs::OpenOptions::new()
.write(true)
.open(tmp.path())
.unwrap()
.set_modified(newer)
.unwrap();
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Reloaded);
assert_eq!(e.lines[1], " A", "the reload landed");
assert!(
e.is_line_hidden(1),
"the fold must survive a reload of the file it was set on"
);
}
/// Keeping `folded` across a same-path reload is only half the job: the
/// cached spans were measured against the OLD text. A reload that keeps the
/// line count but moves the indentation (an external formatter, a
/// `git checkout`) slips past the render-time `fold_epoch_lines` guard, so
/// the stale cache goes on hiding lines the fold no longer covers.
#[test]
fn a_same_path_reload_remeasures_the_fold_spans() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "def f():\n a\n b\ntail\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.toggle_fold(0);
assert!(e.is_line_hidden(1), "the block starts collapsed");
// Same line count, same length: only the indentation moved, so the
// header no longer has a body to fold.
std::fs::write(tmp.path(), "def f():\na \nb \ntail\n").unwrap();
let newer = std::time::SystemTime::now() + std::time::Duration::from_secs(2);
std::fs::OpenOptions::new()
.write(true)
.open(tmp.path())
.unwrap()
.set_modified(newer)
.unwrap();
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Reloaded);
assert_eq!(e.lines[1], "a ", "the reload landed");
assert!(
!e.is_line_hidden(1),
"a line the fold no longer covers must not stay hidden"
);
}
#[test]
fn reload_or_flag_conflict_reloads_clean_buffer() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "old\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
// External edit grows the file so mtime and len both move.
std::fs::write(tmp.path(), "new content here\n").unwrap();
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Reloaded);
assert_eq!(e.lines[0], "new content here");
assert!(!e.disk_conflict);
// A second sweep with no further change is a no-op.
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Unchanged);
}
#[test]
fn reload_or_flag_conflict_flags_dirty_buffer_without_clobbering() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "original\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_str("my unsaved edit");
std::fs::write(tmp.path(), "external change\n").unwrap();
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Conflict);
assert!(
e.lines[0].contains("my unsaved edit"),
"dirty buffer must NOT be reloaded over"
);
assert!(e.disk_conflict);
}
#[test]
fn reload_or_flag_conflict_fires_once_then_stays_quiet() {
// A dirty buffer whose disk keeps differing must announce the conflict
// exactly once so the confirm popup doesn't reopen on every FS poll.
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "original\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_str("my unsaved edit");
std::fs::write(tmp.path(), "external change\n").unwrap();
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Conflict);
// Disk still differs and the buffer is still dirty, but the conflict
// has already been announced.
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Unchanged);
assert!(e.disk_conflict);
}
#[test]
fn revert_paths_to_disk_discards_edits_and_reloads() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "original\n").unwrap();
let mut tabs = EditorTabs::new();
tabs.open_pinned(tmp.path()).unwrap();
tabs.insert_str("my unsaved edit"); // deref to the active editor
std::fs::write(tmp.path(), "external change\n").unwrap();
let reverted = tabs.revert_paths_to_disk(&[tmp.path().to_path_buf()]);
assert_eq!(reverted, vec![tmp.path().to_path_buf()]);
assert_eq!(tabs.lines[0], "external change");
assert!(!tabs.dirty);
assert!(!tabs.disk_conflict);
}
#[test]
fn reload_or_flag_conflict_unchanged_when_disk_matches() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "stable\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
assert_eq!(e.reload_or_flag_conflict(), ExternalChange::Unchanged);
}
#[test]
fn save_refuses_to_clobber_external_change_then_force_overwrites() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "original\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.insert_str("local");
// Someone else writes the file after we opened it.
std::fs::write(tmp.path(), "theirs\n").unwrap();
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::DiskConflict);
assert_eq!(
std::fs::read_to_string(tmp.path()).unwrap(),
"theirs\n",
"a guarded save must NOT clobber the external change"
);
assert!(e.disk_conflict);
// Explicit force overwrites with our buffer and clears the conflict.
e.save_to_disk_force().unwrap();
assert!(
std::fs::read_to_string(tmp.path())
.unwrap()
.contains("local")
);
assert!(!e.disk_conflict);
}
/// A legacy encoding that cannot represent the buffer must never write
/// silently: `encoding_rs` substitutes HTML numeric character references
/// (NOT `?`), so the file ends up holding ASCII text that no longer
/// round-trips, while the buffer still shows the originals.
#[test]
fn saving_text_the_encoding_cannot_represent_is_refused_not_silently_mangled() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "cafe\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.reopen_with_encoding(encoding_rs::WINDOWS_1252).unwrap();
// € lives at 0x80 in Windows-1252, so only the CJK is unmappable.
e.lines = vec![String::from("日本語 costs 5€")];
e.dirty = true;
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::EncodingLoss);
assert_eq!(
std::fs::read_to_string(tmp.path()).unwrap(),
"cafe\n",
"a refused save must leave the file untouched"
);
assert!(e.encoding_loss, "the refusal latches so auto save skips it");
assert_eq!(e.unmappable_chars(), vec!['日', '本', '語']);
// Explicit consent (the second Cmd+S) writes the replacements.
e.lossy_save_armed = true;
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::Saved);
assert_eq!(
std::fs::read(tmp.path()).unwrap(),
b"日本語 costs 5\x80",
"consent writes encoding_rs' references, and € as the 0x80 byte"
);
assert!(!e.encoding_loss);
assert!(!e.lossy_save_armed, "consent is one-shot");
}
/// The refusal must not fire for a buffer the encoding covers, and must
/// not fire at all for UTF-8/UTF-16, which cover everything.
#[test]
fn a_representable_buffer_saves_without_a_prompt() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "cafe\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.reopen_with_encoding(encoding_rs::WINDOWS_1252).unwrap();
e.lines = vec![String::from("café")];
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::Saved);
assert_eq!(std::fs::read(tmp.path()).unwrap(), b"caf\xE9");
assert!(e.unmappable_chars().is_empty());
// The same text in UTF-8 is always representable.
let mut u = Editor::new();
u.open(tmp.path()).unwrap();
u.lines = vec![String::from("日本語")];
assert_eq!(u.encoding, encoding_rs::UTF_8);
assert_eq!(u.save_to_disk().unwrap(), SaveOutcome::Saved);
assert!(u.unmappable_chars().is_empty());
}
/// Switching the buffer to an encoding that covers it must clear the
/// latch, or auto save would stay wedged on a file that is now fine.
#[test]
fn reopening_with_another_encoding_clears_the_encoding_loss_latch() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "日本語").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.encoding = encoding_rs::WINDOWS_1252;
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::EncodingLoss);
assert!(e.encoding_loss);
e.reopen_with_encoding(encoding_rs::UTF_8).unwrap();
assert!(!e.encoding_loss);
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::Saved);
}
/// Armed consent covers the buffer the prompt described. An edit changes
/// what the write would destroy, so it must revoke the stale consent (and
/// the auto-save latch) and make the next save prompt afresh — otherwise
/// characters typed after the prompt get mangled under a consent that
/// never named them.
#[test]
fn an_edit_after_a_lossy_refusal_revokes_the_armed_consent() {
let tmp = NamedTempFile::new().unwrap();
std::fs::write(tmp.path(), "cafe\n").unwrap();
let mut e = Editor::new();
e.open(tmp.path()).unwrap();
e.reopen_with_encoding(encoding_rs::WINDOWS_1252).unwrap();
e.lines = vec![String::from("日")];
e.dirty = true;
assert_eq!(e.save_to_disk().unwrap(), SaveOutcome::EncodingLoss);
e.lossy_save_armed = true; // the app arms the second Cmd+S...
e.insert_char('本'); // ...but the user edits instead of pressing it
assert!(!e.lossy_save_armed, "an edit revokes the stale consent");
assert!(
!e.encoding_loss,
"the latch clears too, so a fixed-up buffer resumes auto save"
);
assert_eq!(
e.save_to_disk().unwrap(),
SaveOutcome::EncodingLoss,
"the next save must prompt again for the changed buffer"
);
assert_eq!(std::fs::read_to_string(tmp.path()).unwrap(), "cafe\n");
}
#[test]
fn a_failed_external_reload_is_reported_not_swallowed() {
// #37: `ReloadFailed` was dropped on the floor — the user's file is
// replaced on disk by something unreadable (here: a directory), the
// sweep tries and fails to reload, and nothing said so.
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("f.txt");
std::fs::write(&path, "one\n").unwrap();
let mut tabs = EditorTabs::new();
tabs.open_pinned(&path).unwrap();
std::fs::remove_file(&path).unwrap();
std::fs::create_dir(&path).unwrap();
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert_eq!(
report.failed,
vec![path.clone()],
"the failure must surface in the report"
);
assert!(!report.is_empty(), "a failed reload is not an empty sweep");
assert_eq!(
tabs.iter_tabs().next().unwrap().lines[0],
"one",
"the last good buffer survives the failed reload"
);
}
#[test]
fn reload_externally_changed_tabs_reloads_background_tab() {
let dir = tempfile::tempdir().unwrap();
let a = dir.path().join("a.txt");
let b = dir.path().join("b.txt");
std::fs::write(&a, "a old\n").unwrap();
std::fs::write(&b, "b old\n").unwrap();
let mut tabs = EditorTabs::new();
tabs.open_pinned(&a).unwrap(); // tab for a
tabs.open_pinned(&b).unwrap(); // tab for b is now active; a is in the background
// External edit to the BACKGROUND file. The new content is the same
// length as the old ("a old\n" / "a NEW\n" are both 6 bytes), so the
// change is detectable only by mtime — and two back-to-back writes can
// land in the same mtime tick on a coarse-granularity filesystem,
// making detection flaky. Force a distinctly-newer mtime so the test is
// deterministic across filesystems (the production path keys off this
// same (mtime, len) stamp).
std::fs::write(&a, "a NEW\n").unwrap();
let bumped = std::time::SystemTime::now() + std::time::Duration::from_secs(2);
std::fs::OpenOptions::new()
.write(true)
.open(&a)
.unwrap()
.set_modified(bumped)
.unwrap();
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert_eq!(report.reloaded, vec![a.clone()]);
assert!(report.conflicts.is_empty());
// A skipped path (a collab guest's shared file) is left untouched.
std::fs::write(&b, "b NEW\n").unwrap();
std::fs::OpenOptions::new()
.write(true)
.open(&b)
.unwrap()
.set_modified(bumped)
.unwrap();
let report = tabs.reload_externally_changed_tabs(&|p| p == b.as_path());
assert!(report.reloaded.is_empty());
let b_tab = tabs
.iter_tabs()
.find(|e| e.path.as_deref() == Some(b.as_path()))
.unwrap();
assert_eq!(b_tab.lines[0], "b old");
let a_tab = tabs
.iter_tabs()
.find(|e| e.path.as_deref() == Some(a.as_path()))
.unwrap();
assert_eq!(a_tab.lines[0], "a NEW");
}
/// The field gesture behind the pdftoppm stderr fix: a PDF is open while
/// pdflatex rewrites it. Mid-write the file is truncated garbage - the
/// FS-sync sweep must fail quietly and keep the last good page, then
/// reload once the write completes.
#[test]
fn open_pdf_tab_keeps_last_good_page_through_a_mid_write_sweep() {
if crate::pdf::detect_backend().is_none() {
eprintln!("skipping: no PDF rasteriser installed");
return;
}
let one_page_pdf = concat!(
"%PDF-1.4\n",
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n",
"2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n",
"3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 4 0 R>>endobj\n",
"4 0 obj<</Length 27>>stream\n0 0 1 rg 10 10 100 100 re f\nendstream endobj\n",
"trailer<</Root 1 0 R/Size 5>>\n%%EOF\n",
);
let bump_mtime = |path: &Path, secs: u64| {
let newer = std::time::SystemTime::now() + std::time::Duration::from_secs(secs);
std::fs::OpenOptions::new()
.write(true)
.open(path)
.unwrap()
.set_modified(newer)
.unwrap();
};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("presentation.pdf");
std::fs::write(&path, one_page_pdf).unwrap();
let mut tabs = EditorTabs::new();
tabs.open_pinned(&path).unwrap();
let good_bytes = {
let tab = tabs.iter_tabs().next().unwrap();
tab.image
.as_ref()
.expect("PDF tab renders a page")
.bytes
.clone()
};
// pdflatex truncates and starts rewriting the file.
std::fs::write(&path, b"%PDF-1.7\ntruncated mid-write, no xref").unwrap();
bump_mtime(&path, 2);
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert!(
report.reloaded.is_empty(),
"a failed render is not a reload"
);
assert!(
report.conflicts.is_empty(),
"a clean tab is never a conflict"
);
let tab = tabs.iter_tabs().next().unwrap();
assert_eq!(
tab.image
.as_ref()
.expect("last good page must survive")
.bytes,
good_bytes,
"mid-write failure must keep showing the last good render"
);
// The write completes; the next sweep picks it up.
std::fs::write(&path, one_page_pdf).unwrap();
bump_mtime(&path, 4);
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert_eq!(report.reloaded, vec![path.clone()]);
let tab = tabs.iter_tabs().next().unwrap();
assert!(tab.image.is_some(), "completed write renders again");
}
/// Reading page 2 of a deck while pdflatex rebuilds the file: the
/// FS-sync reload must come back on page 2, not snap the reader to
/// page 1 and lose their place.
#[test]
fn pdf_reload_keeps_the_current_page() {
if crate::pdf::detect_backend().is_none() {
eprintln!("skipping: no PDF rasteriser installed");
return;
}
let two_page_pdf = concat!(
"%PDF-1.4\n",
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n",
"2 0 obj<</Type/Pages/Kids[3 0 R 5 0 R]/Count 2>>endobj\n",
"3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 4 0 R>>endobj\n",
"4 0 obj<</Length 27>>stream\n0 0 1 rg 10 10 100 100 re f\nendstream endobj\n",
"5 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 6 0 R>>endobj\n",
"6 0 obj<</Length 27>>stream\n1 0 0 rg 10 10 100 100 re f\nendstream endobj\n",
"trailer<</Root 1 0 R/Size 7>>\n%%EOF\n",
);
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("deck.pdf");
std::fs::write(&path, two_page_pdf).unwrap();
let mut tabs = EditorTabs::new();
tabs.open_pinned(&path).unwrap();
assert!(tabs.change_pdf_page(1), "two pages, so page 2 renders");
assert_eq!(tabs.pdf_page(), Some(2));
// pdflatex rewrites the file (same content is enough; only the
// disk stamp needs to move).
std::fs::write(&path, two_page_pdf).unwrap();
let newer = std::time::SystemTime::now() + std::time::Duration::from_secs(2);
std::fs::OpenOptions::new()
.write(true)
.open(&path)
.unwrap()
.set_modified(newer)
.unwrap();
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert_eq!(report.reloaded, vec![path.clone()]);
assert_eq!(
tabs.pdf_page(),
Some(2),
"an external rebuild must not move the reader back to page 1; status: {:?}",
tabs.iter_tabs().next().unwrap().status
);
}
/// The transient arm of the rebuild race (#72): the reader is on page 2,
/// the file is rebuilt, and the rasterisation of THEIR page fails once
/// (a spawn refused under load). That failure must flow into the
/// failed-reload path — last good render, place kept — never leave a
/// half-restored tab sitting on page 1. The next sweep recovers.
#[test]
fn a_transient_page_render_failure_during_reload_keeps_the_readers_place() {
if crate::pdf::detect_backend().is_none() {
eprintln!("skipping: no PDF rasteriser installed");
return;
}
let two_page_pdf = concat!(
"%PDF-1.4\n",
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n",
"2 0 obj<</Type/Pages/Kids[3 0 R 5 0 R]/Count 2>>endobj\n",
"3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 4 0 R>>endobj\n",
"4 0 obj<</Length 27>>stream\n0 0 1 rg 10 10 100 100 re f\nendstream endobj\n",
"5 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 200 200]/Contents 6 0 R>>endobj\n",
"6 0 obj<</Length 27>>stream\n1 0 0 rg 10 10 100 100 re f\nendstream endobj\n",
"trailer<</Root 1 0 R/Size 7>>\n%%EOF\n",
);
let bump_mtime = |path: &Path, secs: u64| {
let newer = std::time::SystemTime::now() + std::time::Duration::from_secs(secs);
std::fs::OpenOptions::new()
.write(true)
.open(path)
.unwrap()
.set_modified(newer)
.unwrap();
};
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("deck.pdf");
std::fs::write(&path, two_page_pdf).unwrap();
let mut tabs = EditorTabs::new();
tabs.open_pinned(&path).unwrap();
assert!(tabs.change_pdf_page(1), "two pages, so page 2 renders");
let good_bytes = tabs
.iter_tabs()
.next()
.unwrap()
.image
.as_ref()
.unwrap()
.bytes
.clone();
std::fs::write(&path, two_page_pdf).unwrap();
bump_mtime(&path, 2);
*crate::pdf::FAIL_RASTERIZE_ONCE_FOR_TEST.lock().unwrap() = Some((path.clone(), 2));
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert!(
report.reloaded.is_empty(),
"a reload that cannot restore the reader's page is a failed \
reload, not a successful one that lost their place"
);
assert_eq!(
tabs.pdf_page(),
Some(2),
"a transient render failure must not snap the reader to page 1; status: {:?}",
tabs.iter_tabs().next().unwrap().status
);
assert_eq!(
tabs.iter_tabs()
.next()
.unwrap()
.image
.as_ref()
.unwrap()
.bytes,
good_bytes,
"the last good render survives the transient failure"
);
// The failure was transient: the next sweep reloads and the reader
// is still on their page.
bump_mtime(&path, 4);
let report = tabs.reload_externally_changed_tabs(&|_| false);
assert_eq!(report.reloaded, vec![path.clone()]);
assert_eq!(
tabs.pdf_page(),
Some(2),
"recovery re-renders the reader's page"
);
}
#[test]
fn insert_str_inserts_newlines() {
let mut e = editor_with("");
e.insert_str("a\nb\nc");
assert_eq!(
e.lines,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
assert_eq!(e.cursor_row, 2);
assert_eq!(e.cursor_col, 1);
}
#[test]
fn build_line_spans_no_highlights() {
let spans = build_line_spans("hello", &[]);
assert_eq!(spans.len(), 1);
}
fn chars(s: &str) -> Vec<char> {
s.chars().collect()
}
#[test]
fn wrap_segments_short_line_is_one_segment() {
assert_eq!(wrap_segments(&chars("hello"), 20), vec![(0, 5)]);
assert_eq!(wrap_segments(&chars(""), 20), vec![(0, 0)]);
// width 0 must not loop forever - one segment covering the line.
assert_eq!(wrap_segments(&chars("abc"), 0), vec![(0, 3)]);
}
#[test]
fn wrap_segments_breaks_after_a_space() {
// "the quick brown" at width 10 breaks after "the quick " (10 chars).
let segs = wrap_segments(&chars("the quick brown"), 10);
assert_eq!(segs, vec![(0, 10), (10, 15)]);
}
#[test]
fn wrap_segments_hard_breaks_a_too_long_word() {
// No space in the first 6 chars, so the word is split at the column.
let segs = wrap_segments(&chars("abcdefghij"), 6);
assert_eq!(segs, vec![(0, 6), (6, 10)]);
}
#[test]
fn wrap_segments_breaks_after_punctuation_like_vscode() {
// VS Code breaks after `/` (a break-after char), so a path folds at
// the slash rather than mid-segment.
let segs = wrap_segments(&chars("abc/defgh"), 6);
assert_eq!(segs, vec![(0, 4), (4, 9)]);
}
#[test]
fn wrap_segments_tile_the_line_without_gaps() {
let line = "alpha beta gamma delta epsilon zeta eta theta";
let segs = wrap_segments(&chars(line), 12);
assert_eq!(segs.first().unwrap().0, 0);
assert_eq!(segs.last().unwrap().1, line.chars().count());
for pair in segs.windows(2) {
assert_eq!(pair[0].1, pair[1].0, "segments must be contiguous");
}
}
#[test]
fn build_line_spans_full_line_highlighted() {
let hi = vec![HiSpan {
start: 0,
end: 5,
style: Style::default(),
}];
let spans = build_line_spans("hello", &hi);
assert_eq!(spans.len(), 1);
}
#[test]
fn build_line_spans_partial_highlights() {
let hi = vec![HiSpan {
start: 1,
end: 3,
style: Style::default(),
}];
let spans = build_line_spans("abcde", &hi);
// Expect: "a", "bc", "de"
assert_eq!(spans.len(), 3);
}
#[test]
fn merge_overlay_semantic_wins_and_base_fills_gaps() {
use ratatui::style::Color;
let base_style = Style::default().fg(Color::Rgb(1, 1, 1));
let over_style = Style::default().fg(Color::Rgb(2, 2, 2));
// Base covers the whole 0..10 line; overlay repaints 3..6.
let base = vec![HiSpan {
start: 0,
end: 10,
style: base_style,
}];
let over = vec![HiSpan {
start: 3,
end: 6,
style: over_style,
}];
let merged = merge_overlay(&base, &over);
// Expect base[0..3], over[3..6], base[6..10], sorted and gapless.
assert_eq!(merged.len(), 3);
assert_eq!((merged[0].start, merged[0].end), (0, 3));
assert_eq!(merged[0].style.fg, Some(Color::Rgb(1, 1, 1)));
assert_eq!((merged[1].start, merged[1].end), (3, 6));
assert_eq!(
merged[1].style.fg,
Some(Color::Rgb(2, 2, 2)),
"overlay must win over base where they overlap"
);
assert_eq!((merged[2].start, merged[2].end), (6, 10));
assert_eq!(merged[2].style.fg, Some(Color::Rgb(1, 1, 1)));
}
#[test]
fn merge_overlay_empty_overlay_returns_base() {
let base = vec![HiSpan {
start: 0,
end: 4,
style: Style::default(),
}];
assert_eq!(merge_overlay(&base, &[]).len(), 1);
}
#[test]
fn editor_selection_normalised_handles_anchor_after_head() {
let s = EditorSelection {
anchor: (5, 4),
head: (2, 1),
};
assert_eq!(s.normalised(), ((2, 1), (5, 4)));
}
#[test]
fn editor_selection_normalised_handles_same_row() {
let s = EditorSelection {
anchor: (3, 9),
head: (3, 2),
};
assert_eq!(s.normalised(), ((3, 2), (3, 9)));
}
#[test]
fn editor_selection_has_area_only_when_endpoints_differ() {
let s = EditorSelection::new(2, 5);
assert!(!s.has_area());
let s2 = EditorSelection {
anchor: (2, 5),
head: (2, 6),
};
assert!(s2.has_area());
}
#[test]
fn start_selection_at_cursor_creates_zero_area_selection() {
let mut e = editor_with("hello");
e.cursor_row = 0;
e.cursor_col = 2;
e.start_selection_at_cursor();
let sel = e.selection.expect("selection should exist");
assert_eq!(sel.anchor, (0, 2));
assert_eq!(sel.head, (0, 2));
assert!(!sel.has_area());
}
#[test]
fn extend_selection_to_cursor_updates_head_only() {
let mut e = editor_with("abcdef");
e.cursor_col = 1;
e.start_selection_at_cursor();
e.cursor_col = 4;
e.extend_selection_to_cursor();
let sel = e.selection.unwrap();
assert_eq!(sel.anchor, (0, 1));
assert_eq!(sel.head, (0, 4));
assert!(sel.has_area());
}
#[test]
fn selection_text_single_line() {
let mut e = editor_with("hello world");
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
assert_eq!(e.selection_text(), "world");
}
#[test]
fn selection_text_handles_reversed_endpoints() {
let mut e = editor_with("hello world");
e.cursor_col = 11;
e.start_selection_at_cursor();
e.cursor_col = 6;
e.extend_selection_to_cursor();
assert_eq!(e.selection_text(), "world");
}
#[test]
fn selection_text_multi_line_includes_newlines() {
let mut e = editor_with("first line\nsecond line\nthird");
e.cursor_row = 0;
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_row = 1;
e.cursor_col = 6;
e.extend_selection_to_cursor();
assert_eq!(e.selection_text(), "line\nsecond");
}
#[test]
fn selection_text_multibyte_chars() {
let mut e = editor_with("héllo");
e.cursor_col = 1;
e.start_selection_at_cursor();
e.cursor_col = 3;
e.extend_selection_to_cursor();
assert_eq!(e.selection_text(), "él");
}
#[test]
fn clear_selection_removes_it() {
let mut e = editor_with("abc");
e.start_selection_at_cursor();
assert!(e.selection.is_some());
e.clear_selection();
assert!(e.selection.is_none());
}
#[test]
fn delete_selection_removes_range_within_one_line() {
let mut e = editor_with("hello world");
e.cursor_col = 5;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
assert!(e.delete_selection());
assert_eq!(e.lines, vec!["hello".to_string()]);
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 5);
assert!(e.selection.is_none());
assert!(e.dirty);
}
#[test]
fn delete_selection_collapses_multiple_lines() {
let mut e = editor_with("first\nsecond\nthird");
e.cursor_row = 0;
e.cursor_col = 2;
e.start_selection_at_cursor();
e.cursor_row = 2;
e.cursor_col = 2;
e.extend_selection_to_cursor();
assert!(e.delete_selection());
assert_eq!(e.lines, vec!["fiird".to_string()]);
assert_eq!(e.cursor_row, 0);
assert_eq!(e.cursor_col, 2);
}
#[test]
fn delete_selection_returns_false_when_no_selection() {
let mut e = editor_with("abc");
assert!(!e.delete_selection());
assert_eq!(e.lines, vec!["abc".to_string()]);
}
#[test]
fn delete_selection_returns_false_when_zero_area() {
let mut e = editor_with("abc");
e.cursor_col = 1;
e.start_selection_at_cursor();
assert!(!e.delete_selection());
assert_eq!(e.lines, vec!["abc".to_string()]);
}
#[test]
fn insert_char_replaces_selection_when_active() {
let mut e = editor_with("hello world");
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
e.insert_char('X');
assert_eq!(e.lines, vec!["hello X".to_string()]);
assert_eq!(e.cursor_col, 7);
assert!(e.selection.is_none());
}
#[test]
fn backspace_deletes_selection_when_active() {
let mut e = editor_with("hello world");
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
e.backspace();
assert_eq!(e.lines, vec!["hello ".to_string()]);
assert!(e.selection.is_none());
}
#[test]
fn delete_forward_deletes_selection_when_active() {
let mut e = editor_with("hello world");
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
e.delete_forward();
assert_eq!(e.lines, vec!["hello ".to_string()]);
assert!(e.selection.is_none());
}
#[test]
fn insert_str_replaces_selection_when_active() {
let mut e = editor_with("hello world");
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
e.insert_str("everyone");
assert_eq!(e.lines, vec!["hello everyone".to_string()]);
assert!(e.selection.is_none());
}
#[test]
fn select_all_spans_entire_buffer() {
let mut e = editor_with("a\nbc\nd");
e.select_all();
let sel = e.selection.unwrap();
let (start, end) = sel.normalised();
assert_eq!(start, (0, 0));
assert_eq!(end, (2, 1));
assert_eq!(e.selection_text(), "a\nbc\nd");
}
#[test]
fn mouse_down_starts_zero_area_selection_at_click() {
let mut e = editor_with("hello");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
e.mouse_down(3, 0); // text_x = 0 + 2 + 1 = 3, click col 3 → editor col 0
assert_eq!(e.cursor_col, 0);
let sel = e.selection.expect("anchor created on mouse down");
assert_eq!(sel.anchor, (0, 0));
assert!(!sel.has_area());
}
#[test]
fn render_never_replaces_character_at_cursor() {
// The hardware caret (DECSCUSR BlinkingBar) overlays the cell at the
// cursor position; the editor's own render must NEVER change the
// symbol there or the blink would visibly swallow the letter.
use ratatui::buffer::Buffer;
let mut e = editor_with("hello");
e.cursor_col = 2;
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 5,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let cell = &buf[(text_x + 2, e.last_inner.y)];
assert_eq!(
cell.symbol(),
"l",
"editor render must leave the underlying glyph alone"
);
}
#[test]
fn cursor_screen_pos_inside_viewport() {
let mut e = editor_with("hello\nworld");
e.last_inner = Rect {
x: 5,
y: 7,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
e.cursor_row = 1;
e.cursor_col = 3;
// text_x = inner.x + gutter + 1 = 5 + 2 + 1 = 8
// cy = inner.y + (cursor_row - scroll) = 7 + 1 = 8
assert_eq!(e.cursor_screen_pos(), Some((8 + 3, 8)));
}
#[test]
fn cursor_screen_pos_returns_none_when_scrolled_off() {
let mut e = editor_with_lines(50);
e.last_inner = Rect {
x: 0,
y: 0,
width: 40,
height: 10,
};
e.last_gutter_width = 3;
e.scroll = 30;
e.cursor_row = 5; // above viewport
assert_eq!(e.cursor_screen_pos(), None);
}
#[test]
fn undo_restores_previous_buffer_and_cursor() {
let mut e = editor_with("abc");
e.cursor_col = 3;
e.insert_char('d');
assert_eq!(e.lines[0], "abcd");
assert!(e.undo());
assert_eq!(e.lines[0], "abc");
assert_eq!(e.cursor_col, 3);
}
#[test]
fn undo_on_empty_stack_returns_false() {
let mut e = editor_with("abc");
assert!(!e.undo());
assert_eq!(e.lines[0], "abc");
}
#[test]
fn undo_coalesces_consecutive_typed_chars_into_one_step() {
let mut e = editor_with("");
e.insert_char('h');
e.insert_char('i');
e.insert_char('!');
// One undo undoes the whole typed run "hi!".
assert!(e.undo());
assert_eq!(e.lines[0], "");
}
#[test]
fn undo_does_not_coalesce_across_movement() {
let mut e = editor_with("abc");
e.cursor_col = 3;
e.insert_char('d');
e.move_left();
e.insert_char('z');
// First undo removes 'z', second undo removes 'd'.
assert!(e.undo());
assert_eq!(e.lines[0], "abcd");
assert!(e.undo());
assert_eq!(e.lines[0], "abc");
}
#[test]
fn undo_does_not_coalesce_across_backspace() {
let mut e = editor_with("abc");
e.cursor_col = 3;
e.insert_char('d');
e.backspace();
e.insert_char('e');
assert!(e.undo());
assert_eq!(e.lines[0], "abc");
assert!(e.undo());
assert_eq!(e.lines[0], "abcd");
assert!(e.undo());
assert_eq!(e.lines[0], "abc");
}
#[test]
fn undo_paste_is_one_step() {
let mut e = editor_with("abc");
e.cursor_col = 3;
e.insert_str("XYZ");
assert_eq!(e.lines[0], "abcXYZ");
assert!(e.undo());
assert_eq!(e.lines[0], "abc");
}
#[test]
fn undo_after_replace_selection_restores_original() {
let mut e = editor_with("hello world");
e.cursor_col = 6;
e.start_selection_at_cursor();
e.cursor_col = 11;
e.extend_selection_to_cursor();
e.insert_char('X');
assert_eq!(e.lines[0], "hello X");
assert!(e.undo());
assert_eq!(e.lines[0], "hello world");
}
#[test]
fn undo_restores_dirty_flag() {
let mut e = editor_with("abc");
assert!(!e.dirty);
e.cursor_col = 3;
e.insert_char('d');
assert!(e.dirty);
e.undo();
assert!(!e.dirty, "undoing the only edit restores the clean state");
}
#[test]
fn editor_render_paints_search_match_cells_with_yellow_bg() {
use ratatui::buffer::Buffer;
let mut e = editor_with("alpha needle bravo needle zulu");
e.set_search_highlight(Some(String::from("needle")));
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 3,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
let yellow = Color::Rgb(0xff, 0xd7, 0x4a);
// First "needle" starts at char index 6 ("alpha "), 6 chars long.
for col in 6..12u16 {
assert_eq!(
buf[(text_x + col, y)].bg,
yellow,
"first match cell {col} must have yellow bg"
);
}
// Cells just outside the match must NOT be yellow.
assert_ne!(buf[(text_x + 5, y)].bg, yellow, "cell before match");
assert_ne!(buf[(text_x + 12, y)].bg, yellow, "cell after match");
// Second "needle" starts at char index 19 ("alpha needle bravo "), 6 chars.
for col in 19..25u16 {
assert_eq!(
buf[(text_x + col, y)].bg,
yellow,
"second match cell {col} must have yellow bg"
);
}
}
#[test]
fn editor_render_lights_other_occurrences_of_selection_in_blue() {
use ratatui::buffer::Buffer;
let mut e = editor_with("alpha needle bravo needle zulu");
// Select the first "needle" (chars 6..12).
e.selection = Some(EditorSelection {
anchor: (0, 6),
head: (0, 12),
});
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 3,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
let occurrence = Color::Rgb(0x37, 0x61, 0x8e);
let selection = Color::Rgb(0x26, 0x4f, 0x78);
// The selected occurrence wears the brighter selection band, not the
// dimmer occurrence blue.
for col in 6..12u16 {
assert_eq!(
buf[(text_x + col, y)].bg,
selection,
"selected occurrence cell {col} must show the selection band"
);
}
// The OTHER "needle" (chars 19..25) wears the occurrence blue.
for col in 19..25u16 {
assert_eq!(
buf[(text_x + col, y)].bg,
occurrence,
"other occurrence cell {col} must show the occurrence blue"
);
}
// A non-matching cell stays unpainted.
assert_ne!(buf[(text_x + 18, y)].bg, occurrence, "space before match");
}
#[test]
fn editor_render_skips_occurrence_highlight_for_whitespace_selection() {
use ratatui::buffer::Buffer;
let mut e = editor_with("a b c d");
// Select two spaces (chars 1..3) — whitespace only.
e.selection = Some(EditorSelection {
anchor: (0, 1),
head: (0, 3),
});
e.focused = true;
assert_eq!(e.selection_occurrence_needle(), None);
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 3,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
let occurrence = Color::Rgb(0x37, 0x61, 0x8e);
for col in 0..10u16 {
assert_ne!(
buf[(text_x + col, y)].bg,
occurrence,
"whitespace-only selection must not light occurrences"
);
}
}
#[test]
fn editor_render_skips_occurrence_highlight_for_multiline_selection() {
let mut e = editor_with("needle\nneedle");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (1, 6),
});
assert_eq!(
e.selection_occurrence_needle(),
None,
"multi-line selections do not drive occurrence highlighting"
);
}
#[test]
fn editor_render_does_not_paint_search_highlight_when_unset() {
use ratatui::buffer::Buffer;
let mut e = editor_with("alpha needle bravo");
e.set_search_highlight(None);
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 3,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
let yellow = Color::Rgb(0xff, 0xd7, 0x4a);
for col in 0..18u16 {
assert_ne!(
buf[(text_x + col, y)].bg,
yellow,
"no cell should be highlighted when search_highlight is None"
);
}
}
#[test]
fn editor_render_search_highlight_is_case_insensitive() {
use ratatui::buffer::Buffer;
let mut e = editor_with("Foo bar FOO baz");
e.set_search_highlight(Some(String::from("foo")));
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 3,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
let yellow = Color::Rgb(0xff, 0xd7, 0x4a);
for col in 0..3u16 {
assert_eq!(buf[(text_x + col, y)].bg, yellow, "cell {col} of 'Foo'");
}
for col in 8..11u16 {
assert_eq!(buf[(text_x + col, y)].bg, yellow, "cell {col} of 'FOO'");
}
}
#[test]
fn open_diff_replaces_blank_initial_tab_with_diff_view() {
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha\nbravo\ncharlie\n").unwrap();
std::fs::write(f2.path(), "alpha\nBRAVO\ncharlie\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
assert_eq!(t.tab_count(), 1, "blank initial slot should be reused");
let active = &t.editors[t.active_index()];
assert!(active.diff.is_some(), "active tab must hold diff data");
let diff = active.diff.as_ref().unwrap();
assert_eq!(diff.left_lines, vec!["alpha", "bravo", "charlie"]);
assert_eq!(diff.right_lines, vec!["alpha", "BRAVO", "charlie"]);
assert_eq!(diff.rows.len(), 3);
}
#[test]
fn tab_full_path_reports_the_whole_path_for_disambiguation() {
// The tab label only shows `file_name()`, so two `app.ts` tabs look
// identical. The hover tooltip must expose the full path to tell
// them apart.
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("app.ts");
std::fs::write(&p, "export const x = 1;\n").unwrap();
let mut t = EditorTabs::new();
t.open_pinned(&p).unwrap();
let idx = t.active_index();
assert_eq!(
t.tab_full_path(idx).as_deref(),
Some(p.to_string_lossy().as_ref()),
"tooltip must expose the full path, not just the file name"
);
}
#[test]
fn tab_full_path_is_none_for_an_unsaved_buffer() {
let t = EditorTabs::new();
assert_eq!(
t.tab_full_path(t.active_index()),
None,
"an untitled buffer has no path to disambiguate"
);
}
#[test]
fn tab_full_path_shows_both_sides_of_a_diff() {
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "a\n").unwrap();
std::fs::write(f2.path(), "b\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let label = t.tab_full_path(t.active_index()).unwrap();
assert!(label.contains(&f1.path().to_string_lossy().into_owned()));
assert!(label.contains(&f2.path().to_string_lossy().into_owned()));
assert!(label.contains('\u{2194}'), "diff tooltip shows both sides");
}
#[test]
fn diff_header_paints_prev_next_arrows_at_the_right_edge() {
// Regression: the user asked for clickable arrows in the diff
// header so they can hop between change hunks without
// hand-scrolling. The arrows must paint at the rightmost cells
// of the header row AND must populate the editor's hit-test
// fields so the click handler can route mouse events to
// prev/next-change navigation.
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha\nbravo\ncharlie\n").unwrap();
std::fs::write(f2.path(), "alpha\nBRAVO\ncharlie\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let active_idx = t.active_index();
t.editors[active_idx].focused = true;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 20,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut t.editors[active_idx], area, &mut buf);
let ed = &t.editors[active_idx];
assert!(
ed.diff_prev_arrow.width > 0,
"prev arrow rect must be tracked so a click can navigate to the previous hunk"
);
assert!(
ed.diff_next_arrow.width > 0,
"next arrow rect must be tracked so a click can navigate to the next hunk"
);
// Glyphs land where the rects say they do.
assert_eq!(
buf[(ed.diff_prev_arrow.x, ed.diff_prev_arrow.y)].symbol(),
"\u{2039}",
"prev arrow cell must paint ‹"
);
assert_eq!(
buf[(ed.diff_next_arrow.x, ed.diff_next_arrow.y)].symbol(),
"\u{203a}",
"next arrow cell must paint ›"
);
// Next sits to the right of prev on the same header row.
assert_eq!(ed.diff_prev_arrow.y, ed.diff_next_arrow.y);
assert!(ed.diff_prev_arrow.x < ed.diff_next_arrow.x);
// Both arrows sit inside the editor's inner rect's right band.
let right_edge = ed.last_inner.x + ed.last_inner.width;
assert!(ed.diff_next_arrow.x < right_edge);
}
#[test]
fn non_diff_tab_clears_diff_arrow_hit_rects_on_render() {
// Regression guard: switching from a diff tab to a regular file
// tab must NOT leave the stale arrow rects in place — otherwise
// a click at those cells on the new tab would mis-route as a
// change-navigation event.
let f = NamedTempFile::new().unwrap();
std::fs::write(f.path(), "alpha\nbravo\ncharlie\n").unwrap();
let mut ed = Editor::new();
ed.open(f.path()).unwrap();
ed.diff_prev_arrow = Rect {
x: 30,
y: 0,
width: 1,
height: 1,
};
ed.diff_next_arrow = Rect {
x: 32,
y: 0,
width: 1,
height: 1,
};
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut ed, area, &mut buf);
assert_eq!(ed.diff_prev_arrow, Rect::default());
assert_eq!(ed.diff_next_arrow, Rect::default());
}
#[test]
fn diff_hit_test_maps_click_in_left_text_column_to_left_side_and_char_col() {
use crate::widgets::diff::DiffSide;
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha\nbravo\ncharlie\n").unwrap();
std::fs::write(f2.path(), "alpha\nBRAVO\ncharlie\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let active_idx = t.active_index();
t.editors[active_idx].focused = true;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut t.editors[active_idx], area, &mut buf);
let ed = &t.editors[active_idx];
let diff = ed.diff.as_ref().unwrap();
// Header is at ed.last_inner.y; body starts at last_inner.y + 1.
let body_top = ed.last_inner.y + 1;
// Left text column begins at l_text_x = inner.x + l_gutter + 2.
let l_gutter = (diff.left_lines.len() + 1).to_string().len() as u16 + 1;
let l_text_x = ed.last_inner.x + l_gutter + 2;
let hit =
crate::widgets::editor::diff_hit_test(diff, ed.last_inner, l_text_x + 2, body_top + 1);
assert_eq!(
hit,
Some((DiffSide::Left, 1, 2)),
"a click two cells into the left text column of the second body row must map to Left, row 1, char col 2"
);
}
#[test]
fn diff_hit_test_maps_click_in_right_text_column_to_right_side() {
use crate::widgets::diff::DiffSide;
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha\nbravo\n").unwrap();
std::fs::write(f2.path(), "alpha\nBRAVO\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let active_idx = t.active_index();
t.editors[active_idx].focused = true;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut t.editors[active_idx], area, &mut buf);
let ed = &t.editors[active_idx];
let diff = ed.diff.as_ref().unwrap();
let body_top = ed.last_inner.y + 1;
let half = ed.last_inner.width / 2;
let r_gutter = (diff.right_lines.len() + 1).to_string().len() as u16 + 1;
let r_text_x = ed.last_inner.x + half + 1 + r_gutter + 2;
let hit =
crate::widgets::editor::diff_hit_test(diff, ed.last_inner, r_text_x + 3, body_top + 1);
assert!(
matches!(hit, Some((DiffSide::Right, 1, 3))),
"a click three cells into the right text column of the second body row must map to Right, row 1, char col 3; got {hit:?}"
);
}
#[test]
fn diff_caret_screen_pos_inverts_the_right_column_hit_test() {
use crate::widgets::diff::DiffSide;
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha\nbravo\n").unwrap();
std::fs::write(f2.path(), "alpha\nBRAVO\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let active_idx = t.active_index();
t.editors[active_idx].focused = true;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut t.editors[active_idx], area, &mut buf);
let body_top = t.editors[active_idx].last_inner.y + 1;
let half = t.editors[active_idx].last_inner.width / 2;
let diff = t.editors[active_idx].diff.as_ref().unwrap();
let r_gutter = (diff.right_lines.len() + 1).to_string().len() as u16 + 1;
let r_text_x = t.editors[active_idx].last_inner.x + half + 1 + r_gutter + 2;
t.editors[active_idx]
.diff
.as_mut()
.unwrap()
.start_selection(DiffSide::Right, 1, 3);
let pos = t.editors[active_idx].diff_caret_screen_pos();
assert_eq!(
pos,
Some((r_text_x + 3, body_top + 1)),
"caret at right row 1 col 3 must land on the cell diff_hit_test maps that screen point back to"
);
}
#[test]
fn render_diff_paints_selection_band_over_selected_cells() {
use crate::widgets::diff::DiffSide;
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha\nbravo\n").unwrap();
std::fs::write(f2.path(), "alpha\nBRAVO\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let active_idx = t.active_index();
t.editors[active_idx].focused = true;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut t.editors[active_idx], area, &mut buf);
let diff_mut = t.editors[active_idx].diff.as_mut().unwrap();
diff_mut.start_selection(DiffSide::Left, 1, 0);
diff_mut.extend_selection_to(1, 5);
ratatui::widgets::Widget::render(&mut t.editors[active_idx], area, &mut buf);
let ed = &t.editors[active_idx];
let diff = ed.diff.as_ref().unwrap();
let l_gutter = (diff.left_lines.len() + 1).to_string().len() as u16 + 1;
let l_text_x = ed.last_inner.x + l_gutter + 2;
let y = ed.last_inner.y + 2;
let band_bg = Color::Rgb(0x26, 0x4f, 0x78);
for col in 0..5u16 {
assert_eq!(
buf[(l_text_x + col, y)].bg,
band_bg,
"cell {col} of the second body row must paint the selection band on the left column"
);
}
}
#[test]
fn open_diff_inserts_a_new_tab_when_an_open_file_already_exists() {
let existing = NamedTempFile::new().unwrap();
std::fs::write(existing.path(), "x\n").unwrap();
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "1\n").unwrap();
std::fs::write(f2.path(), "2\n").unwrap();
let mut t = EditorTabs::new();
t.open_pinned(existing.path()).unwrap();
let before = t.tab_count();
t.open_diff(f1.path(), f2.path()).unwrap();
assert_eq!(t.tab_count(), before + 1, "must insert a new diff tab");
assert!(t.editors[t.active_index()].diff.is_some());
}
#[test]
fn diff_tab_label_uses_arrow_between_filenames() {
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "a\n").unwrap();
std::fs::write(f2.path(), "b\n").unwrap();
let mut t = EditorTabs::new();
t.open_diff(f1.path(), f2.path()).unwrap();
let label = tab_label(&t.editors[t.active_index()]);
let l = f1
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
let r = f2
.path()
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
assert_eq!(label, format!("{l} \u{2194} {r}"));
}
#[test]
fn git_diff_text_tab_label_omits_arrow_when_right_side_is_synthetic() {
// Regression for "git diff master ↔ null" in the tab strip:
// synthetic single-sided diffs (raw `git diff` text view, full-
// file deletion view) leave `right_path` empty; the tab label
// must collapse to just the left label rather than trailing the
// misleading "↔ null".
let mut t = EditorTabs::new();
t.open_git_diff_side_by_side(
std::path::Path::new("git diff master"),
"diff --git a/x b/x\n@@ -1 +1 @@\n-old\n+new\n",
)
.unwrap();
let label = tab_label(&t.editors[t.active_index()]);
assert_eq!(
label, "git diff master",
"synthetic git-diff tab must not show '↔ null' — the right side is virtual"
);
}
#[test]
fn editor_tabs_search_highlight_survives_close_then_open_via_preview() {
// Reported bug: after closing the first tab with X, the next file
// opened from search lost the yellow highlights. The new editor
// was created without the EditorTabs' current term applied.
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "alpha needle bravo\n").unwrap();
std::fs::write(f2.path(), "second needle line\n").unwrap();
let mut t = EditorTabs::new();
t.open_pinned(f1.path()).unwrap();
t.set_search_highlight(
Some(String::from("needle")),
crate::widgets::search::SearchOpts::default(),
);
// Close the first (and only non-blank) tab — leaves the blank
// initial tab.
let close_idx = t
.editors
.iter()
.position(|e| e.path.as_deref() == Some(f1.path()))
.unwrap();
assert!(t.close_tab(close_idx));
// Now open a different file via preview, mirroring "click on a
// search hit" in the running app.
t.open_preview(f2.path()).unwrap();
let active = t
.editors
.iter()
.find(|e| e.path.as_deref() == Some(f2.path()))
.unwrap();
assert_eq!(
active.search_highlight.as_deref(),
Some("needle"),
"newly opened tab must inherit the EditorTabs' current search term"
);
}
#[test]
fn editor_tabs_search_highlight_survives_open_pinned_after_close() {
// Same scenario as above but via open_pinned (double-click /
// Ctrl+Enter). The new editor must also inherit the term.
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "first\n").unwrap();
std::fs::write(f2.path(), "second needle line\n").unwrap();
let mut t = EditorTabs::new();
t.open_pinned(f1.path()).unwrap();
t.open_pinned(f2.path()).unwrap();
t.set_search_highlight(
Some(String::from("needle")),
crate::widgets::search::SearchOpts::default(),
);
// Close f1; both should still be highlighted, and now a fresh open
// of a third file should inherit too.
let f3 = NamedTempFile::new().unwrap();
std::fs::write(f3.path(), "third needle row\n").unwrap();
let close_idx = t
.editors
.iter()
.position(|e| e.path.as_deref() == Some(f1.path()))
.unwrap();
assert!(t.close_tab(close_idx));
t.open_pinned(f3.path()).unwrap();
let third = t
.editors
.iter()
.find(|e| e.path.as_deref() == Some(f3.path()))
.unwrap();
assert_eq!(third.search_highlight.as_deref(), Some("needle"));
}
#[test]
fn editor_tabs_set_search_highlight_propagates_to_every_tab() {
let mut t = EditorTabs::new();
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "hello").unwrap();
std::fs::write(f2.path(), "world").unwrap();
t.open_pinned(f1.path()).unwrap();
t.open_pinned(f2.path()).unwrap();
t.set_search_highlight(
Some(String::from("term")),
crate::widgets::search::SearchOpts::default(),
);
for ed in &t.editors {
assert_eq!(ed.search_highlight.as_deref(), Some("term"));
}
t.set_search_highlight(None, crate::widgets::search::SearchOpts::default());
for ed in &t.editors {
assert!(ed.search_highlight.is_none());
}
}
#[test]
fn editor_starts_with_zero_horizontal_scroll() {
let e = editor_with("abcdef");
assert_eq!(e.scroll_col, 0);
}
#[test]
fn scroll_right_by_advances_horizontal_offset() {
let mut e = editor_with("a".repeat(2000).as_str());
e.scroll_right_by(40);
assert_eq!(e.scroll_col, 40);
e.scroll_right_by(60);
assert_eq!(e.scroll_col, 100);
e.scroll_left_by(30);
assert_eq!(e.scroll_col, 70);
e.scroll_left_by(1000); // saturating
assert_eq!(e.scroll_col, 0);
}
#[test]
fn move_right_past_visible_edge_advances_horizontal_scroll() {
// User-reported bug: long-line files (HTML blob with base64 inline)
// had no horizontal scroll, so moving the cursor right past the
// viewport edge just clamped the visible cursor at the right
// border without actually advancing scroll_col.
let mut e = editor_with("a".repeat(500).as_str());
e.focused = true;
// Render once so last_inner / last_gutter_width are populated.
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_width = e
.last_inner
.width
.saturating_sub(e.last_gutter_width + 2 + u16::from(e.last_scrollbar.width > 0))
as usize;
assert!(text_width > 0 && text_width < 500);
// Move cursor to the very first off-screen column.
e.cursor_col = text_width;
e.move_right();
assert!(
e.scroll_col > 0,
"moving the cursor past the visible edge must advance horizontal scroll"
);
}
#[test]
fn render_starts_line_from_scroll_col() {
// With scroll_col = 3, a long line should display starting from its
// 4th character ('D') at the text origin, not from 'A'. The line must
// overflow the viewport, otherwise render clamps scroll_col to 0 (a
// line that fits has nothing to scroll past).
let mut e = editor_with("ABCDEFGHIJ".repeat(10).as_str());
e.scroll_col = 3;
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
assert_eq!(
buf[(text_x, e.last_inner.y)].symbol(),
"D",
"first visible column must be the (scroll_col)th character"
);
}
#[test]
fn render_clamps_scroll_col_to_content_width() {
// A trackpad swipe can push scroll_col arbitrarily far right via
// scroll_right_by (no clamp of its own). Render must pull it back so
// the buffer can never be stranded entirely off-screen.
let mut e = editor_with("a".repeat(40).as_str());
e.focused = true;
e.scroll_right_by(10_000);
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 5,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_width = e
.last_inner
.width
.saturating_sub(e.last_gutter_width + 2 + u16::from(e.last_scrollbar.width > 0))
as usize;
assert!(text_width > 0);
assert_eq!(
e.scroll_col,
40usize.saturating_sub(text_width),
"scroll_col must clamp to (content cols - text width)"
);
}
#[test]
fn render_shows_horizontal_scrollbar_only_on_overflow() {
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 6,
};
// Short line: no horizontal overflow, no bar.
let mut short = editor_with("abc");
short.focused = true;
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut short).render(area, &mut buf);
assert_eq!(
short.last_hscrollbar,
Rect::default(),
"a line that fits must not reserve a horizontal scrollbar row"
);
// Long line: overflow, bar painted on the bottom inner row.
let mut long = editor_with("a".repeat(500).as_str());
long.focused = true;
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut long).render(area, &mut buf);
assert!(
long.last_hscrollbar.width > 0 && long.last_hscrollbar.height == 1,
"an overflowing line must paint a one-row horizontal scrollbar"
);
assert_eq!(
long.last_hscrollbar.y,
long.last_inner.y + long.last_inner.height - 1,
"the horizontal scrollbar sits on the bottom inner row"
);
}
#[test]
fn scroll_to_bar_x_jumps_horizontal_offset() {
let mut e = editor_with("a".repeat(500).as_str());
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut e).render(area, &mut buf);
assert!(e.last_hscrollbar.width > 0);
// Click the far-right end of the track → scroll to (near) max.
let right_x = e.last_hscrollbar.x + e.last_hscrollbar.width - 1;
assert!(e.scroll_to_bar_x(right_x));
assert!(
e.scroll_col > 0,
"dragging the thumb to the right edge must advance scroll_col"
);
// Click the far-left end → scroll back to 0.
assert!(e.scroll_to_bar_x(e.last_hscrollbar.x));
assert_eq!(e.scroll_col, 0);
}
fn md_editor(text: &str) -> Editor {
let mut e = editor_with(text);
e.lang = Some(LangKind::Markdown);
e
}
fn render_at(e: &mut Editor, w: u16, h: u16) {
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: w,
height: h,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(e as &mut Editor).render(area, &mut buf);
}
/// On hosts that ignore iTerm2's `SetColors` (Ghostty, Kitty, sixel) the
/// image/CSV/diff canvases must paint the theme background explicitly:
/// their `Reset` fill overrides the non-iTerm2 frame prefill (ratatui
/// applies any `Some` bg), so with the chrome themed they were the only
/// host-black islands left in the frame.
#[test]
fn non_iterm2_canvases_paint_the_theme_background_not_reset() {
let themed = crate::theme::Theme::from_id("solarized-dark");
assert_eq!(themed.id(), "solarized-dark", "bundled theme resolves");
assert_eq!(
canvas_bg(false, themed),
themed.editor_bg(),
"a non-iTerm2 host fills the canvas with the theme bg"
);
assert_eq!(
canvas_bg(true, themed),
Color::Reset,
"iTerm2 keeps Reset so the SetColors session bg shows through"
);
// And the fill actually lands on every cell of the canvas.
let mut e = editor_with("");
e.theme = themed;
e.image = Some(ImageView {
bytes: Vec::new(),
format_label: String::from("png"),
pixel_w: 4,
pixel_h: 4,
byte_size: 1,
generation: 0,
pdf: None,
});
let area = Rect {
x: 0,
y: 0,
width: 20,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
render_image_placeholder(
e.image.as_ref().unwrap(),
None,
area,
&mut buf,
canvas_bg(false, themed),
);
assert_eq!(
buf[(1, area.height - 1)].bg,
themed.editor_bg(),
"the canvas body must wear the theme bg, not the host default"
);
}
/// The editor picture sits at `KITTY_Z_BELOW_TEXT_AND_BG`, and Kitty
/// draws any cell with a non-default background OVER an image that deep.
/// Painting the canvas with the explicit theme bg (the CSV/diff island
/// fix) therefore hid the preview behind its own canvas on Ghostty/Kitty
/// — transmitted every frame, never visible. The image canvas must keep
/// the DEFAULT background on the protocols that layer the picture
/// against the cells (Kitty) or blit it over them post-frame (iTerm2);
/// the themed look survives because the bake letterboxes with the theme
/// bg pixel. Sixel and no-graphics hosts keep the themed fill.
#[test]
fn the_kitty_image_canvas_keeps_the_default_bg_so_the_deep_z_preview_shows() {
use crate::iterm2_inline::InlineImageProtocol;
let themed = crate::theme::Theme::from_id("solarized-dark");
assert_eq!(themed.id(), "solarized-dark", "bundled theme resolves");
assert_eq!(
image_canvas_bg(InlineImageProtocol::Kitty, themed),
Color::Reset,
"Kitty layers the deep-z image below non-default-bg cells, so the picture canvas must stay default-bg"
);
assert_eq!(
image_canvas_bg(InlineImageProtocol::ITerm2, themed),
Color::Reset,
"iTerm2 keeps Reset so the SetColors session bg shows through"
);
assert_eq!(
image_canvas_bg(InlineImageProtocol::Sixel, themed),
themed.editor_bg(),
"sixel blits into the cell buffer over the canvas, so the themed fill stays"
);
assert_eq!(
image_canvas_bg(InlineImageProtocol::None, themed),
themed.editor_bg(),
"with no graphics the placeholder is all the user sees; it stays themed"
);
// And the Kitty choice actually lands on every canvas cell.
let image = ImageView {
bytes: Vec::new(),
format_label: String::from("png"),
pixel_w: 4,
pixel_h: 4,
byte_size: 1,
generation: 0,
pdf: None,
};
let area = Rect {
x: 0,
y: 0,
width: 20,
height: 6,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
render_image_placeholder(
&image,
None,
area,
&mut buf,
image_canvas_bg(InlineImageProtocol::Kitty, themed),
);
// The metadata pill on the first row keeps its explicit bg by design:
// the overlay anchors the picture one row BELOW it
// (`update_editor_image_overlay` carves off the header row), so the
// pill never overlaps the image. Every cell in the picture band under
// it must stay default-bg.
assert_ne!(
buf[(1, area.y)].bg,
Color::Reset,
"the metadata pill row keeps its own background"
);
for y in area.y + 1..area.bottom() {
for x in area.x..area.right() {
assert_eq!(
buf[(x, y)].bg,
Color::Reset,
"canvas cell ({x}, {y}) must keep the default background or Kitty paints it over the image"
);
}
}
}
/// Cmd+K Cmd+T while a preview is open: the preview bakes its colors at
/// build time and was rebuilt only when the buffer edited, so it kept the
/// previous theme's colors (chrome and code alike) until the user touched
/// the file or toggled the preview off and on.
#[test]
fn a_theme_switch_rebuilds_an_open_markdown_preview() {
let mut e = md_editor("```notalanguage\nplain code line\n```");
assert!(e.toggle_markdown_preview());
let themed = *crate::theme::Theme::all()
.iter()
.find(|t| t.syntax().fg != crate::theme::SyntaxPalette::BASE16.fg)
.expect("a bundled theme with a non-Base16 code palette");
e.theme = themed;
e.rehighlight_for_theme();
let (r, g, b) = themed.syntax().fg;
let md = e.markdown_preview.as_ref().expect("preview stays open");
let code = md
.lines
.iter()
.flat_map(|l| l.spans.iter())
.find(|s| s.content.as_ref() == "plain code line")
.expect("the code line must render");
assert_eq!(
code.style.fg,
Some(Color::Rgb(r, g, b)),
"the preview must recolor on a theme switch, not wait for an edit"
);
}
#[test]
fn markdown_wraps_long_line_without_horizontal_scrollbar() {
let mut e = md_editor(&"word ".repeat(40)); // 200 chars, one logical line
render_at(&mut e, 30, 10);
assert_eq!(
e.last_hscrollbar,
Rect::default(),
"wrapped markdown must not show a horizontal scrollbar"
);
assert_eq!(e.scroll_col, 0, "wrap mode never scrolls horizontally");
let rows_for_line0 = e
.last_wrap_rows
.iter()
.filter(|r| matches!(r, VisRow::Text { line: 0, .. }))
.count();
assert!(
rows_for_line0 > 1,
"a 200-char paragraph must fold onto multiple visual rows"
);
}
#[test]
fn non_markdown_long_line_still_scrolls_horizontally() {
let mut e = editor_with(&"a".repeat(200));
e.lang = Some(LangKind::Rust);
render_at(&mut e, 30, 10);
assert!(
e.last_hscrollbar.width > 0,
"code keeps the horizontal scrollbar instead of wrapping"
);
}
#[test]
fn markdown_cursor_screen_pos_lands_on_the_wrapped_row() {
let mut e = md_editor(&"a".repeat(100));
render_at(&mut e, 30, 10);
let (_, seg0_start, seg0_end) = e.text_row(0).unwrap();
assert_eq!(seg0_start, 0);
// A column just past the first visual row's content sits on row 2.
e.cursor_row = 0;
e.cursor_col = seg0_end + 1;
let (_, cy) = e.cursor_screen_pos().expect("cursor must be visible");
assert_eq!(
cy,
e.last_inner.y + 1,
"a column past the first segment renders on the second visual row"
);
}
#[test]
fn markdown_click_on_second_visual_row_maps_into_the_line() {
let mut e = md_editor(&"a".repeat(100));
render_at(&mut e, 30, 10);
let seg0_end = e.text_row(0).unwrap().2;
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let (line, col) = e
.buffer_pos_at(text_x, e.last_inner.y + 1)
.expect("a click on the second visual row is a text hit");
assert_eq!(line, 0, "still the same logical line");
assert_eq!(
col, seg0_end,
"the first column of the second visual row is where segment one ended"
);
}
#[test]
fn markdown_mouse_down_below_wrapped_lines_lands_on_the_row_under_the_pointer() {
// Line 0 wraps onto several visual rows; pressing on the row that
// SHOWS line 1 must anchor the selection on line 1, not on the
// linear `scroll + visual_row` (which counts every wrapped
// continuation row as its own line and drifts downward).
let mut e = md_editor(&format!("{}\nsecond\nthird\nfourth", "a".repeat(100)));
render_at(&mut e, 30, 10);
let vis_row = e
.last_wrap_rows
.iter()
.position(|r| matches!(r, VisRow::Text { line: 1, .. }))
.expect("line 1 is on screen");
assert!(vis_row > 1, "line 0 must wrap for the rows to be displaced");
let text_x = e.last_inner.x + e.last_gutter_width + 1;
e.mouse_down(text_x, e.last_inner.y + vis_row as u16);
assert_eq!(
e.cursor_row, 1,
"the press lands on the line under the pointer"
);
assert_eq!(e.cursor_col, 0);
}
#[test]
fn markdown_move_down_steps_within_a_wrapped_paragraph() {
let mut e = md_editor(&"a".repeat(100));
render_at(&mut e, 30, 10);
e.cursor_row = 0;
e.cursor_col = 0;
e.move_down();
assert_eq!(
e.cursor_row, 0,
"move_down stays on the same logical line - it steps to the next wrapped segment"
);
assert!(e.cursor_col > 0, "and advances into that next segment");
}
#[test]
fn markdown_scrolls_to_reveal_tail_of_a_tall_paragraph() {
// One logical line taller than the viewport must still be scrollable
// via sub-line (segment) scroll.
let mut e = md_editor(&"a".repeat(400));
render_at(&mut e, 30, 6);
let before = e.text_row(0).unwrap();
e.scroll_down(3);
render_at(&mut e, 30, 6);
let after = e.text_row(0).unwrap();
assert_eq!(after.0, 0, "still the single logical line");
assert!(
after.1 > before.1,
"scrolling down advances into the paragraph (sub-line scroll)"
);
}
#[test]
fn render_paints_selection_band_on_selected_cells() {
use ratatui::buffer::Buffer;
let mut e = editor_with("hello world");
e.cursor_col = 0;
e.start_selection_at_cursor();
e.cursor_col = 5;
e.extend_selection_to_cursor();
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 5,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
// gutter for 1 line: "1 ".len() = 2 → text_x = 0+1+2+1 = 4
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let selected_bg = Color::Rgb(0x26, 0x4f, 0x78);
// chars 0..5 should be highlighted
for col in 0..5u16 {
let bg = buf[(text_x + col, e.last_inner.y)].bg;
assert_eq!(
bg, selected_bg,
"cell at col {col} should have selection bg, got {bg:?}"
);
}
// char 5 (the space) is OUTSIDE the selection (head exclusive end)
let bg5 = buf[(text_x + 5, e.last_inner.y)].bg;
assert_ne!(bg5, selected_bg, "cell at col 5 should NOT be highlighted");
}
#[test]
fn render_paints_selection_band_across_multiple_lines() {
use ratatui::buffer::Buffer;
let mut e = editor_with("first\nsecond\nthird");
e.cursor_row = 0;
e.cursor_col = 2;
e.start_selection_at_cursor();
e.cursor_row = 2;
e.cursor_col = 2;
e.extend_selection_to_cursor();
e.focused = true;
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 6,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let selected_bg = Color::Rgb(0x26, 0x4f, 0x78);
// Row 0: cols 2..end (all the way past the end of "first")
let row0_y = e.last_inner.y;
assert_eq!(buf[(text_x + 2, row0_y)].bg, selected_bg, "row 0 col 2");
assert_eq!(buf[(text_x + 4, row0_y)].bg, selected_bg, "row 0 col 4");
assert_ne!(
buf[(text_x, row0_y)].bg,
selected_bg,
"row 0 col 0 not selected"
);
// Row 1 (full line "second"): all cells in selection
let row1_y = e.last_inner.y + 1;
assert_eq!(buf[(text_x, row1_y)].bg, selected_bg, "row 1 col 0");
assert_eq!(buf[(text_x + 5, row1_y)].bg, selected_bg, "row 1 col 5");
// Row 2 (last line "third"): cols 0..2 in selection
let row2_y = e.last_inner.y + 2;
assert_eq!(buf[(text_x, row2_y)].bg, selected_bg, "row 2 col 0");
assert_eq!(buf[(text_x + 1, row2_y)].bg, selected_bg, "row 2 col 1");
assert_ne!(
buf[(text_x + 2, row2_y)].bg,
selected_bg,
"row 2 col 2 not selected"
);
}
#[test]
fn double_click_selects_word_and_moves_cursor_to_end() {
let mut e = editor_with("hello world");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
let text_x: u16 = 3;
e.select_word_at(text_x + 7, 0);
let sel = e.selection.expect("word selection created");
assert_eq!(sel.normalised(), ((0, 6), (0, 11)));
assert_eq!(e.cursor_col, 11);
}
#[test]
fn double_click_on_first_word_selects_it_from_column_zero() {
let mut e = editor_with("foo bar");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
let text_x: u16 = 3;
e.select_word_at(text_x + 1, 0);
let sel = e.selection.unwrap();
assert_eq!(sel.normalised(), ((0, 0), (0, 3)));
assert_eq!(e.cursor_col, 3);
}
#[test]
fn double_click_on_whitespace_does_not_create_a_selection() {
let mut e = editor_with("hello world");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
let text_x: u16 = 3;
e.select_word_at(text_x + 5, 0);
assert!(
e.selection.map(|s| !s.has_area()).unwrap_or(true),
"whitespace double-click must not start a non-empty selection"
);
}
#[test]
fn double_click_past_end_of_line_extends_last_word() {
let mut e = editor_with("foo");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
let text_x: u16 = 3;
e.select_word_at(text_x + 12, 0);
let sel = e.selection.unwrap();
assert_eq!(sel.normalised(), ((0, 0), (0, 3)));
assert_eq!(e.cursor_col, 3);
}
#[test]
fn editor_tabs_starts_with_one_empty_editor() {
let t = EditorTabs::new();
assert_eq!(t.tab_count(), 1);
assert_eq!(t.active_index(), 0);
assert!(t.path.is_none());
}
#[test]
fn editor_tabs_open_in_new_tab_appends_and_activates() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/tmp/a.rs"));
t.add_tab_with_path(std::path::PathBuf::from("/tmp/b.rs"));
assert_eq!(t.tab_count(), 2);
assert_eq!(t.active_index(), 1);
assert_eq!(t.path.as_deref(), Some(std::path::Path::new("/tmp/b.rs")));
}
#[test]
fn editor_tabs_new_tab_lands_immediately_after_active() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
// active is now 2 (c). Switch back to a (idx 0), open d → should be at idx 1.
t.select(0);
t.add_tab_with_path(std::path::PathBuf::from("/d"));
let labels: Vec<_> = t
.iter_tabs()
.map(|e| e.path.as_ref().unwrap().to_string_lossy().into_owned())
.collect();
assert_eq!(labels, vec!["/a", "/d", "/b", "/c"]);
assert_eq!(t.active_index(), 1);
}
#[test]
fn editor_tabs_close_active_drops_tab_and_reselects() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
t.select(1); // active = b
assert!(t.close_active());
assert_eq!(t.tab_count(), 2);
// After closing the middle tab, the next tab takes its slot, which is c.
assert_eq!(t.path.as_deref(), Some(std::path::Path::new("/c")));
}
#[test]
fn editor_tabs_close_last_tab_resets_buffer_to_blank() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
assert!(
t.close_active(),
"closing the only tab resets it instead of refusing"
);
assert_eq!(t.tab_count(), 1);
assert!(t.path.is_none());
}
#[test]
fn close_at_maps_a_click_on_the_x_glyph_to_its_tab_index() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/foo.rs"));
t.add_tab_with_path(std::path::PathBuf::from("/bar.rs"));
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let cx0 = t
.close_screen_x(0)
.expect("tab 0 has a close button when count > 1");
let cx1 = t.close_screen_x(1).expect("tab 1 has a close button");
assert_eq!(t.close_at(cx0, area.y), Some(0));
assert_eq!(t.close_at(cx1, area.y), Some(1));
// A click on a non-close cell of the tab still routes to `tab_at`,
// not `close_at`.
let (tab0_x, _) = t.tab_screen_x(0).unwrap();
assert_ne!(tab0_x, cx0);
assert_eq!(t.close_at(tab0_x, area.y), None);
// Clicks outside the strip row are not close clicks.
assert_eq!(t.close_at(cx0, area.y + 2), None);
}
#[test]
fn hovering_the_close_cross_paints_a_pill_on_that_cell() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/foo.rs"));
t.add_tab_with_path(std::path::PathBuf::from("/bar.rs"));
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
// First render to learn where the close crosses landed.
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let cx = t.close_screen_x(0).expect("two tabs -> tab 0 has a cross");
let other = t.close_screen_x(1).expect("tab 1 has a cross");
// Hover the first tab's cross and re-render.
t.hover_pointer = Some((cx, area.y));
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let pill = crate::theme::Theme::default().tab_close_pill_bg();
assert_eq!(
buf[(cx, area.y)].bg,
pill,
"the hovered cross cell wears the pill"
);
assert_eq!(buf[(cx, area.y)].fg, Color::White);
assert_eq!(
buf[(cx, area.y)].symbol(),
"\u{2715}",
"the pill sits behind the cross glyph, not a blank cell"
);
assert_ne!(
buf[(other, area.y)].bg,
pill,
"an un-hovered cross gets no pill"
);
}
#[test]
fn black_theme_close_pill_is_distinct_from_the_active_tab_bg() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/foo.rs"));
t.editors[0].focus_gradient = true; // turn on the Black theme
t.add_tab_with_path(std::path::PathBuf::from("/bar.rs"));
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let active = t.active;
let cx = t
.close_screen_x(active)
.expect("the active tab has a close cross");
t.hover_pointer = Some((cx, area.y));
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let black = crate::theme::Theme::from_id("black");
assert_eq!(
black.tab_active_bg(),
crate::gradient::rgb_color(crate::gradient::POPUP_SEL_BG),
"the Black theme's active chip is still the brand selection teal"
);
assert_eq!(
buf[(cx, area.y)].bg,
black.tab_close_pill_bg(),
"the Black-theme close pill uses the brighter teal"
);
assert_ne!(
buf[(cx, area.y)].bg,
black.tab_active_bg(),
"the pill must not equal the active tab bg, or the cross shows no hover at all"
);
}
#[test]
fn hovering_an_inactive_tab_lifts_its_body_not_the_active_one() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/foo.rs"));
// Adding a tab makes the new one (index 1) active; index 0 is inactive.
t.add_tab_with_path(std::path::PathBuf::from("/bar.rs"));
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let (x0, _) = t.tab_screen_x(0).unwrap();
let (x1, _) = t.tab_screen_x(1).unwrap();
// Hover the inactive tab's label area (one cell past its left pad).
t.hover_pointer = Some((x0 + 1, area.y));
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let hover = crate::theme::Theme::default().tab_hover_bg();
assert_eq!(
buf[(x0 + 1, area.y)].bg,
hover,
"an inactive tab body lifts under the pointer"
);
assert_ne!(
buf[(x1 + 1, area.y)].bg,
hover,
"the active tab keeps its own bg when a sibling is hovered"
);
}
/// The ten manifest themes recolor the editor body; the tab strip must
/// follow, not stay the hardcoded VS-Code navy of the two built-ins. A
/// theme without explicit tab colors derives them: the strip wears the
/// theme's secondary panel fill and the active tab its selection fill.
#[test]
fn tab_strip_follows_the_selected_theme() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/foo.rs"));
let solarized = crate::theme::Theme::from_id("solarized-dark");
assert_eq!(solarized.id(), "solarized-dark", "bundled theme resolves");
t.editors[0].theme = solarized;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
assert_eq!(
buf[(area.width - 1, 0)].bg,
solarized.search_bg(),
"the gap right of the last tab wears the theme's strip color"
);
let (x0, _) = t.tab_screen_x(0).unwrap();
assert_eq!(
buf[(x0 + 1, 0)].bg,
solarized.selection(),
"the active tab wears the theme's selection fill, not the navy chip"
);
}
#[test]
fn close_tab_removes_specific_index_and_keeps_active_pointing_correctly() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
// active = 2 (c). Closing tab 1 (b) shifts c to index 1; active follows.
assert!(t.close_tab(1));
assert_eq!(t.tab_count(), 2);
assert_eq!(t.active_index(), 1);
assert_eq!(t.path.as_deref(), Some(std::path::Path::new("/c")));
}
#[test]
fn close_others_keeps_only_the_chosen_tab_and_makes_it_active() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
// Keep "b" (originally index 1). The other two must go.
let removed = t.close_others(1);
assert_eq!(removed, 2, "close_others must report how many were dropped");
assert_eq!(t.tab_count(), 1);
assert_eq!(t.active_index(), 0);
assert_eq!(t.path.as_deref(), Some(std::path::Path::new("/b")));
assert!(t.focused, "the surviving tab takes focus");
}
#[test]
fn toggle_pin_keeps_pinned_tabs_leftmost_and_follows_the_active_tab() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c")); // active = /c (idx 2)
// Pin /c: it joins the (empty) pinned block at the front and stays
// active even though its index changed from 2 to 0.
assert!(t.toggle_pin(2));
assert!(t.is_pinned(0));
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/c")));
assert_eq!(t.active_index(), 0, "the active tab follows its editor");
assert_eq!(t.tab_path(1).as_deref(), Some(std::path::Path::new("/a")));
assert_eq!(t.tab_path(2).as_deref(), Some(std::path::Path::new("/b")));
// Pinning /b too lands it at the END of the pinned block (after /c).
let b_idx = 2;
assert!(t.toggle_pin(b_idx));
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/c")));
assert_eq!(t.tab_path(1).as_deref(), Some(std::path::Path::new("/b")));
assert!(t.is_pinned(0) && t.is_pinned(1) && !t.is_pinned(2));
// Unpinning /c returns it to the front of the unpinned block.
assert!(!t.toggle_pin(0));
assert!(t.is_pinned(0), "/b is still pinned and stays leftmost");
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/b")));
assert_eq!(t.tab_path(1).as_deref(), Some(std::path::Path::new("/c")));
}
#[test]
fn close_others_keeps_pinned_tabs_alongside_the_kept_tab() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
t.add_tab_with_path(std::path::PathBuf::from("/d"));
t.toggle_pin(0); // pin /a (stays leftmost); order [/a*, /b, /c, /d]
let removed = t.close_others(2); // keep /c
assert_eq!(removed, 2, "only the unpinned non-kept tabs (/b, /d) close");
assert_eq!(t.tab_count(), 2);
assert!(t.is_pinned(0));
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/a")));
assert_eq!(t.tab_path(1).as_deref(), Some(std::path::Path::new("/c")));
assert_eq!(t.active_index(), 1, "the kept tab is active");
}
#[test]
fn close_to_right_keeps_pinned_tabs_to_the_right_of_the_pivot() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
t.add_tab_with_path(std::path::PathBuf::from("/d"));
t.toggle_pin(0); // pin /a
t.toggle_pin(1); // pin /b -> end of pinned block; order [/a*, /b*, /c, /d]
let removed = t.close_to_right(0); // close to the right of /a
assert_eq!(
removed, 2,
"/c and /d close; the pinned /b to the right survives"
);
assert_eq!(t.tab_count(), 2);
assert!(t.is_pinned(0) && t.is_pinned(1));
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/a")));
assert_eq!(t.tab_path(1).as_deref(), Some(std::path::Path::new("/b")));
assert_eq!(t.active_index(), 0, "the pivot tab is active");
}
#[test]
fn keep_open_promotes_a_preview_tab_and_returns_true() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].preview = true;
assert!(t.is_preview(0));
assert!(t.keep_open(0), "promoting a preview tab returns true");
assert!(!t.is_preview(0), "the tab is no longer the preview slot");
}
#[test]
fn keep_open_is_a_noop_on_an_already_permanent_tab() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].preview = false;
assert!(
!t.keep_open(0),
"a permanent tab returns false (nothing to promote)"
);
}
#[test]
fn keep_open_out_of_range_is_false() {
let mut t = EditorTabs::new();
assert!(!t.keep_open(99));
}
#[test]
fn keep_open_only_affects_the_target_tab() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].preview = true;
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.editors[1].preview = true;
t.keep_open(0);
assert!(!t.is_preview(0));
assert!(t.is_preview(1), "the sibling's preview flag is untouched");
}
#[test]
fn is_preview_and_is_pinned_are_false_out_of_range() {
let t = EditorTabs::new();
assert!(!t.is_preview(99));
assert!(!t.is_pinned(99));
}
#[test]
fn toggle_pin_clears_the_preview_flag_when_pinning() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].preview = true;
assert!(t.toggle_pin(0), "pinning returns the new state = true");
assert!(t.is_pinned(0));
assert!(!t.is_preview(0), "a pinned tab is never the preview slot");
}
#[test]
fn toggle_pin_returns_new_state_and_round_trips() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
assert!(t.toggle_pin(0), "first toggle pins");
assert!(!t.toggle_pin(0), "second toggle unpins");
assert!(!t.is_pinned(0));
}
#[test]
fn toggle_pin_out_of_range_is_a_noop_and_false() {
let mut t = EditorTabs::new();
let before = t.tab_count();
assert!(!t.toggle_pin(99));
assert_eq!(t.tab_count(), before, "no panic, no mutation");
}
#[test]
fn close_all_closes_pinned_tabs_too() {
// VS Code parity: Close All does not spare pinned tabs.
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.toggle_pin(0);
let removed = t.close_all();
assert_eq!(removed, 2);
assert_eq!(t.tab_count(), 1, "collapses to a single blank tab");
assert!(!t.is_pinned(0));
}
#[test]
fn close_saved_closes_clean_pinned_tabs() {
// VS Code parity: Close Saved closes a clean tab even when pinned;
// only the dirty tab survives.
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.editors[1].dirty = true;
t.toggle_pin(0); // pin the clean /a
let removed = t.close_saved();
assert_eq!(removed, 1, "the clean pinned tab is still closed");
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/b")));
}
#[test]
fn pinned_tab_paints_the_thumb_tack_instead_of_the_close_cross() {
let f1 = NamedTempFile::new().unwrap();
let f2 = NamedTempFile::new().unwrap();
std::fs::write(f1.path(), "1\n").unwrap();
std::fs::write(f2.path(), "2\n").unwrap();
let mut t = EditorTabs::new();
t.open_pinned(f1.path()).unwrap();
t.open_pinned(f2.path()).unwrap();
t.toggle_pin(0); // pin the first tab (stays leftmost)
let active_idx = t.active_index();
t.editors[active_idx].focused = true;
let area = Rect {
x: 0,
y: 0,
width: 80,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
(&mut t).render(area, &mut buf);
let strip: String = (0..area.width).map(|x| buf[(x, 0)].symbol()).collect();
assert!(
strip.contains('\u{f08d}'),
"the pinned tab shows the thumb-tack glyph in its close cell; strip was {strip:?}"
);
assert!(
strip.contains('\u{2715}'),
"the unpinned tab still shows the close cross; strip was {strip:?}"
);
}
#[test]
fn take_active_editor_removes_and_returns_the_active_tab() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b")); // active = /b
let ed = t.take_active_editor();
assert_eq!(ed.path.as_deref(), Some(std::path::Path::new("/b")));
assert_eq!(t.tab_count(), 1);
assert_eq!(t.tab_path(0).as_deref(), Some(std::path::Path::new("/a")));
}
#[test]
fn take_active_editor_on_the_last_tab_leaves_a_blank_group() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
let ed = t.take_active_editor();
assert_eq!(ed.path.as_deref(), Some(std::path::Path::new("/a")));
assert_eq!(t.tab_count(), 1);
assert!(t.is_blank_initial(), "the group falls back to a blank tab");
}
#[test]
fn push_editor_replaces_a_blank_group_then_appends() {
let mut src = EditorTabs::new();
src.editors[0].path = Some(std::path::PathBuf::from("/a"));
let ed = src.take_active_editor();
let mut dst = EditorTabs::new(); // blank-initial
dst.push_editor(ed);
assert_eq!(dst.tab_count(), 1, "pushing into a blank group replaces it");
assert_eq!(dst.tab_path(0).as_deref(), Some(std::path::Path::new("/a")));
assert_eq!(dst.active_index(), 0);
let mut src2 = EditorTabs::new();
src2.editors[0].path = Some(std::path::PathBuf::from("/b"));
let ed2 = src2.take_active_editor();
dst.push_editor(ed2);
assert_eq!(dst.tab_count(), 2, "a second push appends and activates it");
assert_eq!(dst.active_index(), 1);
assert_eq!(dst.tab_path(1).as_deref(), Some(std::path::Path::new("/b")));
}
#[test]
fn close_others_is_a_noop_when_only_one_tab_is_open() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/only"));
let removed = t.close_others(0);
assert_eq!(removed, 0, "nothing to drop");
assert_eq!(t.tab_count(), 1);
assert_eq!(t.path.as_deref(), Some(std::path::Path::new("/only")));
}
#[test]
fn close_to_right_drops_only_tabs_past_the_pivot_and_keeps_left_side() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
t.add_tab_with_path(std::path::PathBuf::from("/d"));
// Pivot on "b" (index 1). c and d disappear; a and b stay.
let removed = t.close_to_right(1);
assert_eq!(removed, 2);
assert_eq!(t.tab_count(), 2);
assert_eq!(
t.editors[0].path.as_deref(),
Some(std::path::Path::new("/a")),
"left-side tabs are untouched"
);
assert_eq!(
t.editors[1].path.as_deref(),
Some(std::path::Path::new("/b"))
);
assert!(
t.active_index() < t.tab_count(),
"active must still point at a valid tab"
);
}
#[test]
fn close_to_right_at_last_tab_is_a_noop() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
let before = t.tab_count();
let removed = t.close_to_right(1);
assert_eq!(removed, 0);
assert_eq!(t.tab_count(), before);
}
#[test]
fn close_all_collapses_to_a_single_blank_tab() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
let removed = t.close_all();
assert_eq!(removed, 3, "report how many tabs were collapsed");
assert_eq!(t.tab_count(), 1, "always at least one tab survives");
assert!(t.path.is_none(), "the surviving tab is a fresh blank slot");
}
#[test]
fn close_saved_drops_clean_tabs_and_keeps_dirty_ones() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
// Mark "b" (index 1) as having unsaved changes; a and c are saved.
t.editors[1].dirty = true;
let removed = t.close_saved();
assert_eq!(removed, 2, "the two saved tabs (a, c) are closed");
assert_eq!(t.tab_count(), 1, "only the dirty tab survives");
assert_eq!(
t.editors[0].path.as_deref(),
Some(std::path::Path::new("/b")),
"the kept tab is the dirty one"
);
assert_eq!(t.active_index(), 0, "the kept tab becomes active");
assert!(t.editors[0].focused, "the kept tab is focused");
}
#[test]
fn close_saved_collapses_to_blank_when_no_tab_is_dirty() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
let removed = t.close_saved();
assert_eq!(removed, 2, "both saved tabs are closed");
assert_eq!(t.tab_count(), 1, "always at least one tab survives");
assert!(t.path.is_none(), "the survivor is a fresh blank slot");
}
#[test]
fn close_saved_is_a_noop_when_every_tab_is_dirty() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].dirty = true;
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.editors[1].dirty = true;
let removed = t.close_saved();
assert_eq!(removed, 0, "nothing saved means nothing to close");
assert_eq!(t.tab_count(), 2, "both dirty tabs stay open");
}
#[test]
fn close_tab_on_last_remaining_tab_resets_to_blank_buffer() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/only.rs"));
t.editors[0].lines = vec!["something".to_string()];
t.editors[0].dirty = true;
assert!(t.close_tab(0));
assert_eq!(t.tab_count(), 1);
assert!(t.path.is_none());
assert!(t.lines.is_empty() || t.lines == vec![String::new()]);
assert!(!t.dirty);
}
#[test]
fn close_screen_x_is_present_even_for_a_single_open_tab() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/only.rs"));
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 5,
};
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let close_x = t.close_screen_x(0).expect("single tab still shows X");
assert_eq!(t.close_at(close_x, area.y), Some(0));
}
#[test]
fn editor_tabs_tab_at_x_returns_index_of_clicked_tab() {
use ratatui::buffer::Buffer;
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/long_name.rs"));
t.add_tab_with_path(std::path::PathBuf::from("/b.rs"));
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut t).render(area, &mut buf);
let first = t.tab_screen_x(0).expect("tab 0 laid out");
let second = t.tab_screen_x(1).expect("tab 1 laid out");
assert_eq!(t.tab_at(first.0, area.y), Some(0));
assert_eq!(t.tab_at(second.0, area.y), Some(1));
assert_eq!(t.tab_at(area.x + area.width - 1, area.y + 5), None);
}
#[test]
fn open_preview_creates_a_preview_tab_when_none_exists() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].preview = false;
// Stand in for a real disk open: simulate the side-effects directly.
t.add_preview_tab_with_path(std::path::PathBuf::from("/b"));
assert_eq!(t.tab_count(), 2);
assert_eq!(t.preview_index(), Some(1));
assert_eq!(t.active_index(), 1);
}
#[test]
fn open_preview_reuses_existing_preview_slot_for_a_new_path() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.editors[0].preview = false;
t.add_preview_tab_with_path(std::path::PathBuf::from("/b"));
// Now repoint the preview tab at /c — count must NOT grow.
t.repoint_preview_to(std::path::PathBuf::from("/c"));
assert_eq!(t.tab_count(), 2);
assert_eq!(t.preview_index(), Some(1));
assert_eq!(
t.editors[1].path.as_deref(),
Some(std::path::Path::new("/c"))
);
}
#[test]
fn open_preview_reuses_blank_initial_tab_instead_of_stacking() {
let mut t = EditorTabs::new();
let mut f = NamedTempFile::new().unwrap();
write!(f, "hello").unwrap();
t.open_preview(f.path()).unwrap();
assert_eq!(t.tab_count(), 1, "blank initial tab must be reused");
assert_eq!(t.preview_index(), Some(0));
assert_eq!(t.path.as_deref(), Some(f.path()));
}
#[test]
fn open_pinned_reuses_blank_initial_tab_instead_of_stacking() {
let mut t = EditorTabs::new();
let mut f = NamedTempFile::new().unwrap();
write!(f, "hello").unwrap();
t.open_pinned(f.path()).unwrap();
assert_eq!(t.tab_count(), 1, "blank initial tab must be reused");
assert!(
t.preview_index().is_none(),
"pinned open must not leave preview state"
);
}
#[test]
fn open_preview_drops_stale_preview_when_switching_to_pinned_tab() {
let mut t = EditorTabs::new();
let mut f1 = NamedTempFile::new().unwrap();
write!(f1, "a").unwrap();
let mut f2 = NamedTempFile::new().unwrap();
write!(f2, "b").unwrap();
t.open_preview(f1.path()).unwrap(); // preview slot = f1, only tab
t.pin_active(); // f1 pinned
t.open_preview(f2.path()).unwrap(); // preview tab for f2 alongside pinned f1
assert_eq!(t.tab_count(), 2);
t.open_preview(f1.path()).unwrap(); // back to f1 → stale f2 preview must vanish
assert_eq!(t.tab_count(), 1);
assert!(t.preview_index().is_none());
assert_eq!(t.path.as_deref(), Some(f1.path()));
}
#[test]
fn pin_active_clears_preview_flag() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_preview_tab_with_path(std::path::PathBuf::from("/b"));
assert_eq!(t.preview_index(), Some(1));
t.pin_active();
assert!(
t.preview_index().is_none(),
"no tab should be in preview state"
);
assert!(!t.editors[1].preview);
}
#[test]
fn editor_tabs_find_tab_with_path_returns_index_when_open() {
let mut t = EditorTabs::new();
t.editors[0].path = Some(std::path::PathBuf::from("/a"));
t.add_tab_with_path(std::path::PathBuf::from("/b"));
t.add_tab_with_path(std::path::PathBuf::from("/c"));
assert_eq!(t.find_tab_with_path(std::path::Path::new("/a")), Some(0));
assert_eq!(t.find_tab_with_path(std::path::Path::new("/b")), Some(1));
assert_eq!(t.find_tab_with_path(std::path::Path::new("/c")), Some(2));
assert_eq!(t.find_tab_with_path(std::path::Path::new("/missing")), None);
}
#[test]
fn editor_tabs_deref_exposes_active_editor_state() {
let mut t = EditorTabs::new();
t.lines = vec!["abc".to_string()];
t.cursor_col = 3;
// Field access reaches active editor via DerefMut.
assert_eq!(t.lines, vec!["abc".to_string()]);
assert_eq!(t.cursor_col, 3);
}
#[test]
fn mouse_drag_extends_selection() {
let mut e = editor_with("hello world");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
let text_x: u16 = 3;
e.mouse_down(text_x, 0);
e.mouse_drag(text_x + 5, 0);
let sel = e.selection.unwrap();
assert_eq!(sel.anchor, (0, 0));
assert_eq!(sel.head, (0, 5));
assert_eq!(e.cursor_col, 5);
}
#[test]
fn editor_tabs_rename_open_path_updates_matching_tab_only() {
// Renaming a file on disk must update the matching tab so the
// editor keeps tracking the entry. Other tabs must be untouched.
let mut tabs = EditorTabs::new();
tabs.add_tab_with_path(std::path::PathBuf::from("/work/old.txt"));
tabs.add_tab_with_path(std::path::PathBuf::from("/work/other.txt"));
let old = std::path::PathBuf::from("/work/old.txt");
let new = std::path::PathBuf::from("/work/renamed.txt");
tabs.rename_open_path(&old, &new);
let paths: Vec<Option<std::path::PathBuf>> =
tabs.iter_tabs().map(|e| e.path.clone()).collect();
assert!(paths.contains(&Some(new.clone())));
assert!(!paths.contains(&Some(old)));
assert!(paths.contains(&Some(std::path::PathBuf::from("/work/other.txt"))));
}
#[test]
fn editor_tabs_rename_open_path_is_noop_when_path_not_open() {
let mut tabs = EditorTabs::new();
tabs.add_tab_with_path(std::path::PathBuf::from("/work/a.txt"));
let before: Vec<Option<std::path::PathBuf>> =
tabs.iter_tabs().map(|e| e.path.clone()).collect();
tabs.rename_open_path(
std::path::Path::new("/work/never-opened.txt"),
std::path::Path::new("/work/whatever.txt"),
);
let after: Vec<Option<std::path::PathBuf>> =
tabs.iter_tabs().map(|e| e.path.clone()).collect();
assert_eq!(before, after);
}
// ---- Move Line Up / Down (Alt+Up / Alt+Down) ----
#[test]
fn move_line_down_swaps_with_next_and_follows_cursor() {
let mut e = editor_with("a\nb\nc");
e.cursor_row = 0;
e.move_lines_down();
assert_eq!(e.lines, vec!["b", "a", "c"]);
assert_eq!(e.cursor_row, 1, "cursor follows the moved line");
}
#[test]
fn move_line_up_swaps_with_previous_and_follows_cursor() {
let mut e = editor_with("a\nb\nc");
e.cursor_row = 2;
e.move_lines_up();
assert_eq!(e.lines, vec!["a", "c", "b"]);
assert_eq!(e.cursor_row, 1);
}
#[test]
fn move_line_up_at_top_is_noop() {
let mut e = editor_with("a\nb");
e.cursor_row = 0;
e.move_lines_up();
assert_eq!(e.lines, vec!["a", "b"]);
assert_eq!(e.cursor_row, 0);
}
#[test]
fn move_line_down_at_bottom_is_noop() {
let mut e = editor_with("a\nb");
e.cursor_row = 1;
e.move_lines_down();
assert_eq!(e.lines, vec!["a", "b"]);
assert_eq!(e.cursor_row, 1);
}
#[test]
fn move_line_down_moves_whole_selected_block() {
let mut e = editor_with("a\nb\nc\nd");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (1, 1),
});
e.cursor_row = 1;
e.move_lines_down();
assert_eq!(e.lines, vec!["c", "a", "b", "d"]);
assert_eq!(e.cursor_row, 2);
let sel = e.selection.unwrap();
assert_eq!((sel.anchor.0, sel.head.0), (1, 2), "selection rows shift");
}
// ---- Toggle Line Comment (Cmd+/) ----
#[test]
fn toggle_line_comment_adds_rust_token() {
let mut e = editor_with(" let x = 1;");
e.lang = Some(LangKind::Rust);
e.cursor_row = 0;
assert!(e.toggle_line_comment());
assert_eq!(e.lines, vec![" // let x = 1;"]);
}
#[test]
fn toggle_line_comment_removes_existing_rust_token() {
let mut e = editor_with(" // let x = 1;");
e.lang = Some(LangKind::Rust);
e.cursor_row = 0;
assert!(e.toggle_line_comment());
assert_eq!(e.lines, vec![" let x = 1;"]);
}
#[test]
fn toggle_line_comment_python_hash() {
let mut e = editor_with("x = 1");
e.lang = Some(LangKind::Python);
e.cursor_row = 0;
assert!(e.toggle_line_comment());
assert_eq!(e.lines, vec!["# x = 1"]);
}
#[test]
fn toggle_line_comment_refreshes_highlights() {
// Commenting a line must re-run tree-sitter so the line repaints as a
// comment, not keep the stale pre-comment code spans. Without the
// refresh the rendered comment shows code colours shifted by `# `.
let mut e = editor_with("x = 1");
e.lang = Some(LangKind::Python);
e.recompute_highlights();
e.cursor_row = 0;
assert!(e.toggle_line_comment());
let after: Vec<(usize, usize, Option<Color>)> = e.highlights[0]
.iter()
.map(|s| (s.start, s.end, s.style.fg))
.collect();
e.recompute_highlights();
let fresh: Vec<(usize, usize, Option<Color>)> = e.highlights[0]
.iter()
.map(|s| (s.start, s.end, s.style.fg))
.collect();
assert_eq!(
after, fresh,
"toggle_line_comment must refresh syntax highlights to match the new buffer"
);
}
#[test]
fn toggle_line_comment_block_comments_at_min_indent() {
let mut e = editor_with(" a\n b");
e.lang = Some(LangKind::Python);
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (1, 5),
});
assert!(e.toggle_line_comment());
assert_eq!(e.lines, vec![" # a", " # b"]);
}
#[test]
fn toggle_line_comment_block_uncomments_when_all_commented() {
let mut e = editor_with(" # a\n # b");
e.lang = Some(LangKind::Python);
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (1, 7),
});
assert!(e.toggle_line_comment());
assert_eq!(e.lines, vec![" a", " b"]);
}
#[test]
fn toggle_line_comment_unknown_lang_is_noop() {
let mut e = editor_with("x = 1");
e.lang = None;
assert!(!e.toggle_line_comment());
assert_eq!(e.lines, vec!["x = 1"]);
}
// ---- Toggle Block Comment (Shift+Alt+A) ----
#[test]
fn toggle_block_comment_wraps_selection() {
let mut e = editor_with("let x = 1;");
e.lang = Some(LangKind::Rust);
e.selection = Some(EditorSelection {
anchor: (0, 4),
head: (0, 5),
});
assert!(e.toggle_block_comment());
assert_eq!(e.lines, vec!["let /* x */ = 1;"]);
}
#[test]
fn toggle_block_comment_unwraps_when_already_wrapped() {
let mut e = editor_with("let /* x */ = 1;");
e.lang = Some(LangKind::Rust);
e.selection = Some(EditorSelection {
anchor: (0, 4),
head: (0, 11),
});
assert!(e.toggle_block_comment());
assert_eq!(e.lines, vec!["let x = 1;"]);
}
#[test]
fn toggle_block_comment_python_wraps_in_triple_quotes() {
// VS Code's Python language config defines blockComment as `""" """`,
// so Shift+Alt+A wraps the selection in a triple-quoted string.
let mut e = editor_with("x = 1");
e.lang = Some(LangKind::Python);
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (0, 5),
});
assert!(e.toggle_block_comment());
assert_eq!(e.lines, vec!["\"\"\" x = 1 \"\"\""]);
}
#[test]
fn toggle_block_comment_python_unwraps_triple_quotes() {
let mut e = editor_with("\"\"\" x = 1 \"\"\"");
e.lang = Some(LangKind::Python);
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (0, 13),
});
assert!(e.toggle_block_comment());
assert_eq!(e.lines, vec!["x = 1"]);
}
// ---- Join Lines ----
#[test]
fn join_lines_merges_current_with_next() {
let mut e = editor_with("hello\n world");
e.cursor_row = 0;
e.join_lines();
assert_eq!(e.lines, vec!["hello world"]);
}
#[test]
fn join_lines_merges_whole_selection() {
let mut e = editor_with("a\n b\n c\nd");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (2, 3),
});
e.cursor_row = 2;
e.join_lines();
assert_eq!(e.lines, vec!["a b c", "d"]);
}
#[test]
fn join_lines_on_last_line_is_noop() {
let mut e = editor_with("a\nb");
e.cursor_row = 1;
e.join_lines();
assert_eq!(e.lines, vec!["a", "b"]);
}
// ---- Transform Case ----
#[test]
fn transform_upper_on_selection() {
let mut e = editor_with("hello world");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (0, 5),
});
e.transform_selection_case(CaseTransform::Upper);
assert_eq!(e.lines, vec!["HELLO world"]);
}
#[test]
fn transform_lower_on_selection() {
let mut e = editor_with("HELLO world");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (0, 5),
});
e.transform_selection_case(CaseTransform::Lower);
assert_eq!(e.lines, vec!["hello world"]);
}
#[test]
fn transform_title_on_selection() {
let mut e = editor_with("hello there world");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (0, 17),
});
e.transform_selection_case(CaseTransform::Title);
assert_eq!(e.lines, vec!["Hello There World"]);
}
#[test]
fn transform_upper_falls_back_to_word_at_cursor() {
let mut e = editor_with("alpha beta");
e.cursor_row = 0;
e.cursor_col = 7; // inside "beta"
e.transform_selection_case(CaseTransform::Upper);
assert_eq!(e.lines, vec!["alpha BETA"]);
}
// ---- Sort Lines ----
#[test]
fn sort_lines_ascending_over_selection() {
let mut e = editor_with("banana\napple\ncherry");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (2, 6),
});
e.sort_lines(true);
assert_eq!(e.lines, vec!["apple", "banana", "cherry"]);
}
#[test]
fn sort_lines_descending_over_selection() {
let mut e = editor_with("apple\nbanana\ncherry");
e.selection = Some(EditorSelection {
anchor: (0, 0),
head: (2, 6),
});
e.sort_lines(false);
assert_eq!(e.lines, vec!["cherry", "banana", "apple"]);
}
#[test]
fn sort_lines_without_selection_sorts_whole_buffer() {
let mut e = editor_with("c\na\nb");
e.selection = None;
e.sort_lines(true);
assert_eq!(e.lines, vec!["a", "b", "c"]);
}
// ---- Trim Trailing Whitespace ----
#[test]
fn trim_trailing_whitespace_strips_every_line() {
let mut e = editor_with("a \nb\t\n c ");
assert!(e.trim_trailing_whitespace());
assert_eq!(e.lines, vec!["a", "b", " c"]);
}
#[test]
fn trim_trailing_whitespace_clamps_cursor() {
let mut e = editor_with("abc ");
e.cursor_row = 0;
e.cursor_col = 7;
assert!(e.trim_trailing_whitespace());
assert_eq!(e.cursor_col, 3, "cursor clamps to trimmed end");
}
#[test]
fn trim_trailing_whitespace_noop_when_clean() {
let mut e = editor_with("a\nb");
assert!(!e.trim_trailing_whitespace());
assert_eq!(e.lines, vec!["a", "b"]);
}
// ---- Insert Cursor Above / Below ----
#[test]
fn add_cursor_below_pushes_caret_on_next_row() {
let mut e = editor_with("aaaa\nbbbb\ncccc");
e.cursor_row = 0;
e.cursor_col = 2;
e.add_cursor_below();
assert_eq!(e.carets.len(), 1);
let c = e.carets[0];
assert_eq!(c.head, (1, 2));
assert_eq!(c.anchor, (1, 2), "zero-width caret");
}
#[test]
fn add_cursor_above_pushes_caret_on_previous_row() {
let mut e = editor_with("aaaa\nbbbb\ncccc");
e.cursor_row = 2;
e.cursor_col = 3;
e.add_cursor_above();
assert_eq!(e.carets.len(), 1);
assert_eq!(e.carets[0].head, (1, 3));
}
#[test]
fn add_cursor_below_clamps_to_short_line() {
let mut e = editor_with("aaaa\nbb");
e.cursor_row = 0;
e.cursor_col = 4;
e.add_cursor_below();
assert_eq!(e.carets[0].head, (1, 2), "column clamps to line length");
}
#[test]
fn add_cursor_below_at_last_row_is_noop() {
let mut e = editor_with("aaaa\nbbbb");
e.cursor_row = 1;
e.cursor_col = 1;
e.add_cursor_below();
assert!(e.carets.is_empty());
}
#[test]
fn select_next_occurrence_grows_selection_one_match_at_a_time() {
let mut e = editor_with("foo bar foo baz foo");
e.cursor_row = 0;
e.cursor_col = 1; // inside the first "foo"
// First press selects the word under the cursor; no extra carets yet.
assert_eq!(e.select_next_occurrence(), 1);
assert!(!e.has_multi_cursor());
assert_eq!(e.selection.unwrap().normalised(), ((0, 0), (0, 3)));
// Each further press adds the next occurrence as a secondary caret.
assert_eq!(e.select_next_occurrence(), 2);
assert!(e.has_multi_cursor());
assert_eq!(e.select_next_occurrence(), 3);
// Every "foo" is now selected: another press wraps and adds nothing.
assert_eq!(e.select_next_occurrence(), 3);
}
#[test]
fn add_caret_at_screen_adds_and_toggles_a_secondary_caret() {
let mut e = editor_with("fn main() {}\nlet x = 1;");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2;
e.cursor_row = 0;
e.cursor_col = 0;
// Alt+click on line 1, char 3: the old primary becomes a caret and the
// click point becomes the new primary.
assert!(e.add_caret_at_screen(6, 1));
assert_eq!((e.cursor_row, e.cursor_col), (1, 3));
assert_eq!(e.carets.len(), 1, "the previous cursor is now a caret");
assert_eq!(e.carets[0].head, (0, 0));
// Alt+click again on that same caret removes it (toggle).
assert!(e.add_caret_at_screen(3, 0));
assert_eq!(e.carets.len(), 0, "clicking an existing caret removes it");
}
#[test]
fn box_select_spans_rows_into_a_column_of_carets() {
let mut e = editor_with("aaaa\nbbbb\ncccc");
e.last_inner = Rect {
x: 0,
y: 0,
width: 80,
height: 25,
};
e.last_gutter_width = 2; // text_x = 3
// Anchor at row 0 char 1 (screen col 4), drag to row 2 char 3 (col 6).
assert!(e.begin_box_select(4, 0));
e.box_drag_to_screen(6, 2);
assert!(e.box_selecting());
// Head row is the primary; its selection covers cols 1..3.
assert_eq!(e.selection.unwrap().normalised(), ((2, 1), (2, 3)));
// The other two rows become carets over the same column span.
assert_eq!(e.carets.len(), 2);
assert_eq!(e.carets[0].normalised(), ((0, 1), (0, 3)));
assert_eq!(e.carets[1].normalised(), ((1, 1), (1, 3)));
e.end_box_select();
assert!(!e.box_selecting());
}
#[test]
fn select_next_occurrence_noop_off_a_word() {
let mut e = editor_with(" foo");
e.cursor_row = 0;
e.cursor_col = 0; // on whitespace, not a word
assert_eq!(e.select_next_occurrence(), 0);
assert!(e.selection.is_none());
}
// ---- Toggle Word Wrap (Alt+Z) ----
#[test]
fn toggle_wrap_turns_wrap_on_for_code_file() {
let mut e = editor_with("fn main() {}");
e.lang = Some(LangKind::Rust);
assert!(!e.wrap_enabled(), "code files default to no wrap");
e.toggle_wrap();
assert!(e.wrap_enabled());
}
#[test]
fn toggle_wrap_turns_wrap_off_for_markdown() {
let mut e = editor_with("# title");
e.lang = Some(LangKind::Markdown);
assert!(e.wrap_enabled(), "markdown defaults to wrap on");
e.toggle_wrap();
assert!(!e.wrap_enabled());
}
// ---- Go to Bracket (editor.action.jumpToBracket) ----
#[test]
fn jump_to_bracket_from_open_moves_to_matching_close() {
let mut e = editor_with("foo(bar)baz");
e.cursor_row = 0;
e.cursor_col = 3; // immediately before the '('
assert!(e.jump_to_matching_bracket());
assert_eq!((e.cursor_row, e.cursor_col), (0, 7)); // start of ')'
}
#[test]
fn jump_to_bracket_from_close_moves_to_matching_open() {
let mut e = editor_with("foo(bar)baz");
e.cursor_row = 0;
e.cursor_col = 8; // immediately after the ')'
assert!(e.jump_to_matching_bracket());
assert_eq!((e.cursor_row, e.cursor_col), (0, 3)); // start of '('
}
#[test]
fn jump_to_bracket_from_inside_goes_to_enclosing_close() {
let mut e = editor_with("foo(bar)baz");
e.cursor_row = 0;
e.cursor_col = 5; // inside, between 'a' and 'r'
assert!(e.jump_to_matching_bracket());
assert_eq!((e.cursor_row, e.cursor_col), (0, 7)); // start of ')'
}
#[test]
fn jump_to_bracket_respects_nesting() {
let mut e = editor_with("(a(b)c)");
e.cursor_row = 0;
e.cursor_col = 0; // before the outer '('
assert!(e.jump_to_matching_bracket());
assert_eq!((e.cursor_row, e.cursor_col), (0, 6)); // outer ')'
}
#[test]
fn jump_to_bracket_works_across_lines() {
let mut e = editor_with("fn f() {\n x\n}");
e.cursor_row = 0;
e.cursor_col = 7; // before the '{'
assert!(e.jump_to_matching_bracket());
assert_eq!((e.cursor_row, e.cursor_col), (2, 0)); // the '}'
}
#[test]
fn jump_to_bracket_noop_without_a_bracket() {
let mut e = editor_with("hello world");
e.cursor_row = 0;
e.cursor_col = 2;
assert!(!e.jump_to_matching_bracket());
assert_eq!((e.cursor_row, e.cursor_col), (0, 2));
}
// ---- Breadcrumbs bar ----
#[test]
fn sticky_scroll_never_covers_the_caret_row() {
// The sticky band floats over the topmost content rows, and
// `scroll_view_to` pulls the caret down to `scroll` whenever it drifts
// above the viewport — so the caret lands on exactly the row the band
// repaints, and vanishes. VS Code keeps the cursor line clear of its
// sticky widget.
let mut e = editor_with(&format!("fn a() {{\n{}}}\n", " x\n".repeat(60)));
e.scroll = 3;
e.cursor_row = 3;
e.sticky_lines = vec![0];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 20,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let caret = e
.cursor_screen_pos()
.expect("the caret must have a screen row");
assert!(
!e.sticky_click_rows.iter().any(|&(y, _)| y == caret.1),
"the sticky band painted over the caret's own row {}",
caret.1
);
}
#[test]
fn breadcrumb_row_renders_below_tab_strip_and_maps_symbol_clicks() {
let mut tabs = EditorTabs::new();
tabs.breadcrumbs = vec![
Crumb {
label: "src".into(),
target: None,
},
Crumb {
label: "m.rs".into(),
target: None,
},
Crumb {
label: "build".into(),
target: Some((5, 2)),
},
];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut tabs).render(area, &mut buf);
// The bar sits on the row directly below the one-row tab strip.
let by = area.y + 1;
// Somewhere on the bar a click lands on the "build" symbol crumb and
// resolves to its jump target; the path crumbs resolve to nothing.
let hit = (0..area.width).find_map(|x| tabs.breadcrumb_target_at(x, by));
assert_eq!(hit, Some((5, 2)), "clicking the symbol crumb jumps there");
// A click off the bar (the tab strip row) never maps to a crumb.
assert_eq!(tabs.breadcrumb_target_at(10, area.y), None);
}
#[test]
fn breadcrumbs_measure_wide_glyphs_in_display_cells() {
// The bar budgeted and advanced in `char`s while the buffer advances by
// display CELLS, so every crumb after a CJK one was painted right of
// the rect recorded for it and its click missed. Same class as the
// terminal pane pill.
let mut tabs = EditorTabs::new();
tabs.breadcrumbs = vec![
Crumb {
label: "文档文档".into(),
target: None,
},
Crumb {
label: "build".into(),
target: Some((5, 2)),
},
];
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut tabs).render(area, &mut buf);
let by = area.y + 1;
// The column where "build" was actually PAINTED: scan cells, since a
// wide glyph takes one cell and leaves the next as a spacer.
let row: String = (0..area.width)
.map(|x| buf[(x, by)].symbol().to_string())
.collect::<Vec<_>>()
.concat();
// The advance was a char count while the glyphs take two cells each,
// so the separator and the next crumb were painted four cells INTO
// this label and destroyed its second half.
// A wide glyph owns one cell and leaves the next as a spacer, so the
// concatenated row interleaves blanks: count the glyphs instead.
assert_eq!(
(row.matches('文').count(), row.matches('档').count()),
(2, 2),
"the whole wide label must survive, got {row:?}"
);
assert!(
row.contains("build"),
"and the next crumb must still be painted, got {row:?}"
);
// The recorded hit rect must cover every cell the label was painted on,
// or clicking its right half misses.
let (x, w, _) = tabs.breadcrumb_ranges[0];
assert_eq!(w, 8, "a 4-char CJK label occupies 8 cells, not 4");
let (next_x, _, _) = tabs.breadcrumb_ranges[1];
assert!(
next_x >= x + w,
"the next crumb must start past this one, got {next_x} vs {x}+{w}"
);
}
#[test]
fn breadcrumb_bar_hidden_when_there_are_no_crumbs() {
let mut tabs = EditorTabs::new();
tabs.breadcrumbs.clear();
let area = Rect {
x: 0,
y: 0,
width: 60,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut tabs).render(area, &mut buf);
assert_eq!(tabs.breadcrumb_target_at(10, area.y + 1), None);
}
// ---- Sticky scroll ----
#[test]
fn sticky_scroll_pins_header_rows_and_maps_clicks_to_their_lines() {
let text = (0..40)
.map(|i| format!("line{i}"))
.collect::<Vec<_>>()
.join("\n");
let mut e = editor_with(&text);
e.focused = true;
e.scroll = 20;
// The caret must sit inside the viewport and below the band: with it on
// the top visible row there is nowhere for the band to go that would
// not hide it, and render pulls `scroll` back to the caret.
e.cursor_row = 25;
e.sticky_lines = vec![0, 10];
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 15,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let top = e.last_inner.y;
assert_eq!(e.sticky_line_at(5, top), Some(0), "row 0 pins line 0");
assert_eq!(e.sticky_line_at(5, top + 1), Some(10), "row 1 pins line 10");
assert_eq!(
e.sticky_line_at(5, top + 5),
None,
"a content row below the band is not sticky"
);
}
/// The wheel drags the caret along with the viewport (`scroll_view_to`
/// clamps it to `scroll`), which used to park it on the very row the band
/// wants. Suppressing the band there would delete sticky scroll for the
/// one gesture it exists to serve, so the caret lands below the band
/// instead.
#[test]
fn a_wheel_scroll_keeps_the_band_and_parks_the_caret_below_it() {
let text = (0..80)
.map(|i| format!("line{i}"))
.collect::<Vec<_>>()
.join("\n");
let mut e = editor_with(&text);
e.focused = true;
e.sticky_lines = vec![0, 10];
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 15,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf); // seats `last_inner` for scroll_down
assert_eq!(e.cursor_row, 0, "caret starts on the first line");
e.scroll_down(20);
(&mut e).render(area, &mut buf);
let top = e.last_inner.y;
assert_eq!(
e.sticky_line_at(5, top),
Some(0),
"the band must survive a wheel scroll"
);
assert_eq!(e.sticky_line_at(5, top + 1), Some(10));
assert!(
e.cursor_row >= e.scroll + 2,
"caret dragged below the two-row band, not under it (row {}, scroll {})",
e.cursor_row,
e.scroll
);
}
/// The band stops above the caret's PAINTED row. A collapsed fold between
/// the top of the viewport and the caret removes rows, so counting buffer
/// lines overstates how far down the caret is and the band paints over it.
#[test]
fn the_band_stops_above_the_caret_with_a_fold_between_them() {
let mut text: Vec<String> = (0..10).map(|i| format!("head{i}")).collect();
text.push(String::from("def f():"));
text.extend((0..3).map(|i| format!(" body{i}")));
text.extend((0..10).map(|i| format!("tail{i}")));
let mut e = editor_with(&text.join("\n"));
e.focused = true;
e.scroll = 10;
e.toggle_fold(10); // hides lines 11..=13
assert!(e.is_line_hidden(12), "the fold body is hidden");
// Line 14 is the second PAINTED row (row 0 is the fold header on 10),
// even though it is four buffer lines below the top.
e.cursor_row = 14;
e.sticky_lines = vec![0, 5];
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 15,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let top = e.last_inner.y;
assert_eq!(e.sticky_line_at(5, top), Some(0), "outermost header pins");
assert_eq!(
e.sticky_line_at(5, top + 1),
None,
"the caret's own painted row must stay clear of the band"
);
}
#[test]
fn sticky_scroll_hidden_without_pinned_lines() {
let mut e = editor_with("a\nb\nc\n");
e.focused = true;
e.sticky_lines.clear();
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
assert_eq!(e.sticky_line_at(5, e.last_inner.y), None);
}
// ---- Bracket match highlight (editorBracketMatch) ----
#[test]
fn bracket_match_pair_from_open_returns_both_brackets() {
let mut e = editor_with("foo(bar)baz");
e.cursor_col = 3; // immediately before '('
assert_eq!(e.bracket_match_pair(), Some(((0, 3), (0, 7))));
}
#[test]
fn bracket_match_pair_from_after_close_returns_both_brackets() {
let mut e = editor_with("foo(bar)baz");
e.cursor_col = 8; // immediately after ')'
assert_eq!(e.bracket_match_pair(), Some(((0, 3), (0, 7))));
}
#[test]
fn bracket_match_pair_none_when_caret_touches_no_bracket() {
let mut e = editor_with("foo(bar)baz");
e.cursor_col = 5; // inside the pair but not adjacent to a bracket
assert_eq!(e.bracket_match_pair(), None);
}
#[test]
fn render_paints_bracket_match_background_on_both_brackets() {
let mut e = editor_with("foo(bar)baz");
e.focused = true;
e.cursor_col = 3; // beside '('
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 6,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
let y = e.last_inner.y;
let bm = e.theme.bracket_match_bg();
assert_eq!(buf[(text_x + 3, y)].bg, bm, "the '(' is highlighted");
assert_eq!(buf[(text_x + 7, y)].bg, bm, "the ')' is highlighted");
assert_ne!(
buf[(text_x + 5, y)].bg,
bm,
"a non-bracket cell is not highlighted"
);
}
// ---- Select to Bracket (editor.action.selectToBracket) ----
#[test]
fn select_to_bracket_includes_both_brackets() {
let mut e = editor_with("foo(bar)baz");
e.cursor_row = 0;
e.cursor_col = 3; // on the '('
assert!(e.select_to_matching_bracket());
assert_eq!(e.selection_text(), "(bar)");
}
#[test]
fn select_to_bracket_from_inside_selects_enclosing_pair() {
let mut e = editor_with("foo(bar)baz");
e.cursor_row = 0;
e.cursor_col = 5; // inside the parentheses
assert!(e.select_to_matching_bracket());
assert_eq!(e.selection_text(), "(bar)");
}
#[test]
fn select_to_bracket_spans_multiple_lines() {
let mut e = editor_with("fn f() {\n x\n}");
e.cursor_row = 0;
e.cursor_col = 7; // on the '{'
assert!(e.select_to_matching_bracket());
assert_eq!(e.selection_text(), "{\n x\n}");
}
// ---- Transpose Characters (editor.action.transpose) ----
#[test]
fn transpose_swaps_chars_around_cursor_and_advances() {
let mut e = editor_with("abcd");
e.cursor_row = 0;
e.cursor_col = 2; // between 'b' and 'c'
e.transpose_chars();
assert_eq!(e.lines, vec!["acbd"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 3));
}
#[test]
fn transpose_at_line_start_only_advances() {
let mut e = editor_with("abcd");
e.cursor_row = 0;
e.cursor_col = 0;
e.transpose_chars();
assert_eq!(e.lines, vec!["abcd"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 1));
}
#[test]
fn transpose_at_end_of_line_moves_char_across_break() {
let mut e = editor_with("ab\ncd");
e.cursor_row = 0;
e.cursor_col = 2; // end of "ab", not the last line
e.transpose_chars();
assert_eq!(e.lines, vec!["a", "bcd"]);
assert_eq!((e.cursor_row, e.cursor_col), (1, 1));
}
#[test]
fn transpose_at_end_of_final_line_is_noop() {
let mut e = editor_with("abc");
e.cursor_row = 0;
e.cursor_col = 3; // end of the only line
e.transpose_chars();
assert_eq!(e.lines, vec!["abc"]);
assert_eq!((e.cursor_row, e.cursor_col), (0, 3));
}
// ---- Convert Indentation to Spaces / Tabs ----
#[test]
fn indentation_to_spaces_converts_only_leading_tabs() {
let mut e = editor_with("\tfoo\n\t\tbar\nbaz\ta");
e.indentation_to_spaces();
assert_eq!(e.lines, vec![" foo", " bar", "baz\ta"]);
}
#[test]
fn indentation_to_tabs_converts_leading_space_groups() {
let mut e = editor_with(" foo\n bar\nb c");
e.indentation_to_tabs();
// 4 spaces -> 1 tab; 6 spaces -> 1 tab + 2 leftover; interior spaces stay.
assert_eq!(e.lines, vec!["\tfoo", "\t bar", "b c"]);
}
#[test]
fn indent_override_switches_tab_to_a_literal_tab() {
let mut e = editor_with("foo");
// Default: spaces, width 4 -> Tab pads to the next stop with spaces.
e.cursor_row = 0;
e.cursor_col = 0;
e.indent_at_cursor();
assert_eq!(e.lines[0], " foo");
assert_eq!(e.indent_preference(), (4, true));
// Pin tabs: Tab now inserts one literal tab, LSP prefs flip insert_spaces.
let mut t = editor_with("foo");
t.set_indent_style(IndentStyle {
width: 4,
use_spaces: false,
});
t.cursor_row = 0;
t.cursor_col = 0;
t.indent_at_cursor();
assert_eq!(t.lines[0], "\tfoo");
assert_eq!(t.indent_preference(), (4, false));
}
#[test]
fn indent_override_width_changes_status_label_and_unit() {
let mut e = editor_with("");
e.set_indent_style(IndentStyle {
width: 2,
use_spaces: true,
});
assert_eq!(e.indent_style().label(), "Spaces: 2");
assert_eq!(e.indent_style().unit(), " ");
e.set_indent_style(IndentStyle {
width: 4,
use_spaces: false,
});
assert_eq!(e.indent_style().label(), "Tab Size: 4");
assert_eq!(e.indent_style().unit(), "\t");
}
// ---- Trim Final Newlines (files.trimFinalNewlines) ----
#[test]
fn trim_final_newlines_removes_trailing_blank_lines() {
let mut e = editor_with("a\nb\n\n\n");
// editor_with drops the empty trailing element from `lines()`, so build
// the buffer explicitly with trailing blanks.
e.lines = vec![
String::from("a"),
String::from("b"),
String::new(),
String::new(),
];
assert!(e.trim_final_newlines());
assert_eq!(e.lines, vec!["a", "b"]);
}
#[test]
fn trim_final_newlines_keeps_at_least_one_line() {
let mut e = editor_with("");
e.lines = vec![String::new(), String::new(), String::new()];
assert!(e.trim_final_newlines());
assert_eq!(e.lines, vec![""]);
}
#[test]
fn trim_final_newlines_noop_when_no_trailing_blanks() {
let mut e = editor_with("a\nb");
assert!(!e.trim_final_newlines());
assert_eq!(e.lines, vec!["a", "b"]);
}
#[test]
fn fold_range_covers_an_indented_block() {
let e = editor_with("fn a() {\n let x = 1;\n let y = 2;\n}\nfn b() {}");
// Header line 0; body lines 1-2 are more indented; the closing brace on
// line 3 returns to base indent and is not part of the region.
assert_eq!(e.fold_range(0), Some((0, 2)));
// A leaf line with no deeper line below it is not foldable.
assert_eq!(e.fold_range(1), None);
assert_eq!(e.fold_range(4), None);
}
#[test]
fn fold_range_absorbs_interior_blank_lines_but_not_trailing_ones() {
let e = editor_with("fn a() {\n x\n\n y\n}\n");
// The interior blank (line 2) sits between two deeper lines, so it is
// inside the region; the region ends at the last deeper line (3).
assert_eq!(e.fold_range(0), Some((0, 3)));
}
#[test]
fn folding_a_header_hides_its_body_only() {
let mut e = editor_with("fn a() {\n let x = 1;\n let y = 2;\n}\nfn b() {}");
e.toggle_fold(0);
assert!(!e.is_line_hidden(0), "the header line stays visible");
assert!(e.is_line_hidden(1));
assert!(e.is_line_hidden(2));
assert!(
!e.is_line_hidden(3),
"the closing brace at base indent stays"
);
assert!(!e.is_line_hidden(4));
}
#[test]
fn moving_down_steps_over_a_folded_block_instead_of_into_it() {
// `is_line_hidden` was consulted only by the render loops and by the
// fold toggles' own cursor snap. No cursor-movement path knew about
// folds, so Down walked straight into a collapsed region: the caret
// vanished (`cursor_screen_pos` cannot map a hidden line) and anything
// typed went into content the user could not see.
let mut e = editor_with("fn a() {\n x\n y\n}\nafter");
e.toggle_fold(0);
assert_eq!(e.cursor_row, 0, "the fold snapped the caret to the header");
e.move_down();
assert_eq!(
e.cursor_row, 3,
"Down clears the whole collapsed body, landing on the next visible line"
);
assert!(!e.is_line_hidden(e.cursor_row));
e.move_up();
assert_eq!(e.cursor_row, 0, "Up steps back over it the same way");
}
#[test]
fn editing_reveals_a_fold_the_caret_ended_up_inside() {
// Not every way into a fold is an arrow key — search, go-to-definition
// and goto-line all set `cursor_row` directly. Wherever the caret came
// from, an edit must never land on a line the user cannot see.
let mut e = editor_with("fn a() {\n x\n y\n}\nafter");
e.toggle_fold(0);
e.cursor_row = 1;
// Past the indent: typing at column 0 would strip line 1's leading
// whitespace and collapse the fold RANGE, which unhides the line for a
// reason that has nothing to do with the caret.
e.cursor_col = 5;
assert!(e.is_line_hidden(1), "the caret is parked inside the fold");
e.insert_char('Z');
assert!(
!e.is_line_hidden(1),
"the edit revealed the region it landed in"
);
assert!(e.lines[1].contains('Z'), "and the text went to line 1");
}
/// Waiting for an EDIT to reveal the fold is too late for a jump: a caret
/// on a hidden line has no painted row at all, so `cursor_screen_pos`
/// returns `None` and no caret is drawn anywhere on screen. Ctrl+G stands
/// in for every direct `cursor_row` setter (go-to-definition, search).
#[test]
fn jumping_into_a_collapsed_block_opens_it_so_the_caret_is_painted() {
let mut e = editor_with("fn a() {\n x\n y\n}\nafter");
e.focused = true;
e.toggle_fold(0);
assert!(e.is_line_hidden(1), "the block starts collapsed");
e.goto_line(2); // one-based: line index 1, inside the fold
assert_eq!(e.cursor_row, 1);
let area = Rect {
x: 0,
y: 0,
width: 40,
height: 10,
};
let mut buf = Buffer::empty(area);
(&mut e).render(area, &mut buf);
assert!(
!e.is_line_hidden(1),
"the jump target's own block must open"
);
assert!(
e.cursor_screen_pos().is_some(),
"so the caret has somewhere to be painted"
);
}
#[test]
fn opening_another_file_clears_fold_state() {
// `open` resets scroll, cursor, selection, undo, tokens and hints, but
// left `folded` alone. A preview tab is REUSED for the next
// single-click, so the new file arrived with the old file's fold
// headers collapsed — and the line-count guard only fires when the two
// files differ in length.
let dir = tempfile::tempdir().unwrap();
let a = dir.path().join("a.rs");
let b = dir.path().join("b.rs");
std::fs::write(&a, "fn a() {\n x\n}\n").unwrap();
std::fs::write(&b, "fn b() {\n y\n}\n").unwrap();
let mut e = Editor::new();
e.open(&a).unwrap();
e.toggle_fold(0);
assert!(e.is_line_hidden(1), "a.rs is folded");
e.open(&b).unwrap();
assert!(
!e.is_line_hidden(1),
"the next file must not inherit the previous file's folds"
);
}
#[test]
fn the_fold_lookup_is_derived_once_per_fold_change_not_per_query() {
// `is_line_hidden` ran on every rendered row of every frame and
// re-derived itself from `folded`, scanning each folded region. Fold
// All on a large file turned a single frame into millions of
// iterations. The spans are now built once per fold write.
let text = (0..400)
.map(|i| format!("fn f{i}() {{\n body\n}}"))
.collect::<Vec<_>>()
.join("\n");
let mut e = editor_with(&text);
e.fold_all();
assert!(
!e.folded.is_empty(),
"Fold All must actually fold something"
);
FOLD_RANGE_REBUILDS.with(|c| c.set(0));
for line in 0..e.lines.len() {
std::hint::black_box(e.is_line_hidden(line));
}
assert_eq!(
FOLD_RANGE_REBUILDS.with(|c| c.get()),
0,
"querying must never rebuild the spans"
);
e.toggle_fold(0);
assert_eq!(
FOLD_RANGE_REBUILDS.with(|c| c.get()),
1,
"changing the fold set rebuilds exactly once"
);
}
#[test]
fn toggling_a_fold_twice_restores_visibility() {
let mut e = editor_with("fn a() {\n body\n}");
e.toggle_fold(0);
assert!(e.is_line_hidden(1));
e.toggle_fold(0);
assert!(!e.is_line_hidden(1), "a second toggle unfolds the block");
}
#[test]
fn folding_from_inside_the_body_folds_the_enclosing_block() {
let mut e = editor_with("fn a() {\n body\n}");
e.cursor_row = 1;
e.toggle_fold(e.cursor_row);
assert!(
e.is_line_hidden(1),
"folding at a body line folds its owner"
);
assert_eq!(
e.cursor_row, 0,
"cursor on a hidden line snaps up to the header"
);
}
#[test]
fn render_omits_folded_body_lines() {
let mut e = editor_with("fn a() {\n x\n y\n}\nfn b() {}");
e.toggle_fold(0);
render_at(&mut e, 40, 10);
let shown: Vec<usize> = (0..e.last_wrap_rows.len())
.filter_map(|i| e.text_row(i).map(|t| t.0))
.collect();
assert!(shown.contains(&0), "the header row is drawn");
assert!(!shown.contains(&1), "a folded body line is not drawn");
assert!(!shown.contains(&2), "a folded body line is not drawn");
assert!(
shown.contains(&3) && shown.contains(&4),
"lines after the fold are still drawn"
);
}
#[test]
fn fold_all_collapses_every_block_and_unfold_all_restores() {
let mut e = editor_with("fn a() {\n x\n}\nfn b() {\n y\n}");
e.fold_all();
assert!(
e.is_line_hidden(1) && e.is_line_hidden(4),
"every body is hidden"
);
e.unfold_all();
assert!(
!e.is_line_hidden(1) && !e.is_line_hidden(4),
"unfold restores all"
);
}
#[test]
fn clicking_the_gutter_chevron_toggles_the_fold() {
let mut e = editor_with("fn a() {\n x\n}\nfn b() {}");
render_at(&mut e, 40, 10);
// The chevron is the second sign-margin cell; the header sits on the
// first visual row at the top of the inner rect.
let cx = e.last_inner.x + 1;
let cy = e.last_inner.y;
assert!(
e.fold_chevron_at(cx, cy),
"a click on the header chevron hits"
);
assert!(e.is_line_hidden(1), "the body is now folded");
assert!(e.fold_chevron_at(cx, cy), "a second click unfolds");
assert!(!e.is_line_hidden(1));
}
#[test]
fn clicking_off_the_chevron_column_is_not_a_fold_toggle() {
let mut e = editor_with("fn a() {\n x\n}");
render_at(&mut e, 40, 10);
let text_x = e.last_inner.x + e.last_gutter_width + 1;
assert!(
!e.fold_chevron_at(text_x, e.last_inner.y),
"the text area is not the chevron"
);
assert!(!e.is_line_hidden(1));
}
#[test]
fn ghost_carets_paint_participant_colored_cells() {
let f = NamedTempFile::new().unwrap();
std::fs::write(f.path(), "hello world\nsecond line\n").unwrap();
let mut ed = Editor::new();
ed.open(f.path()).unwrap();
let color = Color::Rgb(1, 2, 3);
// One caret at (0, 6); one on a row far past the buffer, which must
// simply never paint.
ed.ghost_carets = vec![(0, 6, color), (99, 0, color)];
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 10,
};
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut ed, area, &mut buf);
let hits: Vec<(u16, u16)> = (area.y..area.bottom())
.flat_map(|y| (area.x..area.right()).map(move |x| (x, y)))
.filter(|&(x, y)| buf[(x, y)].style().bg == Some(color))
.collect();
assert_eq!(hits.len(), 1, "exactly one ghost caret cell: {hits:?}");
let (x, y) = hits[0];
assert_eq!(y, ed.last_inner.y, "ghost sits on the first content row");
let text_x = ed.last_inner.x + ed.last_gutter_width + 1;
assert_eq!(x, text_x + 6, "ghost sits at char column 6");
}
/// A ghost caret's name tag paints on the visual row above the caret at
/// the caret's column; on the viewport's top row it falls back below.
#[test]
fn ghost_caret_labels_paint_name_above_and_fall_back_below() {
let f = NamedTempFile::new().unwrap();
std::fs::write(f.path(), "hello world\nsecond line\nthird line\n").unwrap();
let mut ed = Editor::new();
ed.open(f.path()).unwrap();
let color = Color::Rgb(9, 8, 7);
let area = Rect {
x: 0,
y: 0,
width: 30,
height: 10,
};
let text_x = |ed: &Editor| ed.last_inner.x + ed.last_gutter_width + 1;
// Caret on line 1: the tag paints on the row above (line 0's row).
ed.ghost_caret_labels = vec![(1, 2, "alice".into(), color)];
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut ed, area, &mut buf);
let x0 = text_x(&ed) + 2;
let y0 = ed.last_inner.y;
let shown: String = (0..5)
.map(|i| buf[(x0 + i, y0)].symbol().to_string())
.collect();
assert_eq!(shown, "alice", "tag paints above the caret at its column");
assert_eq!(
buf[(x0, y0)].style().bg,
Some(color),
"tag wears the participant color"
);
// Caret on the top row: the tag falls back to the row below.
ed.ghost_caret_labels = vec![(0, 0, "bob".into(), color)];
let mut buf = ratatui::buffer::Buffer::empty(area);
ratatui::widgets::Widget::render(&mut ed, area, &mut buf);
let x1 = text_x(&ed);
let y1 = ed.last_inner.y + 1;
let shown: String = (0..3)
.map(|i| buf[(x1 + i, y1)].symbol().to_string())
.collect();
assert_eq!(shown, "bob", "tag falls back below on the top row");
}
}