strata-editor 0.1.2

Monaco-class interactive code editor and rich-text document engine for Strata UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
//! High-performance Code Editor controller with virtualized lines, syntax highlighting, and gutter.

use crate::buffer::LineBuffer;
use crate::cursor::{MultiCursor, Position, Selection};
use crate::diagnostics::{DiagnosticCollection, DiagnosticCounts, EditorDiagnostic};
use crate::history::HistoryStack;
use crate::ime::ImeComposition;
use crate::minimap::{minimap_y_to_scroll_top, MinimapConfig};
use crate::search::{FindReplaceState, SearchMatch};
use crate::syntax::{FoldRange, Language, SyntaxHighlighter, TokenType};
use crate::theme::EditorTheme;
use strata_dom::{Event, NodeBlueprint, Tag};

/// High-level code editor managing buffer state, multi-cursors, incremental syntax,
/// search/replace, folding, diagnostics, theming, and virtualized rendering.
#[derive(Debug, Clone)]
pub struct CodeEditor {
    buffer: LineBuffer,
    cursors: MultiCursor,
    history: HistoryStack,
    highlighter: SyntaxHighlighter,
    language: Language,
    folded_lines: Vec<usize>,
    cached_folds: Vec<FoldRange>,
    folds_valid: bool,
    ime: ImeComposition,
    tab_size: usize,
    wrap_column: Option<usize>,
    search: FindReplaceState,
    diagnostics: DiagnosticCollection,
    theme: EditorTheme,
    scroll_top: f32,
    scroll_left: f32,
    viewport_height: f32,
    line_height: f32,
    char_width: f32,
    read_only: bool,
    is_pointer_dragging: bool,
    edit_revision: u64,
}

impl CodeEditor {
    /// Constructs a new code editor with initial source code and language syntax.
    pub fn new(content: &str, language: Language) -> Self {
        let buffer = LineBuffer::from_str(content);
        let mut highlighter = SyntaxHighlighter::with_language(language);
        highlighter.parse_buffer(&buffer);
        let mut diagnostics = DiagnosticCollection::new();
        diagnostics.set_syntax_diagnostics(highlighter.collect_syntax_diagnostics(&buffer));
        Self {
            buffer,
            cursors: MultiCursor::new(),
            history: HistoryStack::default(),
            highlighter,
            language,
            folded_lines: Vec::new(),
            cached_folds: Vec::new(),
            folds_valid: false,
            ime: ImeComposition::new(),
            tab_size: 4,
            wrap_column: None,
            search: FindReplaceState::new(),
            diagnostics,
            theme: EditorTheme::default(),
            scroll_top: 0.0,
            scroll_left: 0.0,
            viewport_height: 480.0,
            line_height: 22.0,
            char_width: 8.4,
            read_only: false,
            is_pointer_dragging: false,
            edit_revision: 0,
        }
    }

    /// Monotonic counter incremented **only** when document buffer text actually mutates.
    /// Non-mutating shortcuts (`Ctrl+F`, `Ctrl+H`, `Ctrl+A`, `Ctrl+C`, navigation, folding)
    /// leave `edit_revision()` unchanged so `on_change` callbacks never fire falsely.
    pub fn edit_revision(&self) -> u64 {
        self.edit_revision
    }

    /// Synchronizes live syntax diagnostics, invalidates cached folds, and refreshes open search results.
    fn on_buffer_mutated(&mut self) {
        self.folds_valid = false;
        self.diagnostics
            .set_syntax_diagnostics(self.highlighter.collect_syntax_diagnostics(&self.buffer));
        if self.search.is_open && !self.search.query.is_empty() {
            self.search.recompute_matches(&self.buffer);
        }
    }

    /// Accesses the active syntax highlighter.
    pub fn highlighter(&self) -> &SyntaxHighlighter {
        &self.highlighter
    }

    /// Accesses the active syntax highlighter mutably.
    pub fn highlighter_mut(&mut self) -> &mut SyntaxHighlighter {
        &mut self.highlighter
    }

    /// Returns the active syntax language.
    pub fn language(&self) -> Language {
        self.language
    }

    /// Changes the syntax language and re-parses the AST directly from the rope buffer.
    pub fn set_language(&mut self, language: Language) {
        self.language = language;
        self.highlighter.set_language(language);
        self.highlighter.parse_buffer(&self.buffer);
        self.folded_lines.clear();
        self.on_buffer_mutated();
    }

    /// Re-parses the document incrementally with the active Tree-Sitter grammar via zero-copy rope slices.
    pub fn reparse(&mut self) {
        let total = self.buffer.line_count();
        self.highlighter
            .reparse_incremental_buffer(&self.buffer, 0, total);
        self.on_buffer_mutated();
    }

    /// Feeds one or more chronological `tree_sitter::InputEdit` records into the syntax
    /// highlighter and performs a localized incremental reparse directly from the rope.
    fn apply_syntax_edits_and_reparse(&mut self, edits: &[tree_sitter::InputEdit]) {
        if edits.is_empty() {
            return;
        }
        self.edit_revision = self.edit_revision.wrapping_add(1);
        let mut dirty_start = usize::MAX;
        let mut dirty_end = 0usize;
        for edit in edits {
            self.highlighter.edit(edit);
            dirty_start = dirty_start.min(edit.start_position.row);
            let row_delta =
                edit.new_end_position.row as isize - edit.old_end_position.row as isize;
            let shifted_end = (dirty_end as isize + row_delta).max(0) as usize;
            dirty_end = shifted_end.max(edit.new_end_position.row.saturating_add(1));
        }
        if dirty_start == usize::MAX {
            dirty_start = 0;
        }
        self.highlighter
            .reparse_incremental_buffer(&self.buffer, dirty_start, dirty_end);
        self.on_buffer_mutated();
    }

    /// Accesses the underlying line buffer.
    pub fn buffer(&self) -> &LineBuffer {
        &self.buffer
    }

    /// Accesses the undo/redo history stack.
    pub fn history(&self) -> &HistoryStack {
        &self.history
    }

    /// Accesses the active Find & Replace state.
    pub fn search(&self) -> &FindReplaceState {
        &self.search
    }

    /// Mutably accesses the active Find & Replace state.
    pub fn search_mut(&mut self) -> &mut FindReplaceState {
        &mut self.search
    }

    /// Accesses the diagnostics collection (live syntax + external LSP diagnostics).
    pub fn diagnostics(&self) -> &DiagnosticCollection {
        &self.diagnostics
    }

    /// Replaces external/LSP diagnostics while preserving live syntax diagnostics.
    pub fn set_external_diagnostics(&mut self, diagnostics: Vec<EditorDiagnostic>) {
        self.diagnostics.set_external(diagnostics);
    }

    /// Clears external/LSP diagnostics.
    pub fn clear_external_diagnostics(&mut self) {
        self.diagnostics.clear_external();
    }

    /// Returns diagnostic severity counts for status bars and badges.
    pub fn diagnostic_counts(&self) -> DiagnosticCounts {
        self.diagnostics.counts()
    }

    /// Accesses the active CSS-variable editor theme.
    pub fn theme(&self) -> &EditorTheme {
        &self.theme
    }

    /// Updates the active editor theme.
    pub fn set_theme(&mut self, theme: EditorTheme) {
        self.theme = theme;
    }

    pub fn read_only(&self) -> bool {
        self.read_only
    }

    pub fn set_read_only(&mut self, read_only: bool) {
        self.read_only = read_only;
    }

    pub fn scroll_top(&self) -> f32 {
        self.scroll_top
    }

    pub fn set_scroll_top(&mut self, scroll_top: f32) {
        let max_scroll = self.max_scroll_top();
        self.scroll_top = scroll_top.clamp(0.0, max_scroll);
    }

    pub fn scroll_left(&self) -> f32 {
        self.scroll_left
    }

    pub fn set_scroll_left(&mut self, scroll_left: f32) {
        self.scroll_left = scroll_left.max(0.0);
    }

    pub fn viewport_height(&self) -> f32 {
        self.viewport_height
    }

    pub fn set_viewport_height(&mut self, height: f32) {
        self.viewport_height = height.max(40.0);
        let max_scroll = self.max_scroll_top();
        self.scroll_top = self.scroll_top.clamp(0.0, max_scroll);
    }

    pub fn line_height(&self) -> f32 {
        self.line_height
    }

