writ 0.14.0

A hybrid markdown editor combining raw text editing with live inline rendering
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
use ropey::Rope;
use std::ops::{Deref, DerefMut, Range};
use std::rc::Rc;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering};
use tree_sitter::{InputEdit, Point};
use undo::Record;

/// Global counter for unique buffer versions.
static NEXT_VERSION: AtomicU64 = AtomicU64::new(1);

use crate::highlight::{HighlightSpan, Highlighter};
use crate::inline::{StyledRegion, extract_all_inline_styles, styles_in_range};
use crate::marker::{
    LineMarkers, ParsedNodes, collect_node_infos, is_line_in_checked_task, is_line_in_code_block,
    markers_at_from_infos,
};
use crate::parser::{MarkdownParser, MarkdownTree};
use crate::table::{RowKind, TableInfo};

/// Compute the byte range for a line (excludes trailing newline).
fn compute_line_byte_range(rope: &Rope, line_idx: usize) -> Range<usize> {
    let start_char = rope.line_to_char(line_idx);
    let end_char = if line_idx + 1 < rope.len_lines() {
        rope.line_to_char(line_idx + 1)
    } else {
        rope.len_chars()
    };
    let start_byte = rope.char_to_byte(start_char);
    let end_byte = rope.char_to_byte(end_char);

    // Exclude trailing newline
    let line_slice = rope.line(line_idx);
    let len = line_slice.len_bytes();
    let has_newline = line_slice.get_byte(len.saturating_sub(1)) == Some(b'\n');
    let adjusted_end = if has_newline {
        end_byte.saturating_sub(1)
    } else {
        end_byte
    };

    start_byte..adjusted_end
}

/// Whether a tree-sitter-md node kind is an ordered-list marker (`1.` or `1)`).
fn is_ordered_list_marker(kind: &str) -> bool {
    kind == "list_marker_dot" || kind == "list_marker_parenthesis"
}

/// Compute `LineMarkers` for `line_idx` from parsed nodes and a rope. Shared by
/// `RenderSnapshot` and `BufferContent`, which differ only in their rope field.
fn line_markers_from(parsed: &ParsedNodes, rope: &Rope, line_idx: usize) -> LineMarkers {
    let range = compute_line_byte_range(rope, line_idx);
    let markers = markers_at_from_infos(&parsed.nodes, rope, range.start, range.end);
    let in_checked_task = is_line_in_checked_task(&parsed.nodes, range.start);
    let in_code_block = is_line_in_code_block(&parsed.nodes, range.start);
    LineMarkers {
        range,
        line_number: line_idx,
        markers,
        in_checked_task,
        in_code_block,
    }
}

/// A snapshot of buffer data for rendering. All fields use Rc for O(1) cloning.
/// LineMarkers are computed lazily per-line using the nodes cache.
#[derive(Clone)]
pub struct RenderSnapshot {
    pub rope: Rope,
    pub inline_styles: Rc<Vec<StyledRegion>>,
    pub code_highlights: Rc<Vec<(Range<usize>, Vec<HighlightSpan>)>>,
    /// Cached parsed nodes for lazy LineMarkers computation and code block queries
    parsed: Rc<ParsedNodes>,
    /// Number of lines in the document
    line_count: usize,
}

impl RenderSnapshot {
    /// Get the number of lines in the document.
    pub fn line_count(&self) -> usize {
        self.line_count
    }

    pub fn line_byte_range(&self, line_idx: usize) -> Range<usize> {
        compute_line_byte_range(&self.rope, line_idx)
    }

    /// Compute LineMarkers for a specific line on demand. O(log n) binary search + marker extraction.
    pub fn line_markers(&self, line_idx: usize) -> LineMarkers {
        line_markers_from(&self.parsed, &self.rope, line_idx)
    }

    /// Tree inline styles bucketed per line (index = line), in one O(n + styles)
    /// pass — the whole-document replacement for calling `inline_styles_in_range`
    /// per line, which is O(n²) because `styles_in_range` scans all earlier styles.
    /// No synthetic checkbox regions (callers that need them inject per line).
    pub fn inline_styles_by_line(&self) -> Vec<Vec<StyledRegion>> {
        let n = self.line_count;
        let mut buckets: Vec<Vec<StyledRegion>> = vec![Vec::new(); n];
        if n == 0 {
            return buckets;
        }
        let line_starts: Vec<usize> = (0..n).map(|i| self.line_byte_range(i).start).collect();
        // Line containing byte `off` (the last line whose start is <= off).
        let line_of = |off: usize| line_starts.partition_point(|&s| s <= off).saturating_sub(1);

        for region in self.inline_styles.iter() {
            let first = line_of(region.full_range.start).min(n - 1);
            // A region overlaps a line iff region.end > line.start, so the last line
            // it touches is the one containing its last byte (end - 1).
            let last_byte = region
                .full_range
                .end
                .saturating_sub(1)
                .max(region.full_range.start);
            let last = line_of(last_byte).max(first).min(n - 1);
            for bucket in buckets.iter_mut().take(last + 1).skip(first) {
                bucket.push(region.clone());
            }
        }
        buckets
    }

    /// Tree inline styles for one line (no synthetic checkbox regions), matching one
    /// bucket of `inline_styles_by_line` but computed in isolation (O(log n)). For
    /// callers that need only a handful of lines — e.g. the few visible ghost lines —
    /// and shouldn't pay to bucket the whole document each rebuild.
    pub fn tree_styles_for_line(&self, line_idx: usize) -> Vec<StyledRegion> {
        let range = self.line_byte_range(line_idx);
        styles_in_range(&self.inline_styles, &range)
            .into_iter()
            .cloned()
            .collect()
    }

    /// Get inline styles for a specific line. O(log n) binary search.
    /// Also injects a synthetic StyledRegion for any checkbox marker on the line.
    pub fn inline_styles_for_line(&self, line_idx: usize) -> Vec<StyledRegion> {
        let range = self.line_byte_range(line_idx);
        let markers = self.line_markers(line_idx);
        self.inline_styles_in_range(&range, &markers)
    }

