iced-code-editor 0.3.11

A custom code editor widget for the Iced GUI framework with syntax highlighting, line numbers, and scrolling support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
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
//! Message handling and update logic.

use iced::Task;
use iced::widget::operation::{focus, select_all};

use crate::text_utils::char_to_byte_index;

use super::command::{
    Command, CompositeCommand, DeleteCharCommand, DeleteForwardCommand,
    DeleteRangeCommand, DuplicateLinesCommand, InsertCharCommand,
    InsertNewlineCommand, InsertTextCommand, MoveLinesCommand,
    ReplaceTextCommand, ToggleCommentCommand, line_comment_token,
};
use super::vim::{
    VimAction, VimInsertPosition, VimMotion, VimOperator, VimPastePosition,
    VimRegister, VimRegisterKind,
};
use super::{
    ArrowDirection, CURSOR_BLINK_INTERVAL, CodeEditor, ImePreedit, IndentStyle,
    LspEditSnapshot, Message, VimMode, cursor_set, lsp,
};

// =========================================================================
// Cursor adjustment helpers for multi-cursor editing
// =========================================================================

/// Describes the kind of edit applied to a single position.
#[derive(Clone, Copy)]
enum EditType {
    /// Insert one char at `(edit_line, edit_col)`.
    InsertChar,
    /// Backspace: delete char at `(edit_line, edit_col - 1)`.
    DeleteCharBack,
    /// Delete-forward: delete char at `(edit_line, edit_col)`.
    DeleteCharForward,
    /// Enter: split `edit_line` at `edit_col`; new line has `extra` indent chars.
    InsertNewline { indent_len: usize },
    /// Backspace-at-col-0: merge `edit_line` into `edit_line - 1`.
    /// `extra` = length of the previous line before merge.
    MergePrev { prev_line_len: usize },
    /// Delete-at-end-of-line: merge `edit_line + 1` into `edit_line`.
    /// `extra` = length of `edit_line` before merge.
    MergeNext { edit_line_len: usize },
}

/// Adjusts a single `(line, col)` pair after an edit.
fn adjust_pos(
    pos: &mut (usize, usize),
    edit_line: usize,
    edit_col: usize,
    kind: EditType,
) {
    match kind {
        EditType::InsertChar => {
            if pos.0 == edit_line && pos.1 >= edit_col {
                pos.1 += 1;
            }
        }
        EditType::DeleteCharBack => {
            if edit_col > 0 && pos.0 == edit_line && pos.1 > edit_col - 1 {
                pos.1 -= 1;
            }
        }
        EditType::DeleteCharForward => {
            if pos.0 == edit_line && pos.1 > edit_col {
                pos.1 -= 1;
            }
        }
        EditType::InsertNewline { indent_len } => {
            if pos.0 > edit_line {
                pos.0 += 1;
            } else if pos.0 == edit_line && pos.1 >= edit_col {
                pos.0 += 1;
                pos.1 = pos.1 - edit_col + indent_len;
            }
        }
        EditType::MergePrev { prev_line_len } => {
            if pos.0 == edit_line {
                pos.0 -= 1;
                pos.1 += prev_line_len;
            } else if pos.0 > edit_line {
                pos.0 -= 1;
            }
        }
        EditType::MergeNext { edit_line_len } => {
            if pos.0 == edit_line + 1 {
                pos.0 = edit_line;
                pos.1 += edit_line_len;
            } else if pos.0 > edit_line + 1 {
                pos.0 -= 1;
            }
        }
    }
}

/// Adjusts all cursors except `skip_idx` after an edit at `(edit_line, edit_col)`.
fn adjust_other_cursors(
    cursors: &mut [cursor_set::Cursor],
    skip_idx: usize,
    edit_line: usize,
    edit_col: usize,
    kind: EditType,
) {
    for (i, cursor) in cursors.iter_mut().enumerate() {
        if i == skip_idx {
            continue;
        }
        adjust_pos(&mut cursor.position, edit_line, edit_col, kind);
        if let Some(ref mut anchor) = cursor.anchor {
            adjust_pos(anchor, edit_line, edit_col, kind);
        }
    }
}

impl CodeEditor {
    // =========================================================================
    // Helper Methods
    // =========================================================================

    /// Performs common cleanup operations after edit operations.
    ///
    /// This method should be called after any operation that modifies the buffer content.
    /// It resets the cursor blink animation, refreshes search matches if search is active,
    /// and invalidates all caches that depend on buffer content or layout:
    /// - `buffer_revision` is bumped to invalidate layout-derived caches
    /// - `visual_lines_cache` is cleared so wrapping is recalculated on next use
    /// - `content_cache` and `overlay_cache` are cleared to rebuild canvas geometry
    fn finish_edit_operation(&mut self) {
        self.reset_cursor_blink();
        self.refresh_search_matches_if_needed();
        // The exact revision value is not semantically meaningful; it only needs
        // to change on edits, so `wrapping_add` is sufficient and overflow-safe.
        let previous_revision = self.buffer_revision;
        self.buffer_revision = self.buffer_revision.wrapping_add(1);
        self.refresh_visual_lines_after_edit(previous_revision);
        self.refresh_max_content_width_after_edit(previous_revision);
        // Truncate the syntax-highlight cache from the first line the edit may
        // have changed. `pre_edit_line` is the topmost active line captured
        // before the edit; the extra line of margin covers edits that merge
        // with the preceding line (e.g. backspace at column 0).
        self.invalidate_highlight_from(self.pre_edit_line.saturating_sub(1));
        self.content_cache.clear();
        self.overlay_cache.clear();
        self.enqueue_incremental_lsp_change();
    }

    /// Returns the topmost logical line currently touched by any cursor or its
    /// selection anchor.
    ///
    /// This is captured before an edit to bound which highlight-cache lines may
    /// change. With no cursors it defaults to line `0`.
    pub(crate) fn min_active_line(&self) -> usize {
        self.cursors
            .iter()
            .map(|cursor| match cursor.anchor {
                Some(anchor) => cursor.position.0.min(anchor.0),
                None => cursor.position.0,
            })
            .min()
            .unwrap_or(0)
    }

    /// Returns the bottommost logical line touched by a cursor or selection.
    fn max_active_line(&self) -> usize {
        self.cursors
            .iter()
            .map(|cursor| match cursor.anchor {
                Some(anchor) => cursor.position.0.max(anchor.0),
                None => cursor.position.0,
            })
            .max()
            .unwrap_or(0)
    }

    /// Captures a conservative old-document line range for an incremental LSP
    /// replacement. Non-editing messages do not allocate or retain a snapshot.
    fn capture_lsp_edit_snapshot(&mut self, message: &Message) {
        if self.lsp_document.is_none() {
            self.lsp_edit_snapshot = None;
            return;
        }

        let is_local_edit = matches!(
            message,
            Message::CharacterInput(_)
                | Message::Tab
                | Message::Enter
                | Message::Backspace
                | Message::Delete
                | Message::DeleteSelection
                | Message::Paste(_)
                | Message::ImeCommit(_)
                | Message::MoveLineUp
                | Message::MoveLineDown
                | Message::DuplicateLineUp
                | Message::DuplicateLineDown
                | Message::ToggleComment
        );
        let is_global_edit = matches!(
            message,
            Message::Undo | Message::Redo | Message::ReplaceAll
        );
        let is_replace_next = matches!(message, Message::ReplaceNext);
        if !is_local_edit && !is_global_edit && !is_replace_next {
            self.lsp_edit_snapshot = None;
            return;
        }

        let line_count = self.buffer.line_count();
        let (mut first_line, mut last_line) = if is_global_edit {
            (0, line_count.saturating_sub(1))
        } else {
            (self.pre_edit_line, self.pre_edit_last_line)
        };
        if is_replace_next
            && let Some(search_match) = self.search_state.current_match()
        {
            first_line = first_line.min(search_match.line);
            last_line = last_line.max(search_match.line);
        }

        let start_line =
            if is_global_edit { 0 } else { first_line.saturating_sub(1) };
        let old_end_exclusive = if is_global_edit {
            line_count
        } else {
            last_line.saturating_add(2).min(line_count)
        };
        let old_end = if old_end_exclusive < line_count {
            lsp::LspPosition {
                line: u32::try_from(old_end_exclusive).unwrap_or(u32::MAX),
                character: 0,
            }
        } else {
            let last_line = line_count.saturating_sub(1);
            lsp::LspPosition {
                line: u32::try_from(last_line).unwrap_or(u32::MAX),
                character: u32::try_from(self.buffer.line_len(last_line))
                    .unwrap_or(u32::MAX),
            }
        };

        self.lsp_edit_snapshot = Some(LspEditSnapshot {
            start_line,
            old_end_exclusive,
            old_line_count: line_count,
            old_end,
        });
    }

    /// Truncates the syntax-highlight cache so logical lines `>= line` are
    /// re-highlighted on next access.
    ///
    /// Lines before the first edited line are unaffected, so the cached prefix
    /// is preserved and edits never trigger a full re-parse from the top of the
    /// file. Has no effect when the cache is empty.
    ///
    /// # Arguments
    ///
    /// * `line` - First logical line to invalidate.
    pub(crate) fn invalidate_highlight_from(&self, line: usize) {
        if let Some(cache) = self.highlight_cache.borrow_mut().as_mut() {
            cache.truncate(line);
        }
    }

    /// Performs common cleanup operations after navigation operations.
    ///
    /// This method should be called after cursor movement operations.
    /// It resets the cursor blink animation and invalidates only the overlay
    /// rendering cache. Cursor movement and selection changes do not modify the
    /// buffer content, so keeping the content cache intact avoids unnecessary
    /// re-rendering of syntax-highlighted text.
    fn finish_navigation_operation(&mut self) {
        self.sync_search_match_from_primary_cursor();
        self.reset_cursor_blink();
        self.overlay_cache.clear();
    }

    /// Starts command grouping with the given label if not already grouping.
    ///
    /// This is used for smart undo functionality, allowing multiple related
    /// operations to be undone as a single unit.
    ///
    /// # Arguments
    ///
    /// * `label` - A descriptive label for the group of commands
    fn ensure_grouping_started(&mut self, label: &str) {
        if !self.is_grouping {
            self.history.begin_group(label);
            self.is_grouping = true;
        }
    }

    /// Ends command grouping if currently active.
    ///
    /// This should be called when a series of related operations is complete,
    /// or when starting a new type of operation that shouldn't be grouped
    /// with previous operations.
    fn end_grouping_if_active(&mut self) {
        if self.is_grouping {
            self.history.end_group();
            self.is_grouping = false;
        }
    }

    fn keep_vim_insert_group(&self) -> bool {
        self.vim_enabled
            && self.vim_state.mode() == VimMode::Insert
            && self.is_grouping
    }

    /// Deletes all active selections across every cursor and performs cleanup.
    ///
    /// # Returns
    ///
    /// `true` if at least one selection was deleted, `false` if no cursor had a selection
    fn delete_selection_if_present(&mut self) -> bool {
        if self.cursors.iter().any(|c| c.has_selection()) {
            self.delete_selection();
            self.finish_edit_operation();
            true
        } else {
            false
        }
    }

    // =========================================================================
    // Text Input Handlers
    // =========================================================================

    /// Handles character input message operations.
    ///
    /// Inserts a character at the current cursor position and adds it to the
    /// undo history. Characters are grouped together for smart undo.
    /// Only processes input when the editor has active focus and is not locked.
    ///
    /// # Arguments
    ///
    /// * `ch` - The character to insert
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible (including
    /// horizontal scroll when wrap is disabled)
    fn handle_character_input_msg(&mut self, ch: char) -> Task<Message> {
        // Guard clause: only process character input if editor has focus and is not locked
        if !self.has_focus() {
            return Task::none();
        }

        // Start grouping if not already grouping (for smart undo)
        self.ensure_grouping_started("Typing");

        // Typing replaces active selections, matching paste and IME commit
        // behavior. Keep the deletion and insertion in the same history group
        // so a single undo restores the replaced text.
        if self.cursors.iter().any(|cursor| cursor.has_selection()) {
            self.delete_selection();
        } else {
            // A plain click leaves a zero-length anchor in place (see
            // `handle_enter`); clear it so it isn't mistaken for a real
            // selection by a later edit.
            self.clear_selection();
        }

        // Multi-cursor: build a sorted index list (descending document order)
        // so that edits at higher positions don't invalidate lower positions.
        let mut order: Vec<usize> = (0..self.cursors.len()).collect();
        order.sort_by(|&a, &b| {
            self.cursors.as_slice()[b]
                .position
                .cmp(&self.cursors.as_slice()[a].position)
        });

        for &idx in &order {
            // Any active selection was deleted above, which also moves the
            // cursor to the original selection start and clears its anchor.
            // The current cursor position is therefore the insertion point.
            let pos = self.cursors.as_slice()[idx].position;
            let mut cmd = InsertCharCommand::new(pos.0, pos.1, ch, pos);
            let mut cursor_pos = pos;
            cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.as_mut_slice()[idx].position = cursor_pos;
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                pos.0,
                pos.1,
                EditType::InsertChar,
            );
            self.history.push(Box::new(cmd));
        }

        self.finish_edit_operation();

        // Auto-trigger LSP completion for identifier characters and trigger characters
        if ch.is_alphanumeric() || ch == '_' || ch == '.' {
            self.lsp_flush_pending_changes();
            self.lsp_request_completion();
        }

        self.scroll_to_cursor()
    }

    /// Handles Tab key press (inserts 4 spaces).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible (including
    /// horizontal scroll when wrap is disabled)
    fn handle_tab(&mut self) -> Task<Message> {
        self.ensure_grouping_started("Tab");

        // A plain click leaves a zero-length anchor in place (see
        // `handle_enter`); clear it so it isn't mistaken for a real selection
        // by a later edit. Tab only reaches here when there is no real
        // selection to indent (see `IndentLines`).
        self.clear_selection();

        // Multi-cursor: process in descending document order
        let mut order: Vec<usize> = (0..self.cursors.len()).collect();
        order.sort_by(|&a, &b| {
            self.cursors.as_slice()[b]
                .position
                .cmp(&self.cursors.as_slice()[a].position)
        });

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;
            match self.indent_style {
                IndentStyle::Spaces(n) => {
                    let mut cursor_pos = pos;
                    for _i in 0..n as usize {
                        let current_col = cursor_pos.1;
                        let mut cmd = InsertCharCommand::new(
                            pos.0,
                            current_col,
                            ' ',
                            cursor_pos,
                        );
                        cmd.execute(&mut self.buffer, &mut cursor_pos);
                        adjust_other_cursors(
                            self.cursors.as_mut_slice(),
                            idx,
                            pos.0,
                            current_col,
                            EditType::InsertChar,
                        );
                        self.history.push(Box::new(cmd));
                    }
                    self.cursors.as_mut_slice()[idx].position = cursor_pos;
                }
                IndentStyle::Tab => {
                    let mut cmd =
                        InsertCharCommand::new(pos.0, pos.1, '\t', pos);
                    let mut cursor_pos = pos;
                    cmd.execute(&mut self.buffer, &mut cursor_pos);
                    adjust_other_cursors(
                        self.cursors.as_mut_slice(),
                        idx,
                        pos.0,
                        pos.1,
                        EditType::InsertChar,
                    );
                    self.cursors.as_mut_slice()[idx].position = cursor_pos;
                    self.history.push(Box::new(cmd));
                }
            }
        }

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Handles Tab key press for focus navigation (when search dialog is not open).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that may navigate focus to another editor
    fn handle_focus_navigation_tab(&mut self) -> Task<Message> {
        // Only handle focus navigation if search dialog is not open
        if !self.search_state.is_open {
            // Lose focus from current editor
            self.has_canvas_focus = false;
            self.show_cursor = false;

            // Return a task that could potentially focus another editor
            // This implements focus chain management by allowing the parent application
            // to handle focus navigation between multiple editors
            Task::none()
        } else {
            Task::none()
        }
    }

    /// Handles Shift+Tab key press for focus navigation (when search dialog is not open).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that may navigate focus to another editor
    fn handle_focus_navigation_shift_tab(&mut self) -> Task<Message> {
        // Only handle focus navigation if search dialog is not open
        if !self.search_state.is_open {
            // Lose focus from current editor
            self.has_canvas_focus = false;
            self.show_cursor = false;

            // Return a task that could potentially focus another editor
            // This implements focus chain management by allowing the parent application
            // to handle focus navigation between multiple editors
            Task::none()
        } else {
            Task::none()
        }
    }

    /// Handles Enter key press (inserts newline).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_enter(&mut self) -> Task<Message> {
        // Standard editing treats Enter as a boundary. In Vim Insert mode the
        // newline belongs to the same insertion session and is closed by Esc.
        let keep_vim_group = self.keep_vim_insert_group();
        if !keep_vim_group {
            self.end_grouping_if_active();
        }

