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
use crate::model::buffer::{Buffer, LineNumber};
use crate::model::cursor::{Cursor, Cursors};
use crate::model::document_model::{
DocumentCapabilities, DocumentModel, DocumentPosition, ViewportContent, ViewportLine,
};
use crate::model::event::{
Event, MarginContentData, MarginPositionData, OverlayFace as EventOverlayFace, PopupData,
PopupPositionData,
};
use crate::model::filesystem::FileSystem;
use crate::model::marker::MarkerList;
use crate::primitives::grammar::GrammarRegistry;
use crate::primitives::highlight_engine::HighlightEngine;
use crate::primitives::highlighter::Language;
use crate::primitives::indent::IndentCalculator;
use crate::primitives::reference_highlighter::ReferenceHighlighter;
use crate::primitives::text_property::TextPropertyManager;
use crate::view::bracket_highlight_overlay::BracketHighlightOverlay;
use crate::view::margin::{MarginAnnotation, MarginContent, MarginManager, MarginPosition};
use crate::view::overlay::{Overlay, OverlayFace, OverlayManager, UnderlineStyle};
use crate::view::popup::{
Popup, PopupContent, PopupKind, PopupListItem, PopupManager, PopupPosition,
};
use crate::view::reference_highlight_overlay::ReferenceHighlightOverlay;
use crate::view::virtual_text::VirtualTextManager;
use anyhow::Result;
use ratatui::style::{Color, Style};
use rust_i18n::t;
use std::cell::RefCell;
use std::ops::Range;
use std::sync::Arc;
/// Display mode for a buffer
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ViewMode {
/// Plain source rendering
Source,
/// Semi-WYSIWYG compose rendering
Compose,
}
/// The complete editor state - everything needed to represent the current editing session
///
/// NOTE: Viewport is NOT stored here - it lives in SplitViewState.
/// This is because viewport is view-specific (each split can view the same buffer
/// at different scroll positions), while EditorState represents the buffer content.
pub struct EditorState {
/// The text buffer
pub buffer: Buffer,
/// All cursors
pub cursors: Cursors,
/// Syntax highlighter (tree-sitter or TextMate based on language)
pub highlighter: HighlightEngine,
/// Auto-indent calculator for smart indentation (RefCell for interior mutability)
pub indent_calculator: RefCell<IndentCalculator>,
/// Overlays for visual decorations (underlines, highlights, etc.)
pub overlays: OverlayManager,
/// Marker list for content-anchored overlay positions
pub marker_list: MarkerList,
/// Virtual text manager for inline hints (type hints, parameter hints, etc.)
pub virtual_texts: VirtualTextManager,
/// Popups for floating windows (completion, documentation, etc.)
pub popups: PopupManager,
/// Margins for line numbers, annotations, gutter symbols, etc.)
pub margins: MarginManager,
/// Cached line number for primary cursor (0-indexed)
/// Maintained incrementally to avoid O(n) scanning on every render
pub primary_cursor_line_number: LineNumber,
/// Current mode (for modal editing, if implemented)
pub mode: String,
/// Text properties for virtual buffers (embedded metadata in text ranges)
/// Used by virtual buffers to store location info, severity, etc.
pub text_properties: TextPropertyManager,
/// Whether to show cursors in this buffer (default true)
/// Can be set to false for virtual buffers like diagnostics panels
pub show_cursors: bool,
/// Whether editing is disabled for this buffer (default false)
/// When true, typing, deletion, cut/paste, undo/redo are blocked
/// but navigation, selection, and copy are still allowed
pub editing_disabled: bool,
/// Whether this buffer is a composite buffer (multi-pane view)
/// When true, the buffer content is rendered by the composite renderer
/// instead of the normal buffer rendering path
pub is_composite_buffer: bool,
/// Whether to show whitespace tab indicators (→) for this buffer
/// Set based on language config; defaults to true
pub show_whitespace_tabs: bool,
/// Whether pressing Tab should insert a tab character instead of spaces.
/// Set based on language config; defaults to false (insert spaces).
pub use_tabs: bool,
/// Tab size (number of spaces per tab character) for rendering.
/// Used for visual display of tab characters and indent calculations.
pub tab_size: usize,
/// Semantic highlighter for word occurrence highlighting
pub reference_highlighter: ReferenceHighlighter,
/// View mode for this buffer (Source or Compose)
pub view_mode: ViewMode,
/// Debug mode: show highlight/overlay byte ranges
/// When enabled, each character shows its byte position and highlight info
pub debug_highlight_mode: bool,
/// Optional compose width for centered rendering
pub compose_width: Option<u16>,
/// Previously configured line number visibility (to restore after Compose)
pub compose_prev_line_numbers: Option<bool>,
/// Optional column guides (e.g., for tables) supplied by layout hints
pub compose_column_guides: Option<Vec<u16>>,
/// Optional transformed view payload for current viewport (tokens + map)
pub view_transform: Option<fresh_core::api::ViewTransformPayload>,
/// Debounced semantic highlight cache
pub reference_highlight_overlay: ReferenceHighlightOverlay,
/// Bracket matching highlight overlay
pub bracket_highlight_overlay: BracketHighlightOverlay,
/// Cached LSP semantic tokens (converted to buffer byte ranges)
pub semantic_tokens: Option<SemanticTokenStore>,
/// The detected language for this buffer (e.g., "rust", "python", "text")
pub language: String,
}
impl EditorState {
/// Create a new editor state with an empty buffer
///
/// Note: width/height parameters are kept for backward compatibility but
/// are no longer used - viewport is now owned by SplitViewState.
pub fn new(
_width: u16,
_height: u16,
large_file_threshold: usize,
fs: Arc<dyn FileSystem + Send + Sync>,
) -> Self {
Self {
buffer: Buffer::new(large_file_threshold, fs),
cursors: Cursors::new(),
highlighter: HighlightEngine::None, // No file path, so no syntax highlighting
indent_calculator: RefCell::new(IndentCalculator::new()),
overlays: OverlayManager::new(),
marker_list: MarkerList::new(),
virtual_texts: VirtualTextManager::new(),
popups: PopupManager::new(),
margins: MarginManager::new(),
primary_cursor_line_number: LineNumber::Absolute(0), // Start at line 0
mode: "insert".to_string(),
text_properties: TextPropertyManager::new(),
show_cursors: true,
editing_disabled: false,
is_composite_buffer: false,
show_whitespace_tabs: true,
use_tabs: false,
tab_size: 4, // Default tab size
reference_highlighter: ReferenceHighlighter::new(),
view_mode: ViewMode::Source,
debug_highlight_mode: false,
compose_width: None,
compose_prev_line_numbers: None,
compose_column_guides: None,
view_transform: None,
reference_highlight_overlay: ReferenceHighlightOverlay::new(),
bracket_highlight_overlay: BracketHighlightOverlay::new(),
semantic_tokens: None,
language: "text".to_string(), // Default to plain text
}
}
/// Set the syntax highlighting language based on a filename or extension
/// This allows virtual buffers to get highlighting even without a real file path
pub fn set_language_from_name(&mut self, name: &str, registry: &GrammarRegistry) {
// Handle virtual buffer names like "*OLD:test.ts*" or "*OURS*.c"
// 1. Strip surrounding * characters
// 2. Extract filename after any prefix like "OLD:" or "NEW:"
let cleaned_name = name.trim_matches('*');
let filename = if let Some(pos) = cleaned_name.rfind(':') {
// Extract part after the last colon (e.g., "OLD:test.ts" -> "test.ts")
&cleaned_name[pos + 1..]
} else {
cleaned_name
};
let path = std::path::Path::new(filename);
self.highlighter = HighlightEngine::for_file(path, registry);
if let Some(language) = Language::from_path(path) {
self.reference_highlighter.set_language(&language);
self.language = language.to_string();
} else {
self.language = "text".to_string();
}
tracing::debug!(
"Set highlighter for virtual buffer based on name: {} -> {} (backend: {}, language: {})",
name,
filename,
self.highlighter.backend_name(),
self.language
);
}
/// Create an editor state from a file
///
/// Note: width/height parameters are kept for backward compatibility but
/// are no longer used - viewport is now owned by SplitViewState.
pub fn from_file(
path: &std::path::Path,
_width: u16,
_height: u16,
large_file_threshold: usize,
registry: &GrammarRegistry,
fs: Arc<dyn FileSystem + Send + Sync>,
) -> anyhow::Result<Self> {
let buffer = Buffer::load_from_file(path, large_file_threshold, fs)?;
let highlighter = HighlightEngine::for_file(path, registry);
let language = Language::from_path(path);
let mut reference_highlighter = ReferenceHighlighter::new();
let language_name = if let Some(lang) = &language {
reference_highlighter.set_language(lang);
lang.to_string()
} else {
"text".to_string()
};
let mut marker_list = MarkerList::new();
if !buffer.is_empty() {
marker_list.adjust_for_insert(0, buffer.len());
}
Ok(Self {
buffer,
cursors: Cursors::new(),
highlighter,
indent_calculator: RefCell::new(IndentCalculator::new()),
overlays: OverlayManager::new(),
marker_list,
virtual_texts: VirtualTextManager::new(),
popups: PopupManager::new(),
margins: MarginManager::new(),
primary_cursor_line_number: LineNumber::Absolute(0),
mode: "insert".to_string(),
text_properties: TextPropertyManager::new(),
show_cursors: true,
editing_disabled: false,
is_composite_buffer: false,
show_whitespace_tabs: true,
use_tabs: false,
tab_size: 4,
reference_highlighter,
view_mode: ViewMode::Source,
debug_highlight_mode: false,
compose_width: None,
compose_prev_line_numbers: None,
compose_column_guides: None,
view_transform: None,
reference_highlight_overlay: ReferenceHighlightOverlay::new(),
bracket_highlight_overlay: BracketHighlightOverlay::new(),
semantic_tokens: None,
language: language_name,
})
}
/// Create an editor state from a file with language configuration.
///
/// This version uses the provided languages configuration for syntax detection,
/// allowing user-configured filename patterns to be respected for highlighting.
///
/// Note: width/height parameters are kept for backward compatibility but
/// are no longer used - viewport is now owned by SplitViewState.
pub fn from_file_with_languages(
path: &std::path::Path,
_width: u16,
_height: u16,
large_file_threshold: usize,
registry: &GrammarRegistry,
languages: &std::collections::HashMap<String, crate::config::LanguageConfig>,
fs: Arc<dyn FileSystem + Send + Sync>,
) -> anyhow::Result<Self> {
let buffer = Buffer::load_from_file(path, large_file_threshold, fs)?;
let highlighter = HighlightEngine::for_file_with_languages(path, registry, languages);
let language = Language::from_path(path);
let mut reference_highlighter = ReferenceHighlighter::new();
let language_name = if let Some(lang) = &language {
reference_highlighter.set_language(lang);
lang.to_string()
} else {
crate::services::lsp::manager::detect_language(path, languages)
.unwrap_or_else(|| "text".to_string())
};
let mut marker_list = MarkerList::new();
if !buffer.is_empty() {
marker_list.adjust_for_insert(0, buffer.len());
}
Ok(Self {
buffer,
cursors: Cursors::new(),
highlighter,
indent_calculator: RefCell::new(IndentCalculator::new()),
overlays: OverlayManager::new(),
marker_list,
virtual_texts: VirtualTextManager::new(),
popups: PopupManager::new(),
margins: MarginManager::new(),
primary_cursor_line_number: LineNumber::Absolute(0),
mode: "insert".to_string(),
text_properties: TextPropertyManager::new(),
show_cursors: true,
editing_disabled: false,
is_composite_buffer: false,
show_whitespace_tabs: true,
use_tabs: false,
tab_size: 4,
reference_highlighter,
view_mode: ViewMode::Source,
debug_highlight_mode: false,
compose_width: None,
compose_prev_line_numbers: None,
compose_column_guides: None,
view_transform: None,
reference_highlight_overlay: ReferenceHighlightOverlay::new(),
bracket_highlight_overlay: BracketHighlightOverlay::new(),
semantic_tokens: None,
language: language_name,
})
}
/// Handle an Insert event - adjusts markers, buffer, highlighter, cursors, and line numbers
fn apply_insert(
&mut self,
position: usize,
text: &str,
cursor_id: crate::model::event::CursorId,
) {
let newlines_inserted = text.matches('\n').count();
// CRITICAL: Adjust markers BEFORE modifying buffer
self.marker_list.adjust_for_insert(position, text.len());
self.margins.adjust_for_insert(position, text.len());
// Insert text into buffer
self.buffer.insert(position, text);
// Invalidate highlight cache for edited range
self.highlighter
.invalidate_range(position..position + text.len());
// Note: reference_highlight_overlay uses markers that auto-adjust,
// so no manual invalidation needed
// Adjust all cursors after the edit
self.cursors.adjust_for_edit(position, 0, text.len());
// Move the cursor that made the edit to the end of the insertion
if let Some(cursor) = self.cursors.get_mut(cursor_id) {
cursor.position = position + text.len();
cursor.clear_selection();
}
// Update primary cursor line number if this was the primary cursor
if cursor_id == self.cursors.primary_id() {
self.primary_cursor_line_number = match self.primary_cursor_line_number {
LineNumber::Absolute(line) => LineNumber::Absolute(line + newlines_inserted),
LineNumber::Relative {
line,
from_cached_line,
} => LineNumber::Relative {
line: line + newlines_inserted,
from_cached_line,
},
};
}
}
/// Handle a Delete event - adjusts markers, buffer, highlighter, cursors, and line numbers
fn apply_delete(
&mut self,
range: &std::ops::Range<usize>,
cursor_id: crate::model::event::CursorId,
deleted_text: &str,
) {
let len = range.len();
let newlines_deleted = deleted_text.matches('\n').count();
// CRITICAL: Adjust markers BEFORE modifying buffer
self.marker_list.adjust_for_delete(range.start, len);
self.margins.adjust_for_delete(range.start, len);
// Delete from buffer
self.buffer.delete(range.clone());
// Invalidate highlight cache for edited range
self.highlighter.invalidate_range(range.clone());
// Note: reference_highlight_overlay uses markers that auto-adjust,
// so no manual invalidation needed
// Adjust all cursors after the edit
self.cursors.adjust_for_edit(range.start, len, 0);
// Move the cursor that made the edit to the start of deletion
if let Some(cursor) = self.cursors.get_mut(cursor_id) {
cursor.position = range.start;
cursor.clear_selection();
}
// Update primary cursor line number if this was the primary cursor
if cursor_id == self.cursors.primary_id() {
self.primary_cursor_line_number = match self.primary_cursor_line_number {
LineNumber::Absolute(line) => {
LineNumber::Absolute(line.saturating_sub(newlines_deleted))
}
LineNumber::Relative {
line,
from_cached_line,
} => LineNumber::Relative {
line: line.saturating_sub(newlines_deleted),
from_cached_line,
},
};
}
}
/// Apply an event to the state - THE ONLY WAY TO MODIFY STATE
/// This is the heart of the event-driven architecture
pub fn apply(&mut self, event: &Event) {
match event {
Event::Insert {
position,
text,
cursor_id,
} => self.apply_insert(*position, text, *cursor_id),
Event::Delete {
range,
cursor_id,
deleted_text,
} => self.apply_delete(range, *cursor_id, deleted_text),
Event::MoveCursor {
cursor_id,
new_position,
new_anchor,
new_sticky_column,
..
} => {
if let Some(cursor) = self.cursors.get_mut(*cursor_id) {
cursor.position = *new_position;
cursor.anchor = *new_anchor;
cursor.sticky_column = *new_sticky_column;
}
// Update primary cursor line number if this is the primary cursor
// Try to get exact line number from buffer, or estimate for large files
if *cursor_id == self.cursors.primary_id() {
self.primary_cursor_line_number =
match self.buffer.offset_to_position(*new_position) {
Some(pos) => LineNumber::Absolute(pos.line),
None => {
// Large file without line metadata - estimate line number
// Use default estimated_line_length of 80 bytes
let estimated_line = *new_position / 80;
LineNumber::Absolute(estimated_line)
}
};
}
}
Event::AddCursor {
cursor_id,
position,
anchor,
} => {
let cursor = if let Some(anchor) = anchor {
Cursor::with_selection(*anchor, *position)
} else {
Cursor::new(*position)
};
// Insert cursor with the specific ID from the event
// This is important for undo/redo to work correctly
self.cursors.insert_with_id(*cursor_id, cursor);
self.cursors.normalize();
}
Event::RemoveCursor { cursor_id, .. } => {
self.cursors.remove(*cursor_id);
}
// View events (Scroll, SetViewport, Recenter) are now handled at Editor level
// via SplitViewState. They should not reach EditorState.apply().
Event::Scroll { .. } | Event::SetViewport { .. } | Event::Recenter => {
// These events are intercepted in Editor::apply_event_to_active_buffer
// and routed to SplitViewState. If we get here, something is wrong.
tracing::warn!("View event {:?} reached EditorState.apply() - should be handled by SplitViewState", event);
}
Event::SetAnchor {
cursor_id,
position,
} => {
// Set the anchor (selection start) for a specific cursor
// Also disable deselect_on_move so movement preserves the selection (Emacs mark mode)
if let Some(cursor) = self.cursors.get_mut(*cursor_id) {
cursor.anchor = Some(*position);
cursor.deselect_on_move = false;
}
}
Event::ClearAnchor { cursor_id } => {
// Clear the anchor and reset deselect_on_move to cancel mark mode
// Also clear block selection if active
if let Some(cursor) = self.cursors.get_mut(*cursor_id) {
cursor.anchor = None;
cursor.deselect_on_move = true;
cursor.clear_block_selection();
}
}
Event::ChangeMode { mode } => {
self.mode = mode.clone();
}
Event::AddOverlay {
namespace,
range,
face,
priority,
message,
extend_to_line_end,
} => {
tracing::debug!(
"AddOverlay: namespace={:?}, range={:?}, face={:?}, priority={}",
namespace,
range,
face,
priority
);
// Convert event overlay face to overlay face
let overlay_face = convert_event_face_to_overlay_face(face);
tracing::trace!("Converted face: {:?}", overlay_face);
let mut overlay = Overlay::with_priority(
&mut self.marker_list,
range.clone(),
overlay_face,
*priority,
);
overlay.namespace = namespace.clone();
overlay.message = message.clone();
overlay.extend_to_line_end = *extend_to_line_end;
let actual_range = overlay.range(&self.marker_list);
tracing::debug!(
"Created overlay with markers - actual range: {:?}, handle={:?}",
actual_range,
overlay.handle
);
self.overlays.add(overlay);
}
Event::RemoveOverlay { handle } => {
tracing::debug!("RemoveOverlay: handle={:?}", handle);
self.overlays
.remove_by_handle(handle, &mut self.marker_list);
}
Event::RemoveOverlaysInRange { range } => {
self.overlays.remove_in_range(range, &mut self.marker_list);
}
Event::ClearNamespace { namespace } => {
tracing::debug!("ClearNamespace: namespace={:?}", namespace);
self.overlays
.clear_namespace(namespace, &mut self.marker_list);
}
Event::ClearOverlays => {
self.overlays.clear(&mut self.marker_list);
}
Event::ShowPopup { popup } => {
let popup_obj = convert_popup_data_to_popup(popup);
self.popups.show(popup_obj);
}
Event::HidePopup => {
self.popups.hide();
}
Event::ClearPopups => {
self.popups.clear();
}
Event::PopupSelectNext => {
if let Some(popup) = self.popups.top_mut() {
popup.select_next();
}
}
Event::PopupSelectPrev => {
if let Some(popup) = self.popups.top_mut() {
popup.select_prev();
}
}
Event::PopupPageDown => {
if let Some(popup) = self.popups.top_mut() {
popup.page_down();
}
}
Event::PopupPageUp => {
if let Some(popup) = self.popups.top_mut() {
popup.page_up();
}
}
Event::AddMarginAnnotation {
line,
position,
content,
annotation_id,
} => {
let margin_position = convert_margin_position(position);
let margin_content = convert_margin_content(content);
let annotation = if let Some(id) = annotation_id {
MarginAnnotation::with_id(*line, margin_position, margin_content, id.clone())
} else {
MarginAnnotation::new(*line, margin_position, margin_content)
};
self.margins.add_annotation(annotation);
}
Event::RemoveMarginAnnotation { annotation_id } => {
self.margins.remove_by_id(annotation_id);
}
Event::RemoveMarginAnnotationsAtLine { line, position } => {
let margin_position = convert_margin_position(position);
self.margins.remove_at_line(*line, margin_position);
}
Event::ClearMarginPosition { position } => {
let margin_position = convert_margin_position(position);
self.margins.clear_position(margin_position);
}
Event::ClearMargins => {
self.margins.clear_all();
}
Event::SetLineNumbers { enabled } => {
self.margins.set_line_numbers(*enabled);
}
// Split events are handled at the Editor level, not at EditorState level
// These are no-ops here as they affect the split layout, not buffer state
Event::SplitPane { .. }
| Event::CloseSplit { .. }
| Event::SetActiveSplit { .. }
| Event::AdjustSplitRatio { .. }
| Event::NextSplit
| Event::PrevSplit => {
// No-op: split events are handled by Editor, not EditorState
}
Event::Batch { events, .. } => {
// Apply all events in the batch sequentially
// This ensures multi-cursor operations are applied atomically
for event in events {
self.apply(event);
}
}
Event::BulkEdit {
new_tree,
new_cursors,
..
} => {
// Restore the new_tree (target tree state for this event)
// - For original application: this is set after apply_events_as_bulk_edit
// - For undo: trees are swapped, so new_tree is the original state
// - For redo: new_tree is the state after edits
if let Some(tree) = new_tree {
self.buffer.restore_piece_tree(tree);
}
// Update cursor positions
for (cursor_id, position, anchor) in new_cursors {
if let Some(cursor) = self.cursors.get_mut(*cursor_id) {
cursor.position = *position;
cursor.anchor = *anchor;
}
}
// Invalidate highlight cache for entire buffer
self.highlighter.invalidate_all();
// Update primary cursor line number
let primary_pos = self.cursors.primary().position;
self.primary_cursor_line_number = match self.buffer.offset_to_position(primary_pos)
{
Some(pos) => crate::model::buffer::LineNumber::Absolute(pos.line),
None => crate::model::buffer::LineNumber::Absolute(0),
};
}
}
}
/// Apply multiple events in sequence
pub fn apply_many(&mut self, events: &[Event]) {
for event in events {
self.apply(event);
}
}
/// Get the primary cursor
pub fn primary_cursor(&self) -> &Cursor {
self.cursors.primary()
}
/// Get the primary cursor mutably (for reading state only, not for modification!)
pub fn primary_cursor_mut(&mut self) -> &mut Cursor {
self.cursors.primary_mut()
}
/// Called when this buffer loses focus (e.g., switching to another buffer,
/// opening a prompt, focusing file explorer, etc.)
/// Dismisses transient popups like Hover and Signature Help.
pub fn on_focus_lost(&mut self) {
if self.popups.dismiss_transient() {
tracing::debug!("Dismissed transient popup on buffer focus loss");
}
}
}
/// Convert event overlay face to the actual overlay face
fn convert_event_face_to_overlay_face(event_face: &EventOverlayFace) -> OverlayFace {
match event_face {
EventOverlayFace::Underline { color, style } => {
let underline_style = match style {
crate::model::event::UnderlineStyle::Straight => UnderlineStyle::Straight,
crate::model::event::UnderlineStyle::Wavy => UnderlineStyle::Wavy,
crate::model::event::UnderlineStyle::Dotted => UnderlineStyle::Dotted,
crate::model::event::UnderlineStyle::Dashed => UnderlineStyle::Dashed,
};
OverlayFace::Underline {
color: Color::Rgb(color.0, color.1, color.2),
style: underline_style,
}
}
EventOverlayFace::Background { color } => OverlayFace::Background {
color: Color::Rgb(color.0, color.1, color.2),
},
EventOverlayFace::Foreground { color } => OverlayFace::Foreground {
color: Color::Rgb(color.0, color.1, color.2),
},
EventOverlayFace::Style { options } => {
use ratatui::style::Modifier;
// Build fallback style from RGB values
let mut style = Style::default();
// Extract foreground color (RGB fallback or default white)
if let Some(ref fg) = options.fg {
if let Some((r, g, b)) = fg.as_rgb() {
style = style.fg(Color::Rgb(r, g, b));
}
}
// Extract background color (RGB fallback)
if let Some(ref bg) = options.bg {
if let Some((r, g, b)) = bg.as_rgb() {
style = style.bg(Color::Rgb(r, g, b));
}
}
// Apply modifiers
let mut modifiers = Modifier::empty();
if options.bold {
modifiers |= Modifier::BOLD;
}
if options.italic {
modifiers |= Modifier::ITALIC;
}
if options.underline {
modifiers |= Modifier::UNDERLINED;
}
if !modifiers.is_empty() {
style = style.add_modifier(modifiers);
}
// Extract theme keys
let fg_theme = options
.fg
.as_ref()
.and_then(|c| c.as_theme_key())
.map(String::from);
let bg_theme = options
.bg
.as_ref()
.and_then(|c| c.as_theme_key())
.map(String::from);
// If theme keys are provided, use ThemedStyle for runtime resolution
if fg_theme.is_some() || bg_theme.is_some() {
OverlayFace::ThemedStyle {
fallback_style: style,
fg_theme,
bg_theme,
}
} else {
OverlayFace::Style { style }
}
}
}
}
/// Convert popup data to the actual popup object
fn convert_popup_data_to_popup(data: &PopupData) -> Popup {
let content = match &data.content {
crate::model::event::PopupContentData::Text(lines) => PopupContent::Text(lines.clone()),
crate::model::event::PopupContentData::List { items, selected } => PopupContent::List {
items: items
.iter()
.map(|item| PopupListItem {
text: item.text.clone(),
detail: item.detail.clone(),
icon: item.icon.clone(),
data: item.data.clone(),
})
.collect(),
selected: *selected,
},
};
let position = match data.position {
PopupPositionData::AtCursor => PopupPosition::AtCursor,
PopupPositionData::BelowCursor => PopupPosition::BelowCursor,
PopupPositionData::AboveCursor => PopupPosition::AboveCursor,
PopupPositionData::Fixed { x, y } => PopupPosition::Fixed { x, y },
PopupPositionData::Centered => PopupPosition::Centered,
PopupPositionData::BottomRight => PopupPosition::BottomRight,
};
// Determine popup kind based on title and content type
let completion_title = t!("lsp.popup_completion").to_string();
let kind = if data.title.as_ref() == Some(&completion_title) {
PopupKind::Completion
} else {
match &content {
PopupContent::List { .. } => PopupKind::List,
PopupContent::Text(_) => PopupKind::Text,
PopupContent::Markdown(_) => PopupKind::Text,
PopupContent::Custom(_) => PopupKind::Text,
}
};
Popup {
kind,
title: data.title.clone(),
description: data.description.clone(),
transient: data.transient,
content,
position,
width: data.width,
max_height: data.max_height,
bordered: data.bordered,
border_style: Style::default().fg(Color::Gray),
background_style: Style::default().bg(Color::Rgb(30, 30, 30)),
scroll_offset: 0,
text_selection: None,
}
}
/// Convert margin position data to the actual margin position
fn convert_margin_position(position: &MarginPositionData) -> MarginPosition {
match position {
MarginPositionData::Left => MarginPosition::Left,
MarginPositionData::Right => MarginPosition::Right,
}
}
/// Convert margin content data to the actual margin content
fn convert_margin_content(content: &MarginContentData) -> MarginContent {
match content {
MarginContentData::Text(text) => MarginContent::Text(text.clone()),
MarginContentData::Symbol { text, color } => {
if let Some((r, g, b)) = color {
MarginContent::colored_symbol(text.clone(), Color::Rgb(*r, *g, *b))
} else {
MarginContent::symbol(text.clone(), Style::default())
}
}
MarginContentData::Empty => MarginContent::Empty,
}
}
impl EditorState {
/// Prepare viewport for rendering (called before frame render)
///
/// This pre-loads all data that will be needed for rendering the current viewport,
/// ensuring that subsequent read-only access during rendering will succeed.
///
/// Takes viewport parameters since viewport is now owned by SplitViewState.
pub fn prepare_for_render(&mut self, top_byte: usize, height: u16) -> Result<()> {
self.buffer.prepare_viewport(top_byte, height as usize)?;
Ok(())
}
// ========== DocumentModel Helper Methods ==========
// These methods provide convenient access to DocumentModel functionality
// while maintaining backward compatibility with existing code.
/// Get text in a range, driving lazy loading transparently
///
/// This is a convenience wrapper around DocumentModel::get_range that:
/// - Drives lazy loading automatically (never fails due to unloaded data)
/// - Uses byte offsets directly
/// - Returns String (not Result) - errors are logged internally
/// - Returns empty string for invalid ranges
///
/// This is the preferred API for getting text ranges. The caller never needs
/// to worry about lazy loading or buffer preparation.
///
/// # Example
/// ```ignore
/// let text = state.get_text_range(0, 100);
/// ```
pub fn get_text_range(&mut self, start: usize, end: usize) -> String {
// TextBuffer::get_text_range_mut() handles lazy loading automatically
match self
.buffer
.get_text_range_mut(start, end.saturating_sub(start))
{
Ok(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
Err(e) => {
tracing::warn!("Failed to get text range {}..{}: {}", start, end, e);
String::new()
}
}
}
/// Get the content of a line by its byte offset
///
/// Returns the line containing the given offset, along with its start position.
/// This uses DocumentModel's viewport functionality for consistent behavior.
///
/// # Returns
/// `Some((line_start_offset, line_content))` if successful, `None` if offset is invalid
pub fn get_line_at_offset(&mut self, offset: usize) -> Option<(usize, String)> {
use crate::model::document_model::DocumentModel;
// Find the start of the line containing this offset
// Scan backwards to find the previous newline or start of buffer
let mut line_start = offset;
while line_start > 0 {
if let Ok(text) = self.buffer.get_text_range_mut(line_start - 1, 1) {
if text.first() == Some(&b'\n') {
break;
}
line_start -= 1;
} else {
break;
}
}
// Get a single line viewport starting at the line start
let viewport = self
.get_viewport_content(
crate::model::document_model::DocumentPosition::byte(line_start),
1,
)
.ok()?;
viewport
.lines
.first()
.map(|line| (line.byte_offset, line.content.clone()))
}
/// Get text from current cursor position to end of line
///
/// This is a common pattern in editing operations. Uses DocumentModel
/// for consistent behavior across file sizes.
pub fn get_text_to_end_of_line(&mut self, cursor_pos: usize) -> Result<String> {
use crate::model::document_model::DocumentModel;
// Get the line containing cursor
let viewport = self.get_viewport_content(
crate::model::document_model::DocumentPosition::byte(cursor_pos),
1,
)?;
if let Some(line) = viewport.lines.first() {
let line_start = line.byte_offset;
let line_end = line_start + line.content.len();
if cursor_pos >= line_start && cursor_pos <= line_end {
let offset_in_line = cursor_pos - line_start;
// Use get() to safely handle potential non-char-boundary offsets
Ok(line.content.get(offset_in_line..).unwrap_or("").to_string())
} else {
Ok(String::new())
}
} else {
Ok(String::new())
}
}
/// Replace cached semantic tokens with a new store.
pub fn set_semantic_tokens(&mut self, store: SemanticTokenStore) {
self.semantic_tokens = Some(store);
}
/// Clear cached semantic tokens (e.g., when tokens are invalidated).
pub fn clear_semantic_tokens(&mut self) {
self.semantic_tokens = None;
}
/// Get the server-provided semantic token result_id if available.
pub fn semantic_tokens_result_id(&self) -> Option<&str> {
self.semantic_tokens
.as_ref()
.and_then(|store| store.result_id.as_deref())
}
}
/// Implement DocumentModel trait for EditorState
///
/// This provides a clean abstraction layer between rendering/editing operations
/// and the underlying text buffer implementation.
impl DocumentModel for EditorState {
fn capabilities(&self) -> DocumentCapabilities {
let line_count = self.buffer.line_count();
DocumentCapabilities {
has_line_index: line_count.is_some(),
uses_lazy_loading: false, // TODO: add large file detection
byte_length: self.buffer.len(),
approximate_line_count: line_count.unwrap_or_else(|| {
// Estimate assuming ~80 bytes per line
self.buffer.len() / 80
}),
}
}
fn get_viewport_content(
&mut self,
start_pos: DocumentPosition,
max_lines: usize,
) -> Result<ViewportContent> {
// Convert to byte offset
let start_offset = self.position_to_offset(start_pos)?;
// Use new efficient line iteration that tracks line numbers during iteration
// by accumulating line_feed_cnt from pieces (single source of truth)
let line_iter = self.buffer.iter_lines_from(start_offset, max_lines)?;
let has_more = line_iter.has_more;
let lines = line_iter
.map(|line_data| ViewportLine {
byte_offset: line_data.byte_offset,
content: line_data.content,
has_newline: line_data.has_newline,
approximate_line_number: line_data.line_number,
})
.collect();
Ok(ViewportContent {
start_position: DocumentPosition::ByteOffset(start_offset),
lines,
has_more,
})
}
fn position_to_offset(&self, pos: DocumentPosition) -> Result<usize> {
match pos {
DocumentPosition::ByteOffset(offset) => Ok(offset),
DocumentPosition::LineColumn { line, column } => {
if !self.has_line_index() {
anyhow::bail!("Line indexing not available for this document");
}
// Use piece tree's position conversion
let position = crate::model::piece_tree::Position { line, column };
Ok(self.buffer.position_to_offset(position))
}
}
}
fn offset_to_position(&self, offset: usize) -> DocumentPosition {
if self.has_line_index() {
if let Some(pos) = self.buffer.offset_to_position(offset) {
DocumentPosition::LineColumn {
line: pos.line,
column: pos.column,
}
} else {
// Line index exists but metadata unavailable - fall back to byte offset
DocumentPosition::ByteOffset(offset)
}
} else {
DocumentPosition::ByteOffset(offset)
}
}
fn get_range(&mut self, start: DocumentPosition, end: DocumentPosition) -> Result<String> {
let start_offset = self.position_to_offset(start)?;
let end_offset = self.position_to_offset(end)?;
if start_offset > end_offset {
anyhow::bail!(
"Invalid range: start offset {} > end offset {}",
start_offset,
end_offset
);
}
let bytes = self
.buffer
.get_text_range_mut(start_offset, end_offset - start_offset)?;
Ok(String::from_utf8_lossy(&bytes).into_owned())
}
fn get_line_content(&mut self, line_number: usize) -> Option<String> {
if !self.has_line_index() {
return None;
}
// Convert line number to byte offset
let line_start_offset = self.buffer.line_start_offset(line_number)?;
// Get line content using iterator
let mut iter = self.buffer.line_iterator(line_start_offset, 80);
if let Some((_start, content)) = iter.next_line() {
let has_newline = content.ends_with('\n');
let line_content = if has_newline {
content[..content.len() - 1].to_string()
} else {
content
};
Some(line_content)
} else {
None
}
}
fn get_chunk_at_offset(&mut self, offset: usize, size: usize) -> Result<(usize, String)> {
let bytes = self.buffer.get_text_range_mut(offset, size)?;
Ok((offset, String::from_utf8_lossy(&bytes).into_owned()))
}
fn insert(&mut self, pos: DocumentPosition, text: &str) -> Result<usize> {
let offset = self.position_to_offset(pos)?;
self.buffer.insert_bytes(offset, text.as_bytes().to_vec());
Ok(text.len())
}
fn delete(&mut self, start: DocumentPosition, end: DocumentPosition) -> Result<()> {
let start_offset = self.position_to_offset(start)?;
let end_offset = self.position_to_offset(end)?;
if start_offset > end_offset {
anyhow::bail!(
"Invalid range: start offset {} > end offset {}",
start_offset,
end_offset
);
}
self.buffer.delete(start_offset..end_offset);
Ok(())
}
fn replace(
&mut self,
start: DocumentPosition,
end: DocumentPosition,
text: &str,
) -> Result<()> {
// Delete then insert
self.delete(start, end)?;
self.insert(start, text)?;
Ok(())
}
fn find_matches(
&mut self,
pattern: &str,
search_range: Option<(DocumentPosition, DocumentPosition)>,
) -> Result<Vec<usize>> {
let (start_offset, end_offset) = if let Some((start, end)) = search_range {
(
self.position_to_offset(start)?,
self.position_to_offset(end)?,
)
} else {
(0, self.buffer.len())
};
// Get text in range
let bytes = self
.buffer
.get_text_range_mut(start_offset, end_offset - start_offset)?;
let text = String::from_utf8_lossy(&bytes);
// Find all matches (simple substring search for now)
let mut matches = Vec::new();
let mut search_offset = 0;
while let Some(pos) = text[search_offset..].find(pattern) {
matches.push(start_offset + search_offset + pos);
search_offset += pos + pattern.len();
}
Ok(matches)
}
}
/// Cached semantic tokens for a buffer.
#[derive(Clone, Debug)]
pub struct SemanticTokenStore {
/// Buffer version the tokens correspond to.
pub version: u64,
/// Server-provided result identifier (if any).
pub result_id: Option<String>,
/// Raw semantic token data (u32 array, 5 integers per token).
pub data: Vec<u32>,
/// All semantic token spans resolved to byte ranges.
pub tokens: Vec<SemanticTokenSpan>,
}
/// A semantic token span resolved to buffer byte offsets.
#[derive(Clone, Debug)]
pub struct SemanticTokenSpan {
pub range: Range<usize>,
pub token_type: String,
pub modifiers: Vec<String>,
}
#[cfg(test)]
mod tests {
use crate::model::filesystem::StdFileSystem;
use std::sync::Arc;
fn test_fs() -> Arc<dyn crate::model::filesystem::FileSystem + Send + Sync> {
Arc::new(StdFileSystem)
}
use super::*;
use crate::model::event::CursorId;
#[test]
fn test_state_new() {
let state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
assert!(state.buffer.is_empty());
assert_eq!(state.cursors.count(), 1);
assert_eq!(state.cursors.primary().position, 0);
}
#[test]
fn test_apply_insert() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
let cursor_id = state.cursors.primary_id();
state.apply(&Event::Insert {
position: 0,
text: "hello".to_string(),
cursor_id,
});
assert_eq!(state.buffer.to_string().unwrap(), "hello");
assert_eq!(state.cursors.primary().position, 5);
assert!(state.buffer.is_modified());
}
#[test]
fn test_apply_delete() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
let cursor_id = state.cursors.primary_id();
// Insert then delete
state.apply(&Event::Insert {
position: 0,
text: "hello world".to_string(),
cursor_id,
});
state.apply(&Event::Delete {
range: 5..11,
deleted_text: " world".to_string(),
cursor_id,
});
assert_eq!(state.buffer.to_string().unwrap(), "hello");
assert_eq!(state.cursors.primary().position, 5);
}
#[test]
fn test_apply_move_cursor() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
let cursor_id = state.cursors.primary_id();
state.apply(&Event::Insert {
position: 0,
text: "hello".to_string(),
cursor_id,
});
state.apply(&Event::MoveCursor {
cursor_id,
old_position: 5,
new_position: 2,
old_anchor: None,
new_anchor: None,
old_sticky_column: 0,
new_sticky_column: 0,
});
assert_eq!(state.cursors.primary().position, 2);
}
#[test]
fn test_apply_add_cursor() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
let cursor_id = CursorId(1);
state.apply(&Event::AddCursor {
cursor_id,
position: 5,
anchor: None,
});
assert_eq!(state.cursors.count(), 2);
}
#[test]
fn test_apply_many() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
let cursor_id = state.cursors.primary_id();
let events = vec![
Event::Insert {
position: 0,
text: "hello ".to_string(),
cursor_id,
},
Event::Insert {
position: 6,
text: "world".to_string(),
cursor_id,
},
];
state.apply_many(&events);
assert_eq!(state.buffer.to_string().unwrap(), "hello world");
}
#[test]
fn test_cursor_adjustment_after_insert() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
let cursor_id = state.cursors.primary_id();
// Add a second cursor at position 5
state.apply(&Event::AddCursor {
cursor_id: CursorId(1),
position: 5,
anchor: None,
});
// Insert at position 0 - should push second cursor forward
state.apply(&Event::Insert {
position: 0,
text: "abc".to_string(),
cursor_id,
});
// Second cursor should be at position 5 + 3 = 8
if let Some(cursor) = state.cursors.get(CursorId(1)) {
assert_eq!(cursor.position, 8);
}
}
// DocumentModel trait tests
mod document_model_tests {
use super::*;
use crate::model::document_model::{DocumentModel, DocumentPosition};
#[test]
fn test_capabilities_small_file() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("line1\nline2\nline3");
let caps = state.capabilities();
assert!(caps.has_line_index, "Small file should have line index");
assert_eq!(caps.byte_length, "line1\nline2\nline3".len());
assert_eq!(caps.approximate_line_count, 3, "Should have 3 lines");
}
#[test]
fn test_position_conversions() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello\nworld\ntest");
// Test ByteOffset -> offset
let pos1 = DocumentPosition::ByteOffset(6);
let offset1 = state.position_to_offset(pos1).unwrap();
assert_eq!(offset1, 6);
// Test LineColumn -> offset
let pos2 = DocumentPosition::LineColumn { line: 1, column: 0 };
let offset2 = state.position_to_offset(pos2).unwrap();
assert_eq!(offset2, 6, "Line 1, column 0 should be at byte 6");
// Test offset -> position (should return LineColumn for small files)
let converted = state.offset_to_position(6);
match converted {
DocumentPosition::LineColumn { line, column } => {
assert_eq!(line, 1);
assert_eq!(column, 0);
}
_ => panic!("Expected LineColumn for small file"),
}
}
#[test]
fn test_get_viewport_content() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("line1\nline2\nline3\nline4\nline5");
let content = state
.get_viewport_content(DocumentPosition::ByteOffset(0), 3)
.unwrap();
assert_eq!(content.lines.len(), 3);
assert_eq!(content.lines[0].content, "line1");
assert_eq!(content.lines[1].content, "line2");
assert_eq!(content.lines[2].content, "line3");
assert!(content.has_more);
}
#[test]
fn test_get_range() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world");
let text = state
.get_range(
DocumentPosition::ByteOffset(0),
DocumentPosition::ByteOffset(5),
)
.unwrap();
assert_eq!(text, "hello");
let text2 = state
.get_range(
DocumentPosition::ByteOffset(6),
DocumentPosition::ByteOffset(11),
)
.unwrap();
assert_eq!(text2, "world");
}
#[test]
fn test_get_line_content() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("line1\nline2\nline3");
let line0 = state.get_line_content(0).unwrap();
assert_eq!(line0, "line1");
let line1 = state.get_line_content(1).unwrap();
assert_eq!(line1, "line2");
let line2 = state.get_line_content(2).unwrap();
assert_eq!(line2, "line3");
}
#[test]
fn test_insert_delete() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world");
// Insert text
let bytes_inserted = state
.insert(DocumentPosition::ByteOffset(6), "beautiful ")
.unwrap();
assert_eq!(bytes_inserted, 10);
assert_eq!(state.buffer.to_string().unwrap(), "hello beautiful world");
// Delete text
state
.delete(
DocumentPosition::ByteOffset(6),
DocumentPosition::ByteOffset(16),
)
.unwrap();
assert_eq!(state.buffer.to_string().unwrap(), "hello world");
}
#[test]
fn test_replace() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world");
state
.replace(
DocumentPosition::ByteOffset(0),
DocumentPosition::ByteOffset(5),
"hi",
)
.unwrap();
assert_eq!(state.buffer.to_string().unwrap(), "hi world");
}
#[test]
fn test_find_matches() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world hello");
let matches = state.find_matches("hello", None).unwrap();
assert_eq!(matches.len(), 2);
assert_eq!(matches[0], 0);
assert_eq!(matches[1], 12);
}
#[test]
fn test_prepare_for_render() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("line1\nline2\nline3\nline4\nline5");
// Should not panic - pass top_byte=0 and height=24 (typical viewport params)
state.prepare_for_render(0, 24).unwrap();
}
#[test]
fn test_helper_get_text_range() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world");
// Test normal range
let text = state.get_text_range(0, 5);
assert_eq!(text, "hello");
// Test middle range
let text2 = state.get_text_range(6, 11);
assert_eq!(text2, "world");
}
#[test]
fn test_helper_get_line_at_offset() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("line1\nline2\nline3");
// Get first line (offset 0)
let (offset, content) = state.get_line_at_offset(0).unwrap();
assert_eq!(offset, 0);
assert_eq!(content, "line1");
// Get second line (offset in middle of line)
let (offset2, content2) = state.get_line_at_offset(8).unwrap();
assert_eq!(offset2, 6); // Line starts at byte 6
assert_eq!(content2, "line2");
// Get last line
let (offset3, content3) = state.get_line_at_offset(12).unwrap();
assert_eq!(offset3, 12);
assert_eq!(content3, "line3");
}
#[test]
fn test_helper_get_text_to_end_of_line() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world\nline2");
// From beginning of line
let text = state.get_text_to_end_of_line(0).unwrap();
assert_eq!(text, "hello world");
// From middle of line
let text2 = state.get_text_to_end_of_line(6).unwrap();
assert_eq!(text2, "world");
// From end of line
let text3 = state.get_text_to_end_of_line(11).unwrap();
assert_eq!(text3, "");
// From second line
let text4 = state.get_text_to_end_of_line(12).unwrap();
assert_eq!(text4, "line2");
}
}
// Virtual text integration tests
mod virtual_text_integration_tests {
use super::*;
use crate::view::virtual_text::VirtualTextPosition;
use ratatui::style::Style;
#[test]
fn test_virtual_text_add_and_query() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world");
// Initialize marker list for buffer
if !state.buffer.is_empty() {
state.marker_list.adjust_for_insert(0, state.buffer.len());
}
// Add virtual text at position 5 (after 'hello')
let vtext_id = state.virtual_texts.add(
&mut state.marker_list,
5,
": string".to_string(),
Style::default(),
VirtualTextPosition::AfterChar,
0,
);
// Query should return the virtual text
let results = state.virtual_texts.query_range(&state.marker_list, 0, 11);
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, 5); // Position
assert_eq!(results[0].1.text, ": string");
// Build lookup should work
let lookup = state.virtual_texts.build_lookup(&state.marker_list, 0, 11);
assert!(lookup.contains_key(&5));
assert_eq!(lookup[&5].len(), 1);
assert_eq!(lookup[&5][0].text, ": string");
// Clean up
state.virtual_texts.remove(&mut state.marker_list, vtext_id);
assert!(state.virtual_texts.is_empty());
}
#[test]
fn test_virtual_text_position_tracking_on_insert() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello world");
// Initialize marker list for buffer
if !state.buffer.is_empty() {
state.marker_list.adjust_for_insert(0, state.buffer.len());
}
// Add virtual text at position 6 (the 'w' in 'world')
let _vtext_id = state.virtual_texts.add(
&mut state.marker_list,
6,
"/*param*/".to_string(),
Style::default(),
VirtualTextPosition::BeforeChar,
0,
);
// Insert "beautiful " at position 6 using Event
let cursor_id = state.cursors.primary_id();
state.apply(&Event::Insert {
position: 6,
text: "beautiful ".to_string(),
cursor_id,
});
// Virtual text should now be at position 16 (6 + 10)
let results = state.virtual_texts.query_range(&state.marker_list, 0, 30);
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, 16); // Position should have moved
assert_eq!(results[0].1.text, "/*param*/");
}
#[test]
fn test_virtual_text_position_tracking_on_delete() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("hello beautiful world");
// Initialize marker list for buffer
if !state.buffer.is_empty() {
state.marker_list.adjust_for_insert(0, state.buffer.len());
}
// Add virtual text at position 16 (the 'w' in 'world')
let _vtext_id = state.virtual_texts.add(
&mut state.marker_list,
16,
": string".to_string(),
Style::default(),
VirtualTextPosition::AfterChar,
0,
);
// Delete "beautiful " (positions 6-16) using Event
let cursor_id = state.cursors.primary_id();
state.apply(&Event::Delete {
range: 6..16,
deleted_text: "beautiful ".to_string(),
cursor_id,
});
// Virtual text should now be at position 6
let results = state.virtual_texts.query_range(&state.marker_list, 0, 20);
assert_eq!(results.len(), 1);
assert_eq!(results[0].0, 6); // Position should have moved back
assert_eq!(results[0].1.text, ": string");
}
#[test]
fn test_multiple_virtual_texts_with_priorities() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("let x = 5");
// Initialize marker list for buffer
if !state.buffer.is_empty() {
state.marker_list.adjust_for_insert(0, state.buffer.len());
}
// Add type hint after 'x' (position 5)
state.virtual_texts.add(
&mut state.marker_list,
5,
": i32".to_string(),
Style::default(),
VirtualTextPosition::AfterChar,
0, // Lower priority - renders first
);
// Add another hint at same position with higher priority
state.virtual_texts.add(
&mut state.marker_list,
5,
" /* inferred */".to_string(),
Style::default(),
VirtualTextPosition::AfterChar,
10, // Higher priority - renders second
);
// Build lookup - should have both, sorted by priority (lower first)
let lookup = state.virtual_texts.build_lookup(&state.marker_list, 0, 10);
assert!(lookup.contains_key(&5));
let vtexts = &lookup[&5];
assert_eq!(vtexts.len(), 2);
// Lower priority first (like layer ordering)
assert_eq!(vtexts[0].text, ": i32");
assert_eq!(vtexts[1].text, " /* inferred */");
}
#[test]
fn test_virtual_text_clear() {
let mut state = EditorState::new(
80,
24,
crate::config::LARGE_FILE_THRESHOLD_BYTES as usize,
test_fs(),
);
state.buffer = Buffer::from_str_test("test");
// Initialize marker list for buffer
if !state.buffer.is_empty() {
state.marker_list.adjust_for_insert(0, state.buffer.len());
}
// Add multiple virtual texts
state.virtual_texts.add(
&mut state.marker_list,
0,
"hint1".to_string(),
Style::default(),
VirtualTextPosition::BeforeChar,
0,
);
state.virtual_texts.add(
&mut state.marker_list,
2,
"hint2".to_string(),
Style::default(),
VirtualTextPosition::AfterChar,
0,
);
assert_eq!(state.virtual_texts.len(), 2);
// Clear all
state.virtual_texts.clear(&mut state.marker_list);
assert!(state.virtual_texts.is_empty());
// Query should return nothing
let results = state.virtual_texts.query_range(&state.marker_list, 0, 10);
assert!(results.is_empty());
}
}
}