    /// Inline styles for a line whose byte `range` and `markers` are already known.
    /// Lets callers that hold both (e.g. `build_line_render`) skip recomputing the
    /// markers — the redundant recompute was ~25% of the per-line render cost.
    pub fn inline_styles_in_range(
        &self,
        range: &Range<usize>,
        markers: &LineMarkers,
    ) -> Vec<StyledRegion> {
        let mut styles: Vec<StyledRegion> = styles_in_range(&self.inline_styles, range)
            .into_iter()
            .cloned()
            .collect();

        // Inject synthetic StyledRegion for checkbox markers
        for marker in &markers.markers {
            if let crate::marker::MarkerKind::Checkbox { checked } = marker.kind {
                // The checkbox marker range is "[ ] " (4 bytes), but we only
                // want to style "[ ]" (3 bytes) with the checkbox style.
                let checkbox_range = marker.range.start..marker.range.start + 3;
                styles.push(StyledRegion {
                    full_range: checkbox_range.clone(),
                    content_range: checkbox_range,
                    style: crate::inline::TextStyle::default(),
                    link_url: None,
                    is_image: false,
                    checkbox: Some(checked),
                    display_text: None,
                });
            }
        }

        // Re-sort by start position to maintain order
        styles.sort_by_key(|s| s.full_range.start);
        styles
    }

    /// Find the table whose `block` byte range contains `offset`. O(log n) binary
    /// search over `tables`, which are sorted by `block.start`.
    pub fn table_containing_offset(&self, offset: usize) -> Option<&TableInfo> {
        let tables = &self.parsed.tables;
        let idx = tables.partition_point(|t| t.block.start <= offset);
        // Candidate is the last table whose block starts at or before `offset`.
        let table = tables.get(idx.checked_sub(1)?)?;
        (offset < table.block.end).then_some(table)
    }

    /// Map a buffer line to the table and row role it belongs to, if any. A line
    /// belongs to a row when the row's `line` byte range contains the line's start.
    pub fn table_row_at_line(&self, line_idx: usize) -> Option<(&TableInfo, RowKind)> {
        let line_start = self.line_byte_range(line_idx).start;
        let table = self.table_containing_offset(line_start)?;

        if table.header.line.contains(&line_start) {
            return Some((table, RowKind::Header));
        }
        if table.delimiter_line.contains(&line_start) {
            return Some((table, RowKind::Delimiter));
        }
        for (i, row) in table.body.iter().enumerate() {
            if row.line.contains(&line_start) {
                return Some((table, RowKind::Body(i)));
            }
        }
        None
    }

    /// Get code highlights for a specific line. O(code_blocks) scan.
    pub fn code_highlights_for_line(&self, line_idx: usize) -> Vec<HighlightSpan> {
        let range = self.line_byte_range(line_idx);
        let mut result = Vec::new();
        for (block_range, highlights) in self.code_highlights.iter() {
            if range.start < block_range.end && range.end > block_range.start {
                for span in highlights {
                    if span.range.start < range.end && span.range.end > range.start {
                        result.push(span.clone());
                    }
                }
            }
        }
        result
    }
}

#[derive(Clone, Debug)]
struct CodeHighlightCache {
    highlights: Rc<Vec<(Range<usize>, Vec<HighlightSpan>)>>,
    valid: bool,
}

impl Default for CodeHighlightCache {
    fn default() -> Self {
        Self {
            highlights: Rc::new(Vec::new()),
            valid: false,
        }
    }
}

#[derive(Clone, Debug)]
pub struct TextEdit {
    offset: usize,
    deleted: String,
    inserted: String,
    cursor_before: usize,
    cursor_after: usize,
    /// Single-character typing/backspace, eligible to merge into a run so one Ctrl+Z
    /// undoes a whole word rather than one character. Multi-char (paste), replacements,
    /// and programmatic edits are false and stay their own undo step.
    coalescable: bool,
}

impl TextEdit {
    pub fn new(
        offset: usize,
        deleted: String,
        inserted: String,
        cursor_before: usize,
        cursor_after: usize,
    ) -> Self {
        // A pure single-character insert or delete is coalescable.
        let coalescable = deleted.chars().count() + inserted.chars().count() == 1;
        Self {
            offset,
            deleted,
            inserted,
            cursor_before,
            cursor_after,
            coalescable,
        }
    }
}

/// Word characters extend the current typing group; anything else (space, punctuation,
/// newline) starts a fresh undo step — the common word-granular undo behavior.
fn is_word_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}

impl undo::Edit for TextEdit {
    type Target = BufferContent;
    type Output = usize; // Returns cursor position

    fn edit(&mut self, target: &mut BufferContent) -> Self::Output {
        target.apply_edit(self.offset, &self.deleted, &self.inserted);
        self.cursor_after
    }

    fn undo(&mut self, target: &mut BufferContent) -> Self::Output {
        target.apply_edit(self.offset, &self.inserted, &self.deleted);
        self.cursor_before
    }

    /// Coalesce contiguous single-character word typing (or backspacing) into one undo
    /// step. `self` is the edit already on the stack; `other` the incoming one.
    fn merge(&mut self, other: Self) -> undo::Merged<Self> {
        if !self.coalescable || !other.coalescable {
            return undo::Merged::No(other);
        }
        // Forward typing: `other` inserts a word char right where `self`'s insert ended.
        if self.deleted.is_empty() && other.deleted.is_empty() {
            let ends_at = self.offset + self.inserted.len();
            let last = self.inserted.chars().next_back();
            let next = other.inserted.chars().next();
            if ends_at == other.offset
                && matches!((last, next), (Some(l), Some(n)) if is_word_char(l) && is_word_char(n))
            {
                self.inserted.push_str(&other.inserted);
                self.cursor_after = other.cursor_after;
                return undo::Merged::Yes;
            }
        }
        // Backspacing: `other` deletes the word char immediately before `self`'s deletion.
        if self.inserted.is_empty() && other.inserted.is_empty() {
            let first = self.deleted.chars().next();
            let prev = other.deleted.chars().next();
            if other.offset + other.deleted.len() == self.offset
                && matches!((first, prev), (Some(f), Some(p)) if is_word_char(f) && is_word_char(p))
            {
                self.deleted.insert_str(0, &other.deleted);
                self.offset = other.offset;
                self.cursor_after = other.cursor_after;
                return undo::Merged::Yes;
            }
        }
        undo::Merged::No(other)
    }
}

pub struct BufferContent {
    text: Rope,
    parser: MarkdownParser,
    tree: Option<MarkdownTree>,
    highlighter: Highlighter,
    code_highlight_cache: CodeHighlightCache,
    /// Cached parsed nodes for lazy LineMarkers computation and code block queries.
    /// Updated when tree changes. Wrapped in Rc for O(1) cloning in render snapshots.
    parsed: Rc<ParsedNodes>,
    /// Cached inline styles, updated when tree changes.
    /// Sorted by start position for efficient binary search lookup.
    /// Wrapped in Rc for O(1) cloning in render snapshots.
    inline_styles: Rc<Vec<StyledRegion>>,
    /// Version counter, incremented on each edit. Used by Editor to detect changes.
    version: u64,
    /// When set, `apply_edit` skips the whole-doc `update_caches` (nodes + inline styles).
    /// Used to batch a compound action (a checkbox toggle cascades through many
    /// revert/reapply edits via history) so the O(n) caches rebuild once at the end
    /// instead of per sub-edit. No consumer may read `parsed`/`inline_styles` while set.
    suspend_caches: bool,
}

