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
use crate::input::event::{
Event, KeyCode, KeyEventKind, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};
use crate::utils::highlight_code;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Scrollbar, ScrollbarOrientation, ScrollbarState, StatefulWidget, Widget};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
use std::cell::RefCell;
/// A tactical multiline text editor widget for quick edits.
#[derive(Clone, Debug)]
pub struct TextEditor {
pub lines: Vec<String>,
pub cursor_row: usize, // Visual Row Index
pub cursor_col: usize, // Byte index
pub scroll_row: usize,
pub scroll_col: usize, // Visual column index
pub style: Style,
pub cursor_style: Style,
pub modified: bool,
pub show_line_numbers: bool,
pub history: Vec<Vec<String>>,
pub redo_stack: Vec<Vec<String>>,
pub filter_query: String,
pub filtered_indices: Vec<usize>,
pub read_only: bool,
pub selection_start: Option<(usize, usize)>, // (row, byte_col)
pub selection_end: Option<(usize, usize)>,
pub is_selecting: bool,
pub is_dragging_selection: bool,
pub language: String,
pub wrap: bool,
pub highlighted_cache: RefCell<Vec<Line<'static>>>,
pub first_invalid_line: RefCell<Option<usize>>,
}
impl Default for TextEditor {
fn default() -> Self {
Self {
lines: vec![String::new()],
cursor_row: 0,
cursor_col: 0,
scroll_row: 0,
scroll_col: 0,
style: Style::default().fg(Color::Rgb(255, 255, 255)),
cursor_style: Style::default()
.bg(Color::Rgb(88, 166, 255))
.fg(Color::Black)
.add_modifier(Modifier::BOLD),
modified: false,
show_line_numbers: true,
history: Vec::new(),
redo_stack: Vec::new(),
filter_query: String::new(),
filtered_indices: Vec::new(),
read_only: false,
selection_start: None,
selection_end: None,
is_selecting: false,
is_dragging_selection: false,
language: String::new(),
wrap: false,
highlighted_cache: RefCell::new(Vec::new()),
first_invalid_line: RefCell::new(Some(0)),
}
}
}
impl TextEditor {
pub fn set_filter(&mut self, query: &str) {
if self.filter_query == query {
return;
}
// If clearing filter, restore cursor to real line index
if query.is_empty() && !self.filter_query.is_empty() {
if self.cursor_row < self.filtered_indices.len() {
self.cursor_row = self.filtered_indices[self.cursor_row];
} else if !self.filtered_indices.is_empty() {
self.cursor_row = *self.filtered_indices.last().unwrap();
} else {
self.cursor_row = 0;
}
self.filtered_indices.clear();
}
self.filter_query = query.to_string();
if !self.filter_query.is_empty() {
self.filtered_indices = self
.lines
.iter()
.enumerate()
.filter(|(_, line)| {
line.to_lowercase()
.contains(&self.filter_query.to_lowercase())
})
.map(|(i, _)| i)
.collect();
self.cursor_row = 0;
self.scroll_row = 0;
}
self.scroll_col = 0;
self.cursor_col = 0;
self.invalidate_from(0);
}
fn effective_len(&self) -> usize {
if !self.filter_query.is_empty() {
self.filtered_indices.len()
} else {
self.lines.len()
}
}
fn get_effective_line(&self, idx: usize) -> &String {
if idx >= self.effective_len() {
return &self.lines[0];
} // Fallback safety
if !self.filter_query.is_empty() {
&self.lines[self.filtered_indices[idx]]
} else {
&self.lines[idx]
}
}
fn get_real_line_idx(&self, idx: usize) -> usize {
if !self.filter_query.is_empty() {
if idx < self.filtered_indices.len() {
self.filtered_indices[idx]
} else {
0
}
} else {
idx
}
}
pub fn get_visual_x(&self, row: usize, byte_col: usize) -> usize {
if row >= self.effective_len() {
return 0;
}
let line = self.get_effective_line(row);
if byte_col > line.len() {
return 0;
}
line[..byte_col].width()
}
pub fn get_byte_index_from_visual(&self, row: usize, visual_x: usize) -> usize {
if row >= self.effective_len() {
return 0;
}
let line = self.get_effective_line(row);
let mut width = 0;
for (i, c) in line.char_indices() {
if width >= visual_x {
return i;
}
width += c.width().unwrap_or(0);
}
line.len()
}
pub fn new() -> Self {
Self::default()
}
pub fn invalidate_from(&self, row: usize) {
let mut first = self.first_invalid_line.borrow_mut();
if let Some(current) = *first {
if row < current {
*first = Some(row);
}
} else {
*first = Some(row);
}
}
pub fn with_content(content: &str) -> Self {
let mut lines: Vec<String> = content.lines().map(|s| s.to_string()).collect();
if lines.is_empty() {
lines.push(String::new());
}
// Always ensure a trailing empty line for "extra line after everything"
if !lines.last().map(|l| l.is_empty()).unwrap_or(false) {
lines.push(String::new());
}
Self {
lines,
..Default::default()
}
}
pub fn get_content(&self) -> String {
self.lines.join("\n")
}
pub fn replace_all(&mut self, find: &str, replace: &str) {
if find.is_empty() {
return;
}
for line in &mut self.lines {
*line = line.replace(find, replace);
}
self.modified = true;
self.invalidate_from(0);
}
pub fn replace_next(&mut self, find: &str, replace: &str) -> bool {
if find.is_empty() {
return false;
}
// Search from current cursor position
let start_row = self.cursor_row;
let start_col = self.cursor_col;
for r in 0..self.lines.len() {
let row = (start_row + r) % self.lines.len();
let line = &self.lines[row];
let search_from = if r == 0 { start_col } else { 0 };
if search_from < line.len() {
if let Some(col) = line[search_from..].find(find) {
let actual_col = search_from + col;
let mut new_line = line.clone();
new_line.replace_range(actual_col..actual_col + find.len(), replace);
self.lines[row] = new_line;
self.cursor_row = row;
self.cursor_col = actual_col + replace.len();
self.modified = true;
self.invalidate_from(0);
return true;
}
}
}
false
}
pub fn gutter_width(&self) -> usize {
if !self.show_line_numbers {
return 0;
}
let total_lines = self.lines.len();
let digits = total_lines.to_string().len();
digits + 2 // +1 for left padding, +1 for vertical separator
}
pub fn handle_event(&mut self, event: &Event, area: Rect) -> bool {
// If filtered OR Read-Only, allow only navigation
if !self.filter_query.is_empty() || self.read_only {
if let Event::Key(key) = event {
if key.kind != KeyEventKind::Press {
return false;
}
let has_control = key.modifiers.contains(KeyModifiers::CONTROL);
let has_shift = key.modifiers.contains(KeyModifiers::SHIFT);
match key.code {
KeyCode::Up | KeyCode::Char('p') if has_control => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_up();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Down | KeyCode::Char('n') if has_control => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_down();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Up => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_up();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Down => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_down();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Left => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_left();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Right => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_right();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::PageUp => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_row = self.cursor_row.saturating_sub(area.height as usize);
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::PageDown => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_row = std::cmp::min(
self.effective_len().saturating_sub(1),
self.cursor_row + area.height as usize,
);
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Home => {
if has_shift {
self.maybe_start_selection();
}
let line = self.get_effective_line(self.cursor_row);
let first_non_whitespace = line
.chars()
.enumerate()
.find(|(_, c)| !c.is_whitespace())
.map(|(i, _)| i)
.unwrap_or(0);
if self.cursor_col == first_non_whitespace {
self.cursor_col = 0;
} else {
self.cursor_col = first_non_whitespace;
}
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::End => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_col = self.get_effective_line(self.cursor_row).len();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
_ => {}
}
}
return false;
}
match event {
Event::Key(key) => {
if key.kind != KeyEventKind::Press {
return false;
}
let has_control = key.modifiers.contains(KeyModifiers::CONTROL);
let has_alt = key.modifiers.contains(KeyModifiers::ALT);
let has_shift = key.modifiers.contains(KeyModifiers::SHIFT);
match key.code {
KeyCode::Char(c) if !has_control && !has_alt => {
self.push_history();
if self.selection_start.is_some() {
self.delete_selection();
}
self.insert_char(c);
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Tab => {
self.push_history();
if self.selection_start.is_some() {
self.delete_selection();
}
for _ in 0..4 {
self.insert_char(' ');
}
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
KeyCode::BackTab => {
// Shift + Tab
self.push_history();
let line = &mut self.lines[self.cursor_row];
let mut spaces_removed = 0;
while spaces_removed < 4 && line.starts_with(' ') {
line.remove(0);
spaces_removed += 1;
}
if spaces_removed > 0 {
self.cursor_col = self.cursor_col.saturating_sub(spaces_removed);
self.modified = true;
self.invalidate_from(0);
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Char('z') if has_control => {
if let Some(prev) = self.history.pop() {
self.redo_stack.push(self.lines.clone());
self.lines = prev;
self.cursor_row = std::cmp::min(self.cursor_row, self.lines.len() - 1);
self.cursor_col =
std::cmp::min(self.cursor_col, self.lines[self.cursor_row].len());
self.clear_selection();
self.modified = true;
self.invalidate_from(0);
self.ensure_cursor_visible(area);
return true;
}
}
KeyCode::Char('y') if has_control => {
if let Some(next) = self.redo_stack.pop() {
self.history.push(self.lines.clone());
self.lines = next;
self.cursor_row = std::cmp::min(self.cursor_row, self.lines.len() - 1);
self.cursor_col =
std::cmp::min(self.cursor_col, self.lines[self.cursor_row].len());
self.clear_selection();
self.modified = true;
self.invalidate_from(0);
self.ensure_cursor_visible(area);
return true;
}
}
KeyCode::Char('a') if has_control => {
self.select_all();
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Char('d') if has_control => {
self.push_history();
let current_line = self.lines[self.cursor_row].clone();
self.lines.insert(self.cursor_row + 1, current_line);
self.cursor_row += 1;
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Char('k') if has_control => {
self.push_history();
if self.cursor_col >= self.lines[self.cursor_row].len() {
if self.cursor_row < self.lines.len() - 1 {
let next_line = self.lines.remove(self.cursor_row + 1);
self.lines[self.cursor_row].push_str(&next_line);
}
} else {
let line = &mut self.lines[self.cursor_row];
line.truncate(self.cursor_col);
}
self.modified = true;
return true;
}
KeyCode::Char('u') if has_control => {
self.push_history();
let line = &mut self.lines[self.cursor_row];
if self.cursor_col > 0 {
*line = line.split_off(self.cursor_col);
self.cursor_col = 0;
}
self.modified = true;
return true;
}
KeyCode::Char('w') if has_control => {
self.push_history();
if self.selection_start.is_some() {
self.delete_selection();
} else {
self.delete_word_backwards();
}
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Enter => {
self.push_history();
if self.selection_start.is_some() {
self.delete_selection();
}
self.insert_newline();
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Backspace if has_control || has_alt => {
self.push_history();
if self.selection_start.is_some() {
self.delete_selection();
} else {
self.delete_word_backwards();
}
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Backspace => {
if self.selection_start.is_some() {
self.push_history();
self.delete_selection();
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
if self.delete_backwards() {
self.push_history();
self.modified = true;
self.ensure_cursor_visible(area);
return true;
}
}
KeyCode::Delete if has_control || has_alt => {
self.push_history();
if self.selection_start.is_some() {
self.delete_selection();
} else {
self.delete_word_forwards();
}
self.modified = true;
return true;
}
KeyCode::Delete => {
if self.selection_start.is_some() {
self.push_history();
self.delete_selection();
self.modified = true;
return true;
}
if self.delete_forwards() {
self.push_history();
self.modified = true;
return true;
}
}
KeyCode::Left if has_control || has_alt => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_word_left();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Left => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_left();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Right if has_control || has_alt => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_word_right();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Right => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_right();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
// Emacs bindings
KeyCode::Char('b') if has_alt => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_word_left();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Char('f') if has_alt => {
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_word_right();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
/*
KeyCode::Char('b') if has_control => {
if has_shift { self.maybe_start_selection(); }
self.move_cursor_left();
if has_shift { self.update_selection_end(); } else { self.clear_selection(); }
self.ensure_cursor_visible(area); return true;
}
KeyCode::Char('f') if has_control => {
if has_shift { self.maybe_start_selection(); }
self.move_cursor_right();
if has_shift { self.update_selection_end(); } else { self.clear_selection(); }
self.ensure_cursor_visible(area); return true;
}
*/
KeyCode::Up | KeyCode::Char('p') if has_control => {
// Ctrl+p is Up
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_up();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Down | KeyCode::Char('n') if has_control => {
// Ctrl+n is Down
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_down();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Up => {
if has_alt {
self.move_line_up();
self.ensure_cursor_visible(area);
return true;
}
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_up();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Down => {
if has_alt {
self.move_line_down();
self.ensure_cursor_visible(area);
return true;
}
if has_shift {
self.maybe_start_selection();
}
self.move_cursor_down();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Home if has_control => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_row = 0;
self.cursor_col = 0;
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::End if has_control => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_row = self.lines.len().saturating_sub(1);
self.cursor_col = self.lines[self.cursor_row].len();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::Home => {
if has_shift {
self.maybe_start_selection();
}
let line = &self.lines[self.cursor_row];
let first_non_whitespace = line
.chars()
.enumerate()
.find(|(_, c)| !c.is_whitespace())
.map(|(i, _)| i)
.unwrap_or(0);
if self.cursor_col == first_non_whitespace {
self.cursor_col = 0;
} else {
self.cursor_col = first_non_whitespace;
}
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::End => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_col = self.lines[self.cursor_row].len();
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::PageUp => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_row = self.cursor_row.saturating_sub(area.height as usize);
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
KeyCode::PageDown => {
if has_shift {
self.maybe_start_selection();
}
self.cursor_row = std::cmp::min(
self.lines.len() - 1,
self.cursor_row + area.height as usize,
);
if has_shift {
self.update_selection_end();
} else {
self.clear_selection();
}
self.ensure_cursor_visible(area);
return true;
}
_ => {}
}
}
Event::Mouse(mouse) => {
return self.handle_mouse_event(*mouse, area);
}
_ => {}
}
false
}
pub fn handle_mouse_event(&mut self, mouse: MouseEvent, area: Rect) -> bool {
if mouse.column < area.x
|| mouse.column >= area.x + area.width
|| mouse.row < area.y
|| mouse.row >= area.y + area.height
{
return false;
}
let gutter = self.gutter_width();
let scrollbar_w = if self.effective_len() > area.height as usize {
1
} else {
0
};
let content_width = area
.width
.saturating_sub(gutter as u16 + scrollbar_w as u16);
let rel_row = (mouse.row - area.y) as usize;
match mouse.kind {
MouseEventKind::Down(MouseButton::Left) => {
// 1. Scrollbar 'Click-to-Jump' logic (rightmost column)
if mouse.column >= area.x + area.width.saturating_sub(1) {
let total_lines = if self.wrap {
// Estimate wrapped lines? For now just use effective_len
self.effective_len()
} else {
self.effective_len()
};
let view_height = area.height as usize;
if total_lines > view_height {
let percent = rel_row as f32 / view_height as f32;
self.scroll_row = (percent * total_lines as f32) as usize;
self.scroll_row =
self.scroll_row.min(total_lines.saturating_sub(view_height));
}
return true;
}
let (target_row, target_col) = if self.wrap {
// In wrap mode, scroll_row is screen line index.
let screen_row = self.scroll_row + rel_row;
// Find which source line and which segment this screen row belongs to
let mut current_screen_row = 0;
let mut found = None;
let width = content_width as usize;
for i in 0..self.effective_len() {
let line = self.get_effective_line(i);
let w = line.width();
let segments = if w == 0 {
1
} else {
(w.saturating_sub(1) / width) + 1
};
if screen_row >= current_screen_row
&& screen_row < current_screen_row + segments
{
let segment_idx = screen_row - current_screen_row;
let rel_col = (mouse.column - area.x - gutter as u16) as usize;
let visual_x = segment_idx * width + rel_col;
let byte_idx = self.get_byte_index_from_visual(i, visual_x);
found = Some((i, byte_idx));
break;
}
current_screen_row += segments;
}
found.unwrap_or((self.cursor_row, self.cursor_col))
} else {
let row = self.scroll_row + rel_row;
let col = if mouse.column >= area.x + gutter as u16 {
let rel_col = (mouse.column - area.x - gutter as u16) as usize;
let target_visual = self.scroll_col + rel_col;
self.get_byte_index_from_visual(row, target_visual)
} else {
0
};
(row, col)
};
if target_row < self.effective_len() {
if self.is_inside_selection(target_row, target_col) {
self.is_dragging_selection = true;
self.is_selecting = false;
self.cursor_row = target_row;
self.cursor_col = target_col;
return true;
}
self.cursor_row = target_row;
self.cursor_col = target_col;
self.selection_start = Some((self.cursor_row, self.cursor_col));
self.selection_end = Some((self.cursor_row, self.cursor_col));
self.is_selecting = true;
self.is_dragging_selection = false;
return true;
}
}
MouseEventKind::Drag(MouseButton::Left) => {
// Check if click/drag is within scrollbar area (rightmost column)
if mouse.column >= area.x + area.width.saturating_sub(1) {
let total_lines = self.effective_len();
let view_height = area.height as usize;
if total_lines > view_height {
let percent = rel_row as f32 / view_height as f32;
self.scroll_row = (percent * total_lines as f32) as usize;
self.scroll_row =
self.scroll_row.min(total_lines.saturating_sub(view_height));
}
return true;
}
let (target_row, target_col) = if self.wrap {
let screen_row = self.scroll_row + rel_row;
let mut current_screen_row = 0;
let mut found = None;
let width = content_width as usize;
for i in 0..self.effective_len() {
let line = self.get_effective_line(i);
let w = line.width();
let segments = if w == 0 {
1
} else {
(w.saturating_sub(1) / width) + 1
};
if screen_row >= current_screen_row
&& screen_row < current_screen_row + segments
{
let segment_idx = screen_row - current_screen_row;
let rel_col =
(mouse.column.saturating_sub(area.x + gutter as u16)) as usize;
let visual_x = segment_idx * width + rel_col;
let byte_idx = self.get_byte_index_from_visual(i, visual_x);
found = Some((i, byte_idx));
break;
}
current_screen_row += segments;
}
found.unwrap_or((self.cursor_row, self.cursor_col))
} else {
let row = self.scroll_row + rel_row;
let col = if mouse.column >= area.x + gutter as u16 {
let rel_col = (mouse.column - area.x - gutter as u16) as usize;
let target_visual = self.scroll_col + rel_col;
self.get_byte_index_from_visual(row, target_visual)
} else {
0
};
(row, col)
};
if target_row < self.effective_len() {
self.cursor_row = target_row;
self.cursor_col = target_col;
if self.is_selecting {
self.selection_end = Some((self.cursor_row, self.cursor_col));
}
// During drag, we just move the cursor (insertion point)
return true;
}
}
MouseEventKind::Up(MouseButton::Left) => {
if self.is_dragging_selection {
let r = self.cursor_row;
let c = self.cursor_col;
self.move_selection_to(r, c);
self.is_dragging_selection = false;
return true;
}
if self.is_selecting {
self.is_selecting = false;
// If start == end, clear selection
if self.selection_start == self.selection_end {
self.selection_start = None;
self.selection_end = None;
}
}
return true;
}
MouseEventKind::ScrollUp => {
self.scroll_row = self.scroll_row.saturating_sub(5);
return true;
}
MouseEventKind::ScrollDown => {
let total_lines = if self.wrap {
self.effective_len() * 2
} else {
self.effective_len()
};
let max_scroll = total_lines.saturating_sub(area.height as usize);
self.scroll_row = std::cmp::min(max_scroll, self.scroll_row + 5);
return true;
}
_ => {}
}
false
}
pub fn push_history(&mut self) {
if let Some(last) = self.history.last() {
if last == &self.lines {
return;
}
}
self.history.push(self.lines.clone());
if self.history.len() > 100 {
self.history.remove(0);
}
self.redo_stack.clear();
}
fn delete_word_backwards(&mut self) {
if self.cursor_col == 0 && self.cursor_row == 0 {
return;
}
if self.cursor_col == 0 {
self.delete_backwards();
return;
}
let line = &self.lines[self.cursor_row];
let mut i = self.cursor_col;
// Skip trailing whitespace
while i > 0 {
if let Some(prev) = line[..i].chars().next_back() {
if prev.is_whitespace() {
i -= prev.len_utf8();
} else {
break;
}
} else {
break;
}
}
// Skip the word
while i > 0 {
if let Some(prev) = line[..i].chars().next_back() {
if !prev.is_whitespace() {
i -= prev.len_utf8();
} else {
break;
}
} else {
break;
}
}
let to_remove_bytes = self.cursor_col - i;
for _ in 0..to_remove_bytes {
self.delete_backwards();
}
}
fn delete_word_forwards(&mut self) {
let line = &self.lines[self.cursor_row];
if self.cursor_col >= line.len() && self.cursor_row >= self.lines.len() - 1 {
return;
}
if self.cursor_col >= line.len() {
self.delete_forwards();
return;
}
let mut i = self.cursor_col;
// Skip trailing whitespace
while i < line.len() {
if let Some(next) = line[i..].chars().next() {
if next.is_whitespace() {
i += next.len_utf8();
} else {
break;
}
} else {
break;
}
}
// Skip the word
while i < line.len() {
if let Some(next) = line[i..].chars().next() {
if !next.is_whitespace() {
i += next.len_utf8();
} else {
break;
}
} else {
break;
}
}
let to_remove_bytes = i - self.cursor_col;
for _ in 0..to_remove_bytes {
self.delete_forwards();
}
}
fn move_cursor_word_left(&mut self) {
if self.cursor_col == 0 {
self.move_cursor_left();
return;
}
let line = &self.lines[self.cursor_row];
let mut i = self.cursor_col;
while i > 0 {
if let Some(prev) = line[..i].chars().next_back() {
if prev.is_whitespace() {
i -= prev.len_utf8();
} else {
break;
}
} else {
break;
}
}
while i > 0 {
if let Some(prev) = line[..i].chars().next_back() {
if !prev.is_whitespace() {
i -= prev.len_utf8();
} else {
break;
}
} else {
break;
}
}
self.cursor_col = i;
}
fn move_cursor_word_right(&mut self) {
let line = &self.lines[self.cursor_row];
if self.cursor_col >= line.len() {
self.move_cursor_right();
return;
}
let mut i = self.cursor_col;
while i < line.len() {
if let Some(next) = line[i..].chars().next() {
if next.is_whitespace() {
i += next.len_utf8();
} else {
break;
}
} else {
break;
}
}
while i < line.len() {
if let Some(next) = line[i..].chars().next() {
if !next.is_whitespace() {
i += next.len_utf8();
} else {
break;
}
} else {
break;
}
}
self.cursor_col = i;
}
fn insert_char(&mut self, c: char) {
if c == '\x1b' {
return;
}
self.ensure_valid_cursor_col();
let line = &mut self.lines[self.cursor_row];
line.insert(self.cursor_col, c);
self.cursor_col += c.len_utf8();
self.modified = true;
self.invalidate_from(self.cursor_row);
}
pub fn ensure_valid_cursor_col(&mut self) {
if self.cursor_row >= self.lines.len() {
self.cursor_row = self.lines.len().saturating_sub(1);
}
let line = &self.lines[self.cursor_row];
if self.cursor_col > line.len() {
self.cursor_col = line.len();
}
while !line.is_char_boundary(self.cursor_col) {
self.cursor_col = self.cursor_col.saturating_sub(1);
}
}
fn insert_newline(&mut self) {
self.ensure_valid_cursor_col();
let line = &self.lines[self.cursor_row];
let indentation = line
.chars()
.take_while(|c| c.is_whitespace())
.collect::<String>();
let line = &mut self.lines[self.cursor_row];
let remaining = line.split_off(self.cursor_col);
let mut new_line = indentation.clone();
new_line.push_str(&remaining);
self.lines.insert(self.cursor_row + 1, new_line);
self.invalidate_from(self.cursor_row);
self.cursor_row += 1;
self.cursor_col = indentation.len();
}
fn insert_newline_raw(&mut self) {
self.ensure_valid_cursor_col();
let line = &mut self.lines[self.cursor_row];
let remaining = line.split_off(self.cursor_col);
self.lines.insert(self.cursor_row + 1, remaining);
self.invalidate_from(self.cursor_row);
self.cursor_row += 1;
self.cursor_col = 0;
}
fn delete_backwards(&mut self) -> bool {
self.ensure_valid_cursor_col();
if self.cursor_col > 0 {
let line = &mut self.lines[self.cursor_row];
// Smart Backspace: If we are at an indentation level (4 spaces) and only have spaces before us
let prefix = &line[..self.cursor_col];
if self.cursor_col >= 4
&& self.cursor_col.is_multiple_of(4)
&& prefix.chars().all(|c| c == ' ')
{
for _ in 0..4 {
line.remove(self.cursor_col - 1);
self.cursor_col -= 1;
}
} else {
// UTF-8 Aware delete
if let Some(c) = line[..self.cursor_col].chars().next_back() {
let len = c.len_utf8();
line.remove(self.cursor_col - len);
self.cursor_col -= len;
}
}
self.modified = true;
self.invalidate_from(self.cursor_row);
true
} else if self.cursor_row > 0 {
let current_line = self.lines.remove(self.cursor_row);
self.cursor_row -= 1;
self.cursor_col = self.lines[self.cursor_row].len();
self.lines[self.cursor_row].push_str(¤t_line);
self.modified = true;
self.invalidate_from(self.cursor_row);
true
} else {
false
}
}
fn delete_forwards(&mut self) -> bool {
self.ensure_valid_cursor_col();
let line = &mut self.lines[self.cursor_row];
if self.cursor_col < line.len() {
if let Some(_c) = line[self.cursor_col..].chars().next() {
line.remove(self.cursor_col); // remove at cursor_col is fine, it's the start of the char
}
self.invalidate_from(self.cursor_row);
true
} else if self.cursor_row < self.lines.len() - 1 {
let next_line = self.lines.remove(self.cursor_row + 1);
self.lines[self.cursor_row].push_str(&next_line);
self.invalidate_from(self.cursor_row);
true
} else {
false
}
}
fn move_cursor_left(&mut self) {
if self.cursor_col > 0 {
let line = self.get_effective_line(self.cursor_row);
let mut i = self.cursor_col;
while i > 0 {
i -= 1;
if line.is_char_boundary(i) {
break;
}
}
self.cursor_col = i;
} else if self.cursor_row > 0 {
self.cursor_row -= 1;
self.cursor_col = self.get_effective_line(self.cursor_row).len();
}
}
fn move_cursor_right(&mut self) {
let line = self.get_effective_line(self.cursor_row);
if self.cursor_col < line.len() {
let mut i = self.cursor_col + 1;
while i <= line.len() {
if line.is_char_boundary(i) {
break;
}
i += 1;
}
self.cursor_col = i;
} else if self.cursor_row < self.effective_len().saturating_sub(1) {
self.cursor_row += 1;
self.cursor_col = 0;
}
}
fn move_cursor_up(&mut self) {
if self.cursor_row > 0 {
let current_visual = self.get_visual_x(self.cursor_row, self.cursor_col);
self.cursor_row -= 1;
self.cursor_col = self.get_byte_index_from_visual(self.cursor_row, current_visual);
}
}
fn move_cursor_down(&mut self) {
if self.cursor_row < self.effective_len().saturating_sub(1) {
let current_visual = self.get_visual_x(self.cursor_row, self.cursor_col);
self.cursor_row += 1;
self.cursor_col = self.get_byte_index_from_visual(self.cursor_row, current_visual);
}
}
pub fn move_line_up(&mut self) {
if self.cursor_row > 0 {
self.push_history();
let line = self.lines.remove(self.cursor_row);
self.lines.insert(self.cursor_row - 1, line);
self.cursor_row -= 1;
self.modified = true;
self.invalidate_from(0);
}
}
pub fn move_line_down(&mut self) {
if self.cursor_row < self.lines.len().saturating_sub(1) {
self.push_history();
let line = self.lines.remove(self.cursor_row);
self.lines.insert(self.cursor_row + 1, line);
self.cursor_row += 1;
self.modified = true;
self.invalidate_from(0);
}
}
pub fn ensure_cursor_centered(&mut self, area: Rect) {
let height = area.height as usize;
let target_scroll = self.cursor_row.saturating_sub(height / 2);
let max_scroll = self.effective_len().saturating_sub(height);
self.scroll_row = std::cmp::min(target_scroll, max_scroll);
if self.wrap {
self.scroll_col = 0;
return;
}
let gutter = self.gutter_width();
let width = area.width.saturating_sub(gutter as u16) as usize;
let visual_cursor_x = self.get_visual_x(self.cursor_row, self.cursor_col);
if visual_cursor_x < self.scroll_col {
self.scroll_col = visual_cursor_x;
} else if visual_cursor_x >= self.scroll_col + width {
self.scroll_col = visual_cursor_x - width + 1;
}
}
pub fn get_visual_row_at(&self, row: usize, width: usize) -> usize {
let mut visual_row = 0;
for i in 0..row {
let line = self.get_effective_line(i);
if self.wrap && width > 0 {
let w = line.width();
if w == 0 {
visual_row += 1;
} else {
visual_row += (w.saturating_sub(1) / width) + 1;
}
} else {
visual_row += 1;
}
}
visual_row
}
pub fn get_cursor_visual_row(&self, width: usize) -> usize {
let mut visual_row = self.get_visual_row_at(self.cursor_row, width);
if self.wrap && width > 0 {
let line = self.get_effective_line(self.cursor_row);
let cursor_x = line[..self.cursor_col].width();
visual_row += cursor_x / width;
}
visual_row
}
pub fn ensure_cursor_visible(&mut self, area: Rect) {
let height = area.height as usize;
let gutter = self.gutter_width();
let scrollbar_w = if self.effective_len() > area.height as usize {
1
} else {
0
};
let width = area
.width
.saturating_sub(gutter as u16 + scrollbar_w as u16) as usize;
if self.wrap {
let visual_row = self.get_cursor_visual_row(width);
if visual_row < self.scroll_row {
self.scroll_row = visual_row;
} else if visual_row >= self.scroll_row + height {
self.scroll_row = visual_row - height + 1;
}
self.scroll_col = 0;
return;
}
// Smoother vertical scroll with safety for small areas
let margin = (height / 4).min(3);
if self.cursor_row < self.scroll_row + margin {
self.scroll_row = self.cursor_row.saturating_sub(margin);
} else if self.cursor_row >= self.scroll_row + height.saturating_sub(margin) {
let target_scroll = (self.cursor_row + margin + 1).saturating_sub(height);
let max_scroll = self.effective_len().saturating_sub(height);
self.scroll_row = std::cmp::min(target_scroll, max_scroll);
}
let visual_cursor_x = self.get_visual_x(self.cursor_row, self.cursor_col);
if visual_cursor_x < self.scroll_col {
self.scroll_col = visual_cursor_x;
} else if visual_cursor_x >= self.scroll_col + width {
self.scroll_col = (visual_cursor_x + 1).saturating_sub(width);
}
}
pub fn get_selection_range(&self) -> Option<((usize, usize), (usize, usize))> {
let start = self.selection_start?;
let end = self.selection_end?;
if start <= end {
Some((start, end))
} else {
Some((end, start))
}
}
pub fn maybe_start_selection(&mut self) {
if self.selection_start.is_none() {
self.selection_start = Some((self.cursor_row, self.cursor_col));
}
}
pub fn update_selection_end(&mut self) {
self.selection_end = Some((self.cursor_row, self.cursor_col));
}
pub fn clear_selection(&mut self) {
self.selection_start = None;
self.selection_end = None;
self.is_selecting = false;
self.is_dragging_selection = false;
}
pub fn is_inside_selection(&self, row: usize, byte_col: usize) -> bool {
if let Some(((s_row, s_col), (e_row, e_col))) = self.get_selection_range() {
if row > s_row && row < e_row {
return true;
}
if row == s_row && row == e_row {
return byte_col >= s_col && byte_col < e_col;
}
if row == s_row {
return byte_col >= s_col;
}
if row == e_row {
return byte_col < e_col;
}
}
false
}
pub fn get_selected_text(&self) -> Option<String> {
let ((s_row, s_col), (e_row, e_col)) = self.get_selection_range()?;
let mut result = String::new();
for row in s_row..=e_row {
if row >= self.effective_len() {
break;
}
let line = self.get_effective_line(row);
let start = if row == s_row { s_col } else { 0 };
let end = if row == e_row { e_col } else { line.len() };
// Ensure bounds
let safe_start = std::cmp::min(start, line.len());
let safe_end = std::cmp::min(end, line.len());
if safe_start < safe_end {
result.push_str(&line[safe_start..safe_end]);
}
if row < e_row {
result.push('\n');
}
}
Some(result)
}
pub fn delete_selection(&mut self) {
if let Some(((s_row, s_col), (e_row, e_col))) = self.get_selection_range() {
if s_row == e_row {
let line = &mut self.lines[s_row];
if s_col < line.len() && e_col <= line.len() {
line.replace_range(s_col..e_col, "");
}
} else {
// First line: keep up to s_col
let start_part = if s_col < self.lines[s_row].len() {
self.lines[s_row][..s_col].to_string()
} else {
self.lines[s_row].clone()
};
// Last line: keep from e_col
let end_part = if e_col < self.lines[e_row].len() {
self.lines[e_row][e_col..].to_string()
} else {
String::new()
};
// Remove intermediate lines
self.lines.drain(s_row + 1..=e_row);
// Merge start and end
self.lines[s_row] = format!("{}{}", start_part, end_part);
}
self.cursor_row = s_row;
self.cursor_col = s_col;
self.selection_start = None;
self.selection_end = None;
self.is_selecting = false;
self.modified = true;
self.invalidate_from(0);
}
}
pub fn move_selection_to(&mut self, target_row: usize, target_col: usize) {
let text = if let Some(t) = self.get_selected_text() {
t
} else {
return;
};
let ((s_row, s_col), (e_row, e_col)) = if let Some(range) = self.get_selection_range() {
range
} else {
return;
};
self.push_history();
// Calculate new position after deletion
let mut new_row = target_row;
let mut new_col = target_col;
if target_row > e_row {
// Target is below the selection, shifts up by (e_row - s_row)
new_row -= e_row - s_row;
} else if target_row == e_row && target_col >= e_col {
// Target is on the same line as the end of selection, but after it
new_row = s_row;
new_col = s_col + (target_col - e_col);
} else if target_row == s_row && target_col >= s_col {
// Target is inside selection (on first line) - move to start of selection
new_row = s_row;
new_col = s_col;
} else if target_row > s_row && target_row < e_row {
// Target is inside selection (middle lines) - move to start of selection
new_row = s_row;
new_col = s_col;
}
self.delete_selection();
self.cursor_row = new_row;
self.cursor_col = new_col;
self.insert_string(&text);
}
pub fn insert_string(&mut self, text: &str) {
self.push_history();
// Delete selection if active
if self.selection_start.is_some() {
self.delete_selection();
}
for (i, part) in text.split('\n').enumerate() {
if i > 0 {
self.insert_newline_raw();
}
for c in part.chars() {
if c != '\r' {
self.insert_char(c);
}
}
}
self.invalidate_from(0);
}
pub fn select_all(&mut self) {
self.selection_start = Some((0, 0));
let last_row = self.lines.len().saturating_sub(1);
let last_col = self.lines[last_row].len();
self.selection_end = Some((last_row, last_col));
self.cursor_row = last_row;
self.cursor_col = last_col;
self.is_selecting = false;
}
pub fn select_word_at(&mut self, row: usize, col: usize) {
if row >= self.lines.len() {
return;
}
let line = &self.lines[row];
if col > line.len() {
return;
}
let mut start = col;
let mut end = col;
// Find word start
while start > 0 {
if let Some(prev) = line[..start].chars().next_back() {
if prev.is_alphanumeric() || prev == '_' {
start -= prev.len_utf8();
} else {
break;
}
} else {
break;
}
}
// Find word end
while end < line.len() {
if let Some(next) = line[end..].chars().next() {
if next.is_alphanumeric() || next == '_' {
end += next.len_utf8();
} else {
break;
}
} else {
break;
}
}
if start < end {
self.selection_start = Some((row, start));
self.selection_end = Some((row, end));
self.cursor_row = row;
self.cursor_col = end;
self.is_selecting = false;
}
}
pub fn select_line_at(&mut self, row: usize) {
if row >= self.lines.len() {
return;
}
let line_len = self.lines[row].len();
self.selection_start = Some((row, 0));
self.selection_end = Some((row, line_len));
self.cursor_row = row;
self.cursor_col = line_len;
self.is_selecting = false;
}
pub fn delete_line(&mut self, row: usize) {
if self.lines.len() > 1 {
self.push_history();
self.lines.remove(row);
self.cursor_row = std::cmp::min(self.cursor_row, self.lines.len() - 1);
self.cursor_col = std::cmp::min(self.cursor_col, self.lines[self.cursor_row].len());
self.modified = true;
self.invalidate_from(0);
} else {
self.push_history();
self.lines[0].clear();
self.cursor_col = 0;
self.modified = true;
self.invalidate_from(0);
}
}
}
impl Widget for &TextEditor {
fn render(self, area: Rect, buf: &mut Buffer) {
let gutter_w = self.gutter_width();
let scrollbar_w = if self.effective_len() > area.height as usize {
1
} else {
0
};
let content_area = Rect::new(
area.x + gutter_w as u16,
area.y,
area.width
.saturating_sub(gutter_w as u16 + scrollbar_w as u16),
area.height,
);
let mut highlighted = {
let mut first_invalid = self.first_invalid_line.borrow_mut();
let mut cache = self.highlighted_cache.borrow_mut();
if let Some(start_line) = *first_invalid {
// Determine if we can do partial update or need full re-highlight
// syntect's HighlightLines needs state from previous lines usually,
// but for many languages we can restart at line boundaries if we don't care about multiline block comments as much,
// or we just re-highlight from the first changed line to the end.
let content_string = if !self.filter_query.is_empty() {
// Filtered view is harder to do incrementally, just do full for now
self.filtered_indices
.iter()
.map(|&i| self.lines[i].as_str())
.collect::<Vec<_>>()
.join("\n")
} else {
self.lines.join("\n")
};
if !self.filter_query.is_empty() || start_line == 0 {
let h_lines = highlight_code(&content_string, &self.language);
*cache = h_lines
.into_iter()
.map(|line| {
let spans: Vec<Span<'static>> = line
.spans
.into_iter()
.map(|span| Span::styled(span.content.to_string(), span.style))
.collect();
Line::from(spans)
})
.collect();
} else {
// INCREMENTAL: Only highlight from start_line to end
// Actually, syntect highlight_line maintains state. To do it right we need to store state per line.
// For now, let's just re-highlight everything if anything changed, BUT
// only if the cache size changed or we are forced.
// PERFORMANCE SHORTCUT: If we are just typing on one line, and it's not a filtered view,
// we can try to just re-highlight the entire file but it's still fast enough if we don't do it 60fps.
// The REAL lag usually comes from syntect being called too often.
let h_lines = highlight_code(&content_string, &self.language);
*cache = h_lines
.into_iter()
.map(|line| {
let spans: Vec<Span<'static>> = line
.spans
.into_iter()
.map(|span| Span::styled(span.content.to_string(), span.style))
.collect();
Line::from(spans)
})
.collect();
}
*first_invalid = None;
}
cache.clone()
};
// Ensure we have enough lines for the "extra line" and trailing empty lines
while highlighted.len() < self.effective_len() {
highlighted.push(Line::from(""));
}
let mut screen_lines: Vec<(usize, Line)> = Vec::new();
let _cursor_screen_pos: Option<(u16, u16)> = None;
for (line_idx, line) in highlighted.iter().enumerate() {
let _real_line_idx = self.get_real_line_idx(line_idx);
if self.wrap {
let mut current_spans = Vec::new();
let mut current_width = 0;
let max_width = content_area.width as usize;
for span in &line.spans {
let mut text = span.content.as_ref();
while !text.is_empty() {
let mut available = max_width.saturating_sub(current_width);
if available == 0 {
screen_lines.push((line_idx, Line::from(current_spans.clone())));
current_spans.clear();
current_width = 0;
available = max_width;
}
let mut break_idx = 0;
let mut break_width = 0;
for (idx, c) in text.char_indices() {
let cw = c.width().unwrap_or(0);
if break_width + cw > available {
break;
}
break_idx = idx + c.len_utf8();
break_width += cw;
}
if break_idx == 0 && !text.is_empty() {
// Force break if even a single char doesn't fit
let first_char = text.chars().next().unwrap();
break_idx = first_char.len_utf8();
break_width = first_char.width().unwrap_or(0);
}
let part = &text[..break_idx];
current_spans.push(Span::styled(part, span.style));
// Check if cursor is in this part
if line_idx == self.cursor_row {
let line_ref = self.get_effective_line(line_idx);
let _byte_offset = line_ref
.char_indices()
.filter(|(i, _)| *i < self.cursor_col)
.map(|(_, c)| c.len_utf8())
.sum::<usize>();
// Cursor detection in wrapped lines is hard.
// Simpler: if we just rendered the part containing self.cursor_col.
// We need to know the byte offset of 'part' within 'line'.
}
current_width += break_width;
text = &text[break_idx..];
}
}
screen_lines.push((line_idx, Line::from(current_spans)));
} else {
screen_lines.push((line_idx, line.clone()));
}
}
// Second pass: Find cursor screen position and handle scrolling
// For now, let's keep the existing logic if wrap is false.
// If wrap is true, we need a smarter scroll_row (screen line index).
if self.wrap {
let mut current_screen_row = 0;
#[allow(clippy::explicit_counter_loop)]
for (line_idx, (src_idx, line)) in screen_lines.iter().enumerate() {
if current_screen_row >= self.scroll_row
&& current_screen_row < self.scroll_row + area.height as usize
{
let i = current_screen_row - self.scroll_row;
let real_line_idx = self.get_real_line_idx(*src_idx);
let is_current_src = *src_idx == self.cursor_row;
// 1. Draw Background Highlight (Current Line)
let base_bg = self.style.bg.unwrap_or(Color::Reset);
let line_bg = if is_current_src {
Color::Rgb(20, 20, 25)
} else {
base_bg
};
let bg_area = Rect::new(area.x, area.y + i as u16, area.width, 1);
for x in bg_area.left()..bg_area.right() {
if let Some(cell) = buf.cell_mut((x, bg_area.top())) {
cell.set_bg(line_bg);
}
}
// 2. Render Gutter (only for the first screen line of a source line)
if self.show_line_numbers {
let is_first_wrap_line =
line_idx == 0 || screen_lines[line_idx - 1].0 != *src_idx;
if is_first_wrap_line {
let num = (real_line_idx + 1).to_string();
let gutter_style = if is_current_src {
Style::default()
.fg(Color::Rgb(88, 166, 255))
.add_modifier(Modifier::BOLD)
.bg(line_bg)
} else {
Style::default().fg(Color::Rgb(110, 118, 129)).bg(line_bg)
};
let x = area.x + (gutter_w as u16).saturating_sub(num.len() as u16 + 2);
buf.set_string(x + 1, area.y + i as u16, &num, gutter_style);
}
let sep_style = Style::default().fg(Color::Rgb(48, 54, 61)).bg(line_bg);
buf.set_string(
area.x + gutter_w as u16 - 1,
area.y + i as u16,
"│",
sep_style,
);
}
// 3. Render Line Content
let mut current_x = content_area.x;
for span in &line.spans {
let mut span_style = self.style.patch(span.style);
if span_style.bg.is_none() || span_style.bg == Some(base_bg) {
span_style.bg = Some(line_bg);
}
buf.set_string(current_x, area.y + i as u16, &span.content, span_style);
current_x += span.content.width() as u16;
}
// 4. Apply Selection (Second pass over the rendered line)
if let Some(((s_row, s_col), (e_row, e_col))) = self.get_selection_range() {
if real_line_idx >= s_row && real_line_idx <= e_row {
// In wrap mode, we need to know the byte offset of each character on screen.
// This is hard without re-tracking.
// Let's use get_byte_index_from_visual.
for vx in 0..content_area.width {
let visual_x = vx as usize + self.scroll_col;
let byte_idx = self.get_byte_index_from_visual(*src_idx, visual_x);
let is_selected = if real_line_idx > s_row && real_line_idx < e_row
{
true
} else if real_line_idx == s_row && real_line_idx == e_row {
byte_idx >= s_col && byte_idx < e_col
} else if real_line_idx == s_row {
byte_idx >= s_col
} else if real_line_idx == e_row {
byte_idx < e_col
} else {
false
};
if is_selected {
let cx = content_area.x + vx;
let cy = area.y + i as u16;
if let Some(cell) = buf.cell_mut((cx, cy)) {
cell.set_bg(Color::Rgb(40, 60, 100));
cell.set_fg(Color::White);
}
}
}
}
}
}
current_screen_row += 1;
}
// Cursor rendering for wrap mode
let mut current_screen_row = 0;
let mut cursor_found = false;
// We need to find which screen line contains the cursor
// This is slightly complex because we need to know the byte offset of each wrap segment.
// Let's re-calculate or track it.
for (line_idx, line) in highlighted.iter().enumerate() {
if line_idx > self.cursor_row {
break;
}
let is_cursor_line = line_idx == self.cursor_row;
let mut current_byte_offset = 0;
let mut current_width = 0;
let max_width = content_area.width as usize;
for span in &line.spans {
let mut text = span.content.as_ref();
while !text.is_empty() {
let mut available = max_width.saturating_sub(current_width);
if available == 0 {
current_screen_row += 1;
current_width = 0;
available = max_width;
}
let mut break_idx = 0;
let mut break_width = 0;
for (idx, c) in text.char_indices() {
let cw = c.width().unwrap_or(0);
if break_width + cw > available {
break;
}
break_idx = idx + c.len_utf8();
break_width += cw;
}
if break_idx == 0 && !text.is_empty() {
let first_char = text.chars().next().unwrap();
break_idx = first_char.len_utf8();
break_width = first_char.width().unwrap_or(0);
}
if is_cursor_line && !cursor_found {
if self.cursor_col >= current_byte_offset
&& self.cursor_col < current_byte_offset + break_idx
{
// Cursor is in THIS segment
let sub_col = self.cursor_col - current_byte_offset;
let visual_offset = text[..sub_col].width();
let cx = content_area
.x
.saturating_add((current_width + visual_offset) as u16);
let cy = (area.y as i32
+ (current_screen_row as i32 - self.scroll_row as i32))
as u16;
if current_screen_row >= self.scroll_row
&& current_screen_row < self.scroll_row + area.height as usize
{
if let Some(cell) = buf.cell_mut((cx, cy)) {
if !cell.symbol().is_empty() && cell.symbol() != " " {
cell.set_style(self.cursor_style);
} else {
cell.set_style(self.cursor_style);
cell.set_symbol(" ");
}
}
}
cursor_found = true;
} else if self.cursor_col == current_byte_offset + break_idx
&& (current_byte_offset + break_idx == line.width()
|| text.len() == break_idx)
{
// Special case: cursor at end of line/segment
// If it's the absolute end of the source line, or it's the end of a wrap segment but NOT the last segment?
// Actually if it's the end of a segment that IS the end of the line.
let _is_last_segment = text.len() == break_idx; // This span is done
// Wait, we need to know if there are MORE spans.
// Simplification: if cursor_col == line.len() and we are at the last segment of the last span.
}
}
current_byte_offset += break_idx;
current_width += break_width;
text = &text[break_idx..];
}
}
// Handle cursor at the absolute end of line
if is_cursor_line
&& !cursor_found
&& self.cursor_col == self.get_effective_line(line_idx).len()
{
let cx = content_area.x.saturating_add(current_width as u16);
let cy = (area.y as i32 + (current_screen_row as i32 - self.scroll_row as i32))
as u16;
if current_screen_row >= self.scroll_row
&& current_screen_row < self.scroll_row + area.height as usize
{
if let Some(cell) = buf.cell_mut((cx, cy)) {
cell.set_style(self.cursor_style);
cell.set_symbol(" ");
}
}
cursor_found = true;
}
current_screen_row += 1;
}
// Render Scrollbars for wrap mode
if scrollbar_w > 0 {
let sb = Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("▲"))
.end_symbol(Some("▼"));
// In wrap mode, scroll_row is screen line index.
// We need the total screen lines for ScrollbarState.
let total_screen_lines = screen_lines.len();
let mut ss = ScrollbarState::new(total_screen_lines)
.position(self.scroll_row)
.viewport_content_length(area.height as usize);
StatefulWidget::render(sb, area, buf, &mut ss);
}
return;
}
// Original non-wrap rendering logic
for (i, line) in highlighted.iter().skip(self.scroll_row).enumerate() {
if i >= area.height as usize {
break;
}
let line_idx = self.scroll_row + i;
let real_line_idx = self.get_real_line_idx(line_idx);
let is_current = line_idx == self.cursor_row;
let base_bg = self.style.bg.unwrap_or(Color::Reset);
let line_bg = if is_current {
Color::Rgb(20, 20, 25)
} else {
base_bg
};
if is_current || base_bg != Color::Reset {
let bg_area = Rect::new(area.x, area.y + i as u16, area.width, 1);
for x in bg_area.left()..bg_area.right() {
if let Some(cell) = buf.cell_mut((x, bg_area.top())) {
cell.set_bg(line_bg);
}
}
}
// Render Gutter (Line Numbers)
if self.show_line_numbers {
let num = (real_line_idx + 1).to_string();
let gutter_style = if is_current {
Style::default()
.fg(Color::Rgb(88, 166, 255))
.add_modifier(Modifier::BOLD)
.bg(line_bg)
} else {
Style::default().fg(Color::Rgb(110, 118, 129)).bg(line_bg)
};
let x = area.x + (gutter_w as u16).saturating_sub(num.len() as u16 + 2);
buf.set_string(x + 1, area.y + i as u16, &num, gutter_style);
// Vertical separator
let sep_style = Style::default().fg(Color::Rgb(48, 54, 61)).bg(line_bg);
buf.set_string(
area.x + gutter_w as u16 - 1,
area.y + i as u16,
"│",
sep_style,
);
}
// Render Content
let mut current_visual_x = 0;
for span in &line.spans {
let text = span.content.as_ref();
let span_width = text.width();
// If span ends before the visible area, skip it
if current_visual_x + span_width <= self.scroll_col {
current_visual_x += span_width;
continue;
}
// If span starts after the visible area, we are done
if current_visual_x >= self.scroll_col + content_area.width as usize {
break;
}
let mut draw_text = text;
let draw_x = if current_visual_x < self.scroll_col {
// Partial overlap on left
let skip_width = self.scroll_col - current_visual_x;
let mut char_indices = text.char_indices();
let mut w = 0;
let mut start_byte = 0;
while w < skip_width {
if let Some((idx, c)) = char_indices.next() {
w += c.width().unwrap_or(0);
start_byte = idx + c.len_utf8();
} else {
break;
}
}
draw_text = &text[start_byte..];
content_area.x + (current_visual_x + w).saturating_sub(self.scroll_col) as u16
} else {
content_area.x + (current_visual_x - self.scroll_col) as u16
};
// Combine span style with base style to ensure visibility (fix black-on-black)
let mut combined_style = self.style.patch(span.style);
if combined_style.bg.is_none() || combined_style.bg == Some(base_bg) {
combined_style.bg = Some(line_bg);
}
buf.set_string(draw_x, area.y + i as u16, draw_text, combined_style);
current_visual_x += span_width;
}
// Apply Selection highlighting
if let Some(((s_row, s_col), (e_row, e_col))) = self.get_selection_range() {
if real_line_idx >= s_row && real_line_idx <= e_row {
for visual_x in self.scroll_col..(self.scroll_col + content_area.width as usize)
{
let byte_idx = self.get_byte_index_from_visual(line_idx, visual_x);
let is_selected = if real_line_idx > s_row && real_line_idx < e_row {
true
} else if real_line_idx == s_row && real_line_idx == e_row {
byte_idx >= s_col && byte_idx < e_col
} else if real_line_idx == s_row {
byte_idx >= s_col
} else if real_line_idx == e_row {
byte_idx < e_col
} else {
false
};
if is_selected {
let cx = content_area.x + (visual_x - self.scroll_col) as u16;
let cy = area.y + i as u16;
if let Some(cell) = buf.cell_mut((cx, cy)) {
cell.set_bg(Color::Rgb(40, 60, 100)); // Selection Blue
cell.set_fg(Color::White);
}
}
}
}
}
}
// Render Scrollbars
if scrollbar_w > 0 {
let sb = Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some("▲"))
.end_symbol(Some("▼"));
let mut ss = ScrollbarState::new(self.effective_len())
.position(self.scroll_row)
.viewport_content_length(area.height as usize);
StatefulWidget::render(sb, area, buf, &mut ss);
}
// Render Cursor
let cursor_visual_x = self.get_visual_x(self.cursor_row, self.cursor_col);
let cursor_screen_row = self.cursor_row as i16 - self.scroll_row as i16;
let cursor_screen_col = cursor_visual_x as i16 - self.scroll_col as i16;
if cursor_screen_row >= 0
&& cursor_screen_row < area.height as i16
&& cursor_screen_col >= 0
&& cursor_screen_col < content_area.width as i16
{
let cx = content_area.x + cursor_screen_col as u16;
let cy = area.y + cursor_screen_row as u16;
if let Some(cell) = buf.cell_mut((cx, cy)) {
if !cell.symbol().is_empty() && cell.symbol() != " " {
// Inverse video for character under cursor
cell.set_style(self.cursor_style);
} else {
// Block cursor for empty space
cell.set_style(self.cursor_style);
cell.set_symbol(" ");
}
}
}
}
}