rgx-cli 0.11.0

A terminal regex tester with real-time matching, multi-engine support, and plain-English explanations
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
use std::collections::VecDeque;

use unicode_width::UnicodeWidthStr;

const MAX_UNDO_STACK: usize = 500;

#[derive(Debug, Clone)]
pub struct Editor {
    content: String,
    cursor: usize,
    scroll_offset: usize,
    vertical_scroll: usize,
    undo_stack: VecDeque<(String, usize)>,
    redo_stack: VecDeque<(String, usize)>,
}

impl Editor {
    pub fn new() -> Self {
        Self {
            content: String::new(),
            cursor: 0,
            scroll_offset: 0,
            vertical_scroll: 0,
            undo_stack: VecDeque::new(),
            redo_stack: VecDeque::new(),
        }
    }

    pub fn with_content(content: String) -> Self {
        let cursor = content.len();
        Self {
            content,
            cursor,
            scroll_offset: 0,
            vertical_scroll: 0,
            undo_stack: VecDeque::new(),
            redo_stack: VecDeque::new(),
        }
    }

    pub fn content(&self) -> &str {
        &self.content
    }

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

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

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

    /// Returns (line, col) of the cursor where col is the display width within the line.
    pub fn cursor_line_col(&self) -> (usize, usize) {
        let before = &self.content[..self.cursor];
        let line = before.matches('\n').count();
        let line_start = before.rfind('\n').map(|p| p + 1).unwrap_or(0);
        let col = UnicodeWidthStr::width(&self.content[line_start..self.cursor]);
        (line, col)
    }

    pub fn line_count(&self) -> usize {
        self.content.matches('\n').count() + 1
    }

    /// Byte offset of the start of line `n` (0-indexed).
    fn line_start(&self, n: usize) -> usize {
        if n == 0 {
            return 0;
        }
        let mut count = 0;
        for (i, c) in self.content.char_indices() {
            if c == '\n' {
                count += 1;
                if count == n {
                    return i + 1;
                }
            }
        }
        self.content.len()
    }

    /// Byte offset of the end of line `n` (before the newline, or end of string).
    fn line_end(&self, n: usize) -> usize {
        let start = self.line_start(n);
        match self.content[start..].find('\n') {
            Some(pos) => start + pos,
            None => self.content.len(),
        }
    }

    /// Content of line `n`.
    fn line_content(&self, n: usize) -> &str {
        &self.content[self.line_start(n)..self.line_end(n)]
    }

    /// Visual cursor column within the current line.
    pub fn visual_cursor(&self) -> usize {
        let (_, col) = self.cursor_line_col();
        col.saturating_sub(self.scroll_offset)
    }

    fn push_undo_snapshot(&mut self) {
        self.undo_stack
            .push_back((self.content.clone(), self.cursor));
        if self.undo_stack.len() > MAX_UNDO_STACK {
            self.undo_stack.pop_front();
        }
        self.redo_stack.clear();
    }

    pub fn undo(&mut self) -> bool {
        if let Some((content, cursor)) = self.undo_stack.pop_back() {
            self.redo_stack
                .push_back((self.content.clone(), self.cursor));
            self.content = content;
            self.cursor = cursor;
            true
        } else {
            false
        }
    }

    pub fn redo(&mut self) -> bool {
        if let Some((content, cursor)) = self.redo_stack.pop_back() {
            self.undo_stack
                .push_back((self.content.clone(), self.cursor));
            self.content = content;
            self.cursor = cursor;
            true
        } else {
            false
        }
    }

    pub fn insert_char(&mut self, c: char) {
        self.push_undo_snapshot();
        self.content.insert(self.cursor, c);
        self.cursor += c.len_utf8();
    }

    pub fn insert_newline(&mut self) {
        self.push_undo_snapshot();
        self.content.insert(self.cursor, '\n');
        self.cursor += 1;
    }

    pub fn delete_back(&mut self) {
        if self.cursor > 0 {
            self.push_undo_snapshot();
            let prev = self.prev_char_boundary();
            self.content.drain(prev..self.cursor);
            self.cursor = prev;
        }
    }

