revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! UTF-8 aware text buffer for editing widgets
//!
//! Provides a reusable text buffer with:
//! - Character-based (not byte-based) cursor positioning
//! - Selection support (anchor + cursor)
//! - Word navigation
//! - Efficient UTF-8 handling
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::utils::TextBuffer;
//!
//! let mut buffer = TextBuffer::new();
//! buffer.insert_char('H');
//! buffer.insert_char('i');
//! buffer.insert_str(" 🎉");
//!
//! assert_eq!(buffer.text(), "Hi 🎉");
//! assert_eq!(buffer.char_count(), 4);  // Not 7 bytes!
//! ```

/// A UTF-8 aware text buffer for single-line text editing
///
/// All positions are in CHARACTER indices, not byte indices.
/// This ensures correct handling of multi-byte UTF-8 characters
/// (emoji, CJK, etc.).
#[derive(Clone, Debug, Default)]
pub struct TextBuffer {
    /// The text content
    content: String,
    /// Pre-computed character array for O(1) access
    chars: Vec<char>,
    /// Pre-computed byte indices for O(1) char_to_byte conversion
    byte_indices: Vec<usize>,
    /// Cursor position in CHARACTER index
    cursor: usize,
    /// Selection anchor in CHARACTER index (where selection started)
    selection_anchor: Option<usize>,
}

impl TextBuffer {
    /// Create a new empty text buffer
    pub fn new() -> Self {
        Self {
            content: String::new(),
            chars: Vec::new(),
            byte_indices: vec![0],
            cursor: 0,
            selection_anchor: None,
        }
    }

    /// Create a text buffer with initial content
    pub fn with_content(text: impl Into<String>) -> Self {
        let content = text.into();
        let mut buffer = Self::new();
        buffer.content = content.clone();
        buffer.rebuild_cache();
        buffer.cursor = buffer.chars.len();
        buffer
    }

    /// Rebuild the character and byte index cache from content
    fn rebuild_cache(&mut self) {
        self.chars = self.content.chars().collect();
        self.byte_indices = self.content.char_indices().map(|(i, _)| i).collect();
        // Add the end byte position for convenience
        self.byte_indices.push(self.content.len());
    }

    // =========================================================================
    // Basic Accessors
    // =========================================================================

    /// Get the text content
    #[inline]
    pub fn text(&self) -> &str {
        &self.content
    }

    /// Get cursor position (character index)
    #[inline]
    pub fn cursor(&self) -> usize {
        self.cursor
    }

    /// Get character count
    #[inline]
    pub fn char_count(&self) -> usize {
        self.chars.len()
    }

