editor-core 0.4.1

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

use crate::commands::{
    AutoPairsConfig, Command, CommandExecutor, CommandResult, CursorCommand, EditCommand,
    TextEditSpec, UndoHistoryRestoreError, UndoHistorySnapshot,
};
use crate::decorations::{Decoration, DecorationLayerId};
use crate::delta::TextDelta;
use crate::intervals::FoldRegion;
use crate::processing::ProcessingEdit;
use crate::search::{SearchError, SearchMatch, SearchOptions, find_all};
use crate::selection_set::selection_direction;
use crate::snippets::SnippetSession;
use crate::state::CursorState;
use crate::{AnchorBias, TextAnchor};
use crate::{
    IndentationConfig, LineEnding, LineIndex, Position, Selection, SelectionDirection,
    TabKeyBehavior, ViewCommand,
};
use crate::{StateChange, StateChangeCallback, StateChangeType, WrapIndent, WrapMode};
use std::collections::{BTreeMap, HashMap};
use std::ops::Range;
use std::sync::Arc;

/// Opaque identifier for an open buffer in a [`Workspace`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BufferId(u64);

impl BufferId {
    /// Create a `BufferId` from a raw numeric id.
    ///
    /// This is intended for interoperability boundaries (e.g. FFI) that persist ids externally.
    pub const fn from_raw(id: u64) -> Self {
        Self(id)
    }

    /// Get the underlying numeric id.
    pub fn get(self) -> u64 {
        self.0
    }
}

/// Opaque identifier for a view into a buffer in a [`Workspace`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ViewId(u64);

impl ViewId {
    /// Create a `ViewId` from a raw numeric id.
    ///
    /// This is intended for interoperability boundaries (e.g. FFI) that persist ids externally.
    pub const fn from_raw(id: u64) -> Self {
        Self(id)
    }

    /// Get the underlying numeric id.
    pub fn get(self) -> u64 {
        self.0
    }
}

/// Metadata attached to a workspace buffer.
#[derive(Debug, Clone)]
pub struct BufferMetadata {
    /// Optional buffer URI/path (host-provided).
    pub uri: Option<String>,
}

/// Result of opening a buffer (a buffer always starts with a default view).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OpenBufferResult {
    /// The created buffer id.
    pub buffer_id: BufferId,
    /// The initial view id into that buffer.
    pub view_id: ViewId,
}

/// A navigation target produced by jump-list operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct JumpTarget {
    /// Target buffer id.
    pub buffer_id: BufferId,
    /// Target position in logical coordinates.
    pub position: Position,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ViewCore {
    cursor_position: Position,
    selection: Option<Selection>,
    secondary_selections: Vec<Selection>,
    viewport_width: usize,
    wrap_mode: WrapMode,
    wrap_indent: WrapIndent,
    tab_width: usize,
    tab_key_behavior: TabKeyBehavior,
    indentation_config: IndentationConfig,
    auto_pairs: AutoPairsConfig,
    snippet_session: Option<SnippetSession>,
    preferred_x_cells: Option<usize>,
}

impl ViewCore {
    fn from_executor(executor: &CommandExecutor) -> Self {
        let editor = executor.editor();
        Self {
            cursor_position: editor.cursor_position(),
            selection: editor.selection().cloned(),
            secondary_selections: editor.secondary_selections().to_vec(),
            viewport_width: editor.viewport_width(),
            wrap_mode: editor.layout_engine().wrap_mode(),
            wrap_indent: editor.layout_engine().wrap_indent(),
            tab_width: editor.layout_engine().tab_width(),
            tab_key_behavior: executor.tab_key_behavior(),
            indentation_config: executor.indentation_config().clone(),
            auto_pairs: executor.auto_pairs_config().clone(),
            snippet_session: executor.snippet_session().cloned(),
            preferred_x_cells: executor.preferred_x_cells(),
        }
    }

    fn apply_to_executor(&self, executor: &mut CommandExecutor) {
        let mut invalidate_visual_rows = false;
        let editor = executor.editor_mut();
        editor.set_cursor_state(
            self.cursor_position,
            self.selection.clone(),
            self.secondary_selections.clone(),
        );

        if editor.viewport_width() != self.viewport_width {
            invalidate_visual_rows = true;
        }

        let before_wrap_mode = editor.layout_engine().wrap_mode();
        let before_wrap_indent = editor.layout_engine().wrap_indent();
        let before_tab_width = editor.layout_engine().tab_width();
        let before_viewport_width = editor.layout_engine().viewport_width();
        if before_wrap_mode != self.wrap_mode
            || before_wrap_indent != self.wrap_indent
            || before_tab_width != self.tab_width
            || before_viewport_width != self.viewport_width
        {
            invalidate_visual_rows = true;
        }

        if invalidate_visual_rows {
            editor.set_view_options(
                self.viewport_width,
                self.wrap_mode,
                self.wrap_indent,
                self.tab_width,
            );
        }

        executor.set_tab_key_behavior(self.tab_key_behavior);
        executor.set_indentation_config(self.indentation_config.clone());
        executor.set_auto_pairs_config(self.auto_pairs.clone());
        executor.set_snippet_session(self.snippet_session.clone());
        executor.set_preferred_x_cells(self.preferred_x_cells);
    }
}

struct BufferEntry {
    meta: BufferMetadata,
    executor: CommandExecutor,
    version: u64,
    last_text_delta: Option<Arc<TextDelta>>,
    bookmarks: BookmarkSet,
    marks: MarkSet,
}

struct ViewEntry {
    buffer: BufferId,
    core: ViewCore,
    version: u64,
    callbacks: Vec<StateChangeCallback>,
    scroll_top: usize,
    scroll_sub_row_offset: u16,
    overscan_rows: usize,
    viewport_height: Option<usize>,
    last_text_delta: Option<Arc<TextDelta>>,
    jump_list: JumpList,
}

/// Workspace-level errors.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorkspaceError {
    /// A buffer with this uri already exists.
    UriAlreadyOpen(String),
    /// A buffer id was not found.
    BufferNotFound(BufferId),
    /// A view id was not found.
    ViewNotFound(ViewId),
    /// Executing a command failed.
    CommandFailed {
        /// Target view id.
        view: ViewId,
        /// Error message.
        message: String,
    },
    /// Applying edits to a buffer failed.
    ApplyEditsFailed {
        /// Target buffer id.
        buffer: BufferId,
        /// Error message.
        message: String,
    },
}

/// Errors produced when restoring undo history for a workspace buffer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorkspaceUndoHistoryRestoreError {
    /// A buffer id was not found.
    BufferNotFound(BufferId),
    /// Restoring the undo history failed (corrupt snapshot or version mismatch).
    RestoreFailed {
        /// Target buffer id.
        buffer: BufferId,
        /// Underlying restore error.
        error: UndoHistoryRestoreError,
    },
}

impl std::fmt::Display for WorkspaceUndoHistoryRestoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BufferNotFound(id) => write!(f, "Buffer not found (id={})", id.get()),
            Self::RestoreFailed { buffer, error } => {
                write!(
                    f,
                    "Restore undo history failed (buffer={}): {}",
                    buffer.get(),
                    error
                )
            }
        }
    }
}

impl std::error::Error for WorkspaceUndoHistoryRestoreError {}

/// Search matches for a single open buffer in a [`Workspace`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkspaceSearchResult {
    /// Buffer id.
    pub id: BufferId,
    /// Optional URI/path metadata.
    pub uri: Option<String>,
    /// All matches in this buffer (character offsets, half-open).
    pub matches: Vec<SearchMatch>,
}

/// Smooth-scrolling state for a view.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ViewSmoothScrollState {
    /// Top visual row anchor.
    pub top_visual_row: usize,
    /// Sub-row offset within `top_visual_row` (0..=65535, normalized).
    pub sub_row_offset: u16,
    /// Overscan rows for prefetching.
    pub overscan_rows: usize,
}

/// Viewport state for a workspace view, including visual totals and smooth-scrolling metadata.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WorkspaceViewportState {
    /// Viewport width (in cells).
    pub width: usize,
    /// Viewport height (line count, host-provided).
    pub height: Option<usize>,
    /// Current top visual row.
    pub scroll_top: usize,
    /// Visible visual range.
    pub visible_lines: Range<usize>,
    /// Total visual line count under current view config (wrap + folding aware).
    pub total_visual_lines: usize,
    /// Smooth-scroll metadata.
    pub smooth_scroll: ViewSmoothScrollState,
    /// Recommended prefetch range using overscan rows.
    pub prefetch_lines: Range<usize>,
}