    pub fn delete_forward(&mut self) {
        if self.cursor < self.content.len() {
            self.push_undo_snapshot();
            let next = self.next_char_boundary();
            self.content.drain(self.cursor..next);
        }
    }

    pub fn move_left(&mut self) {
        if self.cursor > 0 {
            self.cursor = self.prev_char_boundary();
        }
    }

    /// Move cursor left by one char, but not past the start of the current line.
    /// Used by vim Esc (EnterNormalMode) which should not cross line boundaries.
    pub fn move_left_in_line(&mut self) {
        if self.cursor > 0 && self.content.as_bytes()[self.cursor - 1] != b'\n' {
            self.cursor = self.prev_char_boundary();
        }
    }

    pub fn move_right(&mut self) {
        if self.cursor < self.content.len() {
            self.cursor = self.next_char_boundary();
        }
    }

    /// Move cursor left by one word (to previous word boundary).
    pub fn move_word_left(&mut self) {
        if self.cursor == 0 {
            return;
        }
        let before = &self.content[..self.cursor];
        let mut chars = before.char_indices().rev();
        // Skip any non-word chars immediately before cursor
        let mut last_idx = self.cursor;
        for (i, c) in &mut chars {
            if c.is_alphanumeric() || c == '_' {
                last_idx = i;
                break;
            }
            last_idx = i;
        }
        // Skip word chars to find the start of the word
        if last_idx < self.cursor {
            let before_word = &self.content[..last_idx];
            for (i, c) in before_word.char_indices().rev() {
                if !(c.is_alphanumeric() || c == '_') {
                    self.cursor = i + c.len_utf8();
                    return;
                }
            }
            // Reached start of string
            self.cursor = 0;
        } else {
            self.cursor = 0;
        }
    }

    /// Move cursor right by one word (to next word boundary).
    pub fn move_word_right(&mut self) {
        if self.cursor >= self.content.len() {
            return;
        }
        let after = &self.content[self.cursor..];
        let mut chars = after.char_indices();
        // Skip any word chars from current position
        let mut advanced = false;
        for (i, c) in &mut chars {
            if !(c.is_alphanumeric() || c == '_') {
                if advanced {
                    self.cursor += i;
                    // Skip non-word chars to reach next word
                    let remaining = &self.content[self.cursor..];
                    for (j, c2) in remaining.char_indices() {
                        if c2.is_alphanumeric() || c2 == '_' {
                            self.cursor += j;
                            return;
                        }
                    }
                    self.cursor = self.content.len();
                    return;
                }
                // We started on non-word chars, skip them
                let remaining = &self.content[self.cursor + i + c.len_utf8()..];
                for (j, c2) in remaining.char_indices() {
                    if c2.is_alphanumeric() || c2 == '_' {
                        self.cursor = self.cursor + i + c.len_utf8() + j;
                        return;
                    }
                }
                self.cursor = self.content.len();
                return;
            }
            advanced = true;
        }
        self.cursor = self.content.len();
    }

    pub fn move_up(&mut self) {
        let (line, col) = self.cursor_line_col();
        if line > 0 {
            let target_line = line - 1;
            let target_start = self.line_start(target_line);
            let target_content = self.line_content(target_line);
            self.cursor = target_start + byte_offset_at_width(target_content, col);
        }
    }

    pub fn move_down(&mut self) {
        let (line, col) = self.cursor_line_col();
        if line + 1 < self.line_count() {
            let target_line = line + 1;
            let target_start = self.line_start(target_line);
            let target_content = self.line_content(target_line);
            self.cursor = target_start + byte_offset_at_width(target_content, col);
        }
    }

    /// Move to start of current line.
    pub fn move_home(&mut self) {
        let (line, _) = self.cursor_line_col();
        self.cursor = self.line_start(line);
        self.scroll_offset = 0;
    }

    /// Move to end of current line.
    pub fn move_end(&mut self) {
        let (line, _) = self.cursor_line_col();
        self.cursor = self.line_end(line);
    }

    /// Delete character under cursor (vim `x`). Does nothing at end of content.
    pub fn delete_char_at_cursor(&mut self) {
        self.delete_forward();
    }