    /// Check if buffer is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.content.is_empty()
    }

    /// Get byte length
    #[inline]
    pub fn len(&self) -> usize {
        self.content.len()
    }

    // =========================================================================
    // UTF-8 Index Conversion
    // =========================================================================

    /// Convert character index to byte index
    ///
    /// Returns byte position for the character at `char_idx`.
    /// If `char_idx` is beyond the text, returns the byte length.
    #[inline]
    pub fn char_to_byte(&self, char_idx: usize) -> usize {
        self.byte_indices
            .get(char_idx)
            .copied()
            .unwrap_or(self.content.len())
    }

    /// Convert byte index to character index
    ///
    /// Returns the character position that contains `byte_idx`.
    pub fn byte_to_char(&self, byte_idx: usize) -> usize {
        self.byte_indices
            .partition_point(|&i| i <= byte_idx)
            .saturating_sub(1)
    }

    /// Get substring by character range
    pub fn substring(&self, start: usize, end: usize) -> &str {
        if start >= end {
            return "";
        }
        let start_byte = self.char_to_byte(start);
        let end_byte = self.char_to_byte(end);
        &self.content[start_byte..end_byte]
    }

    /// Get character at position
    pub fn char_at(&self, pos: usize) -> Option<char> {
        self.chars.get(pos).copied()
    }

    // =========================================================================
    // Content Modification
    // =========================================================================

    /// Insert a character at cursor position
    ///
    /// Returns the new cursor position.
    pub fn insert_char(&mut self, ch: char) -> usize {
        let byte_idx = self.char_to_byte(self.cursor);
        self.content.insert(byte_idx, ch);
        self.rebuild_cache();
        self.cursor += 1;
        self.cursor
    }

    /// Insert a string at cursor position
    ///
    /// Returns the new cursor position.
    pub fn insert_str(&mut self, s: &str) -> usize {
        let byte_idx = self.char_to_byte(self.cursor);
        self.content.insert_str(byte_idx, s);
        self.rebuild_cache();
        self.cursor += s.chars().count();
        self.cursor
    }

    /// Insert a character at a specific position
    ///
    /// Returns the new cursor position (after the inserted char).
    pub fn insert_char_at(&mut self, pos: usize, ch: char) -> usize {
        let byte_idx = self.char_to_byte(pos);
        self.content.insert(byte_idx, ch);
        self.rebuild_cache();
        pos + 1
    }

    /// Insert a string at a specific position
    ///
    /// Returns the new cursor position (after the inserted string).
    pub fn insert_str_at(&mut self, pos: usize, s: &str) -> usize {
        let byte_idx = self.char_to_byte(pos);
        self.content.insert_str(byte_idx, s);
        self.rebuild_cache();
        pos + s.chars().count()
    }

    /// Delete character before cursor (backspace)
    ///
    /// Returns the deleted character, if any.
    pub fn delete_char_before(&mut self) -> Option<char> {
        if self.cursor == 0 {
            return None;
        }

        self.cursor -= 1;
        let ch = self.chars.get(self.cursor)?;
        let byte_idx = self.byte_indices[self.cursor];
        let byte_len = ch.len_utf8();
        let ch_copy = *ch; // Copy the char before mutating
        self.content.drain(byte_idx..byte_idx + byte_len);
        self.rebuild_cache();
        Some(ch_copy)
    }

    /// Delete character at cursor (delete key)
    ///
    /// Returns the deleted character, if any.
    pub fn delete_char_at(&mut self) -> Option<char> {
        if self.cursor >= self.chars.len() {
            return None;
        }

        let ch = self.chars.get(self.cursor)?;
        let byte_idx = self.byte_indices[self.cursor];
        let byte_len = ch.len_utf8();
        let ch_copy = *ch; // Copy the char before mutating
        self.content.drain(byte_idx..byte_idx + byte_len);
        self.rebuild_cache();
        Some(ch_copy)
    }

    /// Delete a range of characters
    ///
    /// Returns the deleted text.
    pub fn delete_range(&mut self, start: usize, end: usize) -> String {
        let start_byte = self.char_to_byte(start);
        let end_byte = self.char_to_byte(end);
        let deleted: String = self.content.drain(start_byte..end_byte).collect();
        self.rebuild_cache();

        // Adjust cursor if it was in or after the deleted range
        if self.cursor > end {
            self.cursor -= end - start;
        } else if self.cursor > start {
            self.cursor = start;
        }

        deleted
    }

    /// Set content (replaces all text)
    pub fn set_content(&mut self, text: impl Into<String>) {
        self.content = text.into();
        self.rebuild_cache();
        self.cursor = self.chars.len();
        self.selection_anchor = None;
    }

    /// Clear all content
    pub fn clear(&mut self) {
        self.content.clear();
        self.rebuild_cache();
        self.cursor = 0;
        self.selection_anchor = None;
    }

    // =========================================================================
    // Cursor Movement
    // =========================================================================

    /// Set cursor position (clamped to valid range)
    pub fn set_cursor(&mut self, pos: usize) {
        self.cursor = pos.min(self.char_count());
    }

    /// Move cursor left by one character
    ///
    /// Returns true if cursor moved.
    pub fn move_left(&mut self) -> bool {
        if self.cursor > 0 {
            self.cursor -= 1;
            true
        } else {
            false
        }
    }

    /// Move cursor right by one character
    ///
    /// Returns true if cursor moved.
    pub fn move_right(&mut self) -> bool {
        if self.cursor < self.char_count() {
            self.cursor += 1;
            true
        } else {
            false
        }
    }

    /// Move cursor to start
    pub fn move_to_start(&mut self) {
        self.cursor = 0;
    }

    /// Move cursor to end
    pub fn move_to_end(&mut self) {
        self.cursor = self.chars.len();
    }

    /// Move cursor left by one word
    ///
    /// A word is a sequence of non-whitespace characters.
    pub fn move_word_left(&mut self) {
        if self.cursor == 0 {
            return;
        }

        let byte_pos = self.char_to_byte(self.cursor);
        let before_cursor = &self.content[..byte_pos];

        let mut new_pos = self.cursor;

        // Skip whitespace going backwards
        for ch in before_cursor.chars().rev() {
            if ch.is_whitespace() {
                new_pos -= 1;
            } else {
                break;
            }
        }

        // Skip word characters going backwards
        let byte_pos = self.char_to_byte(new_pos);
        let before_new_pos = &self.content[..byte_pos];
        for ch in before_new_pos.chars().rev() {
            if !ch.is_whitespace() {
                new_pos -= 1;
            } else {
                break;
            }
        }

        self.cursor = new_pos;
    }

    /// Move cursor right by one word
    ///
    /// A word is a sequence of non-whitespace characters.
    pub fn move_word_right(&mut self) {
        let char_len = self.chars.len();
        if self.cursor >= char_len {
            return;
        }

        let mut advance = 0;

        // Skip current word characters
        for ch in self.chars.iter().skip(self.cursor) {
            if !ch.is_whitespace() {
                advance += 1;
            } else {
                break;
            }
        }

        // Skip whitespace
        for ch in self.chars.iter().skip(self.cursor + advance) {
            if ch.is_whitespace() {
                advance += 1;
            } else {
                break;
            }
        }

        self.cursor = (self.cursor + advance).min(char_len);
    }

    // =========================================================================
    // Selection
    // =========================================================================

    /// Start selection at current cursor position
    pub fn start_selection(&mut self) {
        if self.selection_anchor.is_none() {
            self.selection_anchor = Some(self.cursor);
        }
    }

    /// Clear selection
    pub fn clear_selection(&mut self) {
        self.selection_anchor = None;
    }

    /// Check if there's an active selection
    pub fn has_selection(&self) -> bool {
        self.selection_anchor.is_some() && self.selection_anchor != Some(self.cursor)
    }

    /// Get selection range (start, end) if there is a selection
    ///
    /// The returned range is normalized (start < end).
    pub fn selection(&self) -> Option<(usize, usize)> {
        self.selection_anchor.map(|anchor| {
            if anchor < self.cursor {
                (anchor, self.cursor)
            } else {
                (self.cursor, anchor)
            }
        })
    }

    /// Get selected text
    pub fn selected_text(&self) -> Option<&str> {
        self.selection()
            .map(|(start, end)| self.substring(start, end))
    }

    /// Select all text
    pub fn select_all(&mut self) {
        self.selection_anchor = Some(0);
        self.cursor = self.char_count();
    }

    /// Delete selected text
    ///
    /// Returns the deleted text, if any.
    pub fn delete_selection(&mut self) -> Option<String> {
        if let Some((start, end)) = self.selection() {
            let deleted = self.delete_range(start, end);
            self.cursor = start;
            self.selection_anchor = None;
            Some(deleted)
        } else {
            None
        }
    }

    // =========================================================================
    // Word Operations
    // =========================================================================

    /// Delete word before cursor
    ///
    /// Returns the deleted text.
    pub fn delete_word_before(&mut self) -> String {
        if self.cursor == 0 {
            return String::new();
        }

        let end = self.cursor;
        self.move_word_left();
        let start = self.cursor;

        self.delete_range(start, end)
    }

    /// Delete word after cursor
    ///
    /// Returns the deleted text.
    pub fn delete_word_after(&mut self) -> String {
        let start = self.cursor;
        self.move_word_right();
        let end = self.cursor;
        self.cursor = start;

        self.delete_range(start, end)
    }

    // =========================================================================
    // Character Classification (for word boundaries)
    // =========================================================================

    /// Check if position is at a word boundary
    pub fn is_word_boundary(&self, pos: usize) -> bool {
        if pos == 0 || pos >= self.chars.len() {
            return true;
        }

        let prev = self.chars.get(pos - 1);
        let curr = self.chars.get(pos);

        match (prev, curr) {
            (Some(p), Some(c)) => {
                let p_word = !(*p).is_whitespace();
                let c_word = !(*c).is_whitespace();
                p_word != c_word
            }
            _ => true,
        }
    }

    /// Find word boundaries around cursor
    ///
    /// Returns (start, end) of the word at cursor position.
    pub fn word_at_cursor(&self) -> (usize, usize) {
        if self.is_empty() {
            return (0, 0);
        }

        let char_len = self.chars.len();
        let mut start = self.cursor.min(char_len.saturating_sub(1));
        let mut end = start;

        // Expand start backwards
        while start > 0 {
            if let Some(ch) = self.chars.get(start - 1) {
                if ch.is_whitespace() {
                    break;
                }
                start -= 1;
            } else {
                break;
            }
        }

        // Expand end forwards
        while end < char_len {
            if let Some(ch) = self.chars.get(end) {
                if ch.is_whitespace() {
                    break;
                }
                end += 1;
            } else {
                break;
            }
        }

        (start, end)
    }

    /// Select the word at cursor position
    pub fn select_word(&mut self) {
        let (start, end) = self.word_at_cursor();
        self.selection_anchor = Some(start);
        self.cursor = end;
    }
}

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

    // =========================================================================
    // Basic Tests
    // =========================================================================

    #[test]
    fn test_new() {
        let buf = TextBuffer::new();
        assert!(buf.is_empty());
        assert_eq!(buf.cursor(), 0);
        assert_eq!(buf.char_count(), 0);
    }

    #[test]
    fn test_with_content() {
        let buf = TextBuffer::with_content("Hello");
        assert_eq!(buf.text(), "Hello");
        assert_eq!(buf.cursor(), 5);
        assert_eq!(buf.char_count(), 5);
    }

    // =========================================================================
    // UTF-8 Tests
    // =========================================================================

    #[test]
    fn test_utf8_emoji() {
        let buf = TextBuffer::with_content("Hi 🎉!");
        assert_eq!(buf.char_count(), 5); // Not 8 bytes
        assert_eq!(buf.char_at(3), Some('🎉'));
        assert_eq!(buf.substring(0, 2), "Hi");
        assert_eq!(buf.substring(3, 4), "🎉");
    }

    #[test]
    fn test_utf8_korean() {
        let buf = TextBuffer::with_content("안녕하세요");
        assert_eq!(buf.char_count(), 5);
        assert_eq!(buf.char_at(0), Some('안'));
        assert_eq!(buf.substring(1, 3), "녕하");
    }

    #[test]
    fn test_char_to_byte() {
        let buf = TextBuffer::with_content("A🎉B");
        assert_eq!(buf.char_to_byte(0), 0); // 'A' starts at 0
        assert_eq!(buf.char_to_byte(1), 1); // '🎉' starts at 1
        assert_eq!(buf.char_to_byte(2), 5); // 'B' starts at 5 (1 + 4 bytes for emoji)
        assert_eq!(buf.char_to_byte(3), 6); // End
        assert_eq!(buf.char_to_byte(100), 6); // Beyond end
    }

    // =========================================================================
    // Insert/Delete Tests
    // =========================================================================

    #[test]
    fn test_insert_char() {
        let mut buf = TextBuffer::new();
        buf.insert_char('H');
        buf.insert_char('i');
        assert_eq!(buf.text(), "Hi");
        assert_eq!(buf.cursor(), 2);
    }

    #[test]
    fn test_insert_str() {
        let mut buf = TextBuffer::new();
        buf.insert_str("Hello");
        assert_eq!(buf.text(), "Hello");
        assert_eq!(buf.cursor(), 5);

        buf.set_cursor(0);
        buf.insert_str("Say ");
        assert_eq!(buf.text(), "Say Hello");
    }

    #[test]
    fn test_insert_emoji() {
        let mut buf = TextBuffer::new();
        buf.insert_str("Hi ");
        buf.insert_char('🎉');
        assert_eq!(buf.text(), "Hi 🎉");
        assert_eq!(buf.cursor(), 4);
    }

    #[test]
    fn test_delete_char_before() {
        let mut buf = TextBuffer::with_content("Hello");
        let deleted = buf.delete_char_before();
        assert_eq!(deleted, Some('o'));
        assert_eq!(buf.text(), "Hell");
        assert_eq!(buf.cursor(), 4);
    }

    #[test]
    fn test_delete_char_before_emoji() {
        let mut buf = TextBuffer::with_content("Hi🎉");
        let deleted = buf.delete_char_before();
        assert_eq!(deleted, Some('🎉'));
        assert_eq!(buf.text(), "Hi");
        assert_eq!(buf.cursor(), 2);
    }

    #[test]
    fn test_delete_char_at() {
        let mut buf = TextBuffer::with_content("Hello");
        buf.set_cursor(1);
        let deleted = buf.delete_char_at();
        assert_eq!(deleted, Some('e'));
        assert_eq!(buf.text(), "Hllo");
    }

    #[test]
    fn test_delete_range() {
        let mut buf = TextBuffer::with_content("Hello World");
        let deleted = buf.delete_range(0, 6);
        assert_eq!(deleted, "Hello ");
        assert_eq!(buf.text(), "World");
    }

    // =========================================================================
    // Cursor Movement Tests
    // =========================================================================

    #[test]
    fn test_move_left_right() {
        let mut buf = TextBuffer::with_content("Hello");

        buf.set_cursor(3);
        assert!(buf.move_left());
        assert_eq!(buf.cursor(), 2);

        assert!(buf.move_right());
        assert_eq!(buf.cursor(), 3);

        buf.set_cursor(0);
        assert!(!buf.move_left()); // Can't move left from 0

        buf.set_cursor(5);
        assert!(!buf.move_right()); // Can't move right from end
    }

    #[test]
    fn test_move_word() {
        let mut buf = TextBuffer::with_content("hello world test");
        buf.set_cursor(0);

        buf.move_word_right();
        assert_eq!(buf.cursor(), 6); // After "hello "

        buf.move_word_right();
        assert_eq!(buf.cursor(), 12); // After "world "

        buf.move_word_left();
        assert_eq!(buf.cursor(), 6);

        buf.move_word_left();
        assert_eq!(buf.cursor(), 0);
    }

    // =========================================================================
    // Selection Tests
    // =========================================================================

    #[test]
    fn test_selection() {
        let mut buf = TextBuffer::with_content("Hello World");
        buf.set_cursor(0);
        buf.start_selection();
        buf.set_cursor(5);

        assert!(buf.has_selection());
        assert_eq!(buf.selection(), Some((0, 5)));
        assert_eq!(buf.selected_text(), Some("Hello"));
    }

    #[test]
    fn test_selection_reverse() {
        let mut buf = TextBuffer::with_content("Hello World");
        buf.set_cursor(5);
        buf.start_selection();
        buf.set_cursor(0);

        assert!(buf.has_selection());
        assert_eq!(buf.selection(), Some((0, 5))); // Normalized
        assert_eq!(buf.selected_text(), Some("Hello"));
    }

    #[test]
    fn test_select_all() {
        let mut buf = TextBuffer::with_content("Hello");
        buf.select_all();

        assert!(buf.has_selection());
        assert_eq!(buf.selection(), Some((0, 5)));
        assert_eq!(buf.selected_text(), Some("Hello"));
    }

    #[test]
    fn test_delete_selection() {
        let mut buf = TextBuffer::with_content("Hello World");
        buf.set_cursor(0);
        buf.start_selection();
        buf.set_cursor(6);

        let deleted = buf.delete_selection();
        assert_eq!(deleted, Some("Hello ".to_string()));
        assert_eq!(buf.text(), "World");
        assert_eq!(buf.cursor(), 0);
    }

    // =========================================================================
    // Word Operation Tests
    // =========================================================================

    #[test]
    fn test_delete_word_before() {
        let mut buf = TextBuffer::with_content("hello world");
        buf.set_cursor(11); // End

        let deleted = buf.delete_word_before();
        assert_eq!(deleted, "world");
        assert_eq!(buf.text(), "hello ");
    }

    #[test]
    fn test_word_at_cursor() {
        let buf = TextBuffer::with_content("hello world");

        let mut buf2 = buf.clone();
        buf2.set_cursor(2);
        assert_eq!(buf2.word_at_cursor(), (0, 5)); // "hello"

        let mut buf3 = buf.clone();
        buf3.set_cursor(7);
        assert_eq!(buf3.word_at_cursor(), (6, 11)); // "world"
    }

    #[test]
    fn test_select_word() {
        let mut buf = TextBuffer::with_content("hello world");
        buf.set_cursor(2);
        buf.select_word();

        assert_eq!(buf.selection(), Some((0, 5)));
        assert_eq!(buf.selected_text(), Some("hello"));
    }

    // =========================================================================
    // Edge Cases
    // =========================================================================

    #[test]
    fn test_empty_operations() {
        let mut buf = TextBuffer::new();

        assert_eq!(buf.delete_char_before(), None);
        assert_eq!(buf.delete_char_at(), None);
        assert!(!buf.move_left());
        assert!(!buf.move_right());
        assert!(!buf.has_selection());
    }

    #[test]
    fn test_set_cursor_clamped() {
        let mut buf = TextBuffer::with_content("Hello");
        buf.set_cursor(100);
        assert_eq!(buf.cursor(), 5); // Clamped to length
    }

    #[test]
    fn test_clear() {
        let mut buf = TextBuffer::with_content("Hello");
        buf.start_selection();
        buf.clear();

        assert!(buf.is_empty());
        assert_eq!(buf.cursor(), 0);
        assert!(!buf.has_selection());
    }
}