fn apply_char_offset_delta(mut offset: usize, delta: &TextDelta) -> usize {
    for edit in &delta.edits {
        let start = edit.start;
        let end = edit.end();
        let deleted_len = edit.deleted_len();
        let inserted_len = edit.inserted_len();

        if offset < start {
            continue;
        }

        if offset < end {
            // If the caret was inside the replaced range, anchor it at the end of the inserted text.
            offset = start.saturating_add(inserted_len);
            continue;
        }

        // After the replaced range: shift by the net length delta.
        if inserted_len >= deleted_len {
            offset = offset.saturating_add(inserted_len - deleted_len);
        } else {
            offset = offset.saturating_sub(deleted_len - inserted_len);
        }
    }

    offset
}

fn apply_position_delta(
    old_index: &LineIndex,
    new_index: &LineIndex,
    pos: Position,
    delta: &TextDelta,
) -> Position {
    let before = old_index.position_to_char_offset(pos.line, pos.column);
    let after = apply_char_offset_delta(before, delta);
    let (line, column) = new_index.char_offset_to_position(after);
    Position::new(line, column)
}

fn apply_selection_delta(
    old_index: &LineIndex,
    new_index: &LineIndex,
    selection: &Selection,
    delta: &TextDelta,
) -> Selection {
    let start = apply_position_delta(old_index, new_index, selection.start, delta);
    let end = apply_position_delta(old_index, new_index, selection.end, delta);
    Selection {
        start,
        end,
        direction: selection_direction(start, end),
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct BookmarkSet {
    anchors: Vec<TextAnchor>,
}

impl BookmarkSet {
    fn toggle_line_start(&mut self, line_start_offset: usize) -> bool {
        let anchor = TextAnchor::new(line_start_offset, AnchorBias::Left);
        match self
            .anchors
            .binary_search_by_key(&anchor.offset, |a| a.offset)
        {
            Ok(idx) => {
                self.anchors.remove(idx);
                false
            }
            Err(idx) => {
                self.anchors.insert(idx, anchor);
                true
            }
        }
    }

    fn clear(&mut self) {
        self.anchors.clear();
    }

    fn apply_delta(&mut self, delta: &TextDelta) {
        for a in &mut self.anchors {
            a.apply_delta(delta);
        }
        self.anchors.sort_by_key(|a| a.offset);
        self.anchors.dedup_by_key(|a| a.offset);
    }

    fn line_numbers(&self, line_index: &LineIndex) -> Vec<usize> {
        let mut lines: Vec<usize> = self
            .anchors
            .iter()
            .map(|a| line_index.char_offset_to_position(a.offset).0)
            .collect();
        lines.sort_unstable();
        lines.dedup();
        lines
    }

    fn next_after_line_start(&self, current_line_start: usize) -> Option<TextAnchor> {
        self.anchors
            .iter()
            .copied()
            .find(|a| a.offset > current_line_start)
            .or_else(|| self.anchors.first().copied())
    }

    fn prev_before_line_start(&self, current_line_start: usize) -> Option<TextAnchor> {
        self.anchors
            .iter()
            .copied()
            .rfind(|a| a.offset < current_line_start)
            .or_else(|| self.anchors.last().copied())
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct MarkSet {
    marks: BTreeMap<String, TextAnchor>,
}

impl MarkSet {
    fn set(&mut self, name: String, offset: usize) {
        self.marks
            .insert(name, TextAnchor::new(offset, AnchorBias::Right));
    }

    fn get(&self, name: &str) -> Option<TextAnchor> {
        self.marks.get(name).copied()
    }

    fn remove(&mut self, name: &str) -> bool {
        self.marks.remove(name).is_some()
    }

    fn clear(&mut self) {
        self.marks.clear();
    }

    fn names(&self) -> Vec<String> {
        self.marks.keys().cloned().collect()
    }

    fn apply_delta(&mut self, delta: &TextDelta) {
        for anchor in self.marks.values_mut() {
            anchor.apply_delta(delta);
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct JumpEntry {
    buffer_id: BufferId,
    anchor: TextAnchor,
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct JumpList {
    back: Vec<JumpEntry>,
    forward: Vec<JumpEntry>,
    max_len: usize,
}

impl JumpList {
    fn new(max_len: usize) -> Self {
        Self {
            back: Vec::new(),
            forward: Vec::new(),
            max_len: max_len.max(1),
        }
    }

    fn record(&mut self, entry: JumpEntry) {
        if self.back.last().is_some_and(|last| *last == entry) {
            return;
        }

        self.back.push(entry);
        self.forward.clear();

        if self.back.len() > self.max_len {
            let overflow = self.back.len() - self.max_len;
            self.back.drain(0..overflow);
        }
    }

    fn back(&mut self, current: JumpEntry) -> Option<JumpEntry> {
        let target = self.back.pop()?;
        if !self.forward.last().is_some_and(|last| *last == current) {
            self.forward.push(current);
        }
        Some(target)
    }

    fn forward(&mut self, current: JumpEntry) -> Option<JumpEntry> {
        let target = self.forward.pop()?;
        if !self.back.last().is_some_and(|last| *last == current) {
            self.back.push(current);
        }
        Some(target)
    }

    fn clear(&mut self) {
        self.back.clear();
        self.forward.clear();
    }

    fn apply_delta(&mut self, buffer_id: BufferId, delta: &TextDelta) {
        for entry in self
            .back
            .iter_mut()
            .chain(self.forward.iter_mut())
            .filter(|e| e.buffer_id == buffer_id)
        {
            entry.anchor.apply_delta(delta);
        }
    }
}

/// A collection of open buffers and their views.
#[derive(Default)]
pub struct Workspace {
    next_buffer_id: u64,
    buffers: BTreeMap<BufferId, BufferEntry>,
    uri_to_buffer: HashMap<String, BufferId>,

    next_view_id: u64,
    views: BTreeMap<ViewId, ViewEntry>,
    active_view: Option<ViewId>,

    intelligence: crate::WorkspaceIntelligence,
}

impl std::fmt::Debug for Workspace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Workspace")
            .field("buffer_count", &self.buffers.len())
            .field("view_count", &self.views.len())
            .field("uri_count", &self.uri_to_buffer.len())
            .field("active_view", &self.active_view)
            .field("intelligence_set_count", &self.intelligence.len())
            .finish()
    }
}

impl Workspace {
    /// Create an empty workspace.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the number of open buffers.
    pub fn len(&self) -> usize {
        self.buffers.len()
    }

    /// Returns `true` if there are no open buffers.
    pub fn is_empty(&self) -> bool {
        self.buffers.is_empty()
    }

    /// Returns the number of open views.
    pub fn view_count(&self) -> usize {
        self.views.len()
    }

    /// Return the active view id (if any).
    pub fn active_view_id(&self) -> Option<ViewId> {
        self.active_view
    }

    /// Return the active buffer id (if any).
    pub fn active_buffer_id(&self) -> Option<BufferId> {
        let view_id = self.active_view?;
        self.views.get(&view_id).map(|v| v.buffer)
    }

    /// Read workspace-scoped language intelligence result sets (references/call hierarchy/etc.).
    pub fn intelligence(&self) -> &crate::WorkspaceIntelligence {
        &self.intelligence
    }

    /// Mutate workspace-scoped language intelligence result sets (references/call hierarchy/etc.).
    pub fn intelligence_mut(&mut self) -> &mut crate::WorkspaceIntelligence {
        &mut self.intelligence
    }

    /// Set the active view.
    pub fn set_active_view(&mut self, id: ViewId) -> Result<(), WorkspaceError> {
        if !self.views.contains_key(&id) {
            return Err(WorkspaceError::ViewNotFound(id));
        }
        self.active_view = Some(id);
        Ok(())
    }

    /// Open a new buffer in the workspace, creating an initial view.
    ///
    /// - `uri` is optional and host-provided (e.g. `file:///...`).
    /// - `text` is the initial contents.
    /// - `viewport_width` is the initial view's wrap width.
    pub fn open_buffer(
        &mut self,
        uri: Option<String>,
        text: &str,
        viewport_width: usize,
    ) -> Result<OpenBufferResult, WorkspaceError> {
        if let Some(uri) = uri.as_ref()
            && self.uri_to_buffer.contains_key(uri)
        {
            return Err(WorkspaceError::UriAlreadyOpen(uri.clone()));
        }

        let buffer_id = BufferId(self.next_buffer_id);
        self.next_buffer_id = self.next_buffer_id.saturating_add(1);

        let executor = CommandExecutor::new(text, viewport_width);
        let meta = BufferMetadata { uri: uri.clone() };
        self.buffers.insert(
            buffer_id,
            BufferEntry {
                meta,
                executor,
                version: 0,
                last_text_delta: None,
                bookmarks: BookmarkSet::default(),
                marks: MarkSet::default(),
            },
        );

        if let Some(uri) = uri {
            self.uri_to_buffer.insert(uri, buffer_id);
        }

        let view_id = self.create_view(buffer_id, viewport_width)?;

        if self.active_view.is_none() {
            self.active_view = Some(view_id);
        }

        Ok(OpenBufferResult { buffer_id, view_id })
    }

    /// Close a buffer (and all its views).
    pub fn close_buffer(&mut self, id: BufferId) -> Result<(), WorkspaceError> {
        let Some(entry) = self.buffers.remove(&id) else {
            return Err(WorkspaceError::BufferNotFound(id));
        };

        if let Some(uri) = entry.meta.uri.as_ref() {
            self.uri_to_buffer.remove(uri);
        }

        let views_to_remove: Vec<ViewId> = self
            .views
            .iter()
            .filter_map(|(vid, v)| if v.buffer == id { Some(*vid) } else { None })
            .collect();
        for view_id in views_to_remove {
            self.views.remove(&view_id);
        }

        if self
            .active_view
            .is_some_and(|active| !self.views.contains_key(&active))
        {
            self.active_view = self.views.keys().next().copied();
        }

        Ok(())
    }

    /// Close a view. If it was the last view of its buffer, the buffer is also closed.
    pub fn close_view(&mut self, id: ViewId) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.remove(&id) else {
            return Err(WorkspaceError::ViewNotFound(id));
        };

        if self.active_view == Some(id) {
            self.active_view = self.views.keys().next().copied();
        }

        let still_has_views = self.views.values().any(|v| v.buffer == view.buffer);
        if !still_has_views {
            self.close_buffer(view.buffer)?;
        }

        Ok(())
    }

    /// Return all open buffer ids in deterministic order.
    pub fn buffer_ids(&self) -> Vec<BufferId> {
        let mut ids: Vec<BufferId> = self.buffers.keys().copied().collect();
        ids.sort_by_key(|id| id.get());
        ids
    }

    /// Return all open view ids in deterministic order.
    pub fn view_ids(&self) -> Vec<ViewId> {
        let mut ids: Vec<ViewId> = self.views.keys().copied().collect();
        ids.sort_by_key(|id| id.get());
        ids
    }

    /// Create a new view into an existing buffer.
    pub fn create_view(
        &mut self,
        buffer: BufferId,
        viewport_width: usize,
    ) -> Result<ViewId, WorkspaceError> {
        let Some(buffer_entry) = self.buffers.get_mut(&buffer) else {
            return Err(WorkspaceError::BufferNotFound(buffer));
        };

        // Create a view state by starting from the executor defaults, but overriding width and
        // clearing selection/cursors.
        let mut core = ViewCore::from_executor(&buffer_entry.executor);
        core.cursor_position = Position::new(0, 0);
        core.selection = None;
        core.secondary_selections.clear();
        core.viewport_width = viewport_width.max(1);
        core.preferred_x_cells = None;

        let view_id = ViewId(self.next_view_id);
        self.next_view_id = self.next_view_id.saturating_add(1);

        self.views.insert(
            view_id,
            ViewEntry {
                buffer,
                core,
                version: 0,
                callbacks: Vec::new(),
                scroll_top: 0,
                scroll_sub_row_offset: 0,
                overscan_rows: 0,
                viewport_height: None,
                last_text_delta: None,
                jump_list: JumpList::new(200),
            },
        );

        Ok(view_id)
    }

    /// Look up a buffer by uri.
    pub fn buffer_id_for_uri(&self, uri: &str) -> Option<BufferId> {
        self.uri_to_buffer.get(uri).copied()
    }

    /// Get a reference to a buffer's line index (logical line/column <-> char offsets).
    pub fn buffer_line_index(&self, buffer_id: BufferId) -> Result<&LineIndex, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.editor().line_index())
    }

    /// Get the document length for a buffer in Unicode scalar values (Rust `char`s).
    pub fn buffer_char_count(&self, buffer_id: BufferId) -> Result<usize, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.editor().char_count())
    }

    /// Get a slice of the buffer text as a `String` by character offset + length.
    ///
    /// Notes:
    /// - `start` and `len` are in Unicode scalar indices (Rust `char`s), not bytes.
    /// - Out-of-bounds ranges are clamped by the underlying text buffer.
    pub fn buffer_text_range(
        &self,
        buffer_id: BufferId,
        start: usize,
        len: usize,
    ) -> Result<String, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.editor().text_range(start, len))
    }

    /// Get all decoration layers for a buffer.
    pub fn buffer_decorations(
        &self,
        buffer_id: BufferId,
    ) -> Result<&BTreeMap<DecorationLayerId, Vec<Decoration>>, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.editor().decorations())
    }

    /// Get the current folding regions for a buffer (user folds + derived folds).
    pub fn folding_regions_for_buffer(
        &self,
        buffer_id: BufferId,
    ) -> Result<Vec<FoldRegion>, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer
            .executor
            .editor()
            .folding_manager()
            .regions()
            .to_vec())
    }

    /// Returns whether a buffer has unsaved text edits.
    ///
    /// Notes:
    /// - This tracks the executor's "clean point" (usually the last `mark_saved_*` call),
    ///   and is restored by undoing back to that clean point.
    pub fn buffer_is_modified(&self, buffer_id: BufferId) -> Result<bool, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(!buffer.executor.is_clean())
    }

    /// Return the preferred line ending for saving this buffer.
    pub fn line_ending_for_buffer(
        &self,
        buffer_id: BufferId,
    ) -> Result<LineEnding, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.line_ending())
    }

    /// Override the preferred line ending for saving this buffer.
    pub fn set_line_ending_for_buffer(
        &mut self,
        buffer_id: BufferId,
        line_ending: LineEnding,
    ) -> Result<(), WorkspaceError> {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        buffer.executor.set_line_ending(line_ending);
        Ok(())
    }

    /// Returns whether the view's underlying buffer has unsaved text edits.
    pub fn is_modified_for_view(&self, view_id: ViewId) -> Result<bool, WorkspaceError> {
        let buffer_id = self.buffer_id_for_view(view_id)?;
        self.buffer_is_modified(buffer_id)
    }

    /// Return the preferred line ending for saving this view's underlying buffer.
    pub fn line_ending_for_view(&self, view_id: ViewId) -> Result<LineEnding, WorkspaceError> {
        let buffer_id = self.buffer_id_for_view(view_id)?;
        self.line_ending_for_buffer(buffer_id)
    }

    /// Override the preferred line ending for saving this view's underlying buffer.
    pub fn set_line_ending_for_view(
        &mut self,
        view_id: ViewId,
        line_ending: LineEnding,
    ) -> Result<(), WorkspaceError> {
        let buffer_id = self.buffer_id_for_view(view_id)?;
        self.set_line_ending_for_buffer(buffer_id, line_ending)
    }

    /// Mark the current state of a buffer as saved (clean point).
    pub fn mark_saved_for_buffer(&mut self, buffer_id: BufferId) -> Result<(), WorkspaceError> {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        buffer.executor.mark_clean();
        Ok(())
    }

    /// Mark the current state of a view's buffer as saved (clean point).
    pub fn mark_saved_for_view(&mut self, view_id: ViewId) -> Result<(), WorkspaceError> {
        let buffer_id = self.buffer_id_for_view(view_id)?;
        self.mark_saved_for_buffer(buffer_id)
    }

    /// Capture a persistable snapshot of a buffer's undo/redo history.
    pub fn undo_history_snapshot_for_buffer(
        &self,
        buffer_id: BufferId,
    ) -> Result<UndoHistorySnapshot, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.undo_history_snapshot())
    }

    /// Restore a buffer's undo/redo history from a previously captured snapshot.
    ///
    /// Notes:
    /// - This does **not** modify the current buffer text.
    /// - Callers should only restore a snapshot into the **same text** it was captured from.
    pub fn restore_undo_history_for_buffer(
        &mut self,
        buffer_id: BufferId,
        snapshot: UndoHistorySnapshot,
    ) -> Result<(), WorkspaceUndoHistoryRestoreError> {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceUndoHistoryRestoreError::BufferNotFound(buffer_id));
        };

        buffer.last_text_delta = None;
        for view in self.views.values_mut() {
            if view.buffer == buffer_id {
                view.last_text_delta = None;
            }
        }

        buffer
            .executor
            .restore_undo_history(snapshot)
            .map_err(|err| WorkspaceUndoHistoryRestoreError::RestoreFailed {
                buffer: buffer_id,
                error: err,
            })?;

        Ok(())
    }

    /// Get a buffer's metadata.
    pub fn buffer_metadata(&self, id: BufferId) -> Option<&BufferMetadata> {
        self.buffers.get(&id).map(|e| &e.meta)
    }

    /// Get the buffer id that a view is pointing at.
    pub fn buffer_id_for_view(&self, id: ViewId) -> Result<BufferId, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.buffer)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the primary cursor position for a view.
    pub fn cursor_position_for_view(&self, id: ViewId) -> Result<Position, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.cursor_position)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the primary selection for a view (None means "empty selection / caret only").
    pub fn selection_for_view(&self, id: ViewId) -> Result<Option<Selection>, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.selection.clone())
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current tab width setting for a view (in monospace cells).
    pub fn tab_width_for_view(&self, id: ViewId) -> Result<usize, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.tab_width)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current viewport width setting for a view (in monospace cells).
    pub fn viewport_width_for_view(&self, id: ViewId) -> Result<usize, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.viewport_width)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current soft wrap mode for a view.
    pub fn wrap_mode_for_view(&self, id: ViewId) -> Result<WrapMode, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.wrap_mode)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current wrapped-line indentation policy for a view.
    pub fn wrap_indent_for_view(&self, id: ViewId) -> Result<WrapIndent, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.wrap_indent)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current tab key behavior for a view.
    pub fn tab_key_behavior_for_view(&self, id: ViewId) -> Result<TabKeyBehavior, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.tab_key_behavior)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current indentation configuration for a view.
    pub fn indentation_config_for_view(
        &self,
        id: ViewId,
    ) -> Result<IndentationConfig, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.indentation_config.clone())
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the current auto-pairs configuration for a view.
    pub fn auto_pairs_config_for_view(
        &self,
        id: ViewId,
    ) -> Result<AutoPairsConfig, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.core.auto_pairs.clone())
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get a view's normalized cursor/selection snapshot.
    ///
    /// This matches the semantics of `EditorStateManager::get_cursor_state`, but for workspace views.
    pub fn cursor_state_for_view(&self, id: ViewId) -> Result<CursorState, WorkspaceError> {
        let Some(view) = self.views.get(&id) else {
            return Err(WorkspaceError::ViewNotFound(id));
        };
        let Some(buffer) = self.buffers.get(&view.buffer) else {
            return Err(WorkspaceError::BufferNotFound(view.buffer));
        };

        let line_index = buffer.executor.editor().line_index();

        let mut selections: Vec<Selection> =
            Vec::with_capacity(1 + view.core.secondary_selections.len());
        let primary = view.core.selection.clone().unwrap_or(Selection {
            start: view.core.cursor_position,
            end: view.core.cursor_position,
            direction: SelectionDirection::Forward,
        });
        selections.push(primary);
        selections.extend(view.core.secondary_selections.iter().cloned());

        let (selections, primary_selection_index) =
            crate::selection_set::normalize_selections(selections, 0);
        let primary = selections
            .get(primary_selection_index)
            .cloned()
            .unwrap_or(Selection {
                start: view.core.cursor_position,
                end: view.core.cursor_position,
                direction: SelectionDirection::Forward,
            });

        let position = primary.end;
        let offset = line_index.position_to_char_offset(position.line, position.column);

        let selection = if primary.start == primary.end {
            None
        } else {
            Some(primary)
        };

        let multi_cursors: Vec<Position> = selections
            .iter()
            .enumerate()
            .filter_map(|(idx, sel)| {
                if idx == primary_selection_index {
                    None
                } else {
                    Some(sel.end)
                }
            })
            .collect();

        Ok(CursorState {
            position,
            offset,
            multi_cursors,
            selection,
            selections,
            primary_selection_index,
        })
    }

    /// Get the scroll position (top visual row) for a view.
    pub fn scroll_top_for_view(&self, id: ViewId) -> Result<usize, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.scroll_top)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get the sub-row smooth-scroll offset for a view.
    pub fn scroll_sub_row_offset_for_view(&self, id: ViewId) -> Result<u16, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.scroll_sub_row_offset)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get overscan rows for a view.
    pub fn overscan_rows_for_view(&self, id: ViewId) -> Result<usize, WorkspaceError> {
        self.views
            .get(&id)
            .map(|v| v.overscan_rows)
            .ok_or(WorkspaceError::ViewNotFound(id))
    }

    /// Get smooth-scroll state for a view.
    pub fn smooth_scroll_state_for_view(
        &self,
        id: ViewId,
    ) -> Result<ViewSmoothScrollState, WorkspaceError> {
        let Some(view) = self.views.get(&id) else {
            return Err(WorkspaceError::ViewNotFound(id));
        };
        Ok(ViewSmoothScrollState {
            top_visual_row: view.scroll_top,
            sub_row_offset: view.scroll_sub_row_offset,
            overscan_rows: view.overscan_rows,
        })
    }

    /// Update a buffer's uri/path.
    pub fn set_buffer_uri(
        &mut self,
        id: BufferId,
        uri: Option<String>,
    ) -> Result<(), WorkspaceError> {
        let Some(entry) = self.buffers.get_mut(&id) else {
            return Err(WorkspaceError::BufferNotFound(id));
        };

        if let Some(next) = uri.as_ref()
            && self.uri_to_buffer.contains_key(next)
            && entry.meta.uri.as_deref() != Some(next.as_str())
        {
            return Err(WorkspaceError::UriAlreadyOpen(next.clone()));
        }

        if let Some(prev) = entry.meta.uri.take() {
            self.uri_to_buffer.remove(&prev);
        }

        if let Some(next) = uri.clone() {
            self.uri_to_buffer.insert(next, id);
        }

        entry.meta.uri = uri;
        Ok(())
    }

    /// Get a view's current version (increments on view-local changes and buffer changes).
    pub fn view_version(&self, id: ViewId) -> Option<u64> {
        self.views.get(&id).map(|v| v.version)
    }

    /// Get the last broadcast text delta for this view (if any).
    pub fn last_text_delta_for_view(&self, id: ViewId) -> Option<&Arc<TextDelta>> {
        self.views.get(&id)?.last_text_delta.as_ref()
    }

    /// Take the last broadcast text delta for this view (if any).
    pub fn take_last_text_delta_for_view(&mut self, id: ViewId) -> Option<Arc<TextDelta>> {
        self.views.get_mut(&id)?.last_text_delta.take()
    }

    /// Take the last text delta for a buffer (if any).
    ///
    /// This is useful for incremental consumers (e.g. LSP sync) that want to observe each buffer
    /// edit exactly once, regardless of how many views exist for that buffer.
    pub fn take_last_text_delta_for_buffer(
        &mut self,
        id: BufferId,
    ) -> Result<Option<Arc<TextDelta>>, WorkspaceError> {
        let Some(buffer) = self.buffers.get_mut(&id) else {
            return Err(WorkspaceError::BufferNotFound(id));
        };
        Ok(buffer.last_text_delta.take())
    }

    /// Subscribe to changes for a view.
    pub fn subscribe_view<F>(&mut self, id: ViewId, callback: F) -> Result<(), WorkspaceError>
    where
        F: FnMut(&StateChange) + Send + 'static,
    {
        let Some(view) = self.views.get_mut(&id) else {
            return Err(WorkspaceError::ViewNotFound(id));
        };

        view.callbacks.push(Box::new(callback));
        Ok(())
    }

    fn notify_view(
        view: &mut ViewEntry,
        change_type: StateChangeType,
        delta: Option<Arc<TextDelta>>,
    ) {
        let old_version = view.version;
        view.version = view.version.saturating_add(1);

        let mut change = StateChange::new(change_type, old_version, view.version);
        if let Some(delta) = delta {
            change = change.with_text_delta(delta);
        }

        for cb in &mut view.callbacks {
            cb(&change);
        }
    }

    fn command_change_type(command: &Command) -> Option<StateChangeType> {
        match command {
            Command::Edit(EditCommand::Delete { length: 0, .. }) => None,
            Command::Edit(EditCommand::Replace {
                length: 0, text, ..
            }) if text.is_empty() => None,
            Command::Edit(EditCommand::EndUndoGroup) => None,
            Command::Edit(_) => Some(StateChangeType::DocumentModified),
            Command::Cursor(
                CursorCommand::MoveTo { .. }
                | CursorCommand::MoveBy { .. }
                | CursorCommand::MoveVisualBy { .. }
                | CursorCommand::MoveToVisual { .. }
                | CursorCommand::MoveToLineStart
                | CursorCommand::MoveToLineEnd
                | CursorCommand::MoveToVisualLineStart
                | CursorCommand::MoveToVisualLineEnd
                | CursorCommand::MoveGraphemeLeft
                | CursorCommand::MoveGraphemeRight
                | CursorCommand::MoveWordLeft
                | CursorCommand::MoveWordRight
                | CursorCommand::MoveToMatchingBracket
                | CursorCommand::FindNext { .. }
                | CursorCommand::FindPrev { .. },
            ) => Some(StateChangeType::CursorMoved),
            Command::Cursor(_) => Some(StateChangeType::SelectionChanged),
            Command::View(ViewCommand::ScrollTo { .. } | ViewCommand::GetViewport { .. }) => None,
            Command::View(_) => Some(StateChangeType::ViewportChanged),
            Command::Style(
                crate::StyleCommand::AddStyle { .. }
                | crate::StyleCommand::RemoveStyle { .. }
                | crate::StyleCommand::UpdateBracketMatchHighlights
                | crate::StyleCommand::ClearBracketMatchHighlights,
            ) => Some(StateChangeType::StyleChanged),
            Command::Style(
                crate::StyleCommand::Fold { .. }
                | crate::StyleCommand::Unfold { .. }
                | crate::StyleCommand::UnfoldAll,
            ) => Some(StateChangeType::FoldingChanged),
        }
    }

    /// Execute a command against a specific view.
    ///
    /// - Cursor/selection state is view-local.
    /// - Text edits and derived-state edits are applied to the underlying buffer.
    /// - Any text delta is broadcast to all views of that buffer.
    pub fn execute(
        &mut self,
        view_id: ViewId,
        command: Command,
    ) -> Result<CommandResult, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let change_type = Self::command_change_type(&command);
        if change_type.is_none() {
            // Still run command because it may validate (e.g. ScrollTo), but treat as no version bump.
        }

        // Borrow maps separately so we can mutably access a view and its buffer.
        let views = &mut self.views;
        let buffers = &mut self.buffers;

        let Some(view) = views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let before_view_core = view.core.clone();
        let before_line_index = buffer.executor.editor().line_index().clone();
        let before_char_count = buffer.executor.editor().char_count();

        // Load view-local state into the executor, execute, then snapshot it back.
        view.core.apply_to_executor(&mut buffer.executor);

        let result = buffer.executor.execute(command.clone()).map_err(|err| {
            WorkspaceError::CommandFailed {
                view: view_id,
                message: err.to_string(),
            }
        })?;

        view.core = ViewCore::from_executor(&buffer.executor);

        let delta = buffer.executor.take_last_text_delta().map(Arc::new);
        let after_char_count = buffer.executor.editor().char_count();

        // Detect no-ops: successful execution but no meaningful state change.
        let view_changed = view.core != before_view_core;
        let buffer_text_changed = delta.is_some()
            // `Backspace`/`DeleteForward` can succeed as boundary no-ops; detect via char count.
            || after_char_count != before_char_count;

        let buffer_derived_changed = matches!(command, Command::Style(_));

        if !(view_changed || buffer_text_changed || buffer_derived_changed) {
            return Ok(result);
        }

        let change_type = if buffer_text_changed {
            StateChangeType::DocumentModified
        } else {
            change_type.unwrap_or(StateChangeType::ViewportChanged)
        };

        if buffer_text_changed || buffer_derived_changed {
            // Broadcast to all views of this buffer.
            let delta_arc = delta.clone();
            if let Some(delta_arc) = delta_arc {
                buffer.last_text_delta = Some(delta_arc.clone());
                for other in views.values_mut() {
                    if other.buffer != buffer_id {
                        continue;
                    }
                    other.last_text_delta = Some(delta_arc.clone());
                }
            } else {
                buffer.last_text_delta = None;
            }

            // Shift other views' cursor/selections through the delta (if any).
            if let Some(ref delta_arc) = delta {
                let new_index = buffer.executor.editor().line_index();
                for (other_id, other) in views.iter_mut() {
                    if other.buffer != buffer_id || *other_id == view_id {
                        continue;
                    }

                    other.core.cursor_position = apply_position_delta(
                        &before_line_index,
                        new_index,
                        other.core.cursor_position,
                        delta_arc,
                    );

                    if let Some(ref sel) = other.core.selection {
                        other.core.selection = Some(apply_selection_delta(
                            &before_line_index,
                            new_index,
                            sel,
                            delta_arc,
                        ));
                    }

                    for sel in &mut other.core.secondary_selections {
                        *sel = apply_selection_delta(&before_line_index, new_index, sel, delta_arc);
                    }

                    if let Some(ref mut session) = other.core.snippet_session {
                        session.apply_delta(delta_arc);
                    }
                }

                // Keep navigation state stable under edits.
                buffer.bookmarks.apply_delta(delta_arc);
                buffer.marks.apply_delta(delta_arc);
                for other in views.values_mut() {
                    if other.buffer != buffer_id {
                        continue;
                    }
                    other.jump_list.apply_delta(buffer_id, delta_arc);
                }
            }

            for other in views.values_mut() {
                if other.buffer != buffer_id {
                    continue;
                }
                Self::notify_view(other, change_type, delta.clone());
            }

            if buffer_text_changed && let Some(uri) = buffer.meta.uri.as_deref() {
                self.intelligence.mark_stale_for_uri(uri);
            }

            buffer.version = buffer.version.saturating_add(1);
        } else {
            Self::notify_view(view, change_type, None);
        }

        Ok(result)
    }

    /// Return `true` if the given view currently has an active snippet session.
    ///
    /// Snippet sessions are created by snippet inserts (for example LSP completion items with
    /// `insertTextFormat == 2`) and allow tab/shift-tab navigation between placeholders.
    pub fn has_active_snippet_session(&self, view_id: ViewId) -> Result<bool, WorkspaceError> {
        let Some(view) = self.views.get(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        Ok(view
            .core
            .snippet_session
            .as_ref()
            .map(|s| s.is_active())
            .unwrap_or(false))
    }

    /// Toggle a bookmark at the **current cursor line** for the given view.
    ///
    /// Returns `true` if a bookmark was added, or `false` if an existing bookmark on that line was
    /// removed.
    pub fn toggle_bookmark_at_cursor_line(
        &mut self,
        view_id: ViewId,
    ) -> Result<bool, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        let Some(view) = self.views.get(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let line_start = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(view.core.cursor_position.line, 0);

        let added = buffer.bookmarks.toggle_line_start(line_start);

        for v in self.views.values_mut() {
            if v.buffer == buffer_id {
                Self::notify_view(v, StateChangeType::NavigationChanged, None);
            }
        }

        Ok(added)
    }

    /// Return all bookmark line numbers (0-based) for a buffer.
    pub fn bookmark_lines(&self, buffer_id: BufferId) -> Result<Vec<usize>, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer
            .bookmarks
            .line_numbers(buffer.executor.editor().line_index()))
    }

    /// Clear all bookmarks for a buffer.
    pub fn clear_bookmarks(&mut self, buffer_id: BufferId) -> Result<(), WorkspaceError> {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        buffer.bookmarks.clear();

        for v in self.views.values_mut() {
            if v.buffer == buffer_id {
                Self::notify_view(v, StateChangeType::NavigationChanged, None);
            }
        }

        Ok(())
    }

    fn move_view_cursor_to_anchor(
        view: &mut ViewEntry,
        buffer: &BufferEntry,
        anchor: TextAnchor,
    ) -> Position {
        let (line, column) = buffer
            .executor
            .editor()
            .line_index()
            .char_offset_to_position(anchor.offset);
        view.core.cursor_position = Position::new(line, column);
        view.core.preferred_x_cells = buffer
            .executor
            .editor()
            .logical_position_to_visual(line, column)
            .map(|(_, x)| x);
        view.core.selection = None;
        view.core.secondary_selections.clear();
        view.core.cursor_position
    }

    /// Move the cursor to the next bookmark (wrapping to the first bookmark).
    ///
    /// Returns the new cursor position, or `None` if there are no bookmarks.
    pub fn goto_next_bookmark(
        &mut self,
        view_id: ViewId,
    ) -> Result<Option<Position>, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let current_line_start = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(
                self.views
                    .get(&view_id)
                    .ok_or(WorkspaceError::ViewNotFound(view_id))?
                    .core
                    .cursor_position
                    .line,
                0,
            );

        let Some(target) = buffer.bookmarks.next_after_line_start(current_line_start) else {
            return Ok(None);
        };

        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let pos = Self::move_view_cursor_to_anchor(view, buffer, target);
        Self::notify_view(view, StateChangeType::SelectionChanged, None);
        Ok(Some(pos))
    }

    /// Move the cursor to the previous bookmark (wrapping to the last bookmark).
    ///
    /// Returns the new cursor position, or `None` if there are no bookmarks.
    pub fn goto_prev_bookmark(
        &mut self,
        view_id: ViewId,
    ) -> Result<Option<Position>, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let current_line_start = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(
                self.views
                    .get(&view_id)
                    .ok_or(WorkspaceError::ViewNotFound(view_id))?
                    .core
                    .cursor_position
                    .line,
                0,
            );

        let Some(target) = buffer.bookmarks.prev_before_line_start(current_line_start) else {
            return Ok(None);
        };

        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let pos = Self::move_view_cursor_to_anchor(view, buffer, target);
        Self::notify_view(view, StateChangeType::SelectionChanged, None);
        Ok(Some(pos))
    }

    /// Set (or replace) a named mark at the current cursor position of the given view.
    pub fn set_mark_at_cursor(
        &mut self,
        view_id: ViewId,
        name: String,
    ) -> Result<(), WorkspaceError> {
        if name.trim().is_empty() {
            return Err(WorkspaceError::CommandFailed {
                view: view_id,
                message: "Mark name cannot be empty".to_string(),
            });
        }

        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        let Some(view) = self.views.get(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let pos = view.core.cursor_position;
        let offset = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(pos.line, pos.column);
        buffer.marks.set(name, offset);

        for v in self.views.values_mut() {
            if v.buffer == buffer_id {
                Self::notify_view(v, StateChangeType::NavigationChanged, None);
            }
        }

        Ok(())
    }

    /// Move the cursor to a named mark (if present).
    ///
    /// Returns the new cursor position, or `None` if the mark does not exist.
    pub fn goto_mark(
        &mut self,
        view_id: ViewId,
        name: &str,
    ) -> Result<Option<Position>, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let Some(anchor) = buffer.marks.get(name) else {
            return Ok(None);
        };

        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let pos = Self::move_view_cursor_to_anchor(view, buffer, anchor);
        Self::notify_view(view, StateChangeType::SelectionChanged, None);
        Ok(Some(pos))
    }

    /// Remove a named mark from a buffer.
    ///
    /// Returns `true` if the mark existed.
    pub fn clear_mark(&mut self, buffer_id: BufferId, name: &str) -> Result<bool, WorkspaceError> {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        let existed = buffer.marks.remove(name);
        if existed {
            for v in self.views.values_mut() {
                if v.buffer == buffer_id {
                    Self::notify_view(v, StateChangeType::NavigationChanged, None);
                }
            }
        }
        Ok(existed)
    }

    /// Return all mark names for a buffer (deterministic order).
    pub fn mark_names(&self, buffer_id: BufferId) -> Result<Vec<String>, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.marks.names())
    }

    /// Clear all marks for a buffer.
    pub fn clear_all_marks(&mut self, buffer_id: BufferId) -> Result<(), WorkspaceError> {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        buffer.marks.clear();
        for v in self.views.values_mut() {
            if v.buffer == buffer_id {
                Self::notify_view(v, StateChangeType::NavigationChanged, None);
            }
        }
        Ok(())
    }

    /// Record the current cursor position as a jump-list location for a view.
    ///
    /// Typical usage: call this *before* performing a “jump” (go-to-definition, search result,
    /// symbol navigation, ...).
    pub fn push_jump_location(&mut self, view_id: ViewId) -> Result<(), WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let pos = view.core.cursor_position;
        let offset = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(pos.line, pos.column);

        view.jump_list.record(JumpEntry {
            buffer_id,
            anchor: TextAnchor::new(offset, AnchorBias::Right),
        });

        Self::notify_view(view, StateChangeType::NavigationChanged, None);
        Ok(())
    }

    /// Jump back in the view's jump list.
    ///
    /// Returns the navigation target (including the buffer id). If the target belongs to the
    /// current view's buffer, this method also moves the cursor and clears selection.
    pub fn jump_back(&mut self, view_id: ViewId) -> Result<Option<JumpTarget>, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let current_pos = self
            .views
            .get(&view_id)
            .ok_or(WorkspaceError::ViewNotFound(view_id))?
            .core
            .cursor_position;
        let current_offset = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(current_pos.line, current_pos.column);
        let current = JumpEntry {
            buffer_id,
            anchor: TextAnchor::new(current_offset, AnchorBias::Right),
        };

        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(target) = view.jump_list.back(current) else {
            return Ok(None);
        };

        let Some(target_buffer) = self.buffers.get(&target.buffer_id) else {
            Self::notify_view(view, StateChangeType::NavigationChanged, None);
            return Ok(None);
        };

        let (line, column) = target_buffer
            .executor
            .editor()
            .line_index()
            .char_offset_to_position(target.anchor.offset);
        let target_pos = Position::new(line, column);

        let out = JumpTarget {
            buffer_id: target.buffer_id,
            position: target_pos,
        };

        if target.buffer_id == buffer_id {
            Self::move_view_cursor_to_anchor(view, buffer, target.anchor);
            Self::notify_view(view, StateChangeType::SelectionChanged, None);
        } else {
            Self::notify_view(view, StateChangeType::NavigationChanged, None);
        }

        Ok(Some(out))
    }

    /// Jump forward in the view's jump list.
    ///
    /// Returns the navigation target (including the buffer id). If the target belongs to the
    /// current view's buffer, this method also moves the cursor and clears selection.
    pub fn jump_forward(&mut self, view_id: ViewId) -> Result<Option<JumpTarget>, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let current_pos = self
            .views
            .get(&view_id)
            .ok_or(WorkspaceError::ViewNotFound(view_id))?
            .core
            .cursor_position;
        let current_offset = buffer
            .executor
            .editor()
            .line_index()
            .position_to_char_offset(current_pos.line, current_pos.column);
        let current = JumpEntry {
            buffer_id,
            anchor: TextAnchor::new(current_offset, AnchorBias::Right),
        };

        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(target) = view.jump_list.forward(current) else {
            return Ok(None);
        };

        let Some(target_buffer) = self.buffers.get(&target.buffer_id) else {
            Self::notify_view(view, StateChangeType::NavigationChanged, None);
            return Ok(None);
        };

        let (line, column) = target_buffer
            .executor
            .editor()
            .line_index()
            .char_offset_to_position(target.anchor.offset);
        let target_pos = Position::new(line, column);

        let out = JumpTarget {
            buffer_id: target.buffer_id,
            position: target_pos,
        };

        if target.buffer_id == buffer_id {
            Self::move_view_cursor_to_anchor(view, buffer, target.anchor);
            Self::notify_view(view, StateChangeType::SelectionChanged, None);
        } else {
            Self::notify_view(view, StateChangeType::NavigationChanged, None);
        }

        Ok(Some(out))
    }

    /// Clear the jump list (both back/forward stacks) for a view.
    pub fn clear_jump_list(&mut self, view_id: ViewId) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        view.jump_list.clear();
        Self::notify_view(view, StateChangeType::NavigationChanged, None);
        Ok(())
    }

    /// Apply a previously produced [`JumpTarget`] to a view (moves the cursor and clears
    /// selection).
    pub fn apply_jump_target(
        &mut self,
        view_id: ViewId,
        target: JumpTarget,
    ) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        if view.buffer != target.buffer_id {
            return Err(WorkspaceError::CommandFailed {
                view: view_id,
                message: "JumpTarget buffer does not match view buffer".to_string(),
            });
        }

        let Some(buffer) = self.buffers.get(&view.buffer) else {
            return Err(WorkspaceError::BufferNotFound(view.buffer));
        };

        view.core.cursor_position = target.position;
        view.core.preferred_x_cells = buffer
            .executor
            .editor()
            .logical_position_to_visual(target.position.line, target.position.column)
            .map(|(_, x)| x);
        view.core.selection = None;
        view.core.secondary_selections.clear();

        Self::notify_view(view, StateChangeType::SelectionChanged, None);
        Ok(())
    }

    /// Set the viewport height for a view (used for `ViewportState` calculations).
    pub fn set_viewport_height(
        &mut self,
        view_id: ViewId,
        height: usize,
    ) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        view.viewport_height = Some(height);
        Ok(())
    }

    /// Set the scroll position (top visual row) for a view.
    pub fn set_scroll_top(
        &mut self,
        view_id: ViewId,
        scroll_top: usize,
    ) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        view.scroll_top = scroll_top;
        Ok(())
    }

    /// Set sub-row smooth-scroll offset for a view.
    pub fn set_scroll_sub_row_offset(
        &mut self,
        view_id: ViewId,
        sub_row_offset: u16,
    ) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        view.scroll_sub_row_offset = sub_row_offset;
        Ok(())
    }

    /// Set overscan rows for a view.
    pub fn set_overscan_rows(
        &mut self,
        view_id: ViewId,
        overscan_rows: usize,
    ) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        view.overscan_rows = overscan_rows;
        Ok(())
    }

    /// Set smooth-scroll state for a view.
    pub fn set_smooth_scroll_state(
        &mut self,
        view_id: ViewId,
        state: ViewSmoothScrollState,
    ) -> Result<(), WorkspaceError> {
        let Some(view) = self.views.get_mut(&view_id) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        view.scroll_top = state.top_visual_row;
        view.scroll_sub_row_offset = state.sub_row_offset;
        view.overscan_rows = state.overscan_rows;
        Ok(())
    }

    /// Get viewport state for a view, including total visual lines and overscan prefetch range.
    pub fn viewport_state_for_view(
        &mut self,
        view_id: ViewId,
    ) -> Result<WorkspaceViewportState, WorkspaceError> {
        let Some((
            buffer_id,
            view_core,
            scroll_top,
            viewport_height,
            sub_row_offset,
            overscan_rows,
        )) = self.views.get(&view_id).map(|v| {
            (
                v.buffer,
                v.core.clone(),
                v.scroll_top,
                v.viewport_height,
                v.scroll_sub_row_offset,
                v.overscan_rows,
            )
        })
        else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        view_core.apply_to_executor(&mut buffer.executor);
        let editor = buffer.executor.editor();

        let total_visual_lines = editor.visual_line_count();
        let visible_end = if let Some(height) = viewport_height {
            scroll_top.saturating_add(height).min(total_visual_lines)
        } else {
            total_visual_lines
        };
        let visible_lines = scroll_top.min(total_visual_lines)..visible_end;
        let prefetch_start = visible_lines.start.saturating_sub(overscan_rows);
        let prefetch_end = visible_lines
            .end
            .saturating_add(overscan_rows)
            .min(total_visual_lines);

        Ok(WorkspaceViewportState {
            width: editor.viewport_width(),
            height: viewport_height,
            scroll_top,
            visible_lines,
            total_visual_lines,
            smooth_scroll: ViewSmoothScrollState {
                top_visual_row: scroll_top,
                sub_row_offset,
                overscan_rows,
            },
            prefetch_lines: prefetch_start..prefetch_end,
        })
    }

    /// Get total visual lines for a view (wrap + folding aware).
    pub fn total_visual_lines_for_view(
        &mut self,
        view_id: ViewId,
    ) -> Result<usize, WorkspaceError> {
        Ok(self.viewport_state_for_view(view_id)?.total_visual_lines)
    }

    /// Map global visual row to `(logical_line, visual_in_logical)` for a view.
    pub fn visual_to_logical_for_view(
        &mut self,
        view_id: ViewId,
        visual_row: usize,
    ) -> Result<(usize, usize), WorkspaceError> {
        let Some((buffer_id, view_core)) =
            self.views.get(&view_id).map(|v| (v.buffer, v.core.clone()))
        else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        view_core.apply_to_executor(&mut buffer.executor);
        Ok(buffer.executor.editor().visual_to_logical_line(visual_row))
    }

    /// Map logical position to global visual `(row, x_cells)` for a view.
    pub fn logical_to_visual_for_view(
        &mut self,
        view_id: ViewId,
        line: usize,
        column: usize,
    ) -> Result<Option<(usize, usize)>, WorkspaceError> {
        let Some((buffer_id, view_core)) =
            self.views.get(&view_id).map(|v| (v.buffer, v.core.clone()))
        else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        view_core.apply_to_executor(&mut buffer.executor);
        Ok(buffer
            .executor
            .editor()
            .logical_position_to_visual(line, column))
    }

    /// Map visual `(row, x_cells)` back to logical position for a view.
    pub fn visual_position_to_logical_for_view(
        &mut self,
        view_id: ViewId,
        visual_row: usize,
        x_cells: usize,
    ) -> Result<Option<Position>, WorkspaceError> {
        let Some((buffer_id, view_core)) =
            self.views.get(&view_id).map(|v| (v.buffer, v.core.clone()))
        else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        view_core.apply_to_executor(&mut buffer.executor);
        Ok(buffer
            .executor
            .editor()
            .visual_position_to_logical(visual_row, x_cells))
    }

    /// Get the full document text for a buffer.
    pub fn buffer_text(&self, buffer_id: BufferId) -> Result<String, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        Ok(buffer.executor.editor().get_text())
    }

    /// Get the full document text converted to the buffer's preferred line ending for saving.
    pub fn buffer_text_for_saving(&self, buffer_id: BufferId) -> Result<String, WorkspaceError> {
        let Some(buffer) = self.buffers.get(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };
        let text = buffer.executor.editor().get_text();
        Ok(buffer.executor.line_ending().apply_to_text(&text))
    }

    /// Get the full document text converted to the view's preferred line ending for saving.
    pub fn text_for_saving_for_view(&self, view_id: ViewId) -> Result<String, WorkspaceError> {
        let buffer_id = self.buffer_id_for_view(view_id)?;
        self.buffer_text_for_saving(buffer_id)
    }

    /// Get styled viewport content for a view (by visual line).
    pub fn get_viewport_content_styled(
        &mut self,
        view_id: ViewId,
        start_visual_row: usize,
        count: usize,
    ) -> Result<crate::HeadlessGrid, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let view_core = self
            .views
            .get(&view_id)
            .map(|v| v.core.clone())
            .ok_or(WorkspaceError::ViewNotFound(view_id))?;

        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        view_core.apply_to_executor(&mut buffer.executor);
        Ok(buffer
            .executor
            .editor()
            .get_headless_grid_styled(start_visual_row, count))
    }

    /// Get lightweight minimap content for a view (by visual line).
    pub fn get_minimap_content(
        &mut self,
        view_id: ViewId,
        start_visual_row: usize,
        count: usize,
    ) -> Result<crate::MinimapGrid, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let view_core = self
            .views
            .get(&view_id)
            .map(|v| v.core.clone())
            .ok_or(WorkspaceError::ViewNotFound(view_id))?;

        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        view_core.apply_to_executor(&mut buffer.executor);
        Ok(buffer
            .executor
            .editor()
            .get_minimap_grid(start_visual_row, count))
    }

    /// Get a decoration-aware composed viewport snapshot for a view (by composed visual line).
    ///
    /// This snapshot can include virtual text (inlay hints, code lens) injected from the buffer's
    /// decoration layers. See [`crate::EditorCore::get_headless_grid_composed`] for details.
    pub fn get_viewport_content_composed(
        &mut self,
        view_id: ViewId,
        start_visual_row: usize,
        count: usize,
    ) -> Result<crate::ComposedGrid, WorkspaceError> {
        let Some(buffer_id) = self.views.get(&view_id).map(|v| v.buffer) else {
            return Err(WorkspaceError::ViewNotFound(view_id));
        };

        let view_core = self
            .views
            .get(&view_id)
            .map(|v| v.core.clone())
            .ok_or(WorkspaceError::ViewNotFound(view_id))?;

        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        view_core.apply_to_executor(&mut buffer.executor);
        Ok(buffer
            .executor
            .editor()
            .get_headless_grid_composed(start_visual_row, count))
    }

    /// Apply derived-state edits to a buffer and broadcast them to all views of that buffer.
    pub fn apply_processing_edits<I>(
        &mut self,
        buffer_id: BufferId,
        edits: I,
    ) -> Result<(), WorkspaceError>
    where
        I: IntoIterator<Item = ProcessingEdit>,
    {
        let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
            return Err(WorkspaceError::BufferNotFound(buffer_id));
        };

        let mut style_changed = false;
        let mut folding_changed = false;
        let mut diagnostics_changed = false;
        let mut decorations_changed = false;
        let mut symbols_changed = false;

        for edit in edits {
            match edit {
                ProcessingEdit::ReplaceStyleLayer { layer, intervals } => {
                    buffer
                        .executor
                        .editor_mut()
                        .replace_style_layer(layer, intervals);
                    style_changed = true;
                }
                ProcessingEdit::ClearStyleLayer { layer } => {
                    buffer.executor.editor_mut().clear_style_layer(layer);
                    style_changed = true;
                }
                ProcessingEdit::ReplaceFoldingRegions {
                    regions,
                    preserve_collapsed,
                } => {
                    buffer
                        .executor
                        .editor_mut()
                        .replace_folding_regions(regions, preserve_collapsed);
                    folding_changed = true;
                }
                ProcessingEdit::ClearFoldingRegions => {
                    buffer.executor.editor_mut().clear_derived_folding_regions();
                    folding_changed = true;
                }
                ProcessingEdit::ReplaceDiagnostics { diagnostics } => {
                    buffer
                        .executor
                        .editor_mut()
                        .replace_diagnostics(diagnostics);
                    diagnostics_changed = true;
                }
                ProcessingEdit::ClearDiagnostics => {
                    buffer.executor.editor_mut().clear_diagnostics();
                    diagnostics_changed = true;
                }
                ProcessingEdit::ReplaceDecorations { layer, decorations } => {
                    buffer
                        .executor
                        .editor_mut()
                        .replace_decorations(layer, decorations);
                    decorations_changed = true;
                }
                ProcessingEdit::ClearDecorations { layer } => {
                    buffer.executor.editor_mut().clear_decorations(layer);
                    decorations_changed = true;
                }
                ProcessingEdit::ReplaceDocumentSymbols { symbols } => {
                    buffer
                        .executor
                        .editor_mut()
                        .replace_document_symbols(symbols);
                    symbols_changed = true;
                }
                ProcessingEdit::ClearDocumentSymbols => {
                    buffer.executor.editor_mut().clear_document_symbols();
                    symbols_changed = true;
                }
            }
        }

        let change_type = if folding_changed {
            Some(StateChangeType::FoldingChanged)
        } else if style_changed {
            Some(StateChangeType::StyleChanged)
        } else if decorations_changed {
            Some(StateChangeType::DecorationsChanged)
        } else if diagnostics_changed {
            Some(StateChangeType::DiagnosticsChanged)
        } else if symbols_changed {
            Some(StateChangeType::SymbolsChanged)
        } else {
            None
        };

        if let Some(change_type) = change_type {
            for view in self.views.values_mut() {
                if view.buffer == buffer_id {
                    Self::notify_view(view, change_type, None);
                }
            }
            buffer.version = buffer.version.saturating_add(1);
        }

        Ok(())
    }

    /// Search across all open buffers in the workspace.
    ///
    /// - This is purely in-memory (no file I/O).
    /// - Match ranges are returned as **character offsets** (half-open).
    pub fn search_all_open_buffers(
        &self,
        query: &str,
        options: SearchOptions,
    ) -> Result<Vec<WorkspaceSearchResult>, SearchError> {
        let mut out: Vec<WorkspaceSearchResult> = Vec::new();

        for (id, entry) in &self.buffers {
            let text = entry.executor.editor().get_text();
            let matches = find_all(&text, query, options)?;
            if matches.is_empty() {
                continue;
            }

            out.push(WorkspaceSearchResult {
                id: *id,
                uri: entry.meta.uri.clone(),
                matches,
            });
        }

        Ok(out)
    }

    /// Apply a set of text edits to multiple open buffers.
    ///
    /// - This is purely in-memory (no file I/O).
    /// - Edits are applied as a single undoable step **per buffer**.
    /// - Buffers are applied in deterministic `BufferId` order.
    pub fn apply_text_edits<I>(
        &mut self,
        edits: I,
    ) -> Result<Vec<(BufferId, usize)>, WorkspaceError>
    where
        I: IntoIterator<Item = (BufferId, Vec<TextEditSpec>)>,
    {
        let mut by_id: BTreeMap<BufferId, Vec<TextEditSpec>> = BTreeMap::new();
        for (id, mut buffer_edits) in edits {
            by_id.entry(id).or_default().append(&mut buffer_edits);
        }

        let mut applied: Vec<(BufferId, usize)> = Vec::new();
        for (buffer_id, buffer_edits) in by_id {
            let edit_count = buffer_edits.len();
            if edit_count == 0 {
                continue;
            }

            let Some(buffer) = self.buffers.get_mut(&buffer_id) else {
                return Err(WorkspaceError::BufferNotFound(buffer_id));
            };

            let before_line_index = buffer.executor.editor().line_index().clone();
            let before_char_count = buffer.executor.editor().char_count();

            // Apply without relying on any specific view selection: load a neutral view state.
            let neutral = ViewCore {
                cursor_position: Position::new(0, 0),
                selection: None,
                secondary_selections: Vec::new(),
                viewport_width: buffer.executor.editor().viewport_width().max(1),
                wrap_mode: buffer.executor.editor().layout_engine().wrap_mode(),
                wrap_indent: buffer.executor.editor().layout_engine().wrap_indent(),
                tab_width: buffer.executor.editor().layout_engine().tab_width(),
                tab_key_behavior: buffer.executor.tab_key_behavior(),
                indentation_config: buffer.executor.indentation_config().clone(),
                auto_pairs: buffer.executor.auto_pairs_config().clone(),
                snippet_session: None,
                preferred_x_cells: None,
            };
            neutral.apply_to_executor(&mut buffer.executor);

            buffer
                .executor
                .execute(Command::Edit(EditCommand::ApplyTextEdits {
                    edits: buffer_edits,
                }))
                .map_err(|err| WorkspaceError::ApplyEditsFailed {
                    buffer: buffer_id,
                    message: err.to_string(),
                })?;

            let delta = buffer.executor.take_last_text_delta().map(Arc::new);
            let after_char_count = buffer.executor.editor().char_count();
            let changed = delta.is_some() || after_char_count != before_char_count;

            if changed {
                if let Some(uri) = buffer.meta.uri.as_deref() {
                    self.intelligence.mark_stale_for_uri(uri);
                }

                if let Some(ref delta_arc) = delta {
                    buffer.last_text_delta = Some(delta_arc.clone());
                    let new_index = buffer.executor.editor().line_index();
                    for view in self.views.values_mut() {
                        if view.buffer != buffer_id {
                            continue;
                        }

                        view.last_text_delta = Some(delta_arc.clone());

                        view.core.cursor_position = apply_position_delta(
                            &before_line_index,
                            new_index,
                            view.core.cursor_position,
                            delta_arc,
                        );
                        if let Some(ref sel) = view.core.selection {
                            view.core.selection = Some(apply_selection_delta(
                                &before_line_index,
                                new_index,
                                sel,
                                delta_arc,
                            ));
                        }
                        for sel in &mut view.core.secondary_selections {
                            *sel = apply_selection_delta(
                                &before_line_index,
                                new_index,
                                sel,
                                delta_arc,
                            );
                        }

                        Self::notify_view(
                            view,
                            StateChangeType::DocumentModified,
                            Some(delta_arc.clone()),
                        );
                    }
                } else {
                    buffer.last_text_delta = None;
                    for view in self.views.values_mut() {
                        if view.buffer == buffer_id {
                            Self::notify_view(view, StateChangeType::DocumentModified, None);
                        }
                    }
                }

                buffer.version = buffer.version.saturating_add(1);
            }

            applied.push((buffer_id, edit_count));
        }

        Ok(applied)
    }
}