fresh-editor 0.1.74

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! Reusable multiline text editing state
//!
//! This module provides `TextEdit`, a multiline text editor with:
//! - Cursor navigation (arrows, home/end)
//! - Selection support (Shift+arrows, Ctrl+A)
//! - Insert/delete operations
//! - Word navigation (Ctrl+arrows)
//!
//! Single-line editing is a special case (one line, newlines disallowed).

use crate::primitives::word_navigation::{find_word_end_bytes, find_word_start_bytes};

/// Multiline text editing state
#[derive(Debug, Clone)]
pub struct TextEdit {
    /// Lines of text
    pub lines: Vec<String>,
    /// Current cursor row (0-indexed)
    pub cursor_row: usize,
    /// Current cursor column (0-indexed, in bytes)
    pub cursor_col: usize,
    /// Selection anchor position (row, col) - for Shift+Arrow selection
    pub selection_anchor: Option<(usize, usize)>,
    /// Whether to allow multiline (newlines)
    pub multiline: bool,
}

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

impl TextEdit {
    /// Create a new empty text edit (multiline by default)
    pub fn new() -> Self {
        Self {
            lines: vec![String::new()],
            cursor_row: 0,
            cursor_col: 0,
            selection_anchor: None,
            multiline: true,
        }
    }

    /// Create a single-line text edit
    pub fn single_line() -> Self {
        Self {
            lines: vec![String::new()],
            cursor_row: 0,
            cursor_col: 0,
            selection_anchor: None,
            multiline: false,
        }
    }

    /// Create from initial text
    pub fn with_text(text: &str) -> Self {
        let lines: Vec<String> = text.lines().map(String::from).collect();
        let lines = if lines.is_empty() {
            vec![String::new()]
        } else {
            lines
        };
        Self {
            lines,
            cursor_row: 0,
            cursor_col: 0,
            selection_anchor: None,
            multiline: true,
        }
    }

    /// Create single-line from initial text (takes first line only)
    pub fn single_line_with_text(text: &str) -> Self {
        let first_line = text.lines().next().unwrap_or("").to_string();
        Self {
            lines: vec![first_line],
            cursor_row: 0,
            cursor_col: 0,
            selection_anchor: None,
            multiline: false,
        }
    }

    /// Get the full text value
    pub fn value(&self) -> String {
        self.lines.join("\n")
    }

    /// Set the text value, resetting cursor to start
    pub fn set_value(&mut self, text: &str) {
        if self.multiline {
            self.lines = text.lines().map(String::from).collect();
            if self.lines.is_empty() {
                self.lines.push(String::new());
            }
        } else {
            self.lines = vec![text.lines().next().unwrap_or("").to_string()];
        }
        self.cursor_row = 0;
        self.cursor_col = 0;
        self.selection_anchor = None;
    }

    /// Get the current line
    pub fn current_line(&self) -> &str {
        self.lines
            .get(self.cursor_row)
            .map(|s| s.as_str())
            .unwrap_or("")
    }

    /// Get number of lines
    pub fn line_count(&self) -> usize {
        self.lines.len()
    }

    // ========================================================================
    // Cursor movement (clears selection)
    // ========================================================================

    /// Move cursor left
    pub fn move_left(&mut self) {
        self.clear_selection();
        self.move_left_internal();
    }

    fn move_left_internal(&mut self) {
        if self.cursor_col > 0 {
            // Move to previous char boundary
            let line = &self.lines[self.cursor_row];
            let mut new_col = self.cursor_col - 1;
            while new_col > 0 && !line.is_char_boundary(new_col) {
                new_col -= 1;
            }
            self.cursor_col = new_col;
        } else if self.cursor_row > 0 && self.multiline {
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
        }
    }

    /// Move cursor right
    pub fn move_right(&mut self) {
        self.clear_selection();
        self.move_right_internal();
    }