        // A mouse click leaves a zero-length anchor in place so a following
        // drag can extend the selection. Enter must clear that anchor before
        // moving the caret to the new line; otherwise the inserted newline
        // becomes selected and the next typed character deletes it.
        //
        // For a real selection, Enter replaces the selected text with a
        // newline. Group both commands so one undo restores the selection.
        let replaces_selection =
            self.cursors.iter().any(|cursor| cursor.has_selection());
        if replaces_selection {
            self.ensure_grouping_started("Enter");
            self.delete_selection();
        } else {
            self.clear_selection();
        }

        // Multi-cursor: process in descending document order
        let mut order: Vec<usize> = (0..self.cursors.len()).collect();
        order.sort_by(|&a, &b| {
            self.cursors.as_slice()[b]
                .position
                .cmp(&self.cursors.as_slice()[a].position)
        });

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;

            // Copy leading whitespace of the current line to the new line (if enabled)
            let indent: String = if self.auto_indent_enabled {
                self.buffer
                    .line(pos.0)
                    .chars()
                    .take_while(|c| c.is_whitespace())
                    .collect()
            } else {
                String::new()
            };
            let indent_len = indent.chars().count();

            let mut cmd =
                InsertNewlineCommand::with_indent(pos.0, pos.1, pos, indent);
            let mut cursor_pos = pos;
            cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.as_mut_slice()[idx].position = cursor_pos;
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                pos.0,
                pos.1,
                EditType::InsertNewline { indent_len },
            );
            self.history.push(Box::new(cmd));
        }

        if replaces_selection && !keep_vim_group {
            self.end_grouping_if_active();
        }

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    // =========================================================================
    // Line Manipulation Handlers
    // =========================================================================

    /// Returns the inclusive line range affected by the primary cursor.
    ///
    /// When the primary cursor has a selection, the range covers every line it
    /// spans. A selection that ends at column 0 of a line does not include that
    /// trailing line (VS Code convention). Without a selection, the range is the
    /// single line the cursor sits on.
    fn primary_line_range(&self) -> (usize, usize) {
        let primary = self.cursors.primary();
        match primary.selection_range() {
            Some((sel_start, sel_end)) => {
                let end_line = if sel_end.1 == 0 && sel_end.0 > sel_start.0 {
                    sel_end.0 - 1
                } else {
                    sel_end.0
                };
                (sel_start.0, end_line)
            }
            None => {
                let line = primary.position.0;
                (line, line)
            }
        }
    }

    /// Shifts the primary cursor's position and selection anchor by `delta`
    /// whole lines (positive moves downward) so the selection follows an edit.
    fn shift_primary_cursor_lines(&mut self, delta: isize) {
        let primary = self.cursors.primary_mut();
        primary.position.0 = primary.position.0.saturating_add_signed(delta);
        if let Some(anchor) = primary.anchor.as_mut() {
            anchor.0 = anchor.0.saturating_add_signed(delta);
        }
    }

    /// Moves the current line, or the lines spanned by the primary selection,
    /// up or down by one line (Alt+Up / Alt+Down).
    ///
    /// Secondary cursors are collapsed onto the primary one. The move is a no-op
    /// when the affected range is already at the corresponding edge of the
    /// buffer.
    ///
    /// # Arguments
    ///
    /// * `down` - `true` to move the range down, `false` to move it up
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn move_lines(&mut self, down: bool) -> Task<Message> {
        self.end_grouping_if_active();
        self.cursors.remove_all_but_primary();

        let (start, end) = self.primary_line_range();

        // Reject moves that would push the range past the buffer edges.
        if down {
            if end + 1 >= self.buffer.line_count() {
                return Task::none();
            }
        } else if start == 0 {
            return Task::none();
        }

        let pos = self.cursors.primary_position();
        let mut cmd = MoveLinesCommand::new(start, end, down, pos);
        let mut cursor_pos = pos;
        cmd.execute(&mut self.buffer, &mut cursor_pos);
        self.shift_primary_cursor_lines(if down { 1 } else { -1 });
        self.history.push(Box::new(cmd));

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Duplicates the current line, or the lines spanned by the primary
    /// selection, above or below (Shift+Alt+Up / Shift+Alt+Down).
    ///
    /// Secondary cursors are collapsed onto the primary one. A downward
    /// duplication moves the cursor onto the new copy; an upward one leaves it
    /// on the (upper) copy.
    ///
    /// # Arguments
    ///
    /// * `down` - `true` to insert the copy below, `false` to insert it above
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn duplicate_lines(&mut self, down: bool) -> Task<Message> {
        self.end_grouping_if_active();
        self.cursors.remove_all_but_primary();

        let (start, end) = self.primary_line_range();
        let pos = self.cursors.primary_position();
        let mut cmd = DuplicateLinesCommand::new(start, end, down, pos);
        let mut cursor_pos = pos;
        cmd.execute(&mut self.buffer, &mut cursor_pos);
        if down {
            let block_len = (end - start + 1) as isize;
            self.shift_primary_cursor_lines(block_len);
        }
        self.history.push(Box::new(cmd));

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Toggles line comments on the current line, or the lines spanned by the
    /// primary selection (Ctrl+/).
    ///
    /// Secondary cursors are collapsed onto the primary one. If every non-blank
    /// line in the range is already commented, the range is uncommented;
    /// otherwise every non-blank line is commented. The operation is a no-op
    /// when the active syntax has no line-comment token (e.g. HTML) or the range
    /// holds only blank lines.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn toggle_comment(&mut self) -> Task<Message> {
        self.end_grouping_if_active();
        self.cursors.remove_all_but_primary();

        let Some(token) = line_comment_token(&self.syntax) else {
            return Task::none();
        };

        let (start, end) = self.primary_line_range();
        let pos = self.cursors.primary_position();
        let mut cmd =
            ToggleCommentCommand::new(&self.buffer, start, end, token, pos);
        if cmd.is_noop() {
            return Task::none();
        }

        // Track the selection anchor across the column shift before executing.
        let new_anchor =
            self.cursors.primary().anchor.map(|a| cmd.adjust_position(a));

        let mut cursor_pos = pos;
        cmd.execute(&mut self.buffer, &mut cursor_pos);
        let primary = self.cursors.primary_mut();
        primary.position = cursor_pos;
        primary.anchor = new_anchor;
        self.history.push(Box::new(cmd));

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    // =========================================================================
    // Deletion Handlers
    // =========================================================================

    /// Handles Backspace key press.
    ///
    /// If there's a selection, deletes the selection. Otherwise, deletes the
    /// character before the cursor.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible if selection was deleted
    fn handle_backspace(&mut self) -> Task<Message> {
        // End grouping on backspace (separate from typing)
        if !self.keep_vim_insert_group() {
            self.end_grouping_if_active();
        }

        // If any cursor has a selection, delete all selections first
        if self.delete_selection_if_present() {
            return self.scroll_to_cursor();
        }

        // A mouse click leaves a zero-length anchor in place so a following
        // drag can extend the selection (see `handle_enter`). Backspace must
        // clear it before moving the caret; otherwise the anchor is left
        // behind at the pre-edit position and a phantom one-character
        // selection appears next to the cursor, which the next Backspace or
        // Delete then eats instead of a single character.
        self.clear_selection();

        // Multi-cursor: process in descending document order
        let mut order: Vec<usize> = (0..self.cursors.len()).collect();
        order.sort_by(|&a, &b| {
            self.cursors.as_slice()[b]
                .position
                .cmp(&self.cursors.as_slice()[a].position)
        });

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;
            // Determine edit type for adjusting other cursors
            let edit_kind = if pos.1 > 0 {
                EditType::DeleteCharBack
            } else if pos.0 > 0 {
                let prev_line_len = self.buffer.line_len(pos.0 - 1);
                EditType::MergePrev { prev_line_len }
            } else {
                // At very start of document: nothing to delete
                continue;
            };
            let mut cmd =
                DeleteCharCommand::new(&self.buffer, pos.0, pos.1, pos);
            let mut cursor_pos = pos;
            cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.as_mut_slice()[idx].position = cursor_pos;
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                pos.0,
                pos.1,
                edit_kind,
            );
            self.history.push(Box::new(cmd));
        }

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Handles Delete key press.
    ///
    /// If there's a selection, deletes the selection. Otherwise, deletes the
    /// character after the cursor.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible if selection was deleted
    fn handle_delete(&mut self) -> Task<Message> {
        // End grouping on delete
        if !self.keep_vim_insert_group() {
            self.end_grouping_if_active();
        }

        // If any cursor has a selection, delete all selections first
        if self.delete_selection_if_present() {
            return self.scroll_to_cursor();
        }

        // See the matching comment in `handle_backspace`: clear any
        // zero-length anchor left by a plain click before editing, so it
        // can't be mistaken for a real selection on a later edit.
        self.clear_selection();

        // Multi-cursor: process in descending document order
        let mut order: Vec<usize> = (0..self.cursors.len()).collect();
        order.sort_by(|&a, &b| {
            self.cursors.as_slice()[b]
                .position
                .cmp(&self.cursors.as_slice()[a].position)
        });

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;
            let line_len = self.buffer.line_len(pos.0);
            let edit_kind = if pos.1 < line_len {
                EditType::DeleteCharForward
            } else if pos.0 + 1 < self.buffer.line_count() {
                EditType::MergeNext { edit_line_len: line_len }
            } else {
                // At very end of document: nothing to delete
                continue;
            };
            let mut cmd =
                DeleteForwardCommand::new(&self.buffer, pos.0, pos.1, pos);
            let mut cursor_pos = pos;
            cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.as_mut_slice()[idx].position = cursor_pos;
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                pos.0,
                pos.1,
                edit_kind,
            );
            self.history.push(Box::new(cmd));
        }

        self.finish_edit_operation();
        Task::none()
    }

    /// Handles explicit selection deletion (Shift+Delete).
    ///
    /// Deletes the selected text if a selection exists.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_delete_selection(&mut self) -> Task<Message> {
        // End grouping on delete selection
        self.end_grouping_if_active();

        if self.cursors.iter().any(|c| c.has_selection()) {
            self.delete_selection();
            self.finish_edit_operation();
            self.scroll_to_cursor()
        } else {
            Task::none()
        }
    }

    // =========================================================================
    // Navigation Handlers
    // =========================================================================

    fn vim_accepts_insert_input(&self) -> bool {
        !self.vim_enabled || self.vim_state.mode() == VimMode::Insert
    }

    fn handle_vim_key_msg(&mut self, key: char) -> Task<Message> {
        if !self.vim_enabled {
            return Task::none();
        }

        let previous_mode = self.vim_state.mode();
        let action = self.vim_state.parse_key(key);
        match action {
            Some(VimAction::Mode(mode)) => {
                self.handle_vim_mode(mode, previous_mode)
            }
            Some(VimAction::Motion { motion, count, explicit_count }) => {
                self.handle_vim_motion(motion, count, explicit_count)
            }
            Some(VimAction::Insert { position, count }) => {
                self.handle_vim_insert(position, count)
            }
            Some(VimAction::Operator {
                operator,
                motion,
                count,
                explicit_count,
            }) => self.handle_vim_motion_operator(
                operator,
                motion,
                count,
                explicit_count,
            ),
            Some(VimAction::LineOperator { operator, count }) => {
                let start_line = self.cursors.primary_position().0;
                let end_line = start_line
                    .saturating_add(count.saturating_sub(1))
                    .min(self.buffer.line_count().saturating_sub(1));
                self.handle_vim_line_operator(
                    operator, start_line, end_line, false,
                )
            }
            Some(VimAction::VisualOperator(operator)) => {
                self.handle_vim_visual_operator(operator)
            }
            Some(VimAction::DeleteCharacters { count }) => {
                self.handle_vim_delete_characters(count)
            }
            Some(VimAction::Paste { position, count }) => {
                self.handle_vim_paste(position, count)
            }
            Some(VimAction::Undo { count }) => {
                self.handle_vim_history(false, count)
            }
            Some(VimAction::Redo { count }) => {
                self.handle_vim_history(true, count)
            }
            Some(VimAction::RepeatSearch { reverse }) => {
                self.handle_vim_repeat_search(reverse)
            }
            Some(VimAction::SubmitSearch(query)) => {
                self.handle_vim_search(&query)
            }
            Some(VimAction::SubmitGotoLine(line)) => {
                self.handle_goto_position(line.saturating_sub(1), 0)
            }
            Some(VimAction::WriteFile { exit_vim }) => {
                if exit_vim {
                    self.set_vim_enabled(false);
                }
                Task::done(Message::WriteRequested)
            }
            Some(VimAction::ExitVimMode) => {
                self.set_vim_enabled(false);
                Task::none()
            }
            Some(VimAction::CommandLineChanged) => {
                self.overlay_cache.clear();
                Task::none()
            }
            None => Task::none(),
        }
    }

    fn handle_vim_search(&mut self, query: &str) -> Task<Message> {
        if !self.search_replace_enabled || query.is_empty() {
            return Task::none();
        }

        self.search_state.close();
        self.search_state.set_query(query.to_owned(), &self.buffer);
        if self.search_state.matches.is_empty() {
            self.overlay_cache.clear();
            return Task::none();
        }

        let cursor = self.cursors.primary_position();
        let next_index = self
            .search_state
            .matches
            .partition_point(|item| (item.line, item.col) <= cursor);
        self.search_state.current_match_index =
            Some(if next_index == self.search_state.matches.len() {
                0
            } else {
                next_index
            });

        if let Some(search_match) = self.search_state.current_match() {
            self.cursors.set_single((search_match.line, search_match.col));
        }
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_repeat_search(&mut self, reverse: bool) -> Task<Message> {
        let Some(last_search) = self.vim_state.last_search().map(str::to_owned)
        else {
            return Task::none();
        };
        if self.search_state.query != last_search {
            self.search_state.set_query(last_search, &self.buffer);
            self.search_state
                .select_match_near_cursor(self.cursors.primary_position());
        }
        if self.search_state.matches.is_empty() {
            return Task::none();
        }

        if reverse {
            self.search_state.previous_match();
        } else {
            self.search_state.next_match();
        }
        if let Some(search_match) = self.search_state.current_match() {
            self.cursors.set_single((search_match.line, search_match.col));
        }
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_delete_characters(&mut self, count: usize) -> Task<Message> {
        let start = self.vim_normal_position(self.cursors.primary_position());
        let line_len = self.buffer.line_len(start.0);
        let end = (start.0, start.1.saturating_add(count).min(line_len));
        self.handle_vim_character_operator(
            VimOperator::Delete,
            start,
            end,
            false,
        )
    }

    fn handle_vim_motion_operator(
        &mut self,
        operator: VimOperator,
        motion: VimMotion,
        count: usize,
        explicit_count: bool,
    ) -> Task<Message> {
        let start = self.vim_normal_position(self.cursors.primary_position());
        if matches!(
            motion,
            VimMotion::Up
                | VimMotion::Down
                | VimMotion::DocumentStart
                | VimMotion::DocumentEnd
        ) {
            let target =
                self.vim_motion_target(start, motion, count, explicit_count);
            return self.handle_vim_line_operator(
                operator,
                start.0.min(target.0),
                start.0.max(target.0),
                false,
            );
        }

        let target =
            self.vim_motion_target(start, motion, count, explicit_count);
        let (range_start, range_end) = match motion {
            VimMotion::Right => (
                start,
                (
                    start.0,
                    start
                        .1
                        .saturating_add(count)
                        .min(self.buffer.line_len(start.0)),
                ),
            ),
            VimMotion::Left => {
                ((start.0, start.1.saturating_sub(count)), start)
            }
            VimMotion::WordEnd | VimMotion::LineEnd => {
                let end = if motion == VimMotion::LineEnd {
                    (start.0, self.buffer.line_len(start.0))
                } else {
                    (
                        target.0,
                        target
                            .1
                            .saturating_add(1)
                            .min(self.buffer.line_len(target.0)),
                    )
                };
                (start.min(end), start.max(end))
            }
            VimMotion::WordForward => {
                let end = if target > start {
                    target
                } else {
                    (start.0, self.buffer.line_len(start.0))
                };
                (start.min(end), start.max(end))
            }
            VimMotion::WordBackward
            | VimMotion::LineStart
            | VimMotion::FirstNonBlank => {
                (start.min(target), start.max(target))
            }
            VimMotion::Up
            | VimMotion::Down
            | VimMotion::DocumentStart
            | VimMotion::DocumentEnd => return Task::none(),
        };

        self.handle_vim_character_operator(
            operator,
            range_start,
            range_end,
            false,
        )
    }

    fn handle_vim_visual_operator(
        &mut self,
        operator: VimOperator,
    ) -> Task<Message> {
        if self.vim_state.mode() == VimMode::VisualLine {
            let (anchor, active) =
                self.vim_state.visual_positions().unwrap_or_else(|| {
                    let position = self.cursors.primary_position();
                    (position, position)
                });
            self.handle_vim_line_operator(
                operator,
                anchor.0.min(active.0),
                anchor.0.max(active.0),
                true,
            )
        } else {
            let Some((start, end)) = self.cursors.primary().selection_range()
            else {
                return Task::none();
            };
            self.handle_vim_character_operator(operator, start, end, true)
        }
    }

    fn handle_vim_character_operator(
        &mut self,
        operator: VimOperator,
        start: (usize, usize),
        end: (usize, usize),
        from_visual: bool,
    ) -> Task<Message> {
        if start == end {
            return Task::none();
        }
        let register = VimRegister {
            text: self.extract_text_range(start, end),
            kind: VimRegisterKind::Characterwise,
        };
        self.apply_vim_operator(operator, start, end, register, from_visual)
    }

    fn handle_vim_line_operator(
        &mut self,
        operator: VimOperator,
        start_line: usize,
        end_line: usize,
        from_visual: bool,
    ) -> Task<Message> {
        let last_line = self.buffer.line_count().saturating_sub(1);
        let start_line = start_line.min(last_line);
        let end_line = end_line.min(last_line).max(start_line);
        let mut text = String::new();
        for line in start_line..=end_line {
            text.push_str(self.buffer.line(line));
            text.push('\n');
        }

        let (start, end) = if end_line < last_line {
            ((start_line, 0), (end_line + 1, 0))
        } else if start_line > 0 {
            (
                (start_line - 1, self.buffer.line_len(start_line - 1)),
                (end_line, self.buffer.line_len(end_line)),
            )
        } else {
            ((0, 0), (end_line, self.buffer.line_len(end_line)))
        };
        self.apply_vim_operator(
            operator,
            start,
            end,
            VimRegister { text, kind: VimRegisterKind::Linewise },
            from_visual,
        )
    }

    fn apply_vim_operator(
        &mut self,
        operator: VimOperator,
        start: (usize, usize),
        end: (usize, usize),
        register: VimRegister,
        from_visual: bool,
    ) -> Task<Message> {
        self.vim_state.register = register;

        if operator == VimOperator::Yank {
            if from_visual {
                self.cursors.set_single(self.vim_normal_position(start));
            } else {
                let position =
                    self.vim_normal_position(self.cursors.primary_position());
                self.cursors.set_single(position);
            }
            self.vim_state.enter_clean_normal_mode();
            self.finish_navigation_operation();
            return self.scroll_to_cursor();
        }

        self.end_grouping_if_active();
        if operator == VimOperator::Change {
            self.ensure_grouping_started("Vim change");
        }

        self.pre_edit_line = start.0.min(end.0);
        self.pre_edit_last_line = start.0.max(end.0);
        self.capture_lsp_edit_snapshot(&Message::DeleteSelection);

        let cursor_before = self.cursors.primary_position();
        let mut command =
            DeleteRangeCommand::new(&self.buffer, start, end, cursor_before);
        let mut cursor_after = cursor_before;
        command.execute(&mut self.buffer, &mut cursor_after);
        self.history.push(Box::new(command));
        self.cursors.set_single(self.vim_normal_position(cursor_after));

        if operator == VimOperator::Change {
            self.vim_state.enter_insert_mode();
        } else {
            self.vim_state.enter_clean_normal_mode();
        }
        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_paste(
        &mut self,
        position: VimPastePosition,
        count: usize,
    ) -> Task<Message> {
        let register = self.vim_state.register.clone();
        if register.text.is_empty() {
            return Task::none();
        }
        self.end_grouping_if_active();

        let current = self.vim_normal_position(self.cursors.primary_position());
        let (insert_at, text, cursor_after) = match register.kind {
            VimRegisterKind::Characterwise => {
                let insert_at = match position {
                    VimPastePosition::BeforeCursor => current,
                    VimPastePosition::AfterCursor => (
                        current.0,
                        current
                            .1
                            .saturating_add(usize::from(
                                self.buffer.line_len(current.0) > 0,
                            ))
                            .min(self.buffer.line_len(current.0)),
                    ),
                };
                (insert_at, register.text.repeat(count.max(1)), insert_at)
            }
            VimRegisterKind::Linewise => {
                let repeated = register.text.repeat(count.max(1));
                match position {
                    VimPastePosition::BeforeCursor => {
                        ((current.0, 0), repeated, (current.0, 0))
                    }
                    VimPastePosition::AfterCursor
                        if current.0 + 1 < self.buffer.line_count() =>
                    {
                        ((current.0 + 1, 0), repeated, (current.0 + 1, 0))
                    }
                    VimPastePosition::AfterCursor => {
                        let text = format!(
                            "\n{}",
                            repeated.strip_suffix('\n').unwrap_or(&repeated)
                        );
                        (
                            (current.0, self.buffer.line_len(current.0)),
                            text,
                            (current.0 + 1, 0),
                        )
                    }
                }
            }
        };

        self.pre_edit_line = insert_at.0;
        self.pre_edit_last_line = insert_at.0;
        self.capture_lsp_edit_snapshot(&Message::Paste(text.clone()));
        let mut command =
            InsertTextCommand::new(insert_at.0, insert_at.1, text, current)
                .with_cursor_after(cursor_after);
        let mut command_cursor = current;
        command.execute(&mut self.buffer, &mut command_cursor);
        self.history.push(Box::new(command));
        self.cursors.set_single(self.vim_normal_position(command_cursor));
        self.vim_state.enter_clean_normal_mode();
        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_history(
        &mut self,
        redo: bool,
        count: usize,
    ) -> Task<Message> {
        self.end_grouping_if_active();
        self.pre_edit_line = 0;
        self.pre_edit_last_line = usize::MAX;
        self.capture_lsp_edit_snapshot(if redo {
            &Message::Redo
        } else {
            &Message::Undo
        });

        let mut cursor = self.cursors.primary_position();
        let mut changed = false;
        for _ in 0..count.max(1) {
            let applied = if redo {
                self.history.redo(&mut self.buffer, &mut cursor)
            } else {
                self.history.undo(&mut self.buffer, &mut cursor)
            };
            if !applied {
                break;
            }
            changed = true;
        }
        if !changed {
            return Task::none();
        }

        self.cursors.set_single(self.vim_normal_position(cursor));
        self.vim_state.enter_clean_normal_mode();
        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_mode(
        &mut self,
        mode: VimMode,
        previous_mode: VimMode,
    ) -> Task<Message> {
        self.end_grouping_if_active();
        match mode {
            VimMode::Normal => {
                let mut active = self
                    .vim_state
                    .visual_positions()
                    .map(|(_, active)| active)
                    .unwrap_or_else(|| self.cursors.primary_position());
                if previous_mode == VimMode::Insert {
                    active.1 = active.1.saturating_sub(1);
                }
                self.vim_state.clear_visual();
                self.cursors.set_single(self.vim_normal_position(active));
            }
            VimMode::Visual | VimMode::VisualLine => {
                let position =
                    self.vim_normal_position(self.cursors.primary_position());
                self.vim_state.begin_visual(position);
                self.apply_vim_visual_selection(
                    position,
                    position,
                    mode == VimMode::VisualLine,
                );
            }
            VimMode::Insert => {}
        }
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_motion(
        &mut self,
        motion: VimMotion,
        count: usize,
        explicit_count: bool,
    ) -> Task<Message> {
        self.end_grouping_if_active();
        match self.vim_state.mode() {
            VimMode::Visual | VimMode::VisualLine => {
                let (anchor, active) =
                    self.vim_state.visual_positions().unwrap_or_else(|| {
                        let position = self.vim_normal_position(
                            self.cursors.primary_position(),
                        );
                        (position, position)
                    });
                let target = self.vim_motion_target(
                    active,
                    motion,
                    count,
                    explicit_count,
                );
                self.vim_state.set_visual_active(target);
                self.apply_vim_visual_selection(
                    anchor,
                    target,
                    self.vim_state.mode() == VimMode::VisualLine,
                );
            }
            VimMode::Normal => {
                let target = self.vim_motion_target(
                    self.cursors.primary_position(),
                    motion,
                    count,
                    explicit_count,
                );
                self.cursors.set_single(target);
                self.overlay_cache.clear();
            }
            VimMode::Insert => return Task::none(),
        }
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    fn handle_vim_insert(
        &mut self,
        position: VimInsertPosition,
        count: usize,
    ) -> Task<Message> {
        self.end_grouping_if_active();
        let current = self
            .vim_state
            .visual_positions()
            .map(|(_, active)| active)
            .unwrap_or_else(|| self.cursors.primary_position());
        self.vim_state.clear_visual();
        let current = self.vim_normal_position(current);
        self.cursors.set_single(current);
        self.ensure_grouping_started("Vim insert");

        match position {
            VimInsertPosition::BeforeCursor => {}
            VimInsertPosition::AfterCursor => {
                let line_len = self.buffer.line_len(current.0);
                self.cursors.primary_mut().position.1 =
                    current.1.saturating_add(1).min(line_len);
            }
            VimInsertPosition::FirstNonBlank => {
                self.cursors.primary_mut().position.1 = self
                    .buffer
                    .line(current.0)
                    .chars()
                    .position(|ch| !ch.is_whitespace())
                    .unwrap_or(0);
            }
            VimInsertPosition::EndOfLine => {
                self.cursors.primary_mut().position.1 =
                    self.buffer.line_len(current.0);
            }
            VimInsertPosition::NewLineBelow => {
                self.cursors.primary_mut().position.1 =
                    self.buffer.line_len(current.0);
                for _ in 0..count.max(1) {
                    let _ = self.update(&Message::Enter);
                }
            }
            VimInsertPosition::NewLineAbove => {
                self.cursors.primary_mut().position.1 = 0;
                let line = current.0;
                for _ in 0..count.max(1) {
                    let _ = self.update(&Message::Enter);
                    self.cursors.set_single((line, 0));
                }
            }
        }

        self.overlay_cache.clear();
        self.reset_cursor_blink();
        self.scroll_to_cursor()
    }

    /// Handles arrow key navigation.
    ///
    /// # Arguments
    ///
    /// * `direction` - The direction of movement
    /// * `shift_pressed` - Whether Shift is held (for selection)
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_arrow_key(
        &mut self,
        direction: ArrowDirection,
        shift_pressed: bool,
    ) -> Task<Message> {
        // End grouping on navigation
        self.end_grouping_if_active();

        if shift_pressed {
            // Set anchor on ALL cursors that don't yet have one
            for cursor in self.cursors.as_mut_slice() {
                if cursor.anchor.is_none() {
                    cursor.set_anchor();
                }
            }
            self.move_cursor(direction);
        } else {
            // Clear all selections, then move all cursors
            self.clear_selection();
            self.move_cursor(direction);
        }
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles Home key press.
    ///
    /// Moves the cursor to the start of the current line.
    ///
    /// # Arguments
    ///
    /// * `shift_pressed` - Whether Shift is held (for selection)
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible (including
    /// horizontal scroll back to x=0 when wrap is disabled)
    fn handle_home(&mut self, shift_pressed: bool) -> Task<Message> {
        if shift_pressed {
            for cursor in self.cursors.as_mut_slice() {
                if cursor.anchor.is_none() {
                    cursor.set_anchor();
                }
                cursor.position.1 = 0;
            }
        } else {
            self.clear_selection();
            for cursor in self.cursors.as_mut_slice() {
                cursor.position.1 = 0;
            }
        }
        self.cursors.sort_and_merge();
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles End key press.
    ///
    /// Moves the cursor to the end of the current line.
    ///
    /// # Arguments
    ///
    /// * `shift_pressed` - Whether Shift is held (for selection)
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible (including
    /// horizontal scroll to end of line when wrap is disabled)
    fn handle_end(&mut self, shift_pressed: bool) -> Task<Message> {
        if shift_pressed {
            for cursor in self.cursors.as_mut_slice() {
                if cursor.anchor.is_none() {
                    cursor.set_anchor();
                }
                cursor.position.1 = self.buffer.line_len(cursor.position.0);
            }
        } else {
            self.clear_selection();
            for cursor in self.cursors.as_mut_slice() {
                cursor.position.1 = self.buffer.line_len(cursor.position.0);
            }
        }
        self.cursors.sort_and_merge();
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles Ctrl+Home key press.
    ///
    /// Moves the cursor to the beginning of the document.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_ctrl_home(&mut self) -> Task<Message> {
        // Move cursor to the beginning of the document
        self.clear_selection();
        self.cursors.set_single((0, 0));
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles Ctrl+End key press.
    ///
    /// Moves the cursor to the end of the document.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_ctrl_end(&mut self) -> Task<Message> {
        // Move cursor to the end of the document
        self.clear_selection();
        let last_line = self.buffer.line_count().saturating_sub(1);
        let last_col = self.buffer.line_len(last_line);
        self.cursors.set_single((last_line, last_col));
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles Page Up key press.
    ///
    /// Scrolls the view up by one page.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_page_up(&mut self) -> Task<Message> {
        self.page_up();
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles Page Down key press.
    ///
    /// Scrolls the view down by one page.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_page_down(&mut self) -> Task<Message> {
        self.page_down();
        self.finish_navigation_operation();
        self.scroll_to_cursor()
    }

    /// Handles direct navigation to an explicit logical position.
    ///
    /// # Arguments
    ///
    /// * `line` - Target line index (0-based)
    /// * `col` - Target column index (0-based)
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    fn handle_goto_position(
        &mut self,
        line: usize,
        col: usize,
    ) -> Task<Message> {
        // End grouping on navigation command
        self.end_grouping_if_active();
        self.set_cursor(line, col)
    }

    // =========================================================================
    // Mouse and Selection Handlers
    // =========================================================================

    /// Synchronises the active search result with a manual primary-cursor
    /// position or selection.
    fn sync_search_match_from_primary_cursor(&mut self) {
        if !self.search_matches_visible() || self.search_state.query.is_empty()
        {
            return;
        }

        let primary = self.cursors.primary();
        let cursor = primary.position;
        let selection = primary.selection_range();
        if self.search_state.select_match_at_cursor(cursor, selection) {
            self.overlay_cache.clear();
        }
    }

    /// Handles mouse click operations.
    ///
    /// Sets focus, ends command grouping, positions cursor, starts selection tracking.
    ///
    /// # Arguments
    ///
    /// * `point` - The click position
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none() as no scrolling is needed)
    fn handle_mouse_click_msg(&mut self, point: iced::Point) -> Task<Message> {
        // Capture focus when clicked using the new focus method
        self.request_focus();

        // Set internal canvas focus state
        self.has_canvas_focus = true;

        // End grouping on mouse click
        self.end_grouping_if_active();

        // Regular click collapses any multi-cursor state to a single cursor
        // positioned at the click location.
        self.cursors.remove_all_but_primary();

        self.handle_mouse_click(point);
        self.reset_cursor_blink();
        // Clear selection on click, then set anchor for potential drag selection
        self.clear_selection();
        self.is_dragging = true;
        self.cursors.primary_mut().set_anchor();
        self.sync_search_match_from_primary_cursor();

        // Show cursor when focused
        self.show_cursor = true;

        Task::none()
    }

    /// Handles mouse drag operations for selection.
    ///
    /// # Arguments
    ///
    /// * `point` - The drag position
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none() as no scrolling is needed)
    fn handle_mouse_drag_msg(&mut self, point: iced::Point) -> Task<Message> {
        if self.is_dragging {
            let before_pos = self.cursors.primary_position();
            self.handle_mouse_drag(point);
            if self.cursors.primary_position() != before_pos {
                // Mouse move events can be very frequent. Only invalidate the
                // overlay cache if the drag actually changed selection/cursor.
                self.overlay_cache.clear();
            }
        }
        Task::none()
    }

    /// Handles mouse release operations.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none() as no scrolling is needed)
    fn handle_mouse_release_msg(&mut self) -> Task<Message> {
        self.is_dragging = false;
        if self.vim_enabled {
            if self.cursors.primary().has_selection() {
                let anchor = self.cursors.primary().anchor.unwrap_or_default();
                let position = self.cursors.primary_position();
                let active = if position >= anchor && position.1 > 0 {
                    (position.0, position.1 - 1)
                } else {
                    position
                };
                let anchor = self.vim_normal_position(anchor);
                let active = self.vim_normal_position(active);
                self.vim_state.set_mode_from_mouse(VimMode::Visual);
                self.vim_state.begin_visual(anchor);
                self.vim_state.set_visual_active(active);
            } else {
                let position =
                    self.vim_normal_position(self.cursors.primary_position());
                self.cursors.set_single(position);
            }
            self.overlay_cache.clear();
        }
        self.sync_search_match_from_primary_cursor();
        Task::none()
    }

    /// Handles a double-click: selects the word under the cursor.
    ///
    /// If the click lands outside any word (e.g. on whitespace), the
    /// selection is cleared and the caret is simply placed there.
    fn handle_double_click_msg(&mut self, point: iced::Point) -> Task<Message> {
        self.request_focus();
        self.has_canvas_focus = true;
        self.end_grouping_if_active();
        self.cursors.remove_all_but_primary();
        if let Some((line, col)) = self.calculate_cursor_from_point(point) {
            let line_content = self.buffer.line(line);
            let start = Self::word_start_in_line(line_content, col);
            let end = Self::word_end_in_line(line_content, col);
            let cursor = self.cursors.primary_mut();
            if start < end {
                cursor.anchor = Some((line, start));
                cursor.position = (line, end);
            } else {
                cursor.anchor = None;
                cursor.position = (line, col);
            }
        }
        self.is_dragging = false;
        self.show_cursor = true;
        self.reset_cursor_blink();
        self.overlay_cache.clear();
        self.sync_search_match_from_primary_cursor();
        Task::none()
    }

    /// Handles a triple-click: selects the whole line under the cursor.
    fn handle_triple_click_msg(&mut self, point: iced::Point) -> Task<Message> {
        self.request_focus();
        self.has_canvas_focus = true;
        self.end_grouping_if_active();
        self.cursors.remove_all_but_primary();
        if let Some((line, _col)) = self.calculate_cursor_from_point(point) {
            let line_len = self.buffer.line_len(line);
            let cursor = self.cursors.primary_mut();
            cursor.anchor = Some((line, 0));
            cursor.position = (line, line_len);
        }
        self.is_dragging = false;
        self.show_cursor = true;
        self.reset_cursor_blink();
        self.overlay_cache.clear();
        self.sync_search_match_from_primary_cursor();
        Task::none()
    }

    /// Handles a right-click before the context menu is displayed.
    ///
    /// A click inside any existing selection preserves it so Cut and Copy act
    /// on the selected text. A click elsewhere collapses the selection and
    /// moves the caret to the clicked position.
    fn handle_context_menu_requested_msg(
        &mut self,
        point: iced::Point,
    ) -> Task<Message> {
        self.request_focus();
        self.has_canvas_focus = true;
        self.focus_locked = false;
        self.show_cursor = true;
        self.is_dragging = false;
        self.end_grouping_if_active();

        if let Some(position) = self.calculate_cursor_from_point(point) {
            let inside_selection = self.cursors.iter().any(|cursor| {
                cursor.selection_range().is_some_and(|(start, end)| {
                    (start..=end).contains(&position)
                })
            });

            if !inside_selection {
                self.cursors.set_single(position);
                self.overlay_cache.clear();
            }
        }

        self.reset_cursor_blink();
        Task::none()
    }

    // =========================================================================
    // Clipboard Handlers
    // =========================================================================

    /// Cuts all selected ranges to the clipboard as a single undoable edit.
    fn handle_cut_msg(&mut self) -> Task<Message> {
        if !self.cursors.iter().any(|cursor| cursor.has_selection()) {
            return Task::none();
        }

        self.end_grouping_if_active();
        self.ensure_grouping_started("Cut");
        let clipboard_task = self.copy_selection();
        self.delete_selection();
        self.end_grouping_if_active();
        self.finish_edit_operation();

        Task::batch([clipboard_task, self.scroll_to_cursor()])
    }

    /// Selects the complete document.
    fn handle_select_all_msg(&mut self) -> Task<Message> {
        self.end_grouping_if_active();

        let last_line = self.buffer.line_count().saturating_sub(1);
        let end = (last_line, self.buffer.line_len(last_line));
        self.cursors.set_single(end);
        self.cursors.primary_mut().anchor = Some((0, 0));
        self.overlay_cache.clear();
        self.reset_cursor_blink();

        self.scroll_to_cursor()
    }

    /// Handles paste operations.
    ///
    /// If the provided text is empty, reads from clipboard. Otherwise pastes
    /// the provided text at the cursor position.
    ///
    /// # Arguments
    ///
    /// * `text` - The text to paste (empty string triggers clipboard read)
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that may read clipboard or scroll to cursor
    fn handle_paste_msg(&mut self, text: &str) -> Task<Message> {
        // End grouping on paste
        self.end_grouping_if_active();

        // If text is empty, we need to read from clipboard
        if text.is_empty() {
            // Return a task that reads clipboard and chains to paste
            iced::clipboard::read().and_then(|clipboard_text| {
                Task::done(Message::Paste(clipboard_text))
            })
        } else {
            // We have the text, paste it
            self.paste_text(text);
            self.finish_edit_operation();
            self.scroll_to_cursor()
        }
    }

    // =========================================================================
    // History (Undo/Redo) Handlers
    // =========================================================================

    /// Handles undo operations.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to cursor if undo succeeded
    fn handle_undo_msg(&mut self) -> Task<Message> {
        // End any current grouping before undoing
        self.end_grouping_if_active();

        let mut cursor_pos = self.cursors.primary_position();
        if self.history.undo(&mut self.buffer, &mut cursor_pos) {
            self.cursors.primary_mut().position = cursor_pos;
            self.clear_selection();
            // An undone command (especially a composite like "Replace All") may
            // touch lines anywhere in the document, so reset the highlight cache
            // entirely rather than trusting the cursor as the change origin.
            self.pre_edit_line = 0;
            self.pre_edit_last_line = usize::MAX;
            self.finish_edit_operation();
            self.scroll_to_cursor()
        } else {
            Task::none()
        }
    }

    /// Handles redo operations.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to cursor if redo succeeded
    fn handle_redo_msg(&mut self) -> Task<Message> {
        let mut cursor_pos = self.cursors.primary_position();
        if self.history.redo(&mut self.buffer, &mut cursor_pos) {
            self.cursors.primary_mut().position = cursor_pos;
            self.clear_selection();
            // A redone command may touch lines anywhere; reset the highlight
            // cache entirely (see `handle_undo_msg`).
            self.pre_edit_line = 0;
            self.pre_edit_last_line = usize::MAX;
            self.finish_edit_operation();
            self.scroll_to_cursor()
        } else {
            Task::none()
        }
    }

    // =========================================================================
    // Search and Replace Handlers
    // =========================================================================

    /// Handles opening the search dialog.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that focuses and selects all in the search input
    fn handle_open_search_msg(&mut self) -> Task<Message> {
        self.goto_line_state.close();
        self.search_state.open_search();
        if !self.search_state.query.is_empty() {
            self.search_state.update_matches(&self.buffer);
            self.search_state
                .select_match_near_cursor(self.cursors.primary_position());
        }
        self.overlay_cache.clear();

        // Focus the search input and select all text if any
        Task::batch([
            focus(self.search_state.search_input_id.clone()),
            select_all(self.search_state.search_input_id.clone()),
        ])
    }

    /// Handles opening the search and replace dialog.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that focuses and selects all in the search input
    fn handle_open_search_replace_msg(&mut self) -> Task<Message> {
        self.goto_line_state.close();
        self.search_state.open_replace();
        if !self.search_state.query.is_empty() {
            self.search_state.update_matches(&self.buffer);
            self.search_state
                .select_match_near_cursor(self.cursors.primary_position());
        }
        self.overlay_cache.clear();

        // Focus the search input and select all text if any
        Task::batch([
            focus(self.search_state.search_input_id.clone()),
            select_all(self.search_state.search_input_id.clone()),
        ])
    }

    /// Handles closing the search dialog.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_close_search_msg(&mut self) -> Task<Message> {
        // Escape with multiple cursors and no open search: collapse to primary cursor
        if self.cursors.is_multi() && !self.search_state.is_open {
            self.cursors.remove_all_but_primary();
            self.overlay_cache.clear();
            return Task::none();
        }
        self.search_state.close();
        self.overlay_cache.clear();
        Task::none()
    }

    /// Opens the go-to-line input and selects the current one-based line.
    fn handle_open_goto_line_msg(&mut self) -> Task<Message> {
        self.search_state.close();
        self.goto_line_state.open(self.cursors.primary_position().0);
        self.overlay_cache.clear();

        Task::batch([
            focus(self.goto_line_state.input_id.clone()),
            select_all(self.goto_line_state.input_id.clone()),
        ])
    }

    /// Closes the go-to-line input without moving the cursor.
    fn handle_close_goto_line_msg(&mut self) -> Task<Message> {
        self.goto_line_state.close();
        self.overlay_cache.clear();
        Task::none()
    }

    /// Updates the one-based line number entered by the user.
    fn handle_goto_line_changed_msg(&mut self, query: &str) -> Task<Message> {
        self.goto_line_state.query = query.to_string();
        Task::none()
    }

    /// Moves to the submitted one-based line and closes the input.
    fn handle_submit_goto_line_msg(&mut self) -> Task<Message> {
        let Some(one_based_line) = self.goto_line_state.target_line() else {
            return Task::none();
        };

        let target_line = one_based_line
            .saturating_sub(1)
            .min(self.buffer.line_count().saturating_sub(1));
        while self.hidden_lines_set().contains(&target_line) {
            let collapsed_count = self.collapsed_folds.len();
            self.unfold_at(target_line);
            if self.collapsed_folds.len() == collapsed_count {
                break;
            }
        }

        self.goto_line_state.close();
        self.handle_goto_position(target_line, 0)
    }

    /// Handles search query text changes.
    ///
    /// # Arguments
    ///
    /// * `query` - The new search query
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to first match if any
    fn handle_search_query_changed_msg(
        &mut self,
        query: &str,
    ) -> Task<Message> {
        self.search_state.set_query(query.to_string(), &self.buffer);
        self.overlay_cache.clear();

        // Move cursor to first match if any
        if let Some(match_pos) = self.search_state.current_match() {
            self.cursors.primary_mut().position =
                (match_pos.line, match_pos.col);
            self.clear_selection();
            return self.scroll_to_cursor();
        }
        Task::none()
    }

    /// Handles replace query text changes.
    ///
    /// # Arguments
    ///
    /// * `replace_text` - The new replacement text
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_replace_query_changed_msg(
        &mut self,
        replace_text: &str,
    ) -> Task<Message> {
        self.search_state.set_replace_with(replace_text.to_string());
        Task::none()
    }

    /// Handles toggling case-sensitive search.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to first match if any
    fn handle_toggle_case_sensitive_msg(&mut self) -> Task<Message> {
        self.search_state.toggle_case_sensitive(&self.buffer);
        self.overlay_cache.clear();

        // Move cursor to first match if any
        if let Some(match_pos) = self.search_state.current_match() {
            self.cursors.primary_mut().position =
                (match_pos.line, match_pos.col);
            self.clear_selection();
            return self.scroll_to_cursor();
        }
        Task::none()
    }

    /// Handles finding the next match.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to the next match if any
    fn handle_find_next_msg(&mut self) -> Task<Message> {
        if !self.search_state.matches.is_empty() {
            self.search_state.next_match();
            if let Some(match_pos) = self.search_state.current_match() {
                self.cursors.primary_mut().position =
                    (match_pos.line, match_pos.col);
                self.clear_selection();
                self.overlay_cache.clear();
                return self.scroll_to_cursor();
            }
        }
        Task::none()
    }

    /// Handles finding the previous match.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to the previous match if any
    fn handle_find_previous_msg(&mut self) -> Task<Message> {
        if !self.search_state.matches.is_empty() {
            self.search_state.previous_match();
            if let Some(match_pos) = self.search_state.current_match() {
                self.cursors.primary_mut().position =
                    (match_pos.line, match_pos.col);
                self.clear_selection();
                self.overlay_cache.clear();
                return self.scroll_to_cursor();
            }
        }
        Task::none()
    }

    /// Handles replacing the current match and moving to the next.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to the next match if any
    fn handle_replace_next_msg(&mut self) -> Task<Message> {
        // Replace current match and move to next
        if let Some(match_pos) = self.search_state.current_match() {
            let query_len = self.search_state.query.chars().count();
            let replace_text = self.search_state.replace_with.clone();

            // Create and execute replace command
            let pos = self.cursors.primary_position();
            let mut cmd = ReplaceTextCommand::new(
                &self.buffer,
                (match_pos.line, match_pos.col),
                query_len,
                replace_text,
                pos,
            );
            let mut cursor_pos = pos;
            cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.primary_mut().position = cursor_pos;
            self.history.push(Box::new(cmd));

            // The replacement starts at the matched line; invalidate highlight
            // from there regardless of where the cursor moved next.
            self.pre_edit_line = self.pre_edit_line.min(match_pos.line);
            self.pre_edit_last_line =
                self.pre_edit_last_line.max(match_pos.line);

            self.clear_selection();
            self.finish_edit_operation();

            // Move to the closest remaining match after the replacement.
            if !self.search_state.matches.is_empty()
                && let Some(next_match) = self.search_state.current_match()
            {
                self.cursors.primary_mut().position =
                    (next_match.line, next_match.col);
            }

            return self.scroll_to_cursor();
        }
        Task::none()
    }

    /// Handles replacing all matches.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to cursor after replacement
    fn handle_replace_all_msg(&mut self) -> Task<Message> {
        // Perform a fresh search to find ALL matches (ignoring the display limit)
        let all_matches = super::search::find_matches(
            &self.buffer,
            &self.search_state.query,
            self.search_state.case_sensitive,
            None, // No limit for Replace All
        );

        if !all_matches.is_empty() {
            let query_len = self.search_state.query.chars().count();
            let replace_text = self.search_state.replace_with.clone();

            // Create composite command for undo
            let mut composite =
                CompositeCommand::new("Replace All".to_string());

            // Process matches in reverse order (to preserve positions)
            for match_pos in all_matches.iter().rev() {
                let pos = self.cursors.primary_position();
                let cmd = ReplaceTextCommand::new(
                    &self.buffer,
                    (match_pos.line, match_pos.col),
                    query_len,
                    replace_text.clone(),
                    pos,
                );
                composite.add(Box::new(cmd));
            }

            // Execute all replacements
            let mut cursor_pos = self.cursors.primary_position();
            composite.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.primary_mut().position = cursor_pos;
            self.history.push(Box::new(composite));

            // Replace All touches matches anywhere in the document, so reset
            // the highlight cache entirely.
            self.pre_edit_line = 0;
            self.pre_edit_last_line = usize::MAX;

            self.clear_selection();
            self.finish_edit_operation();
            self.scroll_to_cursor()
        } else {
            Task::none()
        }
    }

    /// Handles Tab key in search dialog (cycle forward).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that focuses the next field
    fn handle_search_dialog_tab_msg(&mut self) -> Task<Message> {
        // Cycle focus forward (Search → Replace → Search)
        self.search_state.focus_next_field();

        // Focus the appropriate input based on new focused_field
        match self.search_state.focused_field {
            crate::canvas_editor::search::SearchFocusedField::Search => {
                focus(self.search_state.search_input_id.clone())
            }
            crate::canvas_editor::search::SearchFocusedField::Replace => {
                focus(self.search_state.replace_input_id.clone())
            }
        }
    }

    /// Handles Shift+Tab key in search dialog (cycle backward).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that focuses the previous field
    fn handle_search_dialog_shift_tab_msg(&mut self) -> Task<Message> {
        // Cycle focus backward (Replace → Search → Replace)
        self.search_state.focus_previous_field();

        // Focus the appropriate input based on new focused_field
        match self.search_state.focused_field {
            crate::canvas_editor::search::SearchFocusedField::Search => {
                focus(self.search_state.search_input_id.clone())
            }
            crate::canvas_editor::search::SearchFocusedField::Replace => {
                focus(self.search_state.replace_input_id.clone())
            }
        }
    }

    // =========================================================================
    // Focus and IME Handlers
    // =========================================================================

    /// Handles canvas focus gained event.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_canvas_focus_gained_msg(&mut self) -> Task<Message> {
        self.has_canvas_focus = true;
        self.focus_locked = false; // Unlock focus when gained
        self.show_cursor = true;
        self.reset_cursor_blink();
        self.overlay_cache.clear();
        Task::none()
    }

    /// Handles canvas focus lost event.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_canvas_focus_lost_msg(&mut self) -> Task<Message> {
        self.has_canvas_focus = false;
        self.focus_locked = true; // Lock focus when lost to prevent focus stealing
        self.show_cursor = false;
        self.ime_preedit = None;
        self.overlay_cache.clear();
        Task::none()
    }

    /// Handles IME opened event.
    ///
    /// Clears current preedit content to accept new input.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_ime_opened_msg(&mut self) -> Task<Message> {
        self.ime_preedit = None;
        self.overlay_cache.clear();
        Task::none()
    }

    /// Handles IME preedit event.
    ///
    /// Updates the preedit text and selection while the user is composing.
    ///
    /// # Arguments
    ///
    /// * `content` - The preedit text content
    /// * `selection` - The selection range within the preedit text
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_ime_preedit_msg(
        &mut self,
        content: &str,
        selection: &Option<std::ops::Range<usize>>,
    ) -> Task<Message> {
        if content.is_empty() {
            self.ime_preedit = None;
        } else {
            self.ime_preedit = Some(ImePreedit {
                content: content.to_string(),
                selection: selection.clone(),
            });
        }

        self.overlay_cache.clear();
        Task::none()
    }

    /// Handles IME commit event.
    ///
    /// Inserts the committed text at the cursor position.
    ///
    /// # Arguments
    ///
    /// * `text` - The committed text
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to cursor after insertion
    fn handle_ime_commit_msg(&mut self, text: &str) -> Task<Message> {
        self.ime_preedit = None;

        if text.is_empty() || !self.vim_accepts_insert_input() {
            self.overlay_cache.clear();
            return Task::none();
        }

        self.ensure_grouping_started("Typing");

        self.paste_text(text);
        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Handles IME closed event.
    ///
    /// Clears preedit state to return to normal input mode.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_ime_closed_msg(&mut self) -> Task<Message> {
        self.ime_preedit = None;
        self.overlay_cache.clear();
        Task::none()
    }

    // =========================================================================
    // Complex Standalone Handlers
    // =========================================================================

    /// Handles cursor blink tick event.
    ///
    /// Updates cursor visibility for blinking animation.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_tick_msg(&mut self) -> Task<Message> {
        // Handle cursor blinking only if editor has focus
        if self.has_focus()
            && self.last_blink.elapsed() >= CURSOR_BLINK_INTERVAL
        {
            self.cursor_visible = !self.cursor_visible;
            self.last_blink = super::Instant::now();
            self.overlay_cache.clear();
        }

        // Hide cursor if editor doesn't have focus
        if !self.has_focus() {
            self.show_cursor = false;
        }

        Task::none()
    }

    /// Handles viewport scrolled event.
    ///
    /// Manages the virtual scrolling cache window to optimize rendering
    /// for large files. Only clears the cache when scrolling crosses the
    /// cached window boundary or when viewport dimensions change.
    ///
    /// # Arguments
    ///
    /// * `viewport` - The viewport information after scrolling
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently Task::none())
    fn handle_scrolled_msg(
        &mut self,
        viewport: iced::widget::scrollable::Viewport,
    ) -> Task<Message> {
        // Virtual-scrolling cache window:
        // Instead of clearing the canvas cache for every small scroll,
        // we maintain a larger "render window" of visual lines around
        // the visible range. We only clear the cache and re-window
        // when the scroll crosses the window boundary or the viewport
        // size changes significantly. This prevents frequent re-highlighting
        // and layout recomputation for very large files while ensuring
        // the first scroll renders correctly without requiring a click.
        let new_scroll = viewport.absolute_offset().y;
        let new_height = viewport.bounds().height;
        let new_width = viewport.bounds().width;
        let scroll_changed = (self.viewport_scroll - new_scroll).abs() > 0.1;
        let visible_lines_count =
            (new_height / self.line_height).ceil() as usize + 2;
        let first_visible_line =
            (new_scroll / self.line_height).floor() as usize;
        let last_visible_line = first_visible_line + visible_lines_count;
        let margin = visible_lines_count
            * crate::canvas_editor::CACHE_WINDOW_MARGIN_MULTIPLIER;
        let window_start = first_visible_line.saturating_sub(margin);
        let window_end = last_visible_line + margin;
        // Decide whether we need to re-window the cache.
        // Special-case top-of-file: when window_start == 0, allow small forward scrolls
        // without forcing a rewindow, to avoid thrashing when the visible range is near 0.
        let need_rewindow =
            if self.cache_window_end_line > self.cache_window_start_line {
                let lower_boundary_trigger = self.cache_window_start_line > 0
                    && first_visible_line
                        < self
                            .cache_window_start_line
                            .saturating_add(visible_lines_count / 2);
                let upper_boundary_trigger = last_visible_line
                    > self
                        .cache_window_end_line
                        .saturating_sub(visible_lines_count / 2);
                lower_boundary_trigger || upper_boundary_trigger
            } else {
                true
            };
        // Clear cache when viewport dimensions change significantly
        // to ensure proper redraw (e.g., window resize)
        if (self.viewport_height - new_height).abs() > 1.0
            || (self.viewport_width - new_width).abs() > 1.0
            || (scroll_changed && need_rewindow)
        {
            self.cache_window_start_line = window_start;
            self.cache_window_end_line = window_end;
            self.last_first_visible_line = first_visible_line;
            self.content_cache.clear();
            self.overlay_cache.clear();
        }
        self.viewport_scroll = new_scroll;
        self.viewport_height = new_height;
        self.viewport_width = new_width;
        Task::none()
    }

    /// Handles horizontal scrollbar scrolled event (only active when wrap is disabled).
    ///
    /// Updates `horizontal_scroll_offset` and clears render caches when the offset
    /// changes by more than 0.1 pixels to avoid unnecessary redraws.
    ///
    /// # Arguments
    ///
    /// * `viewport` - The viewport information after scrolling
    ///
    /// # Returns
    ///
    /// A `Task<Message>` (currently `Task::none()`)
    fn handle_horizontal_scrolled_msg(
        &mut self,
        viewport: iced::widget::scrollable::Viewport,
    ) -> Task<Message> {
        let new_x = viewport.absolute_offset().x;
        if (self.horizontal_scroll_offset - new_x).abs() > 0.1 {
            self.horizontal_scroll_offset = new_x;
            self.content_cache.clear();
            self.overlay_cache.clear();
        }
        Task::none()
    }

    // =========================================================================
    // Multi-cursor operations
    // =========================================================================

    /// Handles Alt+Click: adds a new cursor at the clicked position without
    /// disturbing existing cursors.
    ///
    /// # Arguments
    ///
    /// * `point` - Canvas-local position of the click
    ///
    /// # Returns
    ///
    /// `Task::none()` — no async work needed
    fn handle_alt_click_msg(&mut self, point: iced::Point) -> Task<Message> {
        if self.vim_enabled {
            return Task::none();
        }
        if let Some(pos) = self.calculate_cursor_from_point(point) {
            self.cursors.add_cursor(pos);
            self.overlay_cache.clear();
            self.reset_cursor_blink();
        }
        Task::none()
    }

    /// Handles Ctrl+Alt+Up: adds a cursor on the line above the primary cursor,
    /// at the same column (clamped to line length).
    ///
    /// # Returns
    ///
    /// `Task::none()`
    fn handle_add_cursor_above_msg(&mut self) -> Task<Message> {
        if self.vim_enabled {
            return Task::none();
        }
        let (line, col) = self.cursors.primary_position();
        if line == 0 {
            return Task::none();
        }
        let new_line = line - 1;
        let new_col = col.min(self.buffer.line_len(new_line));
        self.cursors.add_cursor((new_line, new_col));
        self.overlay_cache.clear();
        self.reset_cursor_blink();
        Task::none()
    }

    /// Handles Ctrl+Alt+Down: adds a cursor on the line below the primary cursor,
    /// at the same column (clamped to line length).
    ///
    /// # Returns
    ///
    /// `Task::none()`
    fn handle_add_cursor_below_msg(&mut self) -> Task<Message> {
        if self.vim_enabled {
            return Task::none();
        }
        let (line, col) = self.cursors.primary_position();
        let last_line = self.buffer.line_count().saturating_sub(1);
        if line >= last_line {
            return Task::none();
        }
        let new_line = line + 1;
        let new_col = col.min(self.buffer.line_len(new_line));
        self.cursors.add_cursor((new_line, new_col));
        self.overlay_cache.clear();
        self.reset_cursor_blink();
        Task::none()
    }

    /// Handles Ctrl+D: selects the next occurrence of the text currently selected
    /// by the primary cursor, or the word under the primary cursor if there is no
    /// selection. A new cursor with that selection is added.
    ///
    /// # Returns
    ///
    /// `Task::none()`
    fn handle_select_next_occurrence_msg(&mut self) -> Task<Message> {
        if self.vim_enabled {
            return Task::none();
        }
        // Determine the search text: selected text on primary cursor, or word under cursor
        let search_text = if let Some(text) = self.get_selected_text() {
            text
        } else {
            // Select word under primary cursor first
            let (line, col) = self.cursors.primary_position();
            let line_str = self.buffer.line(line).to_string();
            let word_start = Self::word_start_in_line(&line_str, col);
            let word_end = Self::word_end_in_line(&line_str, col);
            if word_start == word_end {
                return Task::none();
            }
            // Apply selection to primary cursor and stop: the next Ctrl+D call
            // will find the next occurrence (selection will be non-empty then).
            self.cursors.primary_mut().anchor = Some((line, word_start));
            self.cursors.primary_mut().position = (line, word_end);
            self.overlay_cache.clear();
            return Task::none();
        };

        if search_text.is_empty() {
            return Task::none();
        }

        // Find the search start position: just after the last cursor's selection end
        let search_start = self
            .cursors
            .as_slice()
            .last()
            .map(|last| {
                last.selection_range()
                    .map(|(_, end)| end)
                    .unwrap_or(last.position)
            })
            .unwrap_or((0, 0));

        // Search forward from search_start for the next occurrence
        let (start_line, start_col) = search_start;
        let line_count = self.buffer.line_count();
        let search_char_len = search_text.chars().count();

        for line_offset in 0..=line_count {
            let line_idx = (start_line + line_offset) % line_count;
            let line_str = self.buffer.line(line_idx);

            // On the first iteration, start after start_col; on wrap-around, start from 0
            let search_col = if line_offset == 0 { start_col } else { 0 };

            // Build substring from search_col onward (char-indexed)
            let prefix_bytes = char_to_byte_index(line_str, search_col);
            let haystack = &line_str[prefix_bytes..];

            // The search_text is also char-based; find it as a substring
            if let Some(byte_offset) = haystack.find(search_text.as_str()) {
                // Convert byte_offset back to char offset
                let char_start =
                    search_col + haystack[..byte_offset].chars().count();
                let char_end = char_start + search_char_len;

                // Build cursor with selection for the found occurrence
                let found_cursor = cursor_set::Cursor {
                    position: (line_idx, char_end),
                    anchor: Some((line_idx, char_start)),
                };
                self.cursors.add_cursor_with_selection(found_cursor);
                self.overlay_cache.clear();
                self.reset_cursor_blink();
                return self.scroll_to_cursor();
            }
        }

        Task::none()
    }

    // =========================================================================
    // Main Update Method
    // =========================================================================

    /// Updates the editor state based on messages and returns scroll commands.
    ///
    /// # Arguments
    ///
    /// * `message` - The message to process for updating the editor state
    ///
    /// # Returns
    /// A `Task<Message>` for any asynchronous operations, such as scrolling to keep the cursor visible after state updates
    pub fn update(&mut self, message: &Message) -> Task<Message> {
        // Capture the topmost active line before any edit mutates the buffer,
        // so `finish_edit_operation` can truncate the highlight cache precisely.
        self.pre_edit_line = self.min_active_line();
        self.pre_edit_last_line = self.max_active_line();
        self.capture_lsp_edit_snapshot(message);
        match message {
            // Text input operations
            Message::CharacterInput(ch) if self.vim_accepts_insert_input() => {
                self.handle_character_input_msg(*ch)
            }
            Message::CharacterInput(_) => Task::none(),
            Message::VimKey(ch) => self.handle_vim_key_msg(*ch),
            Message::ToggleVimMode => {
                self.set_vim_enabled(!self.vim_enabled);
                Task::none()
            }
            Message::Tab if self.vim_accepts_insert_input() => {
                self.handle_tab()
            }
            Message::Enter if self.vim_accepts_insert_input() => {
                self.handle_enter()
            }
            Message::Tab | Message::Enter => Task::none(),

            // Deletion operations
            Message::Backspace if self.vim_accepts_insert_input() => {
                self.handle_backspace()
            }
            Message::Delete if self.vim_accepts_insert_input() => {
                self.handle_delete()
            }
            Message::Backspace | Message::Delete => Task::none(),
            Message::DeleteSelection => self.handle_delete_selection(),

            // Navigation operations
            Message::ArrowKey(direction, shift) => {
                self.handle_arrow_key(*direction, *shift)
            }
            Message::Home(shift) => self.handle_home(*shift),
            Message::End(shift) => self.handle_end(*shift),
            Message::CtrlHome => self.handle_ctrl_home(),
            Message::CtrlEnd => self.handle_ctrl_end(),
            Message::GotoPosition(line, col) => {
                self.handle_goto_position(*line, *col)
            }
            Message::OpenGotoLine => self.handle_open_goto_line_msg(),
            Message::CloseGotoLine => self.handle_close_goto_line_msg(),
            Message::GotoLineChanged(query) => {
                self.handle_goto_line_changed_msg(query)
            }
            Message::SubmitGotoLine => self.handle_submit_goto_line_msg(),
            Message::PageUp => self.handle_page_up(),
            Message::PageDown => self.handle_page_down(),

            // Mouse and selection operations
            Message::MouseClick(point) => self.handle_mouse_click_msg(*point),
            Message::MouseDrag(point) => self.handle_mouse_drag_msg(*point),
            Message::MouseHover(point) => self.handle_mouse_drag_msg(*point),
            Message::MouseRelease => self.handle_mouse_release_msg(),
            Message::DoubleClick(point) => self.handle_double_click_msg(*point),
            Message::TripleClick(point) => self.handle_triple_click_msg(*point),
            Message::ContextMenuRequested(point) => {
                self.handle_context_menu_requested_msg(*point)
            }
            Message::WriteRequested
            | Message::CustomContextMenuAction(_)
            | Message::RevealInFileManager => Task::none(),

            // Clipboard operations
            Message::Cut => self.handle_cut_msg(),
            Message::Copy => self.copy_selection(),
            Message::Paste(text) => self.handle_paste_msg(text),
            Message::SelectAll => self.handle_select_all_msg(),

            // History operations
            Message::Undo => self.handle_undo_msg(),
            Message::Redo => self.handle_redo_msg(),

            // Search and replace operations
            Message::OpenSearch => self.handle_open_search_msg(),
            Message::OpenSearchReplace => self.handle_open_search_replace_msg(),
            Message::CloseSearch => self.handle_close_search_msg(),
            Message::SearchQueryChanged(query) => {
                self.handle_search_query_changed_msg(query)
            }
            Message::ReplaceQueryChanged(text) => {
                self.handle_replace_query_changed_msg(text)
            }
            Message::ToggleCaseSensitive => {
                self.handle_toggle_case_sensitive_msg()
            }
            Message::FindNext => self.handle_find_next_msg(),
            Message::FindPrevious => self.handle_find_previous_msg(),
            Message::ReplaceNext => self.handle_replace_next_msg(),
            Message::ReplaceAll => self.handle_replace_all_msg(),
            Message::SearchDialogTab => self.handle_search_dialog_tab_msg(),
            Message::SearchDialogShiftTab => {
                self.handle_search_dialog_shift_tab_msg()
            }
            Message::FocusNavigationTab => self.handle_focus_navigation_tab(),
            Message::FocusNavigationShiftTab => {
                self.handle_focus_navigation_shift_tab()
            }

            // Focus and IME operations
            Message::CanvasFocusGained => self.handle_canvas_focus_gained_msg(),
            Message::CanvasFocusLost => self.handle_canvas_focus_lost_msg(),
            Message::ImeOpened if self.vim_accepts_insert_input() => {
                self.handle_ime_opened_msg()
            }
            Message::ImeOpened => Task::none(),
            Message::ImePreedit(content, selection) => {
                if self.vim_accepts_insert_input() {
                    self.handle_ime_preedit_msg(content, selection)
                } else {
                    Task::none()
                }
            }
            Message::ImeCommit(text) => self.handle_ime_commit_msg(text),
            Message::ImeClosed => self.handle_ime_closed_msg(),

            // UI update operations
            Message::Tick => self.handle_tick_msg(),
            Message::Scrolled(viewport) => self.handle_scrolled_msg(*viewport),
            Message::HorizontalScrolled(viewport) => {
                self.handle_horizontal_scrolled_msg(*viewport)
            }

            // Handle the "Jump to Definition" action triggered by Ctrl+Click.
            // Currently, this returns `Task::none()` as the actual navigation logic
            // is delegated to the `LspClient` implementation or handled elsewhere.
            Message::JumpClick(_point) => Task::none(),

            // Multi-cursor operations
            Message::AltClick(point) => self.handle_alt_click_msg(*point),
            Message::AddCursorAbove => self.handle_add_cursor_above_msg(),
            Message::AddCursorBelow => self.handle_add_cursor_below_msg(),
            Message::SelectNextOccurrence => {
                self.handle_select_next_occurrence_msg()
            }
            Message::ToggleFold(header_line) => {
                self.toggle_fold(*header_line);
                Task::none()
            }
            Message::ToggleFoldAtCursor => {
                self.toggle_fold_at(self.cursors.primary_position().0);
                Task::none()
            }
            Message::FoldAll => {
                self.fold_all();
                Task::none()
            }
            Message::UnfoldAll => {
                self.unfold_all();
                Task::none()
            }

            // Line manipulation operations
            Message::MoveLineUp => self.move_lines(false),
            Message::MoveLineDown => self.move_lines(true),
            Message::DuplicateLineUp => self.duplicate_lines(false),
            Message::DuplicateLineDown => self.duplicate_lines(true),
            Message::ToggleComment => self.toggle_comment(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::canvas_editor::{ArrowDirection, VimMode};
    use std::cell::RefCell;
    use std::rc::Rc;

    fn vim_keys(editor: &mut CodeEditor, keys: &str) {
        for key in keys.chars() {
            let _ = editor.update(&Message::VimKey(key));
        }
    }

    fn focus_editor(editor: &mut CodeEditor) {
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
    }

    fn assert_vim_delete(
        content: &str,
        cursor: (usize, usize),
        keys: &str,
        expected: &str,
        register: &str,
    ) {
        let mut editor = CodeEditor::new(content, "txt").with_vim_enabled(true);
        editor.cursors.set_single(cursor);
        vim_keys(&mut editor, keys);
        assert_eq!(editor.content(), expected, "keys: {keys}");
        assert_eq!(editor.vim_state.register.text, register, "keys: {keys}");
    }

    #[derive(Default)]
    struct VimTestLspClient {
        changes: Rc<RefCell<Vec<Vec<lsp::LspTextChange>>>>,
    }

    impl lsp::LspClient for VimTestLspClient {
        fn did_change(
            &mut self,
            _document: &lsp::LspDocument,
            changes: &[lsp::LspTextChange],
        ) {
            self.changes.borrow_mut().push(changes.to_vec());
        }
    }

    #[test]
    fn test_vim_navigation_normal_key_does_not_insert() {
        let mut editor = CodeEditor::new("abc", "txt").with_vim_enabled(true);

        vim_keys(&mut editor, "l");

        assert_eq!(editor.content(), "abc");
        assert_eq!(editor.cursors.primary_position(), (0, 1));

        let mut standard = CodeEditor::new("abc", "txt");
        focus_editor(&mut standard);
        let _ = standard.update(&Message::CharacterInput('l'));
        assert_eq!(standard.content(), "labc");
    }

    #[test]
    fn test_vim_navigation_insert_and_escape_round_trip() {
        let mut editor = CodeEditor::new("abc", "txt").with_vim_enabled(true);
        focus_editor(&mut editor);

        vim_keys(&mut editor, "i");
        assert_eq!(editor.vim_mode(), Some(VimMode::Insert));
        let _ = editor.update(&Message::CharacterInput('X'));
        assert_eq!(editor.content(), "Xabc");

        let _ = editor.update(&Message::VimKey('\u{1b}'));
        assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
        vim_keys(&mut editor, "l");
        assert_eq!(editor.content(), "Xabc");
        assert_eq!(editor.cursors.primary_position(), (0, 1));
    }

    #[test]
    fn test_vim_navigation_counted_word_and_line_motions() {
        let mut editor =
            CodeEditor::new("one two\nthree four\nfive six", "txt")
                .with_vim_enabled(true);

        vim_keys(&mut editor, "2w");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
        vim_keys(&mut editor, "e");
        assert_eq!(editor.cursors.primary_position(), (1, 4));
        vim_keys(&mut editor, "b");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
        vim_keys(&mut editor, "G");
        assert_eq!(editor.cursors.primary_position(), (2, 0));
        vim_keys(&mut editor, "gg");
        assert_eq!(editor.cursors.primary_position(), (0, 0));
        vim_keys(&mut editor, "2j");
        assert_eq!(editor.cursors.primary_position(), (2, 0));
        vim_keys(&mut editor, "k$");
        assert_eq!(editor.cursors.primary_position(), (1, 9));
        vim_keys(&mut editor, "0");
        assert_eq!(editor.cursors.primary_position(), (1, 0));

        let mut folded = CodeEditor::new(
            "fn main() {\n    let x = 1;\n    if x > 0 {\n        print();\n    }\n}",
            "rs",
        )
        .with_vim_enabled(true);
        folded.toggle_fold(0);
        vim_keys(&mut folded, "j");
        assert_eq!(folded.cursors.primary_position(), (5, 0));
    }

    #[test]
    fn test_vim_navigation_visual_and_visual_line_ranges() {
        let mut editor = CodeEditor::new("abcd\nefgh\nijkl\nmnop", "txt")
            .with_vim_enabled(true);
        editor.cursors.set_single((0, 1));

        vim_keys(&mut editor, "vl");
        assert_eq!(editor.vim_mode(), Some(VimMode::Visual));
        assert_eq!(
            editor.cursors.primary().selection_range(),
            Some(((0, 1), (0, 3)))
        );

        let _ = editor.update(&Message::VimKey('\u{1b}'));
        assert_eq!(editor.vim_mode(), Some(VimMode::Normal));
        assert!(editor.cursors.primary().anchor.is_none());

        editor.cursors.set_single((1, 2));
        vim_keys(&mut editor, "Vj");
        assert_eq!(editor.vim_mode(), Some(VimMode::VisualLine));
        assert_eq!(
            editor.cursors.primary().selection_range(),
            Some(((1, 0), (3, 0)))
        );
    }

    #[test]
    fn test_vim_navigation_unicode_and_empty_line_bounds() {
        let mut editor =
            CodeEditor::new("你🙂好\n\nz", "txt").with_vim_enabled(true);

        vim_keys(&mut editor, "lll");
        assert_eq!(editor.cursors.primary_position(), (0, 2));
        vim_keys(&mut editor, "j");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
        vim_keys(&mut editor, "j");
        assert_eq!(editor.cursors.primary_position(), (2, 0));
        vim_keys(&mut editor, "k");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
        vim_keys(&mut editor, "k$");
        assert_eq!(editor.cursors.primary_position(), (0, 2));
        vim_keys(&mut editor, "0");
        assert_eq!(editor.cursors.primary_position(), (0, 0));
    }

    #[test]
    fn test_vim_navigation_ime_only_commits_in_insert() {
        let mut editor = CodeEditor::new("abc", "txt").with_vim_enabled(true);

        let _ = editor.update(&Message::ImeCommit("".to_owned()));
        assert_eq!(editor.content(), "abc");

        vim_keys(&mut editor, "i");
        let _ = editor.update(&Message::ImeCommit("".to_owned()));
        assert_eq!(editor.content(), "中abc");

        let mut standard = CodeEditor::new("abc", "txt");
        let _ = standard.update(&Message::ImeCommit("".to_owned()));
        assert_eq!(standard.content(), "中abc");
    }

    #[test]
    fn test_vim_navigation_collapses_and_blocks_extra_cursors() {
        let mut editor = CodeEditor::new("same\nsame\nsame", "txt");
        editor.cursors.add_cursor((1, 0));
        assert_eq!(editor.cursors.len(), 2);

        editor.set_vim_enabled(true);
        assert_eq!(editor.cursors.len(), 1);

        let _ = editor.update(&Message::AddCursorBelow);
        let _ = editor.update(&Message::SelectNextOccurrence);
        let _ = editor.update(&Message::SelectNextOccurrence);
        let _ = editor.update(&Message::AltClick(iced::Point::new(
            editor.gutter_width() + 5.0,
            editor.line_height,
        )));
        assert_eq!(editor.cursors.len(), 1);
    }

    #[test]
    fn test_vim_editing_x_and_count() {
        let mut editor =
            CodeEditor::new("abcdef", "txt").with_vim_enabled(true);

        vim_keys(&mut editor, "x");
        assert_eq!(editor.content(), "bcdef");
        assert_eq!(editor.vim_state.register.text, "a");
        assert_eq!(
            editor.vim_state.register.kind,
            super::super::vim::VimRegisterKind::Characterwise
        );

        vim_keys(&mut editor, "2x");
        assert_eq!(editor.content(), "def");
        assert_eq!(editor.vim_state.register.text, "bc");
        assert_eq!(editor.cursors.primary_position(), (0, 0));
    }

    #[test]
    fn test_vim_editing_delete_change_yank_motions() {
        let mut deleted =
            CodeEditor::new("one two three", "txt").with_vim_enabled(true);
        vim_keys(&mut deleted, "dw");
        assert_eq!(deleted.content(), "two three");
        assert_eq!(deleted.vim_state.register.text, "one ");

        let mut yanked =
            CodeEditor::new("one two", "txt").with_vim_enabled(true);
        vim_keys(&mut yanked, "yw");
        assert_eq!(yanked.content(), "one two");
        assert_eq!(yanked.vim_state.register.text, "one ");

        let mut changed =
            CodeEditor::new("one two", "txt").with_vim_enabled(true);
        focus_editor(&mut changed);
        vim_keys(&mut changed, "ce");
        assert_eq!(changed.content(), " two");
        assert_eq!(changed.vim_state.register.text, "one");
        assert_eq!(changed.vim_mode(), Some(VimMode::Insert));
        let _ = changed.update(&Message::CharacterInput('X'));
        vim_keys(&mut changed, "\u{1b}");
        assert_eq!(changed.content(), "X two");

        assert_vim_delete("abc", (0, 2), "dh", "ac", "b");
        assert_vim_delete("abc", (0, 1), "dl", "ac", "b");
        assert_vim_delete("abc def", (0, 6), "db", "abc f", "de");
        assert_vim_delete("abcdef", (0, 3), "d0", "def", "abc");
        assert_vim_delete("  abc", (0, 4), "d^", "  c", "ab");
        assert_vim_delete("abcde", (0, 2), "d$", "ab", "cde");
        assert_vim_delete(
            "one\ntwo\nthree\nfour",
            (2, 0),
            "dgg",
            "four",
            "one\ntwo\nthree\n",
        );
        assert_vim_delete(
            "one\ntwo\nthree\nfour",
            (1, 0),
            "dG",
            "one",
            "two\nthree\nfour\n",
        );
    }

    #[test]
    fn test_vim_editing_doubled_line_operators() {
        let mut deleted =
            CodeEditor::new("one\ntwo\nthree", "txt").with_vim_enabled(true);
        vim_keys(&mut deleted, "dd");
        assert_eq!(deleted.content(), "two\nthree");
        assert_eq!(deleted.vim_state.register.text, "one\n");
        assert_eq!(
            deleted.vim_state.register.kind,
            super::super::vim::VimRegisterKind::Linewise
        );

        let mut yanked =
            CodeEditor::new("one\ntwo\nthree", "txt").with_vim_enabled(true);
        yanked.cursors.set_single((1, 1));
        vim_keys(&mut yanked, "yy");
        assert_eq!(yanked.content(), "one\ntwo\nthree");
        assert_eq!(yanked.vim_state.register.text, "two\n");
        assert_eq!(
            yanked.vim_state.register.kind,
            super::super::vim::VimRegisterKind::Linewise
        );

        let mut changed =
            CodeEditor::new("one\ntwo\nthree", "txt").with_vim_enabled(true);
        changed.cursors.set_single((1, 1));
        vim_keys(&mut changed, "cc");
        assert_eq!(changed.content(), "one\nthree");
        assert_eq!(changed.vim_state.register.text, "two\n");
        assert_eq!(changed.vim_mode(), Some(VimMode::Insert));

        assert_vim_delete(
            "one\ntwo\nthree\nfour",
            (1, 0),
            "dk",
            "three\nfour",
            "one\ntwo\n",
        );
        assert_vim_delete(
            "one\ntwo\nthree\nfour",
            (1, 0),
            "dj",
            "one\nfour",
            "two\nthree\n",
        );
        assert_vim_delete(
            "one\ntwo\nthree",
            (0, 0),
            "2dd",
            "three",
            "one\ntwo\n",
        );
    }

    #[test]
    fn test_vim_editing_visual_operators() {
        let mut deleted =
            CodeEditor::new("abcd\nefgh", "txt").with_vim_enabled(true);
        deleted.cursors.set_single((0, 1));
        vim_keys(&mut deleted, "vld");
        assert_eq!(deleted.content(), "ad\nefgh");
        assert_eq!(deleted.vim_state.register.text, "bc");
        assert_eq!(
            deleted.vim_state.register.kind,
            super::super::vim::VimRegisterKind::Characterwise
        );
        assert_eq!(deleted.vim_mode(), Some(VimMode::Normal));

        let mut yanked =
            CodeEditor::new("one\ntwo\nthree", "txt").with_vim_enabled(true);
        yanked.cursors.set_single((1, 1));
        vim_keys(&mut yanked, "Vjy");
        assert_eq!(yanked.content(), "one\ntwo\nthree");
        assert_eq!(yanked.vim_state.register.text, "two\nthree\n");
        assert_eq!(
            yanked.vim_state.register.kind,
            super::super::vim::VimRegisterKind::Linewise
        );
        assert_eq!(yanked.vim_mode(), Some(VimMode::Normal));

        let mut changed = CodeEditor::new("abcd", "txt").with_vim_enabled(true);
        focus_editor(&mut changed);
        changed.cursors.set_single((0, 1));
        vim_keys(&mut changed, "vlc");
        let _ = changed.update(&Message::CharacterInput('X'));
        vim_keys(&mut changed, "\u{1b}");
        assert_eq!(changed.content(), "aXd");
        assert_eq!(changed.vim_state.register.text, "bc");
        assert_eq!(changed.history.undo_count(), 1);
    }

    #[test]
    fn test_vim_editing_characterwise_and_linewise_paste() {
        let mut characterwise =
            CodeEditor::new("abc", "txt").with_vim_enabled(true);
        vim_keys(&mut characterwise, "yl2lp");
        assert_eq!(characterwise.content(), "abca");
        assert_eq!(characterwise.cursors.primary_position(), (0, 3));

        let mut characterwise_before =
            CodeEditor::new("abc", "txt").with_vim_enabled(true);
        vim_keys(&mut characterwise_before, "yl2lP");
        assert_eq!(characterwise_before.content(), "abac");
        assert_eq!(characterwise_before.cursors.primary_position(), (0, 2));

        let mut linewise =
            CodeEditor::new("one\ntwo", "txt").with_vim_enabled(true);
        vim_keys(&mut linewise, "yyp");
        assert_eq!(linewise.content(), "one\none\ntwo");
        assert_eq!(linewise.cursors.primary_position(), (1, 0));

        let mut linewise_before =
            CodeEditor::new("one\ntwo", "txt").with_vim_enabled(true);
        linewise_before.cursors.set_single((1, 0));
        vim_keys(&mut linewise_before, "yyP");
        assert_eq!(linewise_before.content(), "one\ntwo\ntwo");
        assert_eq!(linewise_before.cursors.primary_position(), (1, 0));
    }

    #[test]
    fn test_vim_editing_operator_counts_multiply() {
        let mut editor =
            CodeEditor::new("one two three four five six seven", "txt")
                .with_vim_enabled(true);

        vim_keys(&mut editor, "2d3w");

        assert_eq!(editor.content(), "seven");
        assert_eq!(
            editor.vim_state.register.text,
            "one two three four five six "
        );
    }

    #[test]
    fn test_vim_editing_undo_redo_is_one_command() {
        let original = "one two three";
        let mut editor =
            CodeEditor::new(original, "txt").with_vim_enabled(true);
        focus_editor(&mut editor);

        vim_keys(&mut editor, "cw");
        let _ = editor.update(&Message::CharacterInput('X'));
        let _ = editor.update(&Message::CharacterInput('Y'));
        vim_keys(&mut editor, "\u{1b}");
        assert_eq!(editor.content(), "XYtwo three");
        assert_eq!(editor.history.undo_count(), 1);

        vim_keys(&mut editor, "u");
        assert_eq!(editor.content(), original);
        assert_eq!(editor.history.redo_count(), 1);

        vim_keys(&mut editor, "\u{12}");
        assert_eq!(editor.content(), "XYtwo three");
        assert_eq!(editor.history.undo_count(), 1);

        let mut opened = CodeEditor::new("one", "txt").with_vim_enabled(true);
        focus_editor(&mut opened);
        vim_keys(&mut opened, "o");
        let _ = opened.update(&Message::CharacterInput('X'));
        vim_keys(&mut opened, "\u{1b}");
        assert_eq!(opened.content(), "one\nX");
        assert_eq!(opened.history.undo_count(), 1);
        vim_keys(&mut opened, "u");
        assert_eq!(opened.content(), "one");
    }

    #[test]
    fn test_vim_editing_emits_incremental_lsp_change() {
        let changes = Rc::new(RefCell::new(Vec::new()));
        let client = VimTestLspClient { changes: Rc::clone(&changes) };
        let content = (0..10)
            .map(|line| format!("line{line}"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut editor = CodeEditor::new(&content, "rs").with_vim_enabled(true);
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///vim.rs", "rust"),
        );
        editor.cursors.set_single((5, 2));

        vim_keys(&mut editor, "x");

        let changes = changes.borrow();
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].len(), 1);
        let change = &changes[0][0];
        assert_eq!(change.range.start.line, 4);
        assert_eq!(change.range.start.character, 0);
        assert_eq!(change.range.end.line, 7);
        assert_eq!(change.range.end.character, 0);
        assert_eq!(change.text, "line4\nlie5\nline6\n");
    }

    #[test]
    fn test_horizontal_scroll_initial_state() {
        let editor = CodeEditor::new("short line", "rs");
        assert!(
            (editor.horizontal_scroll_offset - 0.0).abs() < f32::EPSILON,
            "Initial horizontal scroll offset should be 0"
        );
    }

    #[test]
    fn test_set_wrap_enabled_resets_horizontal_offset() {
        let mut editor = CodeEditor::new("long line", "rs");
        editor.wrap_enabled = false;
        // Simulate a non-zero horizontal scroll
        editor.horizontal_scroll_offset = 100.0;

        // Re-enabling wrap should reset horizontal offset
        editor.set_wrap_enabled(true);
        assert!(
            (editor.horizontal_scroll_offset - 0.0).abs() < f32::EPSILON,
            "Horizontal scroll offset should be reset when wrap is re-enabled"
        );
    }

    #[test]
    fn test_canvas_focus_lost() {
        let mut editor = CodeEditor::new("test", "rs");
        editor.has_canvas_focus = true;

        let _ = editor.update(&Message::CanvasFocusLost);

        assert!(!editor.has_canvas_focus);
        assert!(!editor.show_cursor);
        assert!(editor.focus_locked, "Focus should be locked when lost");
    }

    #[test]
    fn test_canvas_focus_gained_resets_lock() {
        let mut editor = CodeEditor::new("test", "rs");
        editor.has_canvas_focus = false;
        editor.focus_locked = true;

        let _ = editor.update(&Message::CanvasFocusGained);

        assert!(editor.has_canvas_focus);
        assert!(
            !editor.focus_locked,
            "Focus lock should be reset when focus is gained"
        );
    }

    #[test]
    fn test_focus_lock_state() {
        let mut editor = CodeEditor::new("test", "rs");

        // Initially, focus should not be locked
        assert!(!editor.focus_locked);

        // When focus is lost, it should be locked
        let _ = editor.update(&Message::CanvasFocusLost);
        assert!(editor.focus_locked, "Focus should be locked when lost");

        // When focus is regained, it should be unlocked
        editor.request_focus();
        let _ = editor.update(&Message::CanvasFocusGained);
        assert!(!editor.focus_locked, "Focus should be unlocked when regained");

        // Can manually reset focus lock
        editor.focus_locked = true;
        editor.reset_focus_lock();
        assert!(!editor.focus_locked, "Focus lock should be resetable");
    }

    #[test]
    fn test_reset_focus_lock() {
        let mut editor = CodeEditor::new("test", "rs");
        editor.focus_locked = true;

        editor.reset_focus_lock();

        assert!(!editor.focus_locked);
    }

    #[test]
    fn test_home_key() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().position = (0, 5); // Move to middle of line
        let _ = editor.update(&Message::Home(false));
        assert_eq!(editor.cursors.primary_position(), (0, 0));
    }

    #[test]
    fn test_end_key() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().position = (0, 0);
        let _ = editor.update(&Message::End(false));
        assert_eq!(editor.cursors.primary_position(), (0, 11)); // Length of "hello world"
    }

    #[test]
    fn test_arrow_key_with_shift_creates_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().position = (0, 0);

        // Shift+Right should start selection
        let _ = editor.update(&Message::ArrowKey(ArrowDirection::Right, true));
        assert!(editor.cursors.primary().anchor.is_some());
        assert!(editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_arrow_key_without_shift_clears_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        // Regular arrow key should clear selection
        let _ = editor.update(&Message::ArrowKey(ArrowDirection::Right, false));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_typing_with_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('X'));
        assert_eq!(editor.buffer.line(0), "X world");
        assert_eq!(editor.cursors.primary_position(), (0, 1));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_typing_digit_with_reversed_selection_replaces_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().anchor = Some((0, 5));
        editor.cursors.primary_mut().position = (0, 0);

        let _ = editor.update(&Message::CharacterInput('7'));
        assert_eq!(editor.buffer.line(0), "7 world");
        assert_eq!(editor.cursors.primary_position(), (0, 1));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_typing_with_selection_undoes_as_single_edit() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('X'));
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello world");
    }

    #[test]
    fn test_ctrl_home() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().position = (2, 5); // Start at line 3, column 5
        let _ = editor.update(&Message::CtrlHome);
        assert_eq!(editor.cursors.primary_position(), (0, 0)); // Should move to beginning of document
    }

    #[test]
    fn test_ctrl_end() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().position = (0, 0); // Start at beginning
        let _ = editor.update(&Message::CtrlEnd);
        assert_eq!(editor.cursors.primary_position(), (2, 5)); // Should move to end of last line (line3 has 5 chars)
    }

    #[test]
    fn test_ctrl_home_clears_selection() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().position = (2, 5);
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (2, 5);

        let _ = editor.update(&Message::CtrlHome);
        assert_eq!(editor.cursors.primary_position(), (0, 0));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_ctrl_end_clears_selection() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().position = (0, 0);
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (1, 3);

        let _ = editor.update(&Message::CtrlEnd);
        assert_eq!(editor.cursors.primary_position(), (2, 5));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_goto_position_sets_cursor_and_clears_selection() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (1, 2);

        let _ = editor.update(&Message::GotoPosition(1, 3));

        assert_eq!(editor.cursors.primary_position(), (1, 3));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_goto_position_clamps_out_of_range() {
        let mut editor = CodeEditor::new("a\nbb", "py");

        let _ = editor.update(&Message::GotoPosition(99, 99));

        // Clamped to last line (index 1) and end of that line (len = 2)
        assert_eq!(editor.cursors.primary_position(), (1, 2));
    }

    #[test]
    fn test_scroll_sets_initial_cache_window() {
        let content =
            (0..200).map(|i| format!("line{}\n", i)).collect::<String>();
        let mut editor = CodeEditor::new(&content, "py");

        // Simulate initial viewport
        let height = 400.0;
        let width = 800.0;
        let scroll = 0.0;

        // Expected derived ranges
        let visible_lines_count =
            (height / editor.line_height).ceil() as usize + 2;
        let first_visible_line = (scroll / editor.line_height).floor() as usize;
        let last_visible_line = first_visible_line + visible_lines_count;
        let margin = visible_lines_count * 2;
        let window_start = first_visible_line.saturating_sub(margin);
        let window_end = last_visible_line + margin;

        // Apply logic similar to Message::Scrolled branch
        editor.viewport_height = height;
        editor.viewport_width = width;
        editor.viewport_scroll = -1.0;
        let scroll_changed = (editor.viewport_scroll - scroll).abs() > 0.1;
        let need_rewindow = true;
        if (editor.viewport_height - height).abs() > 1.0
            || (editor.viewport_width - width).abs() > 1.0
            || (scroll_changed && need_rewindow)
        {
            editor.cache_window_start_line = window_start;
            editor.cache_window_end_line = window_end;
            editor.last_first_visible_line = first_visible_line;
        }
        editor.viewport_scroll = scroll;

        assert_eq!(editor.last_first_visible_line, first_visible_line);
        assert!(editor.cache_window_end_line > editor.cache_window_start_line);
        assert_eq!(editor.cache_window_start_line, window_start);
        assert_eq!(editor.cache_window_end_line, window_end);
    }

    #[test]
    fn test_small_scroll_keeps_window() {
        let content =
            (0..200).map(|i| format!("line{}\n", i)).collect::<String>();
        let mut editor = CodeEditor::new(&content, "py");
        let height = 400.0;
        let width = 800.0;
        let initial_scroll = 0.0;
        let visible_lines_count =
            (height / editor.line_height).ceil() as usize + 2;
        let first_visible_line =
            (initial_scroll / editor.line_height).floor() as usize;
        let last_visible_line = first_visible_line + visible_lines_count;
        let margin = visible_lines_count * 2;
        let window_start = first_visible_line.saturating_sub(margin);
        let window_end = last_visible_line + margin;
        editor.cache_window_start_line = window_start;
        editor.cache_window_end_line = window_end;
        editor.viewport_height = height;
        editor.viewport_width = width;
        editor.viewport_scroll = initial_scroll;

        // Small scroll inside window
        let small_scroll =
            editor.line_height * (visible_lines_count as f32 / 4.0);
        let first_visible_line2 =
            (small_scroll / editor.line_height).floor() as usize;
        let last_visible_line2 = first_visible_line2 + visible_lines_count;
        let lower_boundary_trigger = editor.cache_window_start_line > 0
            && first_visible_line2
                < editor
                    .cache_window_start_line
                    .saturating_add(visible_lines_count / 2);
        let upper_boundary_trigger = last_visible_line2
            > editor
                .cache_window_end_line
                .saturating_sub(visible_lines_count / 2);
        let need_rewindow = lower_boundary_trigger || upper_boundary_trigger;

        assert!(!need_rewindow, "Small scroll should be inside the window");
        // Window remains unchanged
        assert_eq!(editor.cache_window_start_line, window_start);
        assert_eq!(editor.cache_window_end_line, window_end);
    }

    #[test]
    fn test_large_scroll_rewindows() {
        let content =
            (0..1000).map(|i| format!("line{}\n", i)).collect::<String>();
        let mut editor = CodeEditor::new(&content, "py");
        let height = 400.0;
        let width = 800.0;
        let initial_scroll = 0.0;
        let visible_lines_count =
            (height / editor.line_height).ceil() as usize + 2;
        let first_visible_line =
            (initial_scroll / editor.line_height).floor() as usize;
        let last_visible_line = first_visible_line + visible_lines_count;
        let margin = visible_lines_count * 2;
        editor.cache_window_start_line =
            first_visible_line.saturating_sub(margin);
        editor.cache_window_end_line = last_visible_line + margin;
        editor.viewport_height = height;
        editor.viewport_width = width;
        editor.viewport_scroll = initial_scroll;

        // Large scroll beyond window boundary
        let large_scroll =
            editor.line_height * ((visible_lines_count * 4) as f32);
        let first_visible_line2 =
            (large_scroll / editor.line_height).floor() as usize;
        let last_visible_line2 = first_visible_line2 + visible_lines_count;
        let window_start2 = first_visible_line2.saturating_sub(margin);
        let window_end2 = last_visible_line2 + margin;
        let need_rewindow = first_visible_line2
            < editor
                .cache_window_start_line
                .saturating_add(visible_lines_count / 2)
            || last_visible_line2
                > editor
                    .cache_window_end_line
                    .saturating_sub(visible_lines_count / 2);
        assert!(need_rewindow, "Large scroll should trigger window update");

        // Apply rewindow
        editor.cache_window_start_line = window_start2;
        editor.cache_window_end_line = window_end2;
        editor.last_first_visible_line = first_visible_line2;

        assert_eq!(editor.cache_window_start_line, window_start2);
        assert_eq!(editor.cache_window_end_line, window_end2);
        assert_eq!(editor.last_first_visible_line, first_visible_line2);
    }

    #[test]
    fn test_delete_selection_message() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().position = (0, 0);
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::DeleteSelection);
        assert_eq!(editor.buffer.line(0), " world");
        assert_eq!(editor.cursors.primary_position(), (0, 0));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_delete_selection_multiline() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().position = (0, 2);
        editor.cursors.primary_mut().anchor = Some((0, 2));
        editor.cursors.primary_mut().position = (2, 2);

        let _ = editor.update(&Message::DeleteSelection);
        assert_eq!(editor.buffer.line(0), "line3");
        assert_eq!(editor.cursors.primary_position(), (0, 2));
        assert!(editor.cursors.primary().anchor.is_none());
    }

    #[test]
    fn test_delete_selection_no_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::DeleteSelection);
        // Should do nothing if there's no selection
        assert_eq!(editor.buffer.line(0), "hello world");
        assert_eq!(editor.cursors.primary_position(), (0, 5));
    }

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_ime_preedit_and_commit_chinese() {
        let mut editor = CodeEditor::new("", "py");
        // Simulate IME opened
        let _ = editor.update(&Message::ImeOpened);
        assert!(editor.ime_preedit.is_none());

        // Preedit with Chinese content and a selection range
        let content = "安全与合规".to_string();
        let selection = Some(0..3); // range aligned to UTF-8 character boundary
        let _ = editor
            .update(&Message::ImePreedit(content.clone(), selection.clone()));

        assert!(editor.ime_preedit.is_some());
        assert_eq!(
            editor.ime_preedit.as_ref().unwrap().content.clone(),
            content
        );
        assert_eq!(
            editor.ime_preedit.as_ref().unwrap().selection.clone(),
            selection
        );

        // Commit should insert the text and clear preedit
        let _ = editor.update(&Message::ImeCommit("安全与合规".to_string()));
        assert!(editor.ime_preedit.is_none());
        assert_eq!(editor.buffer.line(0), "安全与合规");
        assert_eq!(
            editor.cursors.primary_position(),
            (0, "安全与合规".chars().count())
        );
    }

    #[test]
    fn test_undo_char_insert() {
        let mut editor = CodeEditor::new("hello", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().position = (0, 5);

        // Type a character
        let _ = editor.update(&Message::CharacterInput('!'));
        assert_eq!(editor.buffer.line(0), "hello!");
        assert_eq!(editor.cursors.primary_position(), (0, 6));

        // Undo should remove it (but first end the grouping)
        editor.history.end_group();
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.cursors.primary_position(), (0, 5));
    }

    #[test]
    fn test_undo_redo_char_insert() {
        let mut editor = CodeEditor::new("hello", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().position = (0, 5);

        // Type a character
        let _ = editor.update(&Message::CharacterInput('!'));
        editor.history.end_group();

        // Undo
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello");

        // Redo
        let _ = editor.update(&Message::Redo);
        assert_eq!(editor.buffer.line(0), "hello!");
        assert_eq!(editor.cursors.primary_position(), (0, 6));
    }

    #[test]
    fn test_undo_backspace() {
        let mut editor = CodeEditor::new("hello", "py");
        editor.cursors.primary_mut().position = (0, 5);

        // Backspace
        let _ = editor.update(&Message::Backspace);
        assert_eq!(editor.buffer.line(0), "hell");
        assert_eq!(editor.cursors.primary_position(), (0, 4));

        // Undo
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.cursors.primary_position(), (0, 5));
    }

    #[test]
    fn test_undo_newline() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().position = (0, 5);

        // Insert newline
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.buffer.line(1), " world");
        assert_eq!(editor.cursors.primary_position(), (1, 0));

        // Undo
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello world");
        assert_eq!(editor.cursors.primary_position(), (0, 5));
    }

    #[test]
    fn test_undo_grouped_typing() {
        let mut editor = CodeEditor::new("hello", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().position = (0, 5);

        // Type multiple characters (they should be grouped)
        let _ = editor.update(&Message::CharacterInput(' '));
        let _ = editor.update(&Message::CharacterInput('w'));
        let _ = editor.update(&Message::CharacterInput('o'));
        let _ = editor.update(&Message::CharacterInput('r'));
        let _ = editor.update(&Message::CharacterInput('l'));
        let _ = editor.update(&Message::CharacterInput('d'));

        assert_eq!(editor.buffer.line(0), "hello world");

        // End the group
        editor.history.end_group();

        // Single undo should remove all grouped characters
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.cursors.primary_position(), (0, 5));
    }

    #[test]
    fn test_navigation_ends_grouping() {
        let mut editor = CodeEditor::new("hello", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().position = (0, 5);

        // Type a character (starts grouping)
        let _ = editor.update(&Message::CharacterInput('!'));
        assert!(editor.is_grouping);

        // Move cursor (ends grouping)
        let _ = editor.update(&Message::ArrowKey(ArrowDirection::Left, false));
        assert!(!editor.is_grouping);

        // Type another character (starts new group)
        let _ = editor.update(&Message::CharacterInput('?'));
        assert!(editor.is_grouping);

        editor.history.end_group();

        // Two separate undo operations
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello!");

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello");
    }

    #[test]
    fn test_edit_increments_revision_and_clears_visual_lines_cache() {
        let mut editor = CodeEditor::new("hello", "rs");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.visual_lines_cached(800.0);
        assert!(
            editor.visual_lines_cache.borrow().is_some(),
            "visual_lines_cached should populate the cache"
        );

        let previous_revision = editor.buffer_revision;

        let _ = editor.update(&Message::CharacterInput('!'));
        assert_eq!(
            editor.buffer_revision,
            previous_revision.wrapping_add(1),
            "buffer_revision should change on buffer edits"
        );
        // `scroll_to_cursor` repopulates the cache after the edit with the new
        // revision, so the cache may be `Some`.  What must never happen is that
        // stale data (an old revision) survives an edit.
        assert!(
            editor
                .visual_lines_cache
                .borrow()
                .as_ref()
                .is_none_or(|c| c.key.buffer_revision == editor.buffer_revision),
            "buffer edits should not leave stale data in the visual lines cache"
        );
    }

    #[test]
    fn test_edit_refreshes_only_affected_search_matches() {
        let mut editor = CodeEditor::new("foo\nfoo\nfoo", "rs");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        editor.search_state.open_search();
        editor.search_state.set_query("foo".to_owned(), &editor.buffer);
        editor.cursors.primary_mut().position = (1, 1);

        let _ = editor.update(&Message::CharacterInput('x'));

        let match_lines: Vec<usize> =
            editor.search_state.matches.iter().map(|item| item.line).collect();
        assert_eq!(match_lines, vec![0, 2]);
    }

    #[test]
    fn test_manual_search_match_selection_updates_current_index() {
        let mut editor =
            CodeEditor::new("foo bar foo baz foo\nno result", "txt");
        editor.search_state.open_search();
        editor.search_state.set_query("foo".to_owned(), &editor.buffer);
        assert_eq!(editor.search_state.current_match_index, Some(0));

        let text_start = editor.gutter_width() + 5.0;
        let char_width = editor.char_width;
        let line_y = editor.line_height / 2.0;
        let point_at_col = |col: usize| {
            iced::Point::new(text_start + char_width * col as f32, line_y)
        };

        let _ = editor.update(&Message::MouseClick(point_at_col(8)));
        let _ = editor.update(&Message::MouseDrag(point_at_col(11)));
        let _ = editor.update(&Message::MouseRelease);

        assert_eq!(
            editor.cursors.primary().selection_range(),
            Some(((0, 8), (0, 11)))
        );
        assert_eq!(editor.search_state.current_match_index, Some(1));

        let no_match_line = iced::Point::new(
            text_start + char_width * 4.0,
            editor.line_height * 1.5,
        );
        let _ = editor.update(&Message::MouseClick(no_match_line));
        let _ = editor.update(&Message::MouseRelease);
        assert_eq!(editor.search_state.current_match_index, Some(1));

        let _ = editor.update(&Message::FindNext);
        assert_eq!(editor.search_state.current_match_index, Some(2));
        let _ = editor.update(&Message::FindPrevious);
        assert_eq!(editor.search_state.current_match_index, Some(1));
    }

    #[test]
    fn test_manual_line_selection_updates_current_search_index() {
        let mut editor =
            CodeEditor::new("foo\nprefix foo suffix\nlast foo", "txt");
        editor.search_state.open_search();
        editor.search_state.set_query("foo".to_owned(), &editor.buffer);
        assert_eq!(editor.search_state.current_match_index, Some(0));

        let line_start = iced::Point::new(
            editor.gutter_width() + 5.0,
            editor.line_height * 1.5,
        );
        let _ = editor.update(&Message::MouseClick(line_start));
        let _ = editor.update(&Message::MouseRelease);

        assert_eq!(editor.cursors.primary_position(), (1, 0));
        assert_eq!(editor.search_state.current_match_index, Some(1));

        let _ = editor.update(&Message::FindNext);
        assert_eq!(editor.search_state.current_match_index, Some(2));

        let mut keyboard_editor =
            CodeEditor::new("foo\nprefix foo suffix\nlast foo", "txt");
        keyboard_editor.search_state.open_search();
        keyboard_editor
            .search_state
            .set_query("foo".to_owned(), &keyboard_editor.buffer);

        let _ = keyboard_editor
            .update(&Message::ArrowKey(ArrowDirection::Down, false));

        assert_eq!(keyboard_editor.cursors.primary_position(), (1, 0));
        assert_eq!(keyboard_editor.search_state.current_match_index, Some(1));
    }

    #[test]
    fn test_incremental_visual_lines_match_full_recalculation_after_newline() {
        use std::collections::HashSet;

        let mut editor = CodeEditor::new("zero\nabcdefgh\nlast", "rs")
            .with_wrap_column(Some(4));
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        editor.cursors.primary_mut().position = (1, 4);

        let _ = editor.visual_lines_cached(800.0);
        let _ = editor.update(&Message::Enter);
        let incremental = editor.visual_lines_cached(800.0);

        let calculator = super::super::wrapping::WrappingCalculator::new(
            editor.wrap_enabled,
            editor.wrap_column,
            editor.full_char_width,
            editor.char_width,
        );
        let expected = calculator.calculate_visual_lines(
            &editor.buffer,
            800.0,
            editor.gutter_width(),
            &HashSet::new(),
        );

        assert_eq!(incremental.as_ref(), &expected);
    }

    #[test]
    fn test_multiple_undo_redo() {
        let mut editor = CodeEditor::new("a", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().position = (0, 1);

        // Make several changes
        let _ = editor.update(&Message::CharacterInput('b'));
        editor.history.end_group();

        let _ = editor.update(&Message::CharacterInput('c'));
        editor.history.end_group();

        let _ = editor.update(&Message::CharacterInput('d'));
        editor.history.end_group();

        assert_eq!(editor.buffer.line(0), "abcd");

        // Undo all
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "abc");

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "ab");

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "a");

        // Redo all
        let _ = editor.update(&Message::Redo);
        assert_eq!(editor.buffer.line(0), "ab");

        let _ = editor.update(&Message::Redo);
        assert_eq!(editor.buffer.line(0), "abc");

        let _ = editor.update(&Message::Redo);
        assert_eq!(editor.buffer.line(0), "abcd");
    }

    #[test]
    fn test_delete_key_with_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::Delete);

        assert_eq!(editor.buffer.line(0), " world");
        assert_eq!(editor.cursors.primary_position(), (0, 0));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_delete_key_without_selection() {
        let mut editor = CodeEditor::new("hello", "py");
        editor.cursors.primary_mut().position = (0, 0);

        let _ = editor.update(&Message::Delete);

        // Should delete the 'h'
        assert_eq!(editor.buffer.line(0), "ello");
        assert_eq!(editor.cursors.primary_position(), (0, 0));
    }

    #[test]
    fn test_backspace_with_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().anchor = Some((0, 6));
        editor.cursors.primary_mut().position = (0, 11);
        editor.cursors.primary_mut().position = (0, 11);

        let _ = editor.update(&Message::Backspace);

        assert_eq!(editor.buffer.line(0), "hello ");
        assert_eq!(editor.cursors.primary_position(), (0, 6));
        assert!(editor.cursors.primary().anchor.is_none());
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_backspace_without_selection() {
        let mut editor = CodeEditor::new("hello", "py");
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::Backspace);

        // Should delete the 'o'
        assert_eq!(editor.buffer.line(0), "hell");
        assert_eq!(editor.cursors.primary_position(), (0, 4));
    }

    #[test]
    fn test_delete_multiline_selection() {
        let mut editor = CodeEditor::new("line1\nline2\nline3", "py");
        editor.cursors.primary_mut().anchor = Some((0, 2));
        editor.cursors.primary_mut().position = (2, 2);
        editor.cursors.primary_mut().position = (2, 2);

        let _ = editor.update(&Message::Delete);

        assert_eq!(editor.buffer.line(0), "line3");
        assert_eq!(editor.cursors.primary_position(), (0, 2));
        assert!(editor.cursors.primary().anchor.is_none());
    }

    #[test]
    fn test_canvas_focus_gained() {
        let mut editor = CodeEditor::new("hello world", "py");
        assert!(!editor.has_canvas_focus);
        assert!(!editor.show_cursor);

        let _ = editor.update(&Message::CanvasFocusGained);

        assert!(editor.has_canvas_focus);
        assert!(editor.show_cursor);
    }

    #[test]
    fn test_mouse_click_gains_focus() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.has_canvas_focus = false;
        editor.show_cursor = false;

        let _ =
            editor.update(&Message::MouseClick(iced::Point::new(100.0, 10.0)));

        assert!(editor.has_canvas_focus);
        assert!(editor.show_cursor);
    }

    #[test]
    fn test_context_click_inside_selection_preserves_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);
        let point = iced::Point::new(
            editor.gutter_width() + 5.0 + editor.char_width * 2.0,
            editor.line_height / 2.0,
        );

        let _ = editor.update(&Message::ContextMenuRequested(point));

        assert_eq!(
            editor.cursors.primary().selection_range(),
            Some(((0, 0), (0, 5)))
        );
    }

    #[test]
    fn test_context_click_outside_selection_moves_caret() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);
        let point = iced::Point::new(
            editor.gutter_width() + 5.0 + editor.char_width * 8.0,
            editor.line_height / 2.0,
        );

        let _ = editor.update(&Message::ContextMenuRequested(point));

        assert_eq!(editor.cursors.primary_position(), (0, 8));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_context_menu_cut_and_select_all() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::Cut);
        assert_eq!(editor.content(), " world");

        let _ = editor.update(&Message::SelectAll);
        assert_eq!(editor.get_selected_text(), Some(" world".to_string()));

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.content(), "hello world");
    }

    #[test]
    fn test_enter_no_indent() {
        let mut editor = CodeEditor::new("hello", "rs");
        editor.cursors.primary_mut().position = (0, 5);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.buffer.line(1), "");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
    }

    #[test]
    fn test_typing_after_enter_does_not_delete_newline_from_click_anchor() {
        let mut editor = CodeEditor::new("hello", "rs");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        // A regular click starts drag tracking with a zero-length anchor.
        editor.cursors.primary_mut().position = (0, 5);
        editor.cursors.primary_mut().anchor = Some((0, 5));

        let _ = editor.update(&Message::Enter);
        let _ = editor.update(&Message::CharacterInput('X'));

        assert_eq!(editor.buffer.line_count(), 2);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.buffer.line(1), "X");
        assert_eq!(editor.cursors.primary_position(), (1, 1));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_enter_replaces_selection_and_undo_restores_text() {
        let mut editor = CodeEditor::new("hello world", "rs");
        editor.set_auto_indent_enabled(false);
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::Enter);

        assert_eq!(editor.buffer.line(0), "");
        assert_eq!(editor.buffer.line(1), " world");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
        assert!(!editor.cursors.primary().has_selection());

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.content(), "hello world");
    }

    #[test]
    fn test_enter_auto_indent_spaces() {
        let mut editor = CodeEditor::new("    hello", "rs");
        editor.cursors.primary_mut().position = (0, 9);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "    hello");
        assert_eq!(editor.buffer.line(1), "    ");
        assert_eq!(editor.cursors.primary_position(), (1, 4));
    }

    #[test]
    fn test_enter_auto_indent_tab() {
        let mut editor = CodeEditor::new("\thello", "rs");
        editor.cursors.primary_mut().position = (0, 6);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "\thello");
        assert_eq!(editor.buffer.line(1), "\t");
        assert_eq!(editor.cursors.primary_position(), (1, 1));
    }

    #[test]
    fn test_enter_auto_indent_undo() {
        let mut editor = CodeEditor::new("    hello", "rs");
        editor.cursors.primary_mut().position = (0, 9);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line_count(), 2);

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line_count(), 1);
        assert_eq!(editor.buffer.line(0), "    hello");
        assert_eq!(editor.cursors.primary_position(), (0, 9));
    }

    // =========================================================================
    // Multi-cursor tests
    // =========================================================================

    #[test]
    fn test_multi_cursor_char_input_different_lines() {
        let mut editor = CodeEditor::new("aaa\nbbb", "rs");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        // Place cursors at (0, 1) and (1, 1)
        editor.cursors.primary_mut().position = (0, 1);
        editor.cursors.add_cursor((1, 1));

        let _ = editor.update(&Message::CharacterInput('X'));

        // Both lines should have 'X' inserted at col 1
        assert_eq!(editor.buffer.line(0), "aXaa");
        assert_eq!(editor.buffer.line(1), "bXbb");
    }

    #[test]
    fn test_multi_cursor_char_input_same_line() {
        let mut editor = CodeEditor::new("abcd", "rs");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        // Place cursors at col 1 and col 3 (same line)
        editor.cursors.primary_mut().position = (0, 1);
        editor.cursors.add_cursor((0, 3));

        let _ = editor.update(&Message::CharacterInput('X'));

        // Process descending: col 3 first → "abcXd"; then col 1 → "aXbcXd"
        // Col 1 cursor adjustment: insert at col 3 does not affect col 1 (col 1 < 3)
        assert_eq!(editor.buffer.line(0), "aXbcXd");
    }

    #[test]
    fn test_add_cursor_above() {
        let mut editor = CodeEditor::new("line0\nline1\nline2", "rs");
        editor.cursors.primary_mut().position = (1, 3);

        let _ = editor.update(&Message::AddCursorAbove);

        assert!(editor.cursors.is_multi());
        // New cursor should be at line 0, col 3
        assert_eq!(editor.cursors.as_slice()[0].position, (0, 3));
    }

    #[test]
    fn test_add_cursor_below() {
        let mut editor = CodeEditor::new("line0\nline1\nline2", "rs");
        editor.cursors.primary_mut().position = (1, 3);

        let _ = editor.update(&Message::AddCursorBelow);

        assert!(editor.cursors.is_multi());
        // New cursor should be at line 2, col 3
        assert_eq!(
            editor
                .cursors
                .as_slice()
                .iter()
                .find(|c| c.position.0 == 2)
                .map(|c| c.position),
            Some((2, 3))
        );
    }

    #[test]
    fn test_escape_collapses_multi_cursor() {
        let mut editor = CodeEditor::new("line0\nline1", "rs");
        editor.cursors.primary_mut().position = (0, 0);
        editor.cursors.add_cursor((1, 0));
        assert!(editor.cursors.is_multi());

        let _ = editor.update(&Message::CloseSearch);

        assert!(!editor.cursors.is_multi());
    }

    #[test]
    fn test_select_next_occurrence_selects_word() {
        let mut editor = CodeEditor::new("foo bar foo", "rs");
        editor.cursors.primary_mut().position = (0, 1); // inside "foo"

        let _ = editor.update(&Message::SelectNextOccurrence);

        // Primary cursor should now have "foo" selected
        let range = editor.cursors.primary().selection_range();
        assert_eq!(range, Some(((0, 0), (0, 3))));
    }

    #[test]
    fn test_select_next_occurrence_adds_cursor_for_second_occurrence() {
        let mut editor = CodeEditor::new("foo bar foo", "rs");
        // Set up primary cursor with "foo" selected
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 3);

        let _ = editor.update(&Message::SelectNextOccurrence);

        // Should now have 2 cursors: primary at "foo" (0..3) and new at "foo" (8..11)
        assert_eq!(editor.cursors.len(), 2);
    }

    #[test]
    fn test_multi_cursor_backspace() {
        let mut editor = CodeEditor::new("abc\ndef", "rs");
        editor.cursors.primary_mut().position = (0, 2);
        editor.cursors.add_cursor((1, 2));

        let _ = editor.update(&Message::Backspace);

        assert_eq!(editor.buffer.line(0), "ac");
        assert_eq!(editor.buffer.line(1), "df");
    }

    #[test]
    fn test_toggle_comment_selection() {
        let mut editor = CodeEditor::new("a\nb\nc", "rs");
        // Select lines 0..=2.
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (2, 1);

        let _ = editor.update(&Message::ToggleComment);
        assert_eq!(editor.buffer.to_string(), "// a\n// b\n// c");
        assert_eq!(editor.cursors.primary_position(), (2, 4));

        // Toggling again uncomments the whole range.
        let _ = editor.update(&Message::ToggleComment);
        assert_eq!(editor.buffer.to_string(), "a\nb\nc");
    }

    #[test]
    fn test_toggle_comment_noop_without_token() {
        let mut editor = CodeEditor::new("<div>", "html");
        let _ = editor.update(&Message::ToggleComment);
        // HTML has no line-comment token, so the buffer is unchanged.
        assert_eq!(editor.buffer.line(0), "<div>");
    }

    #[test]
    fn test_toggle_comment_undo() {
        let mut editor = CodeEditor::new("    let x = 1;", "rs");
        editor.cursors.primary_mut().position = (0, 8);

        let _ = editor.update(&Message::ToggleComment);
        assert_eq!(editor.buffer.line(0), "    // let x = 1;");

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "    let x = 1;");
        assert_eq!(editor.cursors.primary_position(), (0, 8));
    }

    #[test]
    fn test_open_goto_line_prefills_current_one_based_line() {
        let mut editor = CodeEditor::new("one\ntwo\nthree", "rs");
        editor.cursors.primary_mut().position = (1, 2);
        editor.search_state.open_search();

        let _ = editor.update(&Message::OpenGotoLine);

        assert!(editor.goto_line_state.is_open);
        assert_eq!(editor.goto_line_state.query, "2");
        assert!(!editor.search_state.is_open);
    }

    #[test]
    fn test_submit_goto_line_moves_to_one_based_line_and_closes_dialog() {
        let mut editor = CodeEditor::new("one\ntwo\nthree", "rs");
        let _ = editor.update(&Message::OpenGotoLine);
        let _ = editor.update(&Message::GotoLineChanged("3".to_string()));

        let _ = editor.update(&Message::SubmitGotoLine);

        assert_eq!(editor.cursors.primary_position(), (2, 0));
        assert!(!editor.goto_line_state.is_open);
    }

    #[test]
    fn test_submit_goto_line_clamps_to_last_line() {
        let mut editor = CodeEditor::new("one\ntwo\nthree", "rs");
        let _ = editor.update(&Message::OpenGotoLine);
        let _ = editor.update(&Message::GotoLineChanged("99".to_string()));

        let _ = editor.update(&Message::SubmitGotoLine);

        assert_eq!(editor.cursors.primary_position(), (2, 0));
        assert!(!editor.goto_line_state.is_open);
    }

    #[test]
    fn test_submit_goto_line_reveals_folded_target() {
        let mut editor =
            CodeEditor::new("root\n    child\n        nested\ntail", "rs");
        editor.fold_all();
        assert!(editor.hidden_lines_set().contains(&1));
        let _ = editor.update(&Message::OpenGotoLine);
        let _ = editor.update(&Message::GotoLineChanged("2".to_string()));

        let _ = editor.update(&Message::SubmitGotoLine);

        assert_eq!(editor.cursors.primary_position(), (1, 0));
        assert!(!editor.hidden_lines_set().contains(&1));
    }

    #[test]
    fn test_submit_goto_line_keeps_dialog_open_for_invalid_input() {
        let mut editor = CodeEditor::new("one\ntwo\nthree", "rs");
        editor.cursors.primary_mut().position = (1, 1);
        let _ = editor.update(&Message::OpenGotoLine);
        let _ = editor.update(&Message::GotoLineChanged("invalid".to_string()));

        let _ = editor.update(&Message::SubmitGotoLine);

        assert_eq!(editor.cursors.primary_position(), (1, 1));
        assert!(editor.goto_line_state.is_open);
    }
}