impl BufferContent {
    pub fn new() -> Self {
        Self {
            text: Rope::new(),
            parser: MarkdownParser::default(),
            highlighter: Highlighter::new(),
            tree: None,
            code_highlight_cache: CodeHighlightCache::default(),
            parsed: Rc::new(ParsedNodes::default()),
            inline_styles: Rc::new(Vec::new()),
            version: NEXT_VERSION.fetch_add(1, Ordering::Relaxed),
            suspend_caches: false,
        }
    }

    /// Returns the current version number. Incremented on each edit.
    pub fn version(&self) -> u64 {
        self.version
    }

    fn update_caches(&mut self) {
        self.parsed = Rc::new(
            self.tree
                .as_ref()
                .map(|t| collect_node_infos(&t.block_tree().root_node(), &self.text))
                .unwrap_or_default(),
        );

        self.inline_styles = Rc::new(if let Some(ref tree) = self.tree {
            extract_all_inline_styles(tree, &self.text)
        } else {
            Vec::new()
        });
    }

    fn apply_edit(&mut self, offset: usize, to_delete: &str, to_insert: &str) {
        let delete_len = to_delete.len();
        let insert_len = to_insert.len();

        // Build the edit description for tree-sitter before modifying the rope
        let start_point = self.byte_to_point(offset);
        let old_end_point = if delete_len > 0 {
            self.byte_to_point(offset + delete_len)
        } else {
            start_point
        };

        let new_end_position = if insert_len > 0 {
            self.compute_new_end_point(start_point, to_insert)
        } else {
            start_point
        };

        let edit = InputEdit {
            start_byte: offset,
            old_end_byte: offset + delete_len,
            new_end_byte: offset + insert_len,
            start_position: start_point,
            old_end_position: old_end_point,
            new_end_position,
        };

        if delete_len > 0 {
            let char_start = self.text.byte_to_char(offset);
            let char_end = self.text.byte_to_char(offset + delete_len);
            self.text.remove(char_start..char_end);
        }
        if insert_len > 0 {
            let char_offset = self.text.byte_to_char(offset);
            self.text.insert(char_offset, to_insert);
        }

        if let Some(ref mut tree) = self.tree {
            tree.edit(&edit);
        }
        self.tree = self.parser.parse_rope(&self.text, self.tree.as_ref());

        // Renumbering only matters when the edit lands inside an ordered list, which
        // is the uncommon case — skip the tree walk + reparse otherwise.
        if self.edit_in_ordered_list(offset, offset + insert_len) {
            self.normalize_ordered_lists();
        }
        if !self.suspend_caches {
            self.update_caches();
        }
        self.code_highlight_cache.valid = false;
        self.version += 1;
    }

    /// True if the byte range `start..end` lies within (or on the boundary of) an
    /// ordered list node in the current tree. Used to gate `normalize_ordered_lists`.
    fn edit_in_ordered_list(&self, start: usize, end: usize) -> bool {
        let Some(tree) = &self.tree else {
            return false;
        };
        let root = tree.block_tree().root_node();
        let Some(node) = root.descendant_for_byte_range(start, end) else {
            return false;
        };
        let mut current = Some(node);
        while let Some(n) = current {
            if n.kind() == "list" && self.is_ordered_list(&n) {
                return true;
            }
            current = n.parent();
        }
        false
    }

    /// Normalize ordered list numbering - ensure sequential numbers (1, 2, 3...).
    /// Modifies the rope directly and re-parses incrementally if changes were made.
    fn normalize_ordered_lists(&mut self) -> bool {
        let corrections = {
            let Some(tree) = &self.tree else {
                return false;
            };
            self.find_ordered_list_corrections(tree.block_tree().root_node())
        };

        if corrections.is_empty() {
            return false;
        }

        // Apply corrections in reverse order to preserve byte offsets. Each marker
        // rewrite is fed to `tree.edit` so the follow-up parse stays incremental.
        for (marker_range, correct_number, is_parenthesis) in corrections.into_iter().rev() {
            let new_marker = if is_parenthesis {
                format!("{correct_number}) ")
            } else {
                format!("{correct_number}. ")
            };
            let new_len = new_marker.len();

            let start_point = self.byte_to_point(marker_range.start);
            let old_end_point = self.byte_to_point(marker_range.end);
            let new_end_point = self.compute_new_end_point(start_point, &new_marker);

            let char_start = self.text.byte_to_char(marker_range.start);
            let char_end = self.text.byte_to_char(marker_range.end);
            self.text.remove(char_start..char_end);

            let char_offset = self.text.byte_to_char(marker_range.start);
            self.text.insert(char_offset, &new_marker);

            if let Some(tree) = self.tree.as_mut() {
                tree.edit(&InputEdit {
                    start_byte: marker_range.start,
                    old_end_byte: marker_range.end,
                    new_end_byte: marker_range.start + new_len,
                    start_position: start_point,
                    old_end_position: old_end_point,
                    new_end_position: new_end_point,
                });
            }
        }

        self.tree = self.parser.parse_rope(&self.text, self.tree.as_ref());
        true
    }

    /// Returns corrections as (range, correct_number, is_parenthesis_style)
    fn find_ordered_list_corrections(
        &self,
        root: tree_sitter::Node,
    ) -> Vec<(Range<usize>, usize, bool)> {
        let mut corrections = Vec::new();
        self.collect_list_corrections(&root, &mut corrections);
        corrections
    }

    fn collect_list_corrections(
        &self,
        node: &tree_sitter::Node,
        corrections: &mut Vec<(Range<usize>, usize, bool)>,
    ) {
        if node.kind() == "list" && self.is_ordered_list(node) {
            let mut item_number = 1;
            for child in node.children(&mut node.walk()) {
                if child.kind() == "list_item"
                    && let Some((marker_range, current_number, is_parenthesis)) =
                        self.extract_ordered_marker(&child)
                {
                    if current_number != item_number {
                        corrections.push((marker_range, item_number, is_parenthesis));
                    }
                    item_number += 1;
                }
            }
        }

        for child in node.children(&mut node.walk()) {
            self.collect_list_corrections(&child, corrections);
        }
    }