    fn move_right_internal(&mut self) {
        let line_len = self
            .lines
            .get(self.cursor_row)
            .map(|l| l.len())
            .unwrap_or(0);
        if self.cursor_col < line_len {
            // Move to next char boundary
            let line = &self.lines[self.cursor_row];
            let mut new_col = self.cursor_col + 1;
            while new_col < line.len() && !line.is_char_boundary(new_col) {
                new_col += 1;
            }
            self.cursor_col = new_col;
        } else if self.cursor_row + 1 < self.lines.len() && self.multiline {
            self.cursor_row += 1;
            self.cursor_col = 0;
        }
    }

    /// Move cursor up
    pub fn move_up(&mut self) {
        self.clear_selection();
        self.move_up_internal();
    }

    fn move_up_internal(&mut self) {
        if self.cursor_row > 0 {
            self.cursor_row -= 1;
            let line_len = self.lines[self.cursor_row].len();
            self.cursor_col = self.cursor_col.min(line_len);
        }
    }

    /// Move cursor down
    pub fn move_down(&mut self) {
        self.clear_selection();
        self.move_down_internal();
    }

    fn move_down_internal(&mut self) {
        if self.cursor_row + 1 < self.lines.len() {
            self.cursor_row += 1;
            let line_len = self.lines[self.cursor_row].len();
            self.cursor_col = self.cursor_col.min(line_len);
        }
    }

    /// Move to start of line
    pub fn move_home(&mut self) {
        self.clear_selection();
        self.cursor_col = 0;
    }

    /// Move to end of line
    pub fn move_end(&mut self) {
        self.clear_selection();
        self.cursor_col = self
            .lines
            .get(self.cursor_row)
            .map(|l| l.len())
            .unwrap_or(0);
    }

    /// Move to start of previous word
    pub fn move_word_left(&mut self) {
        self.clear_selection();
        self.move_word_left_internal();
    }

    fn move_word_left_internal(&mut self) {
        let line = &self.lines[self.cursor_row];
        if self.cursor_col > 0 {
            let new_col = find_word_start_bytes(line.as_bytes(), self.cursor_col);
            if new_col < self.cursor_col {
                self.cursor_col = new_col;
                return;
            }
        }
        // At start of line, move to end of previous line
        if self.cursor_row > 0 && self.multiline {
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
        }
    }

    /// Move to start of next word
    pub fn move_word_right(&mut self) {
        self.clear_selection();
        self.move_word_right_internal();
    }

    fn move_word_right_internal(&mut self) {
        let line = &self.lines[self.cursor_row];
        if self.cursor_col < line.len() {
            let new_col = find_word_end_bytes(line.as_bytes(), self.cursor_col);
            if new_col > self.cursor_col {
                self.cursor_col = new_col;
                return;
            }
        }
        // At end of line, move to start of next line
        if self.cursor_row + 1 < self.lines.len() && self.multiline {
            self.cursor_row += 1;
            self.cursor_col = 0;
        }
    }

    // ========================================================================
    // Selection support
    // ========================================================================

    /// Check if there's an active selection
    pub fn has_selection(&self) -> bool {
        if let Some((anchor_row, anchor_col)) = self.selection_anchor {
            anchor_row != self.cursor_row || anchor_col != self.cursor_col
        } else {
            false
        }
    }

    /// Get selection range as ((start_row, start_col), (end_row, end_col))
    /// where start is before end in document order
    pub fn selection_range(&self) -> Option<((usize, usize), (usize, usize))> {
        let (anchor_row, anchor_col) = self.selection_anchor?;
        if anchor_row == self.cursor_row && anchor_col == self.cursor_col {
            return None;
        }

        let (start, end) = if anchor_row < self.cursor_row
            || (anchor_row == self.cursor_row && anchor_col < self.cursor_col)
        {
            ((anchor_row, anchor_col), (self.cursor_row, self.cursor_col))
        } else {
            ((self.cursor_row, self.cursor_col), (anchor_row, anchor_col))
        };
        Some((start, end))
    }