    pub fn set_line_height(&mut self, line_height: f32) {
        self.line_height = line_height.max(12.0);
    }

    pub fn char_width(&self) -> f32 {
        self.char_width
    }

    pub fn set_char_width(&mut self, char_width: f32) {
        self.char_width = char_width.max(4.0);
    }

    pub fn max_scroll_top(&self) -> f32 {
        let visible_rows = self.visible_logical_lines().len().max(1) as f32;
        (visible_rows * self.line_height - self.viewport_height).max(0.0)
    }

    /// Accesses the active cursors.
    pub fn cursors(&self) -> &MultiCursor {
        &self.cursors
    }

    /// Accesses the active cursors mutably.
    pub fn cursors_mut(&mut self) -> &mut MultiCursor {
        &mut self.cursors
    }

    /// Returns the active IME composition state.
    pub fn ime(&self) -> &ImeComposition {
        &self.ime
    }

    /// Mutably accesses the IME composition state.
    pub fn ime_mut(&mut self) -> &mut ImeComposition {
        &mut self.ime
    }

    /// Returns the configured tab size (in spaces).
    pub fn tab_size(&self) -> usize {
        self.tab_size
    }

    /// Configures tab size.
    pub fn set_tab_size(&mut self, size: usize) {
        self.tab_size = size;
    }

    /// Returns the soft-wrap column limit, if enabled.
    pub fn wrap_column(&self) -> Option<usize> {
        self.wrap_column
    }

    /// Configures soft-wrap column limit.
    pub fn set_wrap_column(&mut self, wrap: Option<usize>) {
        self.wrap_column = wrap;
    }

    /// Total number of lines in the buffer.
    pub fn line_count(&self) -> usize {
        self.buffer.line_count()
    }

    /// Serializes entire buffer content to a String.
    pub fn text(&self) -> String {
        self.buffer.to_string()
    }

    /// Replaces entire buffer content and resets history.
    pub fn set_text(&mut self, text: &str) {
        self.buffer = LineBuffer::from_str(text);
        self.cursors.set_single(Position::ZERO);
        self.history = HistoryStack::default();
        self.folded_lines.clear();
        self.scroll_top = 0.0;
        self.scroll_left = 0.0;
        self.highlighter.parse_buffer(&self.buffer);
        self.edit_revision = self.edit_revision.wrapping_add(1);
        self.on_buffer_mutated();
    }

    /// Inserts a character at all active cursors (or handles Enter with auto-indentation).
    pub fn insert_char(&mut self, ch: char) {
        if ch == '\n' {
            self.insert_newline();
        } else {
            self.insert_text(&ch.to_string());
        }
    }

    /// Inserts a newline with automatic indentation calculated from the syntax engine.
    pub fn insert_newline(&mut self) {
        self.cursors.normalize();
        let before_all = self.cursors.all().to_vec();
        let descending = self.cursors.selections_descending();

        let mut operations = Vec::with_capacity(descending.len() * 2);
        let mut syntax_edits = Vec::with_capacity(descending.len() * 2);
        let mut processed_carets: Vec<Position> = Vec::with_capacity(descending.len());

        for sel in descending {
            let (start, end) = sel.ordered_range();
            if start != end {
                let (del_edit, _) = self.buffer.compute_input_edit(start, end, "");
                let deleted = self
                    .buffer
                    .delete_range(start.line, start.col, end.line, end.col);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start,
                    end,
                    text: deleted,
                });
            }

            let indent_spaces = self
                .highlighter
                .compute_auto_indent(&self.buffer, start.line);
            let indent_str = if indent_spaces > 0 {
                " ".repeat(indent_spaces)
            } else {
                String::new()
            };
            let inserted_text = format!("\n{indent_str}");
            let (ins_edit, _) = self.buffer.compute_input_edit(start, start, &inserted_text);

            self.buffer.split_line(start.line, start.col);
            let new_line = start.line + 1;
            if !indent_str.is_empty() {
                self.buffer.insert_str(new_line, 0, &indent_str);
            }
            syntax_edits.push(ins_edit);

            operations.push(crate::history::EditOperation::Insert {
                pos: start,
                text: inserted_text,
            });

            let lines_slice: Vec<&str> = vec!["", &indent_str];
            let my_new_pos = Position::new(new_line, indent_spaces);

            for caret in &mut processed_carets {
                *caret = crate::cursor::shift_position_after_edit(*caret, start, end, &lines_slice);
            }