    fn is_ordered_list(&self, list_node: &tree_sitter::Node) -> bool {
        for child in list_node.children(&mut list_node.walk()) {
            if child.kind() == "list_item"
                && let Some(marker) = child.children(&mut child.walk()).next()
            {
                return is_ordered_list_marker(marker.kind());
            }
        }
        false
    }

    /// Extract ordered list marker info: (range, current_number, is_parenthesis_style)
    fn extract_ordered_marker(
        &self,
        list_item: &tree_sitter::Node,
    ) -> Option<(Range<usize>, usize, bool)> {
        for marker in list_item.children(&mut list_item.walk()) {
            if is_ordered_list_marker(marker.kind()) {
                let start = marker.start_byte();
                let end = marker.end_byte();
                let is_parenthesis = marker.kind() == "list_marker_parenthesis";

                // Extract digits from the marker using rope slice
                let char_start = self.text.byte_to_char(start);
                let char_end = self.text.byte_to_char(end);
                let slice = self.text.slice(char_start..char_end);

                let number: usize = slice
                    .chars()
                    .take_while(|c| c.is_ascii_digit())
                    .collect::<String>()
                    .parse()
                    .unwrap_or(1);

                return Some((start..end, number, is_parenthesis));
            }
        }
        None
    }

    fn byte_to_point(&self, byte_offset: usize) -> Point {
        let byte_offset = byte_offset.min(self.text.len_bytes());
        let char_offset = self.text.byte_to_char(byte_offset);
        let line = self.text.char_to_line(char_offset);
        let line_start_char = self.text.line_to_char(line);
        let line_start_byte = self.text.char_to_byte(line_start_char);
        let column = byte_offset - line_start_byte;
        Point::new(line, column)
    }

    fn compute_new_end_point(&self, start: Point, text: &str) -> Point {
        let newlines: Vec<_> = text.match_indices('\n').collect();
        if newlines.is_empty() {
            Point::new(start.row, start.column + text.len())
        } else {
            let last_newline_pos = newlines.last().unwrap().0;
            let column = text.len() - last_newline_pos - 1;
            Point::new(start.row + newlines.len(), column)
        }
    }

    pub fn text(&self) -> String {
        self.text.to_string()
    }

    /// Whether the content equals `other`, comparing the rope in place (no `String` alloc).
    pub fn content_eq(&self, other: &str) -> bool {
        self.text == *other
    }

    pub fn len_bytes(&self) -> usize {
        self.text.len_bytes()
    }

    pub fn len_chars(&self) -> usize {
        self.text.len_chars()
    }

    pub fn is_empty(&self) -> bool {
        self.text.len_bytes() == 0
    }

    /// Check if buffer ends with the given string (efficient, doesn't copy whole buffer).
    pub fn ends_with(&self, suffix: &str) -> bool {
        let len = self.text.len_bytes();
        let suffix_len = suffix.len();
        if len < suffix_len {
            return false;
        }
        let start = len - suffix_len;
        self.text
            .byte_slice(start..len)
            .as_str()
            .map(|s| s == suffix)
            .unwrap_or(false)
    }

    /// Get a single byte at the given offset, if it exists.
    pub fn byte_at(&self, offset: usize) -> Option<u8> {
        if offset >= self.text.len_bytes() {
            return None;
        }
        self.text
            .byte_slice(offset..offset + 1)
            .as_str()
            .and_then(|s| s.bytes().next())
    }

    pub fn rope(&self) -> &Rope {
        &self.text
    }

    pub fn tree(&self) -> Option<&MarkdownTree> {
        self.tree.as_ref()
    }

    /// Get a reference to the parsed nodes for structural queries.
    pub fn parsed(&self) -> &ParsedNodes {
        &self.parsed
    }

    /// Compute LineMarkers for a specific line on demand.
    pub fn line_markers(&self, line_idx: usize) -> LineMarkers {
        line_markers_from(&self.parsed, &self.text, line_idx)
    }

    /// Compute LineMarkers for all lines. Only available in tests.
    #[cfg(test)]
    pub fn lines(&self) -> Vec<LineMarkers> {
        (0..self.line_count())
            .map(|i| self.line_markers(i))
            .collect()
    }

    /// Create a render snapshot for efficient virtualized rendering.
    /// Ensures code highlight cache is valid before creating the snapshot.
    /// All Rc clones are O(1). LineMarkers are computed lazily per-line.
    pub fn render_snapshot(&mut self) -> RenderSnapshot {
        if !self.code_highlight_cache.valid {
            self.rebuild_code_highlight_cache();
        }

        RenderSnapshot {
            rope: self.text.clone(),
            inline_styles: Rc::clone(&self.inline_styles),
            code_highlights: Rc::clone(&self.code_highlight_cache.highlights),
            parsed: Rc::clone(&self.parsed),
            line_count: self.text.len_lines(),
        }
    }

    pub fn byte_to_line(&self, byte_offset: usize) -> usize {
        let char_offset = self.text.byte_to_char(byte_offset);
        self.text.char_to_line(char_offset)
    }

    pub fn line_to_byte(&self, line: usize) -> usize {
        let char_offset = self.text.line_to_char(line);
        self.text.char_to_byte(char_offset)
    }

    pub fn line_count(&self) -> usize {
        self.text.len_lines()
    }

    /// Returns true if the line has no content (only markers or whitespace).
    /// Code fences are always considered content.
    pub fn is_line_empty(&self, line_idx: usize) -> bool {
        if line_idx >= self.line_count() {
            return true;
        }
        let line = self.line_markers(line_idx);

        if line.is_fence() {
            return false;
        }

        self.slice_cow(line.content_start()..line.range.end)
            .trim()
            .is_empty()
    }

    #[cfg(test)]
    pub fn line(&self, line_idx: usize) -> String {
        let line = self.text.line(line_idx);
        // Avoid double allocation - trim in place
        let mut s = line.to_string();
        if s.ends_with('\n') {
            s.pop();
        }
        s
    }

    pub fn line_byte_range(&self, line_idx: usize) -> Range<usize> {
        compute_line_byte_range(&self.text, line_idx)
    }

    fn slice(&self, range: Range<usize>) -> String {
        let char_start = self.text.byte_to_char(range.start);
        let char_end = self.text.byte_to_char(range.end);
        self.text.slice(char_start..char_end).to_string()
    }