    /// Get selected text
    pub fn selected_text(&self) -> Option<String> {
        let ((start_row, start_col), (end_row, end_col)) = self.selection_range()?;

        if start_row == end_row {
            let line = &self.lines[start_row];
            let end_col = end_col.min(line.len());
            let start_col = start_col.min(end_col);
            Some(line[start_col..end_col].to_string())
        } else {
            let mut result = String::new();
            // First line from start_col to end
            let first_line = &self.lines[start_row];
            result.push_str(&first_line[start_col.min(first_line.len())..]);
            result.push('\n');
            // Middle lines (full)
            for row in (start_row + 1)..end_row {
                result.push_str(&self.lines[row]);
                result.push('\n');
            }
            // Last line from start to end_col
            let last_line = &self.lines[end_row];
            result.push_str(&last_line[..end_col.min(last_line.len())]);
            Some(result)
        }
    }

    /// Delete selection and return the deleted text
    pub fn delete_selection(&mut self) -> Option<String> {
        let ((start_row, start_col), (end_row, end_col)) = self.selection_range()?;
        let deleted = self.selected_text()?;

        if start_row == end_row {
            let line = &mut self.lines[start_row];
            let end_col = end_col.min(line.len());
            let start_col = start_col.min(end_col);
            line.drain(start_col..end_col);
        } else {
            let end_col = end_col.min(self.lines[end_row].len());
            let after_end = self.lines[end_row][end_col..].to_string();
            self.lines[start_row].truncate(start_col);
            self.lines[start_row].push_str(&after_end);
            // Remove the lines in between
            for _ in (start_row + 1)..=end_row {
                self.lines.remove(start_row + 1);
            }
        }

        self.cursor_row = start_row;
        self.cursor_col = start_col;
        self.selection_anchor = None;
        Some(deleted)
    }

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

    /// Start or extend selection
    fn ensure_anchor(&mut self) {
        if self.selection_anchor.is_none() {
            self.selection_anchor = Some((self.cursor_row, self.cursor_col));
        }
    }

    /// Move cursor left with selection (Shift+Left)
    pub fn move_left_selecting(&mut self) {
        self.ensure_anchor();
        self.move_left_internal();
    }

    /// Move cursor right with selection (Shift+Right)
    pub fn move_right_selecting(&mut self) {
        self.ensure_anchor();
        self.move_right_internal();
    }

    /// Move cursor up with selection (Shift+Up)
    pub fn move_up_selecting(&mut self) {
        self.ensure_anchor();
        self.move_up_internal();
    }

    /// Move cursor down with selection (Shift+Down)
    pub fn move_down_selecting(&mut self) {
        self.ensure_anchor();
        self.move_down_internal();
    }

    /// Move to start of line with selection (Shift+Home)
    pub fn move_home_selecting(&mut self) {
        self.ensure_anchor();
        self.cursor_col = 0;
    }

    /// Move to end of line with selection (Shift+End)
    pub fn move_end_selecting(&mut self) {
        self.ensure_anchor();
        self.cursor_col = self
            .lines
            .get(self.cursor_row)
            .map(|l| l.len())
            .unwrap_or(0);
    }

    /// Move word left with selection (Ctrl+Shift+Left)
    pub fn move_word_left_selecting(&mut self) {
        self.ensure_anchor();
        self.move_word_left_internal();
    }

    /// Move word right with selection (Ctrl+Shift+Right)
    pub fn move_word_right_selecting(&mut self) {
        self.ensure_anchor();
        self.move_word_right_internal();
    }

    /// Select all text (Ctrl+A)
    pub fn select_all(&mut self) {
        self.selection_anchor = Some((0, 0));
        self.cursor_row = self.lines.len().saturating_sub(1);
        self.cursor_col = self.lines.last().map(|l| l.len()).unwrap_or(0);
    }

    // ========================================================================
    // Editing operations
    // ========================================================================