    /// Delete the current line (vim `dd`).
    pub fn delete_line(&mut self) {
        self.push_undo_snapshot();
        let (line, _) = self.cursor_line_col();
        let start = self.line_start(line);
        let end = self.line_end(line);
        let line_count = self.line_count();

        if line_count == 1 {
            self.content.clear();
            self.cursor = 0;
        } else if line + 1 < line_count {
            // Not the last line — delete line including trailing newline
            self.content.drain(start..end + 1);
            self.cursor = start;
        } else {
            // Last line — delete leading newline + line content
            self.content.drain(start - 1..end);
            let prev = line.saturating_sub(1);
            self.cursor = self.line_start(prev);
        }
    }

    /// Clear the current line's content but keep the line (vim `cc`).
    pub fn clear_line(&mut self) {
        self.push_undo_snapshot();
        let (line, _) = self.cursor_line_col();
        let start = self.line_start(line);
        let end = self.line_end(line);
        self.content.drain(start..end);
        self.cursor = start;
    }

    /// Insert a string at cursor (single undo snapshot). Used for paste.
    pub fn insert_str(&mut self, s: &str) {
        if s.is_empty() {
            return;
        }
        self.push_undo_snapshot();
        self.content.insert_str(self.cursor, s);
        self.cursor += s.len();
    }

    /// Insert a new line below current and move cursor there (vim `o`).
    pub fn open_line_below(&mut self) {
        self.push_undo_snapshot();
        let (line, _) = self.cursor_line_col();
        let end = self.line_end(line);
        self.content.insert(end, '\n');
        self.cursor = end + 1;
    }

    /// Insert a new line above current and move cursor there (vim `O`).
    pub fn open_line_above(&mut self) {
        self.push_undo_snapshot();
        let (line, _) = self.cursor_line_col();
        let start = self.line_start(line);
        self.content.insert(start, '\n');
        self.cursor = start;
    }

    /// Move cursor to first non-whitespace character on current line (vim `^`).
    pub fn move_to_first_non_blank(&mut self) {
        let (line, _) = self.cursor_line_col();
        let start = self.line_start(line);
        let line_text = self.line_content(line);
        let offset = line_text
            .char_indices()
            .find(|(_, c)| !c.is_whitespace())
            .map(|(i, _)| i)
            .unwrap_or(0);
        self.cursor = start + offset;
    }

    /// Move cursor to start of first line (vim `gg`).
    pub fn move_to_first_line(&mut self) {
        self.cursor = 0;
    }

    /// Move cursor to start of last line (vim `G`).
    pub fn move_to_last_line(&mut self) {
        let last = self.line_count().saturating_sub(1);
        self.cursor = self.line_start(last);
    }

    /// Move cursor forward to end of current/next word (vim `e`).
    pub fn move_word_forward_end(&mut self) {
        if self.cursor >= self.content.len() {
            return;
        }
        let is_word_char = |c: char| c.is_alphanumeric() || c == '_';
        let after = &self.content[self.cursor..];
        let mut chars = after.char_indices().peekable();

        // Always advance at least one character
        if chars.next().is_none() {
            return;
        }

        // Skip whitespace
        while let Some(&(_, c)) = chars.peek() {
            if !c.is_whitespace() {
                break;
            }
            chars.next();
        }

        // Find end of the word
        if let Some(&(first_offset, first)) = chars.peek() {
            let first_is_word = is_word_char(first);
            let mut last_offset = first_offset;
            chars.next();

            for (i, c) in chars {
                if is_word_char(c) != first_is_word || c.is_whitespace() {
                    break;
                }
                last_offset = i;
            }
            self.cursor += last_offset;
        }
    }

    /// Update horizontal scroll for the current line.
    pub fn update_scroll(&mut self, visible_width: usize) {
        let (_, col) = self.cursor_line_col();
        if col < self.scroll_offset {
            self.scroll_offset = col;
        } else if col >= self.scroll_offset + visible_width {
            self.scroll_offset = col - visible_width + 1;
        }
    }

    /// Update vertical scroll to keep cursor visible within `visible_height` lines.
    pub fn update_vertical_scroll(&mut self, visible_height: usize) {
        let (line, _) = self.cursor_line_col();
        if line < self.vertical_scroll {
            self.vertical_scroll = line;
        } else if line >= self.vertical_scroll + visible_height {
            self.vertical_scroll = line - visible_height + 1;
        }
    }