    /// Get a byte slice from the rope, borrowing if possible.
    /// Returns a Cow that borrows if the slice fits in one chunk, allocates otherwise.
    pub fn slice_cow(&self, range: Range<usize>) -> std::borrow::Cow<'_, str> {
        let len = self.text.len_bytes();
        // Clamp range to valid bounds
        let start = range.start.min(len);
        let end = range.end.min(len);
        if start >= end {
            return std::borrow::Cow::Borrowed("");
        }
        let char_start = self.text.byte_to_char(start);
        let char_end = self.text.byte_to_char(end);
        let slice = self.text.slice(char_start..char_end);
        match slice.as_str() {
            Some(s) => std::borrow::Cow::Borrowed(s),
            None => std::borrow::Cow::Owned(slice.to_string()),
        }
    }

    fn rebuild_code_highlight_cache(&mut self) {
        let highlights = Rc::make_mut(&mut self.code_highlight_cache.highlights);
        highlights.clear();

        for code_block in &self.parsed.code_blocks {
            let language = code_block.info_string_range.as_ref().and_then(|range| {
                let char_start = self.text.byte_to_char(range.start);
                let char_end = self.text.byte_to_char(range.end);
                let slice = self.text.slice(char_start..char_end);
                let lang = slice.to_string().trim().to_string();
                if lang.is_empty() { None } else { Some(lang) }
            });

            if let Some(lang) = language
                && !code_block.content_range.is_empty()
            {
                let char_start = self.text.byte_to_char(code_block.content_range.start);
                let char_end = self.text.byte_to_char(code_block.content_range.end);
                let slice = self.text.slice(char_start..char_end);
                let code_content = slice.to_string();

                let mut spans = self.highlighter.highlight(&code_content, &lang);
                for span in &mut spans {
                    span.range.start += code_block.content_range.start;
                    span.range.end += code_block.content_range.start;
                }
                highlights.push((code_block.block_range.clone(), spans));
            }
        }

        self.code_highlight_cache.valid = true;
    }
}

impl Default for BufferContent {
    fn default() -> Self {
        Self::new()
    }
}

pub struct Buffer {
    content: BufferContent,
    history: Record<TextEdit>,
}

impl Deref for Buffer {
    type Target = BufferContent;

    fn deref(&self) -> &Self::Target {
        &self.content
    }
}

impl DerefMut for Buffer {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.content
    }
}

impl Buffer {
    pub fn new() -> Self {
        Self {
            content: BufferContent::new(),
            history: Record::new(),
        }
    }

    pub fn is_dirty(&self) -> bool {
        !self.history.is_saved()
    }

    pub fn mark_clean(&mut self) {
        self.history.set_saved();
    }

    pub fn insert(&mut self, byte_offset: usize, text: &str, cursor_before: usize) -> usize {
        let cursor_after = byte_offset + text.len();
        let edit = TextEdit::new(
            byte_offset,
            String::new(),
            text.to_string(),
            cursor_before,
            cursor_after,
        );
        self.history.edit(&mut self.content, edit)
    }

    pub fn delete(&mut self, byte_range: Range<usize>, cursor_before: usize) -> usize {
        let deleted = self.content.slice(byte_range.clone());
        let cursor_after = byte_range.start;
        let edit = TextEdit::new(
            byte_range.start,
            deleted,
            String::new(),
            cursor_before,
            cursor_after,
        );
        self.history.edit(&mut self.content, edit)
    }

    pub fn replace(&mut self, byte_range: Range<usize>, text: &str, cursor_before: usize) -> usize {
        let deleted = self.content.slice(byte_range.clone());
        let cursor_after = byte_range.start + text.len();
        let edit = TextEdit::new(
            byte_range.start,
            deleted,
            text.to_string(),
            cursor_before,
            cursor_after,
        );
        self.history.edit(&mut self.content, edit)
    }

    /// Current head index into the undo history. Pair with `coalesce_since` to
    /// collapse a batch of edits into a single undo entry.
    pub fn undo_head(&self) -> usize {
        self.history.head()
    }

    /// Collapse every edit made since `head` (a prior `undo_head` value) into one
    /// undo entry mapping the state at `head` (`text_before`) directly to
    /// `text_after`. Reverts the intervening edits, then splices a single minimal
    /// replace, so undo/redo treat the whole batch as one step. `cursor_before`/
    /// `cursor_after` are recorded on the entry so undo/redo restore the cursor.
    pub fn coalesce_since(
        &mut self,
        head: usize,
        text_before: &str,
        text_after: &str,
        cursor_before: usize,
        cursor_after: usize,
    ) {
        let (start, old_end, replacement) = minimal_diff(text_before, text_after);
        // Suspend the whole-doc cache rebuild across the batch's revert + reapply; rebuild
        // once at the end. Every exit path below runs the reset+rebuild, so the caches can
        // never stay frozen. Discard the reverted batch either way; only push a new entry
        // if the batch produced a net visible change.
        self.content.suspend_caches = true;
        self.history.go_to(&mut self.content, head);
        if start != old_end || !replacement.is_empty() {
            let mut edit = TextEdit::new(
                start,
                text_before[start..old_end].to_string(),
                replacement,
                cursor_before,
                cursor_after,
            );
            // A coalesced batch is one discrete action — never fold it into prior typing.
            edit.coalescable = false;
            self.history.edit(&mut self.content, edit);
        }
        self.content.suspend_caches = false;
        self.content.update_caches();
    }

    pub fn undo(&mut self) -> Option<usize> {
        self.history.undo(&mut self.content)
    }

    pub fn redo(&mut self) -> Option<usize> {
        self.history.redo(&mut self.content)
    }

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

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

    /// Load a buffer from a file. Returns the buffer and whether the file was modified.
    /// Currently always returns false for modified since we don't normalize on load.
    pub fn from_file(path: &std::path::Path) -> std::io::Result<(Self, bool)> {
        let content = std::fs::read_to_string(path).unwrap_or_default();

        // Parse the file exactly as-is - no normalization
        let mut buffer: Buffer = content.parse().expect("Buffer parsing is infallible");

        // Mark as clean since we just loaded
        buffer.history.set_saved();

        // Second return value is false - we never modify the file on load
        Ok((buffer, false))
    }
}

impl Default for Buffer {
    fn default() -> Self {
        Self::new()
    }
}

/// Minimal replace transforming `old` into `new`: strip the common prefix and
/// suffix, returning `(start, old_end, replacement)` where replacing
/// `old[start..old_end]` with `replacement` yields `new`. Boundaries are snapped
/// to UTF-8 char boundaries so the result is safe to apply to a rope.
fn minimal_diff(old: &str, new: &str) -> (usize, usize, String) {
    let old_b = old.as_bytes();
    let new_b = new.as_bytes();

    let max_pre = old_b.len().min(new_b.len());
    let mut start = 0;
    while start < max_pre && old_b[start] == new_b[start] {
        start += 1;
    }
    while start > 0 && !old.is_char_boundary(start) {
        start -= 1;
    }

    let max_suf = old_b.len().min(new_b.len()) - start;
    let mut suf = 0;
    while suf < max_suf && old_b[old_b.len() - 1 - suf] == new_b[new_b.len() - 1 - suf] {
        suf += 1;
    }
    let mut old_end = old_b.len() - suf;
    while old_end < old_b.len() && !old.is_char_boundary(old_end) {
        old_end += 1;
    }

    let new_end = new_b.len() - (old_b.len() - old_end);
    (start, old_end, new[start..new_end].to_string())
}