    /// Insert a character at cursor position
    pub fn insert_char(&mut self, c: char) {
        // Delete selection first if any
        if self.has_selection() {
            self.delete_selection();
        }

        if c == '\n' && self.multiline {
            // Split line at cursor
            let current_line = &self.lines[self.cursor_row];
            let col = self.cursor_col.min(current_line.len());
            let (before, after) = current_line.split_at(col);
            let before = before.to_string();
            let after = after.to_string();
            self.lines[self.cursor_row] = before;
            self.lines.insert(self.cursor_row + 1, after);
            self.cursor_row += 1;
            self.cursor_col = 0;
        } else if c != '\n' {
            if self.cursor_row < self.lines.len() {
                let line = &mut self.lines[self.cursor_row];
                let col = self.cursor_col.min(line.len());
                line.insert(col, c);
                self.cursor_col = col + c.len_utf8();
            }
        }
        // Ignore newline in single-line mode
    }

    /// Insert a string at cursor position
    pub fn insert_str(&mut self, text: &str) {
        if self.has_selection() {
            self.delete_selection();
        }
        for c in text.chars() {
            // In single-line mode, skip newlines
            if c == '\n' && !self.multiline {
                continue;
            }
            self.insert_char(c);
        }
    }

    /// Delete character before cursor (backspace)
    pub fn backspace(&mut self) {
        if self.has_selection() {
            self.delete_selection();
            return;
        }

        if self.cursor_col > 0 {
            let line = &mut self.lines[self.cursor_row];
            // Find previous char boundary
            let mut del_start = self.cursor_col - 1;
            while del_start > 0 && !line.is_char_boundary(del_start) {
                del_start -= 1;
            }
            line.drain(del_start..self.cursor_col);
            self.cursor_col = del_start;
        } else if self.cursor_row > 0 && self.multiline {
            // Join with previous line
            let current_line = self.lines.remove(self.cursor_row);
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
            self.lines[self.cursor_row].push_str(&current_line);
        }
    }

    /// Delete character at cursor (delete key)
    pub fn delete(&mut self) {
        if self.has_selection() {
            self.delete_selection();
            return;
        }

        let line_len = self
            .lines
            .get(self.cursor_row)
            .map(|l| l.len())
            .unwrap_or(0);
        if self.cursor_col < line_len {
            let line = &mut self.lines[self.cursor_row];
            // Find next char boundary
            let mut del_end = self.cursor_col + 1;
            while del_end < line.len() && !line.is_char_boundary(del_end) {
                del_end += 1;
            }
            line.drain(self.cursor_col..del_end);
        } else if self.cursor_row + 1 < self.lines.len() && self.multiline {
            // Join with next line
            let next_line = self.lines.remove(self.cursor_row + 1);
            self.lines[self.cursor_row].push_str(&next_line);
        }
    }

    /// Delete from cursor to end of word (Ctrl+Delete)
    pub fn delete_word_forward(&mut self) {
        if self.has_selection() {
            self.delete_selection();
            return;
        }

        let line = &self.lines[self.cursor_row];
        let word_end = find_word_end_bytes(line.as_bytes(), self.cursor_col);
        if word_end > self.cursor_col {
            let line = &mut self.lines[self.cursor_row];
            line.drain(self.cursor_col..word_end);
        } else if self.cursor_row + 1 < self.lines.len() && self.multiline {
            // At end of line, join with next
            let next_line = self.lines.remove(self.cursor_row + 1);
            self.lines[self.cursor_row].push_str(&next_line);
        }
    }

    /// Delete from start of word to cursor (Ctrl+Backspace)
    pub fn delete_word_backward(&mut self) {
        if self.has_selection() {
            self.delete_selection();
            return;
        }

        let line = &self.lines[self.cursor_row];
        let word_start = find_word_start_bytes(line.as_bytes(), self.cursor_col);
        if word_start < self.cursor_col {
            let line = &mut self.lines[self.cursor_row];
            line.drain(word_start..self.cursor_col);
            self.cursor_col = word_start;
        } else if self.cursor_row > 0 && self.multiline {
            // At start of line, join with previous
            let current_line = self.lines.remove(self.cursor_row);
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
            self.lines[self.cursor_row].push_str(&current_line);
        }
    }