            processed_carets.push(my_new_pos);
        }

        let new_cursors: Vec<Selection> =
            processed_carets.into_iter().map(Selection::point).collect();

        self.cursors = MultiCursor::from_selections(new_cursors);

        self.history
            .push_transaction(crate::history::EditorTransaction::new(
                operations,
                before_all,
                self.cursors.all().to_vec(),
            ));

        self.apply_syntax_edits_and_reparse(&syntax_edits);
    }

    /// Inserts text at all cursor positions atomically in descending document order.
    pub fn insert_text(&mut self, text: &str) {
        if text.is_empty() {
            return;
        }

        self.cursors.normalize();
        let before_all = self.cursors.all().to_vec();
        let descending = self.cursors.selections_descending();

        let mut operations = Vec::with_capacity(descending.len());
        let mut syntax_edits = Vec::with_capacity(descending.len() * 2);
        let mut processed_carets: Vec<Position> = Vec::with_capacity(descending.len());

        let text_lines: Vec<&str> = text.split('\n').collect();

        for sel in descending {
            let (start, end) = sel.ordered_range();
            if start != end {
                let (del_edit, _) = self.buffer.compute_input_edit(start, end, "");
                let deleted = self
                    .buffer
                    .delete_range(start.line, start.col, end.line, end.col);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start,
                    end,
                    text: deleted,
                });
            }

            let (ins_edit, _) = self.buffer.compute_input_edit(start, start, text);
            self.buffer.insert_str(start.line, start.col, text);
            syntax_edits.push(ins_edit);
            operations.push(crate::history::EditOperation::Insert {
                pos: start,
                text: text.to_string(),
            });

            let my_new_pos = if text_lines.len() == 1 {
                Position::new(start.line, start.col + text_lines[0].chars().count())
            } else {
                Position::new(
                    start.line + text_lines.len() - 1,
                    text_lines.last().map_or(0, |l| l.chars().count()),
                )
            };

            for caret in &mut processed_carets {
                *caret = crate::cursor::shift_position_after_edit(*caret, start, end, &text_lines);
            }

            processed_carets.push(my_new_pos);
        }

        let new_cursors: Vec<Selection> =
            processed_carets.into_iter().map(Selection::point).collect();

        self.cursors = MultiCursor::from_selections(new_cursors);

        self.history
            .push_transaction(crate::history::EditorTransaction::new(
                operations,
                before_all,
                self.cursors.all().to_vec(),
            ));

        self.apply_syntax_edits_and_reparse(&syntax_edits);
    }

    /// Performs backspace deletion at all active cursors atomically in descending document order.
    pub fn delete_backwards(&mut self) {
        self.cursors.normalize();
        let before_all = self.cursors.all().to_vec();
        let descending = self.cursors.selections_descending();

        let mut operations = Vec::with_capacity(descending.len());
        let mut syntax_edits = Vec::with_capacity(descending.len());
        let mut processed_carets: Vec<Position> = Vec::with_capacity(descending.len());

        for sel in descending {
            let (start, end) = sel.ordered_range();
            if start != end {
                let (del_edit, _) = self.buffer.compute_input_edit(start, end, "");
                let deleted = self
                    .buffer
                    .delete_range(start.line, start.col, end.line, end.col);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start,
                    end,
                    text: deleted,
                });

                for caret in &mut processed_carets {
                    *caret = crate::cursor::shift_position_after_edit(*caret, start, end, &[""]);
                }

                processed_carets.push(start);
            } else if start.col > 0 {
                let del_start = Position::new(start.line, start.col - 1);
                let (del_edit, _) = self.buffer.compute_input_edit(del_start, start, "");
                let deleted =
                    self.buffer
                        .delete_range(del_start.line, del_start.col, start.line, start.col);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start: del_start,
                    end: start,
                    text: deleted,
                });

                for caret in &mut processed_carets {
                    *caret =
                        crate::cursor::shift_position_after_edit(*caret, del_start, start, &[""]);
                }

                processed_carets.push(del_start);
            } else if start.line > 0 {
                let prev_line = start.line - 1;
                let prev_len = self.buffer.line_len(prev_line);
                let del_start = Position::new(prev_line, prev_len);
                let del_end = Position::new(start.line, 0);
                let (del_edit, _) = self.buffer.compute_input_edit(del_start, del_end, "");

                self.buffer.join_line_with_next(prev_line);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start: del_start,
                    end: del_end,
                    text: "\n".to_string(),
                });

                for caret in &mut processed_carets {
                    *caret = crate::cursor::shift_position_after_edit(
                        *caret,
                        del_start,
                        del_end,
                        &[""],
                    );
                }

                processed_carets.push(del_start);
            } else {
                processed_carets.push(start);
            }
        }

        let new_cursors: Vec<Selection> =
            processed_carets.into_iter().map(Selection::point).collect();

        self.cursors = MultiCursor::from_selections(new_cursors);

        if !operations.is_empty() {
            self.history
                .push_transaction(crate::history::EditorTransaction::new(
                    operations,
                    before_all,
                    self.cursors.all().to_vec(),
                ));
            self.apply_syntax_edits_and_reparse(&syntax_edits);
        }
    }

    /// Performs forward Delete key deletion at all active cursors atomically.
    pub fn delete_forward(&mut self) {
        self.cursors.normalize();
        let before_all = self.cursors.all().to_vec();
        let descending = self.cursors.selections_descending();

        let mut operations = Vec::with_capacity(descending.len());
        let mut syntax_edits = Vec::with_capacity(descending.len());
        let mut processed_carets: Vec<Position> = Vec::with_capacity(descending.len());

        for sel in descending {
            let (start, end) = sel.ordered_range();
            if start != end {
                let (del_edit, _) = self.buffer.compute_input_edit(start, end, "");
                let deleted = self
                    .buffer
                    .delete_range(start.line, start.col, end.line, end.col);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start,
                    end,
                    text: deleted,
                });
                for caret in &mut processed_carets {
                    *caret = crate::cursor::shift_position_after_edit(*caret, start, end, &[""]);
                }
                processed_carets.push(start);
            } else {
                let line_len = self.buffer.line_len(start.line);
                if start.col < line_len {
                    let del_end = Position::new(start.line, start.col + 1);
                    let (del_edit, _) = self.buffer.compute_input_edit(start, del_end, "");
                    let deleted =
                        self.buffer
                            .delete_range(start.line, start.col, del_end.line, del_end.col);
                    syntax_edits.push(del_edit);
                    operations.push(crate::history::EditOperation::Delete {
                        start,
                        end: del_end,
                        text: deleted,
                    });
                    for caret in &mut processed_carets {
                        *caret = crate::cursor::shift_position_after_edit(
                            *caret,
                            start,
                            del_end,
                            &[""],
                        );
                    }
                    processed_carets.push(start);
                } else if start.line + 1 < self.buffer.line_count() {
                    let del_end = Position::new(start.line + 1, 0);
                    let (del_edit, _) = self.buffer.compute_input_edit(start, del_end, "");
                    self.buffer.join_line_with_next(start.line);
                    syntax_edits.push(del_edit);
                    operations.push(crate::history::EditOperation::Delete {
                        start,
                        end: del_end,
                        text: "\n".to_string(),
                    });
                    for caret in &mut processed_carets {
                        *caret = crate::cursor::shift_position_after_edit(
                            *caret,
                            start,
                            del_end,
                            &[""],
                        );
                    }
                    processed_carets.push(start);
                } else {
                    processed_carets.push(start);
                }
            }
        }

        let new_cursors: Vec<Selection> =
            processed_carets.into_iter().map(Selection::point).collect();
        self.cursors = MultiCursor::from_selections(new_cursors);

        if !operations.is_empty() {
            self.history
                .push_transaction(crate::history::EditorTransaction::new(
                    operations,
                    before_all,
                    self.cursors.all().to_vec(),
                ));
            self.apply_syntax_edits_and_reparse(&syntax_edits);
        }
    }

    /// Replaces an arbitrary `[start, end)` range with `replacement` as a single undoable
    /// transaction and drives an incremental Tree-Sitter / line-state update.
    /// Returns the resulting end `Position` of `replacement`.
    pub fn replace_range(
        &mut self,
        start: Position,
        end: Position,
        replacement: &str,
    ) -> Position {
        let (s, e) = if start <= end {
            (start, end)
        } else {
            (end, start)
        };
        let before_all = self.cursors.all().to_vec();
        let mut operations = Vec::with_capacity(2);
        let mut syntax_edits = Vec::with_capacity(2);

        if s != e {
            let (del_edit, _) = self.buffer.compute_input_edit(s, e, "");
            let deleted = self.buffer.delete_range(s.line, s.col, e.line, e.col);
            syntax_edits.push(del_edit);
            operations.push(crate::history::EditOperation::Delete {
                start: s,
                end: e,
                text: deleted,
            });
        }

        let new_end = if !replacement.is_empty() {
            let (ins_edit, end_pos) = self.buffer.compute_input_edit(s, s, replacement);
            self.buffer.insert_str(s.line, s.col, replacement);
            syntax_edits.push(ins_edit);
            operations.push(crate::history::EditOperation::Insert {
                pos: s,
                text: replacement.to_string(),
            });
            end_pos
        } else {
            s
        };

        self.cursors.set_single(new_end);
        if !operations.is_empty() {
            self.history
                .push_transaction(crate::history::EditorTransaction::new(
                    operations,
                    before_all,
                    self.cursors.all().to_vec(),
                ));
            self.apply_syntax_edits_and_reparse(&syntax_edits);
        }
        new_end
    }

    /// Atomically replaces multiple disjoint `(start, end, replacement)` ranges in descending
    /// document order as a single undo transaction and a single incremental AST reparse.
    pub fn replace_ranges_atomic(&mut self, mut replacements: Vec<(Position, Position, String)>) {
        if replacements.is_empty() {
            return;
        }
        // Normalize and sort descending so earlier offsets remain valid as later ones change.
        for item in &mut replacements {
            if item.0 > item.1 {
                std::mem::swap(&mut item.0, &mut item.1);
            }
        }
        replacements.sort_by_key(|item| std::cmp::Reverse(item.0));

        let before_all = self.cursors.all().to_vec();
        let mut operations = Vec::with_capacity(replacements.len() * 2);
        let mut syntax_edits = Vec::with_capacity(replacements.len() * 2);
        let mut last_pos = Position::ZERO;

        for (s, e, repl) in replacements {
            if s != e {
                let (del_edit, _) = self.buffer.compute_input_edit(s, e, "");
                let deleted = self.buffer.delete_range(s.line, s.col, e.line, e.col);
                syntax_edits.push(del_edit);
                operations.push(crate::history::EditOperation::Delete {
                    start: s,
                    end: e,
                    text: deleted,
                });
            }
            if !repl.is_empty() {
                let (ins_edit, end_pos) = self.buffer.compute_input_edit(s, s, &repl);
                self.buffer.insert_str(s.line, s.col, &repl);
                syntax_edits.push(ins_edit);
                operations.push(crate::history::EditOperation::Insert { pos: s, text: repl });
                last_pos = end_pos;
            } else {
                last_pos = s;
            }
        }

        self.cursors.set_single(last_pos);
        if !operations.is_empty() {
            self.history
                .push_transaction(crate::history::EditorTransaction::new(
                    operations,
                    before_all,
                    self.cursors.all().to_vec(),
                ));
            self.apply_syntax_edits_and_reparse(&syntax_edits);
        }
    }

    /// Undoes the last recorded transaction with incremental AST synchronization.
    pub fn undo(&mut self) {
        if let Some((restored, edits)) = self.history.undo_with_edits(&mut self.buffer) {
            self.cursors = MultiCursor::from_selections(restored);
            self.apply_syntax_edits_and_reparse(&edits);
        }
    }

    /// Redoes the last undone transaction with incremental AST synchronization.
    pub fn redo(&mut self) {
        if let Some((restored, edits)) = self.history.redo_with_edits(&mut self.buffer) {
            self.cursors = MultiCursor::from_selections(restored);
            self.apply_syntax_edits_and_reparse(&edits);
        }
    }

    /// Moves all active cursors left by 1 character.
    pub fn move_left(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut new_cursors = Vec::with_capacity(before.len());

        for sel in &before {
            let focus = sel.focus;
            let new_focus = if focus.col > 0 {
                Position::new(focus.line, focus.col - 1)
            } else if focus.line > 0 {
                let prev_line = focus.line - 1;
                let prev_len = self.buffer.line_len(prev_line);
                Position::new(prev_line, prev_len)
            } else {
                Position::ZERO
            };

            let anchor = if select { sel.anchor } else { new_focus };
            new_cursors.push(Selection::range(anchor, new_focus));
        }

        self.cursors = MultiCursor::from_selections(new_cursors);
    }

    /// Moves all active cursors right by 1 character.
    pub fn move_right(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut new_cursors = Vec::with_capacity(before.len());

        for sel in &before {
            let focus = sel.focus;
            let cur_line_len = self.buffer.line_len(focus.line);

            let new_focus = if focus.col < cur_line_len {
                Position::new(focus.line, focus.col + 1)
            } else if focus.line + 1 < self.buffer.line_count() {
                Position::new(focus.line + 1, 0)
            } else {
                Position::new(focus.line, cur_line_len)
            };

            let anchor = if select { sel.anchor } else { new_focus };
            new_cursors.push(Selection::range(anchor, new_focus));
        }

        self.cursors = MultiCursor::from_selections(new_cursors);
    }

    /// Moves all active cursors up by 1 line.
    pub fn move_up(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut new_cursors = Vec::with_capacity(before.len());

        for sel in &before {
            let focus = sel.focus;
            let new_focus = if focus.line > 0 {
                let prev_line = focus.line - 1;
                let prev_len = self.buffer.line_len(prev_line);
                Position::new(prev_line, focus.col.min(prev_len))
            } else {
                Position::new(0, 0)
            };

            let anchor = if select { sel.anchor } else { new_focus };
            new_cursors.push(Selection::range(anchor, new_focus));
        }

        self.cursors = MultiCursor::from_selections(new_cursors);
    }

    /// Moves all active cursors down by 1 line.
    pub fn move_down(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut new_cursors = Vec::with_capacity(before.len());

        for sel in &before {
            let focus = sel.focus;
            let new_focus = if focus.line + 1 < self.buffer.line_count() {
                let next_line = focus.line + 1;
                let next_len = self.buffer.line_len(next_line);
                Position::new(next_line, focus.col.min(next_len))
            } else {
                let last_len = self.buffer.line_len(focus.line);
                Position::new(focus.line, last_len)
            };

            let anchor = if select { sel.anchor } else { new_focus };
            new_cursors.push(Selection::range(anchor, new_focus));
        }

        self.cursors = MultiCursor::from_selections(new_cursors);
    }

    /// Moves all active cursors to the start of their respective line.
    pub fn move_to_line_start(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut new_cursors = Vec::with_capacity(before.len());

        for sel in &before {
            let new_focus = Position::new(sel.focus.line, 0);
            let anchor = if select { sel.anchor } else { new_focus };
            new_cursors.push(Selection::range(anchor, new_focus));
        }

        self.cursors = MultiCursor::from_selections(new_cursors);
    }

    /// Moves all active cursors to the end of their respective line.
    pub fn move_to_line_end(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut new_cursors = Vec::with_capacity(before.len());

        for sel in &before {
            let line_len = self.buffer.line_len(sel.focus.line);
            let new_focus = Position::new(sel.focus.line, line_len);
            let anchor = if select { sel.anchor } else { new_focus };
            new_cursors.push(Selection::range(anchor, new_focus));
        }

        self.cursors = MultiCursor::from_selections(new_cursors);
    }

    /// Selects all text in the document.
    pub fn select_all(&mut self) {
        let total_lines = self.buffer.line_count();
        let last_line = total_lines.saturating_sub(1);
        let last_col = self.buffer.line_len(last_line);
        self.cursors.set_single(Position::ZERO);
        self.cursors.all_mut()[0] =
            Selection::range(Position::ZERO, Position::new(last_line, last_col));
    }

    /// Ensures structural fold ranges are computed and cached for the current buffer state.
    pub fn ensure_folds_cached(&mut self) -> &[FoldRange] {
        if !self.folds_valid {
            self.cached_folds = self.highlighter.compute_fold_ranges(&self.buffer);
            self.folds_valid = true;
            // Prune stale folded line indices that no longer correspond to a fold start.
            self.folded_lines.retain(|&line| {
                self.cached_folds.iter().any(|f| f.start_line == line)
            });
        }
        &self.cached_folds
    }

    /// Returns all structural fold ranges for the current document (using cache when valid).
    pub fn fold_ranges(&self) -> Vec<FoldRange> {
        if self.folds_valid {
            self.cached_folds.clone()
        } else {
            self.highlighter.compute_fold_ranges(&self.buffer)
        }
    }

    /// Toggles code folding on the specified line.
    pub fn toggle_fold(&mut self, line: usize) {
        if let Some(pos) = self.folded_lines.iter().position(|&l| l == line) {
            self.folded_lines.remove(pos);
        } else {
            self.folded_lines.push(line);
        }
    }

    /// Toggles folding at `line` only if `line` starts a foldable range; returns `true` if toggled.
    pub fn toggle_fold_at(&mut self, line: usize) -> bool {
        let is_fold_start = self
            .ensure_folds_cached()
            .iter()
            .any(|f| f.start_line == line && f.end_line > f.start_line);
        if !is_fold_start {
            return false;
        }
        self.toggle_fold(line);
        true
    }

    /// Collapses every foldable block in the document.
    pub fn fold_all(&mut self) {
        let starts: Vec<usize> = self
            .ensure_folds_cached()
            .iter()
            .filter(|f| f.end_line > f.start_line)
            .map(|f| f.start_line)
            .collect();
        self.folded_lines = starts;
        self.folded_lines.sort_unstable();
        self.folded_lines.dedup();
    }

    /// Expands all folded regions in the document.
    pub fn unfold_all(&mut self) {
        self.folded_lines.clear();
    }

    /// Checks if the line is currently folded.
    pub fn is_folded(&self, line: usize) -> bool {
        self.folded_lines.contains(&line)
    }

    /// If `line` is currently folded, returns the number of hidden lines beneath it.
    pub fn folded_hidden_count(&self, line: usize) -> Option<usize> {
        if !self.is_folded(line) {
            return None;
        }
        let folds = self.fold_ranges();
        folds
            .iter()
            .filter(|f| f.start_line == line && f.end_line > line)
            .map(|f| f.end_line - f.start_line)
            .max()
    }

    /// Returns the ordered list of visible 0-indexed logical line numbers after skipping
    /// lines concealed inside collapsed fold regions.
    pub fn visible_logical_lines(&self) -> Vec<usize> {
        let total = self.buffer.line_count();
        if self.folded_lines.is_empty() {
            return (0..total).collect();
        }
        let folds = self.fold_ranges();
        let mut collapsed_spans: Vec<(usize, usize)> = folds
            .iter()
            .filter(|f| self.folded_lines.contains(&f.start_line) && f.end_line > f.start_line)
            .map(|f| (f.start_line + 1, f.end_line.min(total.saturating_sub(1))))
            .collect();
        if collapsed_spans.is_empty() {
            return (0..total).collect();
        }
        collapsed_spans.sort_unstable_by_key(|&(s, _)| s);

        let mut visible = Vec::with_capacity(total);
        let mut line = 0usize;
        let mut span_idx = 0usize;
        while line < total {
            while span_idx < collapsed_spans.len() && collapsed_spans[span_idx].1 < line {
                span_idx += 1;
            }
            if span_idx < collapsed_spans.len()
                && line >= collapsed_spans[span_idx].0
                && line <= collapsed_spans[span_idx].1
            {
                line = collapsed_spans[span_idx].1 + 1;
                continue;
            }
            visible.push(line);
            line += 1;
        }
        visible
    }

    /// Maps a 0-indexed logical line number to its visible display row index (or `None` if hidden inside a fold).
    pub fn logical_to_display_row(&self, logical_line: usize) -> Option<usize> {
        if self.folded_lines.is_empty() {
            return (logical_line < self.buffer.line_count()).then_some(logical_line);
        }
        let visible = self.visible_logical_lines();
        visible.binary_search(&logical_line).ok()
    }

    /// Maps a visible display row index back to its 0-indexed logical line number.
    pub fn display_row_to_logical(&self, display_row: usize) -> Option<usize> {
        if self.folded_lines.is_empty() {
            return (display_row < self.buffer.line_count()).then_some(display_row);
        }
        let visible = self.visible_logical_lines();
        visible.get(display_row).copied()
    }

    // -------------------------------------------------------------------------
    // Find & Replace Controller Delegation
    // -------------------------------------------------------------------------

    pub fn open_find(&mut self) {
        self.search.open_find();
        if !self.search.query.is_empty() {
            self.search.recompute_matches(&self.buffer);
        }
    }

    pub fn open_replace(&mut self) {
        self.search.open_replace();
        if !self.search.query.is_empty() {
            self.search.recompute_matches(&self.buffer);
        }
    }

    pub fn close_find(&mut self) {
        self.search.close();
    }

    pub fn set_find_query(&mut self, query: impl Into<String>) {
        self.search.set_query(query, &self.buffer);
        if let Some(m) = self.search.active_match().cloned() {
            self.select_search_match(&m);
        }
    }

    pub fn set_replace_text(&mut self, replacement: impl Into<String>) {
        self.search.set_replacement(replacement);
    }

    pub fn find_next(&mut self) -> Option<SearchMatch> {
        if self.search.matches.is_empty() && !self.search.query.is_empty() {
            self.search.recompute_matches(&self.buffer);
        }
        let m = self.search.find_next()?;
        self.select_search_match(&m);
        Some(m)
    }

    pub fn find_prev(&mut self) -> Option<SearchMatch> {
        if self.search.matches.is_empty() && !self.search.query.is_empty() {
            self.search.recompute_matches(&self.buffer);
        }
        let m = self.search.find_prev()?;
        self.select_search_match(&m);
        Some(m)
    }

    pub fn replace_current(&mut self) -> bool {
        if self.read_only {
            return false;
        }
        let mut search = std::mem::take(&mut self.search);
        let replaced = search.replace_current(self);
        self.search = search;
        if let Some(m) = self.search.active_match().cloned() {
            self.select_search_match(&m);
        }
        replaced
    }

    pub fn replace_all(&mut self) -> usize {
        if self.read_only {
            return 0;
        }
        let mut search = std::mem::take(&mut self.search);
        let count = search.replace_all(self);
        self.search = search;
        count
    }

    fn select_search_match(&mut self, m: &SearchMatch) {
        self.cursors.set_single(m.start);
        self.cursors.all_mut()[0] = Selection::range(m.start, m.end);
        self.ensure_line_visible(m.start.line);
    }

    // -------------------------------------------------------------------------
    // Monaco-Class Editing, Navigation, Scrolling & Input Event Handlers
    // -------------------------------------------------------------------------

    /// Toggles single-line comments (`//` or `#`) across all lines touched by active selections.
    pub fn toggle_line_comment(&mut self) {
        if self.read_only {
            return;
        }
        let Some(prefix) = self.language.line_comment_prefix() else {
            return;
        };
        let mut lines: Vec<usize> = Vec::new();
        for sel in self.cursors.all() {
            let (s, e) = sel.ordered_range();
            for line in s.line..=e.line {
                if !lines.contains(&line) {
                    lines.push(line);
                }
            }
        }
        if lines.is_empty() {
            return;
        }
        lines.sort_unstable();

        let all_commented = lines.iter().all(|&l| {
            self.buffer
                .line(l)
                .map(|txt| {
                    let t = txt.trim_start();
                    t.is_empty() || t.starts_with(prefix)
                })
                .unwrap_or(true)
        });

        let mut edits = Vec::new();
        let prefix_chars = prefix.chars().count();
        for line in lines {
            let Some(txt) = self.buffer.line(line) else {
                continue;
            };
            let indent = txt.chars().take_while(|c| *c == ' ' || *c == '\t').count();
            if all_commented {
                let after_indent: String = txt.chars().skip(indent).collect();
                if let Some(rest) = after_indent.strip_prefix(prefix) {
                    let remove_len = if rest.starts_with(' ') {
                        prefix_chars + 1
                    } else {
                        prefix_chars
                    };
                    edits.push((
                        Position::new(line, indent),
                        Position::new(line, indent + remove_len),
                        String::new(),
                    ));
                }
            } else {
                edits.push((
                    Position::new(line, indent),
                    Position::new(line, indent),
                    format!("{prefix} "),
                ));
            }
        }
        self.replace_ranges_atomic(edits);
    }

    /// Indents selected lines by `tab_size` spaces (or inserts spaces at collapsed carets).
    pub fn indent_selection(&mut self) {
        if self.read_only {
            return;
        }
        let spaces = " ".repeat(self.tab_size.max(1));
        let has_multiline = self
            .cursors
            .all()
            .iter()
            .any(|s| s.anchor.line != s.focus.line);
        if !has_multiline && self.cursors.all().iter().all(|s| s.is_collapsed()) {
            self.insert_text(&spaces);
            return;
        }

        let mut lines = Vec::new();
        for sel in self.cursors.all() {
            let (s, e) = sel.ordered_range();
            for line in s.line..=e.line {
                if !lines.contains(&line) {
                    lines.push(line);
                }
            }
        }
        let edits: Vec<(Position, Position, String)> = lines
            .into_iter()
            .map(|l| (Position::new(l, 0), Position::new(l, 0), spaces.clone()))
            .collect();
        self.replace_ranges_atomic(edits);
    }

    /// Outdents selected lines by removing up to `tab_size` leading spaces (or one leading `\t`).
    pub fn outdent_selection(&mut self) {
        if self.read_only {
            return;
        }
        let mut lines = Vec::new();
        for sel in self.cursors.all() {
            let (s, e) = sel.ordered_range();
            for line in s.line..=e.line {
                if !lines.contains(&line) {
                    lines.push(line);
                }
            }
        }
        let mut edits = Vec::new();
        for line in lines {
            let Some(txt) = self.buffer.line(line) else {
                continue;
            };
            if txt.starts_with('\t') {
                edits.push((Position::new(line, 0), Position::new(line, 1), String::new()));
            } else {
                let leading_spaces = txt.chars().take_while(|&c| c == ' ').count();
                let remove = leading_spaces.min(self.tab_size.max(1));
                if remove > 0 {
                    edits.push((
                        Position::new(line, 0),
                        Position::new(line, remove),
                        String::new(),
                    ));
                }
            }
        }
        self.replace_ranges_atomic(edits);
    }

    /// Moves all cursors left by one word boundary.
    pub fn move_word_left(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut next = Vec::with_capacity(before.len());
        for sel in &before {
            let focus = sel.focus;
            let new_focus = if focus.col == 0 {
                if focus.line > 0 {
                    let prev = focus.line - 1;
                    Position::new(prev, self.buffer.line_len(prev))
                } else {
                    Position::ZERO
                }
            } else if let Some(line_str) = self.buffer.line(focus.line) {
                let chars: Vec<char> = line_str.chars().collect();
                let mut col = focus.col.min(chars.len());
                while col > 0 && chars[col - 1].is_whitespace() {
                    col -= 1;
                }
                if col > 0 {
                    let is_word = is_word_char(chars[col - 1]);
                    while col > 0 && is_word_char(chars[col - 1]) == is_word && !chars[col - 1].is_whitespace() {
                        col -= 1;
                    }
                }
                Position::new(focus.line, col)
            } else {
                Position::new(focus.line, 0)
            };
            let anchor = if select { sel.anchor } else { new_focus };
            next.push(Selection::range(anchor, new_focus));
        }
        self.cursors = MultiCursor::from_selections(next);
    }

    /// Moves all cursors right by one word boundary.
    pub fn move_word_right(&mut self, select: bool) {
        let before = self.cursors.all().to_vec();
        let mut next = Vec::with_capacity(before.len());
        for sel in &before {
            let focus = sel.focus;
            let cur_len = self.buffer.line_len(focus.line);
            let new_focus = if focus.col >= cur_len {
                if focus.line + 1 < self.buffer.line_count() {
                    Position::new(focus.line + 1, 0)
                } else {
                    Position::new(focus.line, cur_len)
                }
            } else if let Some(line_str) = self.buffer.line(focus.line) {
                let chars: Vec<char> = line_str.chars().collect();
                let mut col = focus.col.min(chars.len());
                while col < chars.len() && chars[col].is_whitespace() {
                    col += 1;
                }
                if col < chars.len() {
                    let is_word = is_word_char(chars[col]);
                    while col < chars.len() && is_word_char(chars[col]) == is_word && !chars[col].is_whitespace() {
                        col += 1;
                    }
                }
                Position::new(focus.line, col)
            } else {
                Position::new(focus.line, cur_len)
            };
            let anchor = if select { sel.anchor } else { new_focus };
            next.push(Selection::range(anchor, new_focus));
        }
        self.cursors = MultiCursor::from_selections(next);
    }

    /// Moves cursors up by one viewport page.
    pub fn page_up(&mut self, select: bool) {
        let step = ((self.viewport_height / self.line_height.max(1.0)).floor() as usize).max(1);
        let before = self.cursors.all().to_vec();
        let mut next = Vec::with_capacity(before.len());
        for sel in &before {
            let target_line = sel.focus.line.saturating_sub(step);
            let target_col = sel.focus.col.min(self.buffer.line_len(target_line));
            let new_focus = Position::new(target_line, target_col);
            let anchor = if select { sel.anchor } else { new_focus };
            next.push(Selection::range(anchor, new_focus));
        }
        self.cursors = MultiCursor::from_selections(next);
        self.ensure_cursor_visible();
    }

    /// Moves cursors down by one viewport page.
    pub fn page_down(&mut self, select: bool) {
        let step = ((self.viewport_height / self.line_height.max(1.0)).floor() as usize).max(1);
        let max_line = self.buffer.line_count().saturating_sub(1);
        let before = self.cursors.all().to_vec();
        let mut next = Vec::with_capacity(before.len());
        for sel in &before {
            let target_line = (sel.focus.line + step).min(max_line);
            let target_col = sel.focus.col.min(self.buffer.line_len(target_line));
            let new_focus = Position::new(target_line, target_col);
            let anchor = if select { sel.anchor } else { new_focus };
            next.push(Selection::range(anchor, new_focus));
        }
        self.cursors = MultiCursor::from_selections(next);
        self.ensure_cursor_visible();
    }

    /// Selects the identifier/word surrounding `pos` (used on double-click).
    pub fn select_word_at(&mut self, pos: Position) {
        let line = pos.line.min(self.buffer.line_count().saturating_sub(1));
        let Some(line_str) = self.buffer.line(line) else {
            return;
        };
        let chars: Vec<char> = line_str.chars().collect();
        if chars.is_empty() {
            self.cursors.set_single(Position::new(line, 0));
            return;
        }
        let idx = pos.col.min(chars.len().saturating_sub(1));
        let target_is_word = is_word_char(chars[idx]);
        let mut start = idx;
        while start > 0 && is_word_char(chars[start - 1]) == target_is_word && !chars[start - 1].is_whitespace() {
            start -= 1;
        }
        let mut end = idx + 1;
        while end < chars.len() && is_word_char(chars[end]) == target_is_word && !chars[end].is_whitespace() {
            end += 1;
        }
        self.cursors.set_single(Position::new(line, start));
        self.cursors.all_mut()[0] =
            Selection::range(Position::new(line, start), Position::new(line, end));
    }

    /// Selects the entire logical line (used on triple-click).
    pub fn select_line(&mut self, line: usize) {
        let clamped = line.min(self.buffer.line_count().saturating_sub(1));
        let len = self.buffer.line_len(clamped);
        self.cursors.set_single(Position::new(clamped, 0));
        self.cursors.all_mut()[0] =
            Selection::range(Position::new(clamped, 0), Position::new(clamped, len));
    }

    /// Scrolls the viewport vertically by `delta_y` pixels.
    pub fn scroll_by(&mut self, delta_y: f32) {
        self.set_scroll_top(self.scroll_top + delta_y);
    }

    /// Scrolls so that `logical_line` is positioned near the top of the viewport.
    pub fn scroll_to_line(&mut self, logical_line: usize) {
        if let Some(display_row) = self.logical_to_display_row(logical_line) {
            self.set_scroll_top(display_row as f32 * self.line_height);
        }
    }

    /// Adjusts `scroll_top` minimally so `logical_line` sits inside the visible viewport.
    pub fn ensure_line_visible(&mut self, logical_line: usize) {
        let Some(display_row) = self.logical_to_display_row(logical_line) else {
            return;
        };
        let row_top = display_row as f32 * self.line_height;
        let row_bottom = row_top + self.line_height;
        if row_top < self.scroll_top {
            self.set_scroll_top(row_top);
        } else if row_bottom > self.scroll_top + self.viewport_height {
            self.set_scroll_top(row_bottom - self.viewport_height);
        }
    }

    /// Scrolls minimally to keep the primary caret visible.
    pub fn ensure_cursor_visible(&mut self) {
        let line = self.cursors.primary().focus.line;
        self.ensure_line_visible(line);
    }

    /// Maps an editor-local pointer `(rel_x, rel_y)` to a document [`Position`].
    pub fn position_from_point(&self, rel_x: f32, rel_y: f32, gutter_width: f32) -> Position {
        let display_row = ((rel_y + self.scroll_top).max(0.0) / self.line_height.max(1.0)).floor() as usize;
        let visible = self.visible_logical_lines();
        let line = if visible.is_empty() {
            0
        } else {
            visible[display_row.min(visible.len() - 1)]
        };
        let code_x = (rel_x - gutter_width + self.scroll_left).max(0.0);
        let raw_col = (code_x / self.char_width.max(1.0)).round() as usize;
        let max_col = self.buffer.line_len(line);
        Position::new(line, raw_col.min(max_col))
    }

    /// Handles pointer press inside the code viewport (click, Shift+click, Alt+click multi-cursor,
    /// double-click word selection, triple-click line selection).
    pub fn handle_pointer_down(
        &mut self,
        rel_x: f32,
        rel_y: f32,
        detail: i32,
        alt_key: bool,
        shift_key: bool,
        gutter_width: f32,
    ) {
        let pos = self.position_from_point(rel_x, rel_y, gutter_width);
        self.is_pointer_dragging = true;
        if detail >= 3 {
            self.select_line(pos.line);
        } else if detail == 2 {
            self.select_word_at(pos);
        } else if alt_key {
            self.cursors.add_cursor(pos);
        } else if shift_key {
            let anchor = self.cursors.primary().anchor;
            self.cursors.set_single(anchor);
            self.cursors.all_mut()[0] = Selection::range(anchor, pos);
        } else {
            self.cursors.set_single(pos);
        }
    }

    /// Extends the active primary selection while dragging the pointer.
    pub fn handle_pointer_drag(&mut self, rel_x: f32, rel_y: f32, gutter_width: f32) {
        if !self.is_pointer_dragging {
            return;
        }
        let pos = self.position_from_point(rel_x, rel_y, gutter_width);
        let anchor = self.cursors.primary().anchor;
        self.cursors.set_single(anchor);
        self.cursors.all_mut()[0] = Selection::range(anchor, pos);
    }

    /// Ends a pointer drag gesture.
    pub fn handle_pointer_up(&mut self) {
        self.is_pointer_dragging = false;
    }

    /// Handles wheel scrolling over the editor viewport.
    pub fn handle_wheel(&mut self, delta_y: f32) {
        self.scroll_by(delta_y);
    }

    /// Handles click or drag inside the Canvas2D minimap to jump/scrub `scroll_top`.
    pub fn handle_minimap_pointer(
        &mut self,
        pointer_y: f32,
        minimap_height: f32,
        config: &MinimapConfig,
    ) {
        let target = minimap_y_to_scroll_top(
            pointer_y,
            minimap_height,
            self.visible_logical_lines().len().max(1),
            self.viewport_height,
            self.line_height,
            config,
        );
        self.set_scroll_top(target);
    }

    /// Copies the active selection(s) to Strata's unified clipboard and returns the copied string.
    /// When all cursors are collapsed, copies the primary cursor's full logical line (Monaco/VS Code parity).
    pub fn copy_selection(&self) -> Option<String> {
        let mut parts = Vec::new();
        for &sel in self.cursors.all() {
            if !sel.is_collapsed() {
                let payload = crate::clipboard::ClipboardBridge::from_selection(&self.buffer, sel);
                parts.push(payload.plain_text);
            }
        }
        let copied = if !parts.is_empty() {
            parts.join("\n")
        } else {
            let line = self.cursors.primary().focus.line;
            let text = self.buffer.line(line)?;
            format!("{text}\n")
        };
        strata_dom::clipboard::set_clipboard_text(&copied);
        Some(copied)
    }

    /// Cuts the active selection(s) (or primary logical line when collapsed) to Strata's unified clipboard.
    pub fn cut_selection(&mut self) -> Option<String> {
        if self.read_only {
            return None;
        }
        let copied = self.copy_selection()?;
        let has_selection = self.cursors.all().iter().any(|s| !s.is_collapsed());
        if has_selection {
            let edits: Vec<(Position, Position, String)> = self
                .cursors
                .all()
                .iter()
                .filter(|s| !s.is_collapsed())
                .map(|s| {
                    let (start, end) = s.ordered_range();
                    (start, end, String::new())
                })
                .collect();
            self.replace_ranges_atomic(edits);
        } else {
            let line = self.cursors.primary().focus.line;
            let line_count = self.buffer.line_count();
            let cur_len = self.buffer.line_len(line);
            if line + 1 < line_count {
                self.replace_range(Position::new(line, 0), Position::new(line + 1, 0), "");
            } else if line > 0 {
                let prev_len = self.buffer.line_len(line - 1);
                self.replace_range(
                    Position::new(line - 1, prev_len),
                    Position::new(line, cur_len),
                    "",
                );
            } else if cur_len > 0 {
                self.replace_range(Position::new(0, 0), Position::new(0, cur_len), "");
            }
        }
        Some(copied)
    }

    /// Pastes UTF-8 text (normalizing CRLF/CR line endings) at all active cursors.
    pub fn paste_text(&mut self, text: &str) -> bool {
        if self.read_only || text.is_empty() {
            return false;
        }
        let normalized = text.replace("\r\n", "\n").replace('\r', "\n");
        if normalized.is_empty() {
            return false;
        }
        self.insert_text(&normalized);
        true
    }

    /// Handles a W3C DOM `EventType::Input` event (e.g. atomic multi-character `ImeCommit`).
    pub fn handle_input_event(&mut self, e: &Event) -> bool {
        if self.read_only {
            return false;
        }
        let text = if !e.value.is_empty() {
            e.value.as_str()
        } else if let Some(ref data) = e.data {
            data.as_str()
        } else {
            ""
        };
        if text.is_empty() {
            return false;
        }
        if self.paste_text(text) {
            self.ensure_cursor_visible();
            true
        } else {
            false
        }
    }

    /// Handles a W3C-compatible Strata [`Event`] keydown with full Monaco keybindings.
    /// Returns `true` if the event was handled and mutated editor state.
    pub fn handle_key_event(&mut self, e: &Event) -> bool {
        let cmd = e.ctrl_key || e.meta_key;
        let key = e.key.as_str();

        if cmd {
            match key {
                "f" | "F" => {
                    self.open_find();
                    return true;
                }
                "h" | "H" => {
                    self.open_replace();
                    return true;
                }
                "a" | "A" => {
                    self.select_all();
                    return true;
                }
                "c" | "C" => {
                    let _ = self.copy_selection();
                    return true;
                }
                "x" | "X" => {
                    if !self.read_only {
                        let _ = self.cut_selection();
                        self.ensure_cursor_visible();
                    }
                    return true;
                }
                "v" | "V" => {
                    if !self.read_only {
                        if let Some(clip) = strata_dom::clipboard::get_clipboard_text() {
                            if self.paste_text(&clip) {
                                self.ensure_cursor_visible();
                            }
                        }
                    }
                    return true;
                }
                "z" | "Z" => {
                    if !self.read_only {
                        if e.shift_key {
                            self.redo();
                        } else {
                            self.undo();
                        }
                        self.ensure_cursor_visible();
                    }
                    return true;
                }
                "y" | "Y" => {
                    if !self.read_only {
                        self.redo();
                        self.ensure_cursor_visible();
                    }
                    return true;
                }
                "/" => {
                    self.toggle_line_comment();
                    return true;
                }
                "[" | "{" if e.shift_key => {
                    let line = self.cursors.primary().focus.line;
                    self.toggle_fold_at(line);
                    return true;
                }
                "]" | "}" if e.shift_key => {
                    let line = self.cursors.primary().focus.line;
                    if self.is_folded(line) {
                        self.toggle_fold(line);
                    }
                    return true;
                }
                "ArrowLeft" => {
                    self.move_word_left(e.shift_key);
                    self.ensure_cursor_visible();
                    return true;
                }
                "ArrowRight" => {
                    self.move_word_right(e.shift_key);
                    self.ensure_cursor_visible();
                    return true;
                }
                "Home" => {
                    let anchor = if e.shift_key {
                        self.cursors.primary().anchor
                    } else {
                        Position::ZERO
                    };
                    self.cursors.set_single(Position::ZERO);
                    self.cursors.all_mut()[0] = Selection::range(anchor, Position::ZERO);
                    self.ensure_cursor_visible();
                    return true;
                }
                "End" => {
                    let last_line = self.buffer.line_count().saturating_sub(1);
                    let end = Position::new(last_line, self.buffer.line_len(last_line));
                    let anchor = if e.shift_key {
                        self.cursors.primary().anchor
                    } else {
                        end
                    };
                    self.cursors.set_single(end);
                    self.cursors.all_mut()[0] = Selection::range(anchor, end);
                    self.ensure_cursor_visible();
                    return true;
                }
                _ => return false,
            }
        }

        match key {
            "Escape" => {
                if self.search.is_open {
                    self.close_find();
                    return true;
                }
                if self.cursors.count() > 1 {
                    let p = self.cursors.primary().focus;
                    self.cursors.set_single(p);
                    return true;
                }
                false
            }
            "ArrowLeft" => {
                self.move_left(e.shift_key);
                self.ensure_cursor_visible();
                true
            }
            "ArrowRight" => {
                self.move_right(e.shift_key);
                self.ensure_cursor_visible();
                true
            }
            "ArrowUp" => {
                self.move_up(e.shift_key);
                self.ensure_cursor_visible();
                true
            }
            "ArrowDown" => {
                self.move_down(e.shift_key);
                self.ensure_cursor_visible();
                true
            }
            "Home" => {
                self.move_to_line_start(e.shift_key);
                self.ensure_cursor_visible();
                true
            }
            "End" => {
                self.move_to_line_end(e.shift_key);
                self.ensure_cursor_visible();
                true
            }
            "PageUp" => {
                self.page_up(e.shift_key);
                true
            }
            "PageDown" => {
                self.page_down(e.shift_key);
                true
            }
            "Enter" => {
                if !self.read_only {
                    self.insert_newline();
                    self.ensure_cursor_visible();
                }
                true
            }
            "Backspace" => {
                if !self.read_only {
                    self.delete_backwards();
                    self.ensure_cursor_visible();
                }
                true
            }
            "Delete" => {
                if !self.read_only {
                    self.delete_forward();
                    self.ensure_cursor_visible();
                }
                true
            }
            "Tab" => {
                if !self.read_only {
                    if e.shift_key {
                        self.outdent_selection();
                    } else {
                        self.indent_selection();
                    }
                }
                true
            }
            other if !e.alt_key && other.chars().count() == 1 => {
                if let Some(ch) = other.chars().next() {
                    if !ch.is_control() {
                        if !self.read_only {
                            self.insert_char(ch);
                            self.ensure_cursor_visible();
                        }
                        return true;
                    }
                }
                false
            }
            _ => false,
        }
    }

    /// Renders a complete code editor view blueprint with default styling.
    pub fn render(&self) -> NodeBlueprint {
        self.render_blueprint("")
    }

    /// Renders a complete code editor view blueprint with line numbering gutter and syntax tokens.
    pub fn render_blueprint(&self, class_style: &str) -> NodeBlueprint {
        if self.buffer.line_count() > 100 {
            return self.render_virtualized(0.0, 800.0, 24.0, class_style);
        }

        let mut row_nodes = Vec::new();

        let visible_lines = self.buffer.slice_lines(0, self.buffer.line_count());
        let active_line = self.cursors.primary().focus.line;

        for (l_idx, line_str) in visible_lines {
            let is_active = l_idx == active_line;

            let row_class = if is_active {
                "flex items-center font-mono text-sm leading-6 bg-slate-800/40 hover:bg-slate-800/60"
            } else {
                "flex items-center font-mono text-sm leading-6 hover:bg-slate-800/30"
            };

            let visual_sublines = if let Some(cols) = self.wrap_column {
                crate::soft_wrap::wrap_logical_line(line_str, l_idx, cols, true)
            } else {
                vec![crate::soft_wrap::VisualLine {
                    logical_line: l_idx,
                    visual_subline: 0,
                    text: line_str.to_string(),
                    char_start: 0,
                    char_end: line_str.chars().count(),
                    is_continuation: false,
                }]
            };

            for vline in visual_sublines {
                let gutter_text = if vline.is_continuation {
                    "↳".to_string()
                } else {
                    (l_idx + 1).to_string()
                };

                let gutter_node = NodeBlueprint::element(
                    Tag::Span,
                    vec![
                        "w-12 text-right pr-4 select-none text-slate-500 font-mono text-xs"
                            .to_string(),
                    ],
                    Vec::new(),
                    vec![NodeBlueprint::text(gutter_text)],
                );

                let tokens = self.highlighter.highlight_line_ast(l_idx, &vline.text);
                let line_token_nodes = Self::build_token_nodes(&vline.text, tokens);

                let code_node = NodeBlueprint::element(
                    Tag::Span,
                    vec!["flex-1 pl-2 whitespace-pre".to_string()],
                    Vec::new(),
                    line_token_nodes,
                );

                row_nodes.push(NodeBlueprint::element(
                    Tag::Div,
                    vec![row_class.to_string()],
                    Vec::new(),
                    vec![gutter_node, code_node],
                ));
            }
        }

        let container_class = format!(
            "bg-slate-950 text-slate-200 border border-slate-800 rounded-lg p-4 font-mono overflow-x-auto {}",
            class_style
        );

        NodeBlueprint::element(Tag::Div, vec![container_class], Vec::new(), row_nodes)
    }

    /// Maps a semantic [`TokenType`] to a Tailwind utility class string.
    pub fn token_type_class(token_type: TokenType) -> &'static str {
        match token_type {
            TokenType::Keyword => "text-purple-400 font-semibold",
            TokenType::ControlFlow => "text-fuchsia-400 font-semibold",
            TokenType::Type => "text-amber-400 font-medium",
            TokenType::Function => "text-blue-400",
            TokenType::Macro => "text-indigo-300 font-medium",
            TokenType::Identifier => "text-slate-200",
            TokenType::Parameter => "text-orange-200",
            TokenType::Property => "text-sky-300",
            TokenType::String => "text-emerald-400",
            TokenType::StringEscape => "text-teal-300 font-semibold",
            TokenType::Number => "text-orange-400",
            TokenType::Boolean => "text-amber-300 font-semibold",
            TokenType::Comment => "text-slate-500 italic",
            TokenType::DocComment => "text-slate-400 italic",
            TokenType::Operator => "text-rose-400",
            TokenType::Punctuation => "text-slate-400",
            TokenType::Tag => "text-cyan-400",
            TokenType::Attribute => "text-sky-300",
            TokenType::Namespace => "text-amber-200",
            TokenType::Heading => "text-cyan-300 font-bold",
            TokenType::Link => "text-blue-300 underline",
            TokenType::Variable => "text-violet-300",
        }
    }

    fn build_token_nodes(
        line_text: &str,
        tokens: Vec<crate::syntax::SyntaxToken>,
    ) -> Vec<NodeBlueprint> {
        let mut line_token_nodes = Vec::new();
        if tokens.is_empty() {
            line_token_nodes.push(NodeBlueprint::text(line_text));
            return line_token_nodes;
        }

        let mut last_col = 0usize;
        for token in tokens {
            let start_col = token.start_col.min(line_text.len());
            let end_col = token.end_col.min(line_text.len());
            if start_col > last_col
                && line_text.is_char_boundary(last_col)
                && line_text.is_char_boundary(start_col)
            {
                line_token_nodes.push(NodeBlueprint::text(&line_text[last_col..start_col]));
            }

            let color_class = Self::token_type_class(token.token_type);
            line_token_nodes.push(NodeBlueprint::element(
                Tag::Span,
                vec![color_class.to_string()],
                Vec::new(),
                vec![NodeBlueprint::text(token.text)],
            ));
            last_col = end_col.max(last_col);
        }
        if last_col < line_text.len() && line_text.is_char_boundary(last_col) {
            line_token_nodes.push(NodeBlueprint::text(&line_text[last_col..]));
        }
        line_token_nodes
    }

    /// Renders a virtualized viewport slice of the code editor.
    ///
    /// Only the lines currently visible in the scroll viewport (plus an overscan margin)
    /// are laid out and styled into DOM blueprint nodes. Spacer containers at the top
    /// and bottom maintain the exact natural scroll height of the entire document.
    pub fn render_virtualized(
        &self,
        scroll_top: f32,
        viewport_height: f32,
        line_height: f32,
        class_style: &str,
    ) -> NodeBlueprint {
        let total_lines = self.buffer.line_count();
        let effective_lh = line_height.max(1.0);

        let first_visible = (scroll_top / effective_lh).floor() as usize;
        let visible_count = (viewport_height / effective_lh).ceil() as usize;

        // Apply a 2-line overscan buffer for smooth scrolling
        let start_line = first_visible.saturating_sub(2).min(total_lines);
        let end_line = (first_visible + visible_count + 2).min(total_lines);

        let top_spacer_height = start_line as f32 * effective_lh;
        let bottom_spacer_height = (total_lines.saturating_sub(end_line)) as f32 * effective_lh;

        let mut row_nodes = Vec::new();

        // Top spacer for virtual scroll height preservation
        if top_spacer_height > 0.0 {
            row_nodes.push(NodeBlueprint::element(
                Tag::Div,
                vec![format!(
                    "h-[{:.1}px] w-full pointer-events-none",
                    top_spacer_height
                )],
                Vec::new(),
                Vec::new(),
            ));
        }

        let visible_lines = self
            .buffer
            .slice_lines(start_line, end_line.saturating_sub(start_line));
        let active_line = self.cursors.primary().focus.line;

        for (l_idx, line_str) in visible_lines {
            let is_active = l_idx == active_line;

            let row_class = if is_active {
                "flex items-center font-mono text-sm leading-6 bg-slate-800/40 hover:bg-slate-800/60"
            } else {
                "flex items-center font-mono text-sm leading-6 hover:bg-slate-800/30"
            };

            let visual_sublines = if let Some(cols) = self.wrap_column {
                crate::soft_wrap::wrap_logical_line(line_str, l_idx, cols, true)
            } else {
                vec![crate::soft_wrap::VisualLine {
                    logical_line: l_idx,
                    visual_subline: 0,
                    text: line_str.to_string(),
                    char_start: 0,
                    char_end: line_str.chars().count(),
                    is_continuation: false,
                }]
            };

            for vline in visual_sublines {
                let gutter_text = if vline.is_continuation {
                    "↳".to_string()
                } else {
                    (l_idx + 1).to_string()
                };

                let gutter_class = if vline.is_continuation {
                    "w-12 text-right pr-4 select-none text-slate-600 font-mono text-xs"
                } else {
                    "w-12 text-right pr-4 select-none text-slate-500 font-mono text-xs"
                };

                let gutter_node = NodeBlueprint::element(
                    Tag::Span,
                    vec![gutter_class.to_string()],
                    Vec::new(),
                    vec![NodeBlueprint::text(gutter_text)],
                );

                // Tokens
                let tokens = self.highlighter.highlight_line_ast(l_idx, &vline.text);
                let line_token_nodes = Self::build_token_nodes(&vline.text, tokens);

                let code_node = NodeBlueprint::element(
                    Tag::Span,
                    vec!["flex-1 pl-2 whitespace-pre".to_string()],
                    Vec::new(),
                    line_token_nodes,
                );

                row_nodes.push(NodeBlueprint::element(
                    Tag::Div,
                    vec![row_class.to_string()],
                    Vec::new(),
                    vec![gutter_node, code_node],
                ));
            }
        }

        // Bottom spacer for virtual scroll height preservation
        if bottom_spacer_height > 0.0 {
            row_nodes.push(NodeBlueprint::element(
                Tag::Div,
                vec![format!(
                    "h-[{:.1}px] w-full pointer-events-none",
                    bottom_spacer_height
                )],
                Vec::new(),
                Vec::new(),
            ));
        }

        let container_class = format!(
            "bg-slate-950 text-slate-200 border border-slate-800 rounded-lg p-4 font-mono overflow-y-auto {}",
            class_style
        );

        NodeBlueprint::element(Tag::Div, vec![container_class], Vec::new(), row_nodes)
    }
}

#[inline]
fn is_word_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}