impl FromStr for Buffer {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let text = Rope::from_str(s);
        let mut parser = MarkdownParser::default();
        let tree = parser.parse_rope(&text, None);

        let mut content = BufferContent {
            text,
            parser,
            highlighter: Highlighter::new(),
            tree,
            code_highlight_cache: CodeHighlightCache::default(),
            parsed: Rc::new(ParsedNodes::default()),
            inline_styles: Rc::new(Vec::new()),
            version: NEXT_VERSION.fetch_add(1, Ordering::Relaxed),
            suspend_caches: false,
        };

        content.update_caches();

        let mut history = Record::new();
        history.set_saved();

        Ok(Self { content, history })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn apply_diff(old: &str, new: &str) -> String {
        let (start, old_end, replacement) = minimal_diff(old, new);
        let mut result = String::new();
        result.push_str(&old[..start]);
        result.push_str(&replacement);
        result.push_str(&old[old_end..]);
        result
    }

    #[test]
    fn minimal_diff_reconstructs_and_is_minimal() {
        // Single-char change in the middle: tight span, empty ends.
        let (start, old_end, repl) = minimal_diff("- [ ] task", "- [x] task");
        assert_eq!((start, old_end, repl.as_str()), (3, 4, "x"));

        // Pure insertion and reconstruction across several shapes.
        for (old, new) in [
            ("- [ ] task", "- [x] ~~task~~"),
            ("~~hello~~", "hello"),
            ("abc", "abc"),
            ("", "xyz"),
            ("xyz", ""),
        ] {
            assert_eq!(apply_diff(old, new), new, "reconstruct {old:?} -> {new:?}");
        }
    }

    #[test]
    fn minimal_diff_respects_utf8_boundaries() {
        // Edits adjacent to multibyte chars must land on char boundaries.
        let old = "café ☕ done";
        let new = "café ☕ DONE";
        let (start, old_end, _) = minimal_diff(old, new);
        assert!(old.is_char_boundary(start) && old.is_char_boundary(old_end));
        assert_eq!(apply_diff(old, new), new);

        let old2 = "α β γ";
        let new2 = "α X γ";
        assert_eq!(apply_diff(old2, new2), new2);
    }

    /// The linear `inline_styles_by_line` bucketing must match the original
    /// per-line `styles_in_range` (tree styles only), including multi-line emphasis
    /// that spans a soft line break and an enclosing region over nested ones.
    #[test]
    fn inline_styles_by_line_matches_styles_in_range() {
        let src = "**bold** and *italic*\n\
                   a `code` span here\n\
                   soft **wrapped\n\
                   emphasis** across lines\n\
                   plain final line\n";
        let mut buf: Buffer = src.parse().unwrap();
        let snap = buf.render_snapshot();
        let buckets = snap.inline_styles_by_line();
        assert_eq!(buckets.len(), snap.line_count());
        for (i, bucket) in buckets.iter().enumerate() {
            let range = snap.line_byte_range(i);
            let expected: Vec<StyledRegion> = styles_in_range(&snap.inline_styles, &range)
                .into_iter()
                .cloned()
                .collect();
            assert_eq!(
                *bucket, expected,
                "line {i} bucket mismatch (range {range:?})"
            );
        }
        // The multi-line emphasis must appear in both spanned lines' buckets.
        let spans_line2 = buckets[2].iter().any(|s| s.style.bold);
        let spans_line3 = buckets[3].iter().any(|s| s.style.bold);
        assert!(
            spans_line2 && spans_line3,
            "multi-line bold should bucket into both lines"
        );
    }

    #[test]
    fn test_new_buffer_is_empty() {
        let buf = Buffer::new();
        assert!(buf.is_empty());
        assert_eq!(buf.len_bytes(), 0);
    }

    #[test]
    fn test_from_str() {
        let buf: Buffer = "hello world".parse().unwrap();
        assert_eq!(buf.text(), "hello world");
        assert_eq!(buf.len_bytes(), 11);
        assert!(!buf.is_empty());
    }

    #[test]
    fn test_insert() {
        let mut buf: Buffer = "hello world".parse().unwrap();
        buf.insert(5, " beautiful", 5);
        assert_eq!(buf.text(), "hello beautiful world");
    }

    #[test]
    fn test_delete() {
        let mut buf: Buffer = "hello world".parse().unwrap();
        buf.delete(5..11, 11);
        assert_eq!(buf.text(), "hello");
    }

    #[test]
    fn test_replace() {
        let mut buf: Buffer = "hello world".parse().unwrap();
        buf.replace(6..11, "rust", 11);
        assert_eq!(buf.text(), "hello rust");
    }

    #[test]
    fn test_line_operations() {
        let buf: Buffer = "line one\nline two\nline three".parse().unwrap();
        assert_eq!(buf.line_count(), 3);
        assert_eq!(buf.line(0), "line one");
        assert_eq!(buf.line(1), "line two");
        assert_eq!(buf.line(2), "line three");
    }

    #[test]
    fn test_byte_to_line() {
        let buf: Buffer = "abc\ndef\nghi".parse().unwrap();
        assert_eq!(buf.byte_to_line(0), 0);
        assert_eq!(buf.byte_to_line(3), 0);
        assert_eq!(buf.byte_to_line(4), 1);
        assert_eq!(buf.byte_to_line(8), 2);
    }

    #[test]
    fn test_tree_exists_after_parse() {
        let buf: Buffer = "# Hello\n\nSome **bold** text.".parse().unwrap();
        assert!(buf.tree().is_some());
    }

    #[test]
    fn test_tree_updated_after_edit() {
        let mut buf: Buffer = "# Hello".parse().unwrap();
        let tree1 = buf.tree().map(|t| t.block_tree().root_node().to_sexp());

        buf.insert(7, " World", 7);
        let tree2 = buf.tree().map(|t| t.block_tree().root_node().to_sexp());

        assert_ne!(tree1, tree2);
    }

    #[test]
    fn test_undo_insert() {
        let mut buf: Buffer = "hello".parse().unwrap();
        buf.insert(5, " world", 5);
        assert_eq!(buf.text(), "hello world");

        let cursor = buf.undo();
        assert_eq!(cursor, Some(5));
        assert_eq!(buf.text(), "hello");
    }