    /// Set cursor by display column (single-line editors / mouse click).
    pub fn set_cursor_by_col(&mut self, col: usize) {
        self.cursor = byte_offset_at_width(&self.content, col);
    }

    /// Set cursor by (line, col) position (multi-line editors / mouse click).
    pub fn set_cursor_by_position(&mut self, line: usize, col: usize) {
        let target_line = line.min(self.line_count().saturating_sub(1));
        let start = self.line_start(target_line);
        let line_text = self.line_content(target_line);
        self.cursor = start + byte_offset_at_width(line_text, col);
    }

    fn prev_char_boundary(&self) -> usize {
        let mut pos = self.cursor - 1;
        while !self.content.is_char_boundary(pos) {
            pos -= 1;
        }
        pos
    }

    fn next_char_boundary(&self) -> usize {
        let mut pos = self.cursor + 1;
        while pos < self.content.len() && !self.content.is_char_boundary(pos) {
            pos += 1;
        }
        pos
    }
}

/// Convert a target display column width to a byte offset within a line string.
fn byte_offset_at_width(line: &str, target_width: usize) -> usize {
    let mut width = 0;
    for (i, c) in line.char_indices() {
        if width >= target_width {
            return i;
        }
        width += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
    }
    line.len()
}

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

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

    #[test]
    fn test_insert_and_content() {
        let mut editor = Editor::new();
        editor.insert_char('h');
        editor.insert_char('i');
        assert_eq!(editor.content(), "hi");
        assert_eq!(editor.cursor(), 2);
    }

    #[test]
    fn test_delete_back() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.delete_back();
        assert_eq!(editor.content(), "hell");
    }

    #[test]
    fn test_cursor_movement() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.move_left();
        assert_eq!(editor.cursor(), 4);
        editor.move_home();
        assert_eq!(editor.cursor(), 0);
        editor.move_end();
        assert_eq!(editor.cursor(), 5);
    }

    #[test]
    fn test_insert_newline() {
        let mut editor = Editor::new();
        editor.insert_char('a');
        editor.insert_newline();
        editor.insert_char('b');
        assert_eq!(editor.content(), "a\nb");
        assert_eq!(editor.cursor(), 3);
    }

    #[test]
    fn test_cursor_line_col() {
        let editor = Editor::with_content("abc\ndef\nghi".to_string());
        // cursor is at end: line 2, col 3
        assert_eq!(editor.cursor_line_col(), (2, 3));
    }

    #[test]
    fn test_move_up_down() {
        let mut editor = Editor::with_content("abc\ndef\nghi".to_string());
        // cursor at end of "ghi" (line 2, col 3)
        editor.move_up();
        assert_eq!(editor.cursor_line_col(), (1, 3));
        assert_eq!(&editor.content()[..editor.cursor()], "abc\ndef");
        editor.move_up();
        assert_eq!(editor.cursor_line_col(), (0, 3));
        assert_eq!(&editor.content()[..editor.cursor()], "abc");
        // move_up at top does nothing
        editor.move_up();
        assert_eq!(editor.cursor_line_col(), (0, 3));
        // move back down
        editor.move_down();
        assert_eq!(editor.cursor_line_col(), (1, 3));
    }

    #[test]
    fn test_move_up_clamps_column() {
        let mut editor = Editor::with_content("abcdef\nab\nxyz".to_string());
        // cursor at end: line 2, col 3
        editor.move_up();
        // line 1 is "ab" (col 2) — should clamp to end of line
        assert_eq!(editor.cursor_line_col(), (1, 2));
        editor.move_up();
        // line 0 is "abcdef" — col 2
        assert_eq!(editor.cursor_line_col(), (0, 2));
    }

    #[test]
    fn test_line_helpers() {
        let editor = Editor::with_content("abc\ndef\nghi".to_string());
        assert_eq!(editor.line_count(), 3);
        assert_eq!(editor.line_content(0), "abc");
        assert_eq!(editor.line_content(1), "def");
        assert_eq!(editor.line_content(2), "ghi");
    }

    #[test]
    fn test_home_end_multiline() {
        let mut editor = Editor::with_content("abc\ndef".to_string());
        // cursor at end of "def" (line 1)
        editor.move_home();
        // should go to start of line 1
        assert_eq!(editor.cursor(), 4); // "abc\n" = 4 bytes
        assert_eq!(editor.cursor_line_col(), (1, 0));
        editor.move_end();
        assert_eq!(editor.cursor(), 7); // "abc\ndef" = 7 bytes
        assert_eq!(editor.cursor_line_col(), (1, 3));
    }

    #[test]
    fn test_vertical_scroll() {
        let mut editor = Editor::with_content("a\nb\nc\nd\ne".to_string());
        editor.update_vertical_scroll(3);
        // cursor at line 4, visible_height 3 => scroll to 2
        assert_eq!(editor.vertical_scroll(), 2);
    }

    #[test]
    fn test_undo_insert() {
        let mut editor = Editor::new();
        editor.insert_char('a');
        editor.insert_char('b');
        assert_eq!(editor.content(), "ab");
        editor.undo();
        assert_eq!(editor.content(), "a");
        editor.undo();
        assert_eq!(editor.content(), "");
        // Undo on empty stack returns false
        assert!(!editor.undo());
    }

    #[test]
    fn test_undo_delete() {
        let mut editor = Editor::with_content("abc".to_string());
        editor.delete_back();
        assert_eq!(editor.content(), "ab");
        editor.undo();
        assert_eq!(editor.content(), "abc");
    }

    #[test]
    fn test_redo() {
        let mut editor = Editor::new();
        editor.insert_char('a');
        editor.insert_char('b');
        editor.undo();
        assert_eq!(editor.content(), "a");
        editor.redo();
        assert_eq!(editor.content(), "ab");
        // Redo on empty stack returns false
        assert!(!editor.redo());
    }

    #[test]
    fn test_redo_cleared_on_new_edit() {
        let mut editor = Editor::new();
        editor.insert_char('a');
        editor.insert_char('b');
        editor.undo();
        // Now type something different — redo stack should clear
        editor.insert_char('c');
        assert_eq!(editor.content(), "ac");
        assert!(!editor.redo());
    }

    #[test]
    fn test_set_cursor_by_col() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.set_cursor_by_col(3);
        assert_eq!(editor.cursor(), 3);
    }

    #[test]
    fn test_set_cursor_by_position() {
        let mut editor = Editor::with_content("abc\ndef\nghi".to_string());
        editor.set_cursor_by_position(1, 2);
        assert_eq!(editor.cursor_line_col(), (1, 2));
    }

    #[test]
    fn test_move_word_right() {
        let mut editor = Editor::with_content("hello world foo".to_string());
        editor.cursor = 0;
        editor.move_word_right();
        // Should skip past "hello" and stop at start of "world"
        assert_eq!(editor.cursor(), 6);
        editor.move_word_right();
        assert_eq!(editor.cursor(), 12);
        editor.move_word_right();
        assert_eq!(editor.cursor(), 15); // end
    }

    #[test]
    fn test_move_word_left() {
        let mut editor = Editor::with_content("hello world foo".to_string());
        // cursor at end
        editor.move_word_left();
        assert_eq!(editor.cursor(), 12); // start of "foo"
        editor.move_word_left();
        assert_eq!(editor.cursor(), 6); // start of "world"
        editor.move_word_left();
        assert_eq!(editor.cursor(), 0); // start of "hello"
    }

    #[test]
    fn test_delete_back_across_newline() {
        let mut editor = Editor::with_content("abc\ndef".to_string());
        // cursor at start of "def" (byte 4)
        editor.cursor = 4;
        editor.delete_back();
        assert_eq!(editor.content(), "abcdef");
        assert_eq!(editor.cursor(), 3);
    }

    #[test]
    fn test_delete_char_at_cursor() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.cursor = 0;
        editor.delete_char_at_cursor();
        assert_eq!(editor.content(), "ello");
        assert_eq!(editor.cursor(), 0);
    }

    #[test]
    fn test_delete_char_at_cursor_end() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.delete_char_at_cursor();
        assert_eq!(editor.content(), "hello");
    }

    #[test]
    fn test_delete_line() {
        let mut editor = Editor::with_content("abc\ndef\nghi".to_string());
        editor.cursor = 5;
        editor.delete_line();
        assert_eq!(editor.content(), "abc\nghi");
        assert_eq!(editor.cursor(), 4);
    }

    #[test]
    fn test_delete_line_last() {
        let mut editor = Editor::with_content("abc\ndef".to_string());
        editor.cursor = 5;
        editor.delete_line();
        assert_eq!(editor.content(), "abc");
        assert_eq!(editor.cursor(), 0);
    }

    #[test]
    fn test_delete_line_single() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.cursor = 2;
        editor.delete_line();
        assert_eq!(editor.content(), "");
        assert_eq!(editor.cursor(), 0);
    }

    #[test]
    fn test_clear_line() {
        let mut editor = Editor::with_content("abc\ndef\nghi".to_string());
        editor.cursor = 5;
        editor.clear_line();
        assert_eq!(editor.content(), "abc\n\nghi");
        assert_eq!(editor.cursor(), 4);
    }

    #[test]
    fn test_clear_line_single() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.cursor = 2;
        editor.clear_line();
        assert_eq!(editor.content(), "");
        assert_eq!(editor.cursor(), 0);
    }

    #[test]
    fn test_insert_str() {
        let mut editor = Editor::with_content("hd".to_string());
        editor.cursor = 1;
        editor.insert_str("ello worl");
        assert_eq!(editor.content(), "hello world");
        assert_eq!(editor.cursor(), 10);
    }

    #[test]
    fn test_insert_str_undo() {
        let mut editor = Editor::with_content("ad".to_string());
        editor.cursor = 1;
        editor.insert_str("bc");
        assert_eq!(editor.content(), "abcd");
        editor.undo();
        assert_eq!(editor.content(), "ad");
    }

    #[test]
    fn test_open_line_below() {
        let mut editor = Editor::with_content("abc\ndef".to_string());
        editor.cursor = 1;
        editor.open_line_below();
        assert_eq!(editor.content(), "abc\n\ndef");
        assert_eq!(editor.cursor(), 4);
    }

    #[test]
    fn test_open_line_above() {
        let mut editor = Editor::with_content("abc\ndef".to_string());
        editor.cursor = 5;
        editor.open_line_above();
        assert_eq!(editor.content(), "abc\n\ndef");
        assert_eq!(editor.cursor(), 4);
    }

    #[test]
    fn test_move_to_first_non_blank() {
        let mut editor = Editor::with_content("   hello".to_string());
        editor.cursor = 7;
        editor.move_to_first_non_blank();
        assert_eq!(editor.cursor(), 3);
    }

    #[test]
    fn test_move_to_first_line() {
        let mut editor = Editor::with_content("abc\ndef\nghi".to_string());
        editor.move_to_first_line();
        assert_eq!(editor.cursor(), 0);
    }

    #[test]
    fn test_move_to_last_line() {
        let mut editor = Editor::with_content("abc\ndef\nghi".to_string());
        editor.cursor = 0;
        editor.move_to_last_line();
        let (line, _) = editor.cursor_line_col();
        assert_eq!(line, 2);
    }

    #[test]
    fn test_move_word_forward_end() {
        let mut editor = Editor::with_content("hello world foo".to_string());
        editor.cursor = 0;
        editor.move_word_forward_end();
        assert_eq!(editor.cursor(), 4);
        editor.move_word_forward_end();
        assert_eq!(editor.cursor(), 10);
    }

    #[test]
    fn test_move_left_in_line_normal() {
        let mut editor = Editor::with_content("hello".to_string());
        // cursor at end (byte 5)
        editor.move_left_in_line();
        assert_eq!(editor.cursor(), 4);
    }

    #[test]
    fn test_move_left_in_line_at_line_start() {
        let mut editor = Editor::with_content("abc\ndef".to_string());
        // cursor at start of "def" (byte 4, right after '\n')
        editor.cursor = 4;
        editor.move_left_in_line();
        // Should NOT cross the newline
        assert_eq!(editor.cursor(), 4);
    }

    #[test]
    fn test_move_left_in_line_at_content_start() {
        let mut editor = Editor::with_content("hello".to_string());
        editor.cursor = 0;
        editor.move_left_in_line();
        assert_eq!(editor.cursor(), 0);
    }
}