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
//! Message handling and update logic.
use iced::Task;
use iced::widget::operation::{focus, select_all};
use super::command::{
Command, CompositeCommand, DeleteCharCommand, DeleteForwardCommand,
InsertCharCommand, InsertNewlineCommand, ReplaceTextCommand,
};
use super::{
ArrowDirection, CURSOR_BLINK_INTERVAL, CodeEditor, ImePreedit, IndentStyle,
Message, cursor_set,
};
// =========================================================================
// 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.
self.buffer_revision = self.buffer_revision.wrapping_add(1);
*self.visual_lines_cache.borrow_mut() = None;
self.content_cache.clear();
self.overlay_cache.clear();
self.enqueue_lsp_change();
}
/// 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.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;
}
}
/// 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");
// 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 {
let cursor = &self.cursors.as_slice()[idx];
// Insert at the selection start when the cursor has an active selection,
// otherwise insert at the cursor position.
let pos = match cursor.anchor {
Some(anchor) if anchor < cursor.position => anchor,
_ => cursor.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");
// 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> {
// End grouping on enter
self.end_grouping_if_active();
// 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));
}
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)
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();
}
// 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
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();
}
// 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
// =========================================================================
/// 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
// =========================================================================
/// 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();
// 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;
Task::none()
}
// =========================================================================
// Clipboard Handlers
// =========================================================================
/// 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();
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();
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.search_state.open_search();
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.search_state.open_replace();
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()
}
/// 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));
// Update matches after replacement
self.search_state.update_matches(&self.buffer);
// Move to next match if available
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);
}
self.clear_selection();
self.finish_edit_operation();
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));
// Update matches (should be empty now)
self.search_state.update_matches(&self.buffer);
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.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 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> {
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> {
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> {
// 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();
for line_offset in 0..=line_count {
let line_idx = (start_line + line_offset) % line_count;
let line_str = self.buffer.line(line_idx).to_string();
let chars: Vec<char> = line_str.chars().collect();
// 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: usize =
chars.iter().take(search_col).map(|c| c.len_utf8()).sum();
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_text.chars().count();
// 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> {
match message {
// Text input operations
Message::CharacterInput(ch) => self.handle_character_input_msg(*ch),
Message::Tab => self.handle_tab(),
Message::Enter => self.handle_enter(),
// Deletion operations
Message::Backspace => self.handle_backspace(),
Message::Delete => self.handle_delete(),
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::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(),
// Clipboard operations
Message::Copy => self.copy_selection(),
Message::Paste(text) => self.handle_paste_msg(text),
// 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 => self.handle_ime_opened_msg(),
Message::ImePreedit(content, selection) => {
self.handle_ime_preedit_msg(content, selection)
}
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()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::canvas_editor::ArrowDirection;
#[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'));
// Current behavior: character is inserted at cursor, selection is NOT automatically deleted
// This is expected behavior - user must delete selection first (Backspace/Delete) or use Paste
assert_eq!(editor.buffer.line(0), "Xhello 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_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_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_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");
}
}