    #[test]
    fn test_redo_insert() {
        let mut buf: Buffer = "hello".parse().unwrap();
        buf.insert(5, " world", 5);
        buf.undo();

        let cursor = buf.redo();
        assert_eq!(cursor, Some(11));
        assert_eq!(buf.text(), "hello world");
    }

    #[test]
    fn test_undo_delete() {
        let mut buf: Buffer = "hello world".parse().unwrap();
        buf.delete(5..11, 11);
        assert_eq!(buf.text(), "hello");

        let cursor = buf.undo();
        assert_eq!(cursor, Some(11));
        assert_eq!(buf.text(), "hello world");
    }

    #[test]
    fn test_dirty_state() {
        let mut buf: Buffer = "hello".parse().unwrap();
        assert!(!buf.is_dirty());

        buf.insert(5, " world", 5);
        assert!(buf.is_dirty());

        buf.mark_clean();
        assert!(!buf.is_dirty());

        buf.insert(11, "!", 11);
        assert!(buf.is_dirty());
    }

    #[test]
    fn test_dirty_after_undo_to_save_point() {
        let mut buf: Buffer = "hello".parse().unwrap();
        buf.insert(5, " world", 5);
        buf.mark_clean();
        assert!(!buf.is_dirty());

        buf.insert(11, "!", 11);
        assert!(buf.is_dirty());

        buf.undo();
        assert!(!buf.is_dirty());
    }

    #[test]
    fn test_dirty_save_point_unreachable() {
        let mut buf: Buffer = "hello".parse().unwrap();
        buf.insert(5, " world", 5);
        buf.mark_clean();

        buf.undo();
        assert!(buf.is_dirty());

        buf.insert(5, " rust", 5);
        assert!(buf.is_dirty());

        buf.undo();
        assert!(buf.is_dirty());
    }

    #[test]
    fn test_multiple_undo_redo() {
        // Multi-char inserts are non-coalescable, so each is its own undo step — this
        // exercises the undo/redo *stack* mechanics (single-char typing coalesces, which
        // is covered separately by the editor coalescing tests).
        let mut buf: Buffer = "a".parse().unwrap();
        buf.insert(1, "bb", 1);
        buf.insert(3, "cc", 3);
        buf.insert(5, "dd", 5);
        assert_eq!(buf.text(), "abbccdd");

        buf.undo();
        assert_eq!(buf.text(), "abbcc");
        buf.undo();
        assert_eq!(buf.text(), "abb");
        buf.undo();
        assert_eq!(buf.text(), "a");

        buf.redo();
        assert_eq!(buf.text(), "abb");
        buf.redo();
        assert_eq!(buf.text(), "abbcc");
        buf.redo();
        assert_eq!(buf.text(), "abbccdd");
    }

    #[test]
    fn test_ordered_list_no_normalization() {
        // Numbers are preserved as-is (no renumbering)
        let buf: Buffer = "1. First\n5. Second\n9. Third\n".parse().unwrap();
        assert_eq!(buf.text(), "1. First\n5. Second\n9. Third\n");
    }

    #[test]
    fn test_ordered_list_parenthesis_no_normalization() {
        // Parenthesis style numbers are preserved as-is
        let buf: Buffer = "1) First\n5) Second\n9) Third\n".parse().unwrap();
        assert_eq!(buf.text(), "1) First\n5) Second\n9) Third\n");
    }

    #[test]
    fn test_unordered_list_unchanged() {
        let buf: Buffer = "- First\n- Second\n- Third\n".parse().unwrap();
        assert_eq!(buf.text(), "- First\n- Second\n- Third\n");
    }

    /// Structural edits that MERGE two ordered lists still renumber, even though the
    /// gate only runs `normalize_ordered_lists` for edits landing in an ordered list:
    /// a merge leaves the edit offset at the seam, which is inside the resulting list,
    /// so `edit_in_ordered_list` still fires. Guards the `normalize` gating (F2) against
    /// a false-negative where merged lists would keep stale numbers.
    #[test]
    fn merging_ordered_lists_renumbers() {
        // Blank-line-separated lists each restarting at 1.; delete the blank to merge.
        let mut buf: Buffer = "1. a\n\n1. b\n".parse().unwrap();
        let blank = buf.text().find("a\n\n").unwrap() + 2; // the second '\n'
        buf.delete(blank..blank + 1, blank);
        assert_eq!(buf.text(), "1. a\n2. b\n", "merged lists renumber");

        // A malformed list (2., 5.) brought to byte 0 by a leading-paragraph delete:
        // the offset-0 descendant must still resolve into the list.
        let mut buf2: Buffer = "x\n\n2. a\n5. b\n".parse().unwrap();
        let end = buf2.text().find("2. a").unwrap();
        buf2.delete(0..end, 0);
        assert_eq!(buf2.text(), "1. a\n2. b\n", "list at offset 0 renumbers");
    }

    #[test]
    fn edit_in_ordered_list_renumbers() {
        // A non-sequential list gets renumbered when the edit lands inside it.
        let mut buf: Buffer = "1. a\n5. b\n".parse().unwrap();
        let off = buf.text().find('a').unwrap() + 1;
        buf.insert(off, "X", off);
        assert_eq!(buf.text(), "1. aX\n2. b\n", "in-list edit renumbers");
    }

    #[test]
    fn edit_outside_ordered_list_does_not_renumber() {
        // Editing a paragraph after a non-sequential list must not perturb its numbers.
        let mut buf: Buffer = "1. a\n5. b\n\nhello\n".parse().unwrap();
        let off = buf.text().find("hello").unwrap() + 5;
        buf.insert(off, "X", off);
        assert_eq!(
            buf.text(),
            "1. a\n5. b\n\nhelloX\n",
            "out-of-list edit leaves list numbering untouched"
        );
    }

    #[test]
    fn delete_in_ordered_list_renumbers() {
        // Deleting a middle item renumbers the remainder (empty edit range inside list).
        let mut buf: Buffer = "1. a\n2. b\n3. c\n".parse().unwrap();
        let start = buf.text().find("2.").unwrap();
        let end = buf.text().find("3.").unwrap();
        buf.delete(start..end, start);
        assert_eq!(buf.text(), "1. a\n2. c\n", "in-list delete renumbers");
    }

    // Line extraction tests (moved from lines.rs)

    use crate::marker::MarkerKind;

    #[test]
    fn test_lines_empty_buffer() {
        let buf: Buffer = "".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(lines.len(), 1);
        assert_eq!(lines[0].range, 0..0);
        assert!(lines[0].markers.is_empty());
    }

    #[test]
    fn test_lines_single_newline() {
        let buf: Buffer = "\n".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0].range, 0..0);
        assert_eq!(lines[1].range, 1..1);
    }

    #[test]
    fn test_lines_paragraph() {
        let buf: Buffer = "Hello\n\nWorld\n".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(lines.len(), 4);
        assert_eq!(lines[0].range, 0..5);
        assert_eq!(lines[1].range, 6..6); // blank line
        assert_eq!(lines[2].range, 7..12);
    }

    #[test]
    fn test_heading_markers() {
        let buf: Buffer = "# Hello\n".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0].heading_level(), Some(1));
        assert_eq!(lines[0].marker_range(), Some(0..2));
    }

    #[test]
    fn test_heading_levels() {
        let buf: Buffer = "# H1\n## H2\n### H3\n".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(lines[0].heading_level(), Some(1));
        assert_eq!(lines[1].heading_level(), Some(2));
        assert_eq!(lines[2].heading_level(), Some(3));
    }

    #[test]
    fn test_unordered_list_markers() {
        let buf: Buffer = "- Item 1\n- Item 2\n".parse().unwrap();
        let lines = buf.lines();

        assert!(
            lines[0]
                .markers
                .iter()
                .any(|m| matches!(m.kind, MarkerKind::ListItem { ordered: false, .. }))
        );
        assert_eq!(lines[0].marker_range(), Some(0..2));
    }

    #[test]
    fn test_ordered_list_markers() {
        let buf: Buffer = "1. First\n2. Second\n".parse().unwrap();
        let lines = buf.lines();

        assert!(
            lines[0]
                .markers
                .iter()
                .any(|m| matches!(m.kind, MarkerKind::ListItem { ordered: true, .. }))
        );
    }

    #[test]
    fn test_checkbox_markers() {
        let buf: Buffer = "- [ ] Unchecked\n- [x] Checked\n".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(lines[0].checkbox(), Some(false));
        assert_eq!(lines[1].checkbox(), Some(true));
    }

    #[test]
    fn test_blockquote_markers() {
        let buf: Buffer = "> Quote\n".parse().unwrap();
        let lines = buf.lines();

        assert!(lines[0].has_border());
        assert!(
            lines[0]
                .markers
                .iter()
                .any(|m| matches!(m.kind, MarkerKind::BlockQuote))
        );
    }

    #[test]
    fn test_is_line_empty_blockquote_only() {
        // A line with just "> " (blockquote marker, no content) should be empty
        let buf: Buffer = "> hey\n> \n> > hey".parse().unwrap();
        assert!(!buf.is_line_empty(0)); // "> hey" has content
        assert!(buf.is_line_empty(1)); // "> " is empty (marker only)
        assert!(!buf.is_line_empty(2)); // "> > hey" has content
    }

    #[test]
    fn test_marker_range_includes_trailing_space() {
        // "> hey" - marker should be "> " (bytes 0..2)
        let buf: Buffer = "> hey".parse().unwrap();
        let lines = buf.lines();
        assert_eq!(lines[0].marker_range(), Some(0..2)); // "> " includes the space

        // "- item" - marker should be "- " (bytes 0..2)
        let buf: Buffer = "- item".parse().unwrap();
        let lines = buf.lines();
        assert_eq!(lines[0].marker_range(), Some(0..2)); // "- " includes the space

        // "> > hey" - marker should be "> > " (bytes 0..4)
        let buf: Buffer = "> > hey".parse().unwrap();
        let lines = buf.lines();
        assert_eq!(lines[0].marker_range(), Some(0..4)); // "> > " includes both spaces
    }

    #[test]
    fn test_nested_blockquote_lines() {
        let buf: Buffer = "> Level 1\n> > Level 2\n".parse().unwrap();
        let lines = buf.lines();

        assert_eq!(
            lines[0]
                .markers
                .iter()
                .filter(|m| matches!(m.kind, MarkerKind::BlockQuote))
                .count(),
            1
        );
        assert_eq!(
            lines[1]
                .markers
                .iter()
                .filter(|m| matches!(m.kind, MarkerKind::BlockQuote))
                .count(),
            2
        );
    }

    #[test]
    fn test_list_in_blockquote_lines() {
        let buf: Buffer = "> - Item\n".parse().unwrap();
        let lines = buf.lines();

        assert!(
            lines[0]
                .markers
                .iter()
                .any(|m| matches!(m.kind, MarkerKind::BlockQuote))
        );
        assert!(
            lines[0]
                .markers
                .iter()
                .any(|m| matches!(m.kind, MarkerKind::ListItem { .. }))
        );
    }

    #[test]
    fn test_code_block_fence_lines() {
        let buf: Buffer = "```rust\nlet x = 1;\n```\n".parse().unwrap();
        let lines = buf.lines();

        assert!(lines[0].is_fence());
        assert!(!lines[1].is_fence());
        assert!(lines[2].is_fence());
    }

    #[test]
    fn test_thematic_break_lines() {
        let buf: Buffer = "---\n".parse().unwrap();
        let lines = buf.lines();

        assert!(
            lines[0]
                .markers
                .iter()
                .any(|m| matches!(m.kind, MarkerKind::ThematicBreak))
        );
    }

    #[test]
    fn test_nested_list_continuation() {
        let buf: Buffer = "- Item 1\n  - Nested\n".parse().unwrap();
        let lines = buf.lines();

        let continuation = lines[1].continuation_rope(buf.rope());
        assert!(continuation.contains("- "));
    }

    #[test]
    fn test_substitution() {
        let buf: Buffer = "- Item\n".parse().unwrap();
        let lines = buf.lines();

        // List markers are now rendered as spacers, not substitution
        let sub = lines[0].substitution_rope(buf.rope());
        assert_eq!(sub, "");
    }

    #[test]
    fn test_list_in_blockquote_continuation() {
        let buf: Buffer = "> - Item\n".parse().unwrap();
        let lines = buf.lines();

        let continuation = lines[0].continuation_rope(buf.rope());
        assert_eq!(continuation, "> - ");
    }

    #[test]
    fn test_multiline_blockquote_with_list_continuation() {
        let buf: Buffer = "> hey\n>\n> - foo\n".parse().unwrap();
        let lines = buf.lines();

        let continuation = lines[2].continuation_rope(buf.rope());
        assert_eq!(continuation, "> - ");
    }

    #[test]
    fn test_code_block_in_blockquote() {
        let buf: Buffer = "> ```rust\n> fn main() {}\n> ```\n".parse().unwrap();
        let lines = buf.lines();

        assert!(lines[0].is_fence());
        assert!(lines[0].has_border());
    }
}