    /// Delete from cursor to end of line (Ctrl+K)
    pub fn delete_to_end(&mut self) {
        if self.has_selection() {
            self.delete_selection();
            return;
        }

        if let Some(line) = self.lines.get_mut(self.cursor_row) {
            line.truncate(self.cursor_col);
        }
    }

    /// Clear all text
    pub fn clear(&mut self) {
        self.lines = vec![String::new()];
        self.cursor_row = 0;
        self.cursor_col = 0;
        self.selection_anchor = None;
    }
}

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

    #[test]
    fn test_single_line_basic() {
        let mut edit = TextEdit::single_line();
        edit.insert_str("hello world");
        assert_eq!(edit.value(), "hello world");
        assert_eq!(edit.cursor_col, 11);
    }

    #[test]
    fn test_single_line_ignores_newlines() {
        let mut edit = TextEdit::single_line();
        edit.insert_str("hello\nworld");
        assert_eq!(edit.value(), "helloworld");
        assert_eq!(edit.line_count(), 1);
    }

    #[test]
    fn test_multiline_basic() {
        let mut edit = TextEdit::new();
        edit.insert_str("hello\nworld");
        assert_eq!(edit.value(), "hello\nworld");
        assert_eq!(edit.line_count(), 2);
        assert_eq!(edit.cursor_row, 1);
        assert_eq!(edit.cursor_col, 5);
    }

    #[test]
    fn test_selection_single_line() {
        let mut edit = TextEdit::single_line_with_text("hello world");
        edit.cursor_col = 6; // After "hello "

        edit.move_right_selecting();
        edit.move_right_selecting();
        edit.move_right_selecting();
        edit.move_right_selecting();
        edit.move_right_selecting();

        assert!(edit.has_selection());
        assert_eq!(edit.selected_text(), Some("world".to_string()));
    }

    #[test]
    fn test_selection_multiline() {
        let mut edit = TextEdit::with_text("line1\nline2\nline3");
        edit.cursor_row = 0;
        edit.cursor_col = 3; // After "lin"

        // Select to middle of line 2
        edit.move_down_selecting();
        edit.move_right_selecting();
        edit.move_right_selecting();

        assert!(edit.has_selection());
        let selected = edit.selected_text().unwrap();
        assert_eq!(selected, "e1\nline2");
    }

    #[test]
    fn test_delete_selection() {
        let mut edit = TextEdit::with_text("hello world");
        edit.cursor_col = 0;

        // Select "hello "
        for _ in 0..6 {
            edit.move_right_selecting();
        }

        let deleted = edit.delete_selection();
        assert_eq!(deleted, Some("hello ".to_string()));
        assert_eq!(edit.value(), "world");
        assert_eq!(edit.cursor_col, 0);
    }

    #[test]
    fn test_backspace_with_selection() {
        let mut edit = TextEdit::with_text("hello world");
        edit.select_all();
        edit.backspace();
        assert_eq!(edit.value(), "");
    }

    #[test]
    fn test_insert_replaces_selection() {
        let mut edit = TextEdit::with_text("hello world");
        edit.select_all();
        edit.insert_str("goodbye");
        assert_eq!(edit.value(), "goodbye");
    }

    #[test]
    fn test_word_navigation() {
        let mut edit = TextEdit::single_line_with_text("one two three");
        edit.cursor_col = 0;

        edit.move_word_right();
        assert_eq!(edit.cursor_col, 3); // After "one"

        edit.move_word_right();
        assert_eq!(edit.cursor_col, 7); // After "two"

        edit.move_word_left();
        assert_eq!(edit.cursor_col, 4); // Start of "two"
    }
}