iced-code-editor 0.7.0

A custom code editor widget for the Iced GUI framework with syntax highlighting, line numbers, and scrolling support.
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
//! Text input, tab, enter, and focus-navigation message handlers.

use iced::Task;

use super::{EditType, adjust_other_cursors};
use crate::canvas_editor::editing::command::{
    Command, DeleteCharCommand, InsertCharCommand, InsertNewlineCommand,
};
use crate::canvas_editor::{CodeEditor, IndentStyle, Message};

/// Returns the closing character auto-inserted for an opening bracket or
/// quote, or `None` if `ch` doesn't start an auto-closeable pair.
///
/// # Examples
///
/// ```text
/// assert_eq!(matching_close('('), Some(')'));
/// assert_eq!(matching_close('"'), Some('"'));
/// assert_eq!(matching_close('x'), None);
/// ```
fn matching_close(ch: char) -> Option<char> {
    match ch {
        '(' => Some(')'),
        '[' => Some(']'),
        '{' => Some('}'),
        '"' => Some('"'),
        '\'' => Some('\''),
        _ => None,
    }
}

/// Returns `true` if `ch` is one of the closing brackets/quotes managed by
/// auto-close (`)`, `]`, `}`, `"`, `'`).
fn is_closing_char(ch: char) -> bool {
    matches!(ch, ')' | ']' | '}' | '"' | '\'')
}

impl CodeEditor {
    /// Handles character input message operations.
    ///
    /// Inserts a character at the current cursor position and adds it to the
    /// undo history. Characters are grouped together for smart undo.
    /// Only processes input when the editor has active focus and is not locked.
    ///
    /// When [`auto_close_brackets`](CodeEditor::auto_close_brackets) is enabled:
    /// - Typing an opening bracket/quote while a selection is active wraps the
    ///   selection in the pair (surround selection) instead of replacing it.
    /// - Typing an opening bracket/quote with no selection auto-inserts the
    ///   matching closing character, leaving the cursor between the two.
    /// - Typing a closing character that already sits at the cursor "types
    ///   through" it (the cursor just steps over it) instead of duplicating it.
    ///
    /// # Arguments
    ///
    /// * `ch` - The character to insert
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible (including
    /// horizontal scroll when wrap is disabled)
    pub(crate) fn handle_character_input_msg(
        &mut self,
        ch: char,
    ) -> Task<Message> {
        // Guard clause: only process character input if editor has focus and is not locked
        if !self.has_focus() {
            return Task::none();
        }

        // Start grouping if not already grouping (for smart undo)
        self.ensure_grouping_started();

        let has_any_selection =
            self.cursors.iter().any(|cursor| cursor.has_selection());
        let surround_close = if self.auto_close_brackets && has_any_selection {
            matching_close(ch)
        } else {
            None
        };

        if let Some(close) = surround_close {
            self.surround_selections_with_pair(ch, close);
        } else {
            // Typing replaces active selections, matching paste and IME commit
            // behavior. Keep the deletion and insertion in the same history group
            // so a single undo restores the replaced text.
            if has_any_selection {
                self.delete_selection();
            } else {
                // A plain click leaves a zero-length anchor in place (see
                // `handle_enter`); clear it so it isn't mistaken for a real
                // selection by a later edit.
                self.clear_selection();
            }

            // Multi-cursor: build a sorted index list (descending document order)
            // so that edits at higher positions don't invalidate lower positions.
            let order = self.cursors.descending_order();

            for &idx in &order {
                // Any active selection was deleted above, which also moves the
                // cursor to the original selection start and clears its anchor.
                // The current cursor position is therefore the insertion point.
                let pos = self.cursors.as_slice()[idx].position;

                if self.auto_close_brackets
                    && is_closing_char(ch)
                    && self.char_at(pos) == Some(ch)
                {
                    // An auto-inserted closer already sits here: step over it
                    // instead of inserting a duplicate.
                    self.cursors.as_mut_slice()[idx].position =
                        (pos.0, pos.1 + 1);
                    continue;
                }

                if self.auto_close_brackets
                    && let Some(close) = matching_close(ch)
                    && self.should_auto_close(pos)
                {
                    self.insert_pair_at_cursor(idx, pos, ch, close);
                    continue;
                }

                let mut cmd = InsertCharCommand::new(pos.0, pos.1, ch, pos);
                let mut cursor_pos = pos;
                cmd.execute(&mut self.buffer, &mut cursor_pos);
                self.cursors.as_mut_slice()[idx].position = cursor_pos;
                adjust_other_cursors(
                    self.cursors.as_mut_slice(),
                    idx,
                    pos.0,
                    pos.1,
                    EditType::InsertChar,
                );
                self.history.push(Box::new(cmd));
            }
        }

        self.finish_edit_operation();

        // Auto-trigger LSP completion for identifier characters and trigger characters
        if ch.is_alphanumeric() || ch == '_' || ch == '.' {
            self.lsp_flush_pending_changes();
            self.lsp_request_completion();
        }

        self.scroll_to_cursor()
    }

    /// Returns the character at `(line, col)`, or `None` if `col` is at or
    /// past the end of the line.
    fn char_at(&self, pos: (usize, usize)) -> Option<char> {
        self.buffer.line(pos.0).chars().nth(pos.1)
    }

    /// Returns `true` when auto-closing a pair at `pos` would not wrap
    /// existing text.
    ///
    /// Auto-close only triggers when the cursor is at the end of the line, or
    /// is followed by whitespace or another closing bracket/quote. This
    /// avoids inserting a spurious pair in the middle of an identifier (e.g.
    /// typing `'` inside `sn|ake`).
    fn should_auto_close(&self, pos: (usize, usize)) -> bool {
        match self.char_at(pos) {
            None => true,
            Some(next) => next.is_whitespace() || is_closing_char(next),
        }
    }

    /// Inserts `open` immediately followed by `close` at `pos` for cursor
    /// `idx`, leaving that cursor positioned between the two characters.
    ///
    /// Pushed as two grouped [`InsertCharCommand`]s (grouped with the current
    /// typing group) so each has independent, correct undo semantics — a
    /// single command spanning both chars with the cursor resting *between*
    /// them (rather than after, as `InsertTextCommand` assumes) would make
    /// `InsertTextCommand::undo`'s backward walk from the resting cursor
    /// delete the wrong character. Adjusts every other cursor for the
    /// resulting 2-character insert.
    fn insert_pair_at_cursor(
        &mut self,
        idx: usize,
        pos: (usize, usize),
        open: char,
        close: char,
    ) {
        let mut open_cmd = InsertCharCommand::new(pos.0, pos.1, open, pos);
        let mut cursor_pos = pos;
        open_cmd.execute(&mut self.buffer, &mut cursor_pos);
        self.history.push(Box::new(open_cmd));

        let mut close_cmd =
            InsertCharCommand::new(pos.0, pos.1 + 1, close, cursor_pos);
        close_cmd.execute(&mut self.buffer, &mut cursor_pos);
        self.history.push(Box::new(close_cmd));

        // Rest the cursor between the two inserted characters rather than
        // after the close char (where `close_cmd`'s own `cursor_after` puts it).
        self.cursors.as_mut_slice()[idx].position = (pos.0, pos.1 + 1);

        // Two characters were inserted at the same column: apply the
        // single-char adjustment twice so other cursors shift by 2.
        adjust_other_cursors(
            self.cursors.as_mut_slice(),
            idx,
            pos.0,
            pos.1,
            EditType::InsertChar,
        );
        adjust_other_cursors(
            self.cursors.as_mut_slice(),
            idx,
            pos.0,
            pos.1,
            EditType::InsertChar,
        );
    }

    /// Wraps every selected cursor's text in `open`/`close`, and inserts the
    /// pair at any cursor with no active selection.
    ///
    /// Cursors are processed in descending document order (by the start of
    /// their selection, or their position when unselected) so that edits at
    /// higher positions don't invalidate earlier ones. Wrapped selections
    /// keep their original direction (anchor/position) so the originally
    /// selected text stays selected between the newly inserted pair.
    fn surround_selections_with_pair(&mut self, open: char, close: char) {
        let order = self.cursors.descending_order_by_key(
            |_| true,
            |c| c.selection_range().map_or(c.position, |(s, _)| s),
        );

        for idx in order {
            let cursor = self.cursors.as_slice()[idx].clone();
            let Some((start, end)) = cursor.selection_range() else {
                self.insert_pair_at_cursor(idx, cursor.position, open, close);
                continue;
            };
            let anchor_is_start = cursor.anchor == Some(start);

            let mut open_cmd =
                InsertCharCommand::new(start.0, start.1, open, cursor.position);
            let mut cursor_pos = cursor.position;
            open_cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.history.push(Box::new(open_cmd));
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                start.0,
                start.1,
                EditType::InsertChar,
            );

            // The open char shifted everything after it on the same line by
            // one column, including `end` if the selection is single-line.
            let close_col = if end.0 == start.0 { end.1 + 1 } else { end.1 };
            let mut close_cmd =
                InsertCharCommand::new(end.0, close_col, close, cursor_pos);
            close_cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.history.push(Box::new(close_cmd));
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                end.0,
                close_col,
                EditType::InsertChar,
            );

            let new_start = (start.0, start.1 + 1);
            let new_end = (end.0, close_col);
            let cursor = &mut self.cursors.as_mut_slice()[idx];
            if anchor_is_start {
                cursor.anchor = Some(new_start);
                cursor.position = new_end;
            } else {
                cursor.anchor = Some(new_end);
                cursor.position = new_start;
            }
        }
    }

    /// Handles Tab key press (inserts 4 spaces).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible (including
    /// horizontal scroll when wrap is disabled)
    pub(crate) fn handle_tab(&mut self) -> Task<Message> {
        self.ensure_grouping_started();

        // A plain click leaves a zero-length anchor in place (see
        // `handle_enter`); clear it so it isn't mistaken for a real selection
        // by a later edit. Tab only reaches here when there is no real
        // selection to indent (see `IndentLines`).
        self.clear_selection();

        // Multi-cursor: process in descending document order
        let order = self.cursors.descending_order();

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;
            match self.indent_style {
                IndentStyle::Spaces(n) => {
                    let mut cursor_pos = pos;
                    for _i in 0..n as usize {
                        let current_col = cursor_pos.1;
                        let mut cmd = InsertCharCommand::new(
                            pos.0,
                            current_col,
                            ' ',
                            cursor_pos,
                        );
                        cmd.execute(&mut self.buffer, &mut cursor_pos);
                        adjust_other_cursors(
                            self.cursors.as_mut_slice(),
                            idx,
                            pos.0,
                            current_col,
                            EditType::InsertChar,
                        );
                        self.history.push(Box::new(cmd));
                    }
                    self.cursors.as_mut_slice()[idx].position = cursor_pos;
                }
                IndentStyle::Tab => {
                    let mut cmd =
                        InsertCharCommand::new(pos.0, pos.1, '\t', pos);
                    let mut cursor_pos = pos;
                    cmd.execute(&mut self.buffer, &mut cursor_pos);
                    adjust_other_cursors(
                        self.cursors.as_mut_slice(),
                        idx,
                        pos.0,
                        pos.1,
                        EditType::InsertChar,
                    );
                    self.cursors.as_mut_slice()[idx].position = cursor_pos;
                    self.history.push(Box::new(cmd));
                }
            }
        }

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Handles Shift+Tab (removes one indentation unit).
    ///
    /// Removes up to one indentation level immediately before each cursor.
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible.
    pub(crate) fn handle_backtab(&mut self) -> Task<Message> {
        self.ensure_grouping_started();

        // A plain click leaves a zero-length anchor behind.
        self.clear_selection();

        // Process cursors from bottom/right to top/left so edits do not
        // invalidate positions of cursors that still need processing.
        let order = self.cursors.descending_order();

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;

            if pos.1 == 0 {
                continue;
            }

            let remove_count = match self.indent_style {
                IndentStyle::Spaces(n) => {
                    let line = self.buffer.line(pos.0);
                    let bytes = line.as_bytes();

                    // Remove up to `n` spaces immediately before the cursor.
                    let mut count = 0;
                    while count < n as usize
                        && pos.1 > count
                        && bytes
                            .get(pos.1 - count - 1)
                            .is_some_and(|&b| b == b' ')
                    {
                        count += 1;
                    }
                    count
                }
                IndentStyle::Tab => {
                    let line = self.buffer.line(pos.0);

                    if line.chars().nth(pos.1 - 1) == Some('\t') {
                        1
                    } else {
                        0
                    }
                }
            };

            let mut cursor_pos = pos;

            for _ in 0..remove_count {
                let delete_pos = cursor_pos;

                let mut cmd = DeleteCharCommand::new(
                    &self.buffer,
                    delete_pos.0,
                    delete_pos.1,
                    delete_pos,
                );

                cmd.execute(&mut self.buffer, &mut cursor_pos);

                adjust_other_cursors(
                    self.cursors.as_mut_slice(),
                    idx,
                    delete_pos.0,
                    delete_pos.1,
                    EditType::DeleteCharBack,
                );

                self.history.push(Box::new(cmd));
            }

            self.cursors.as_mut_slice()[idx].position = cursor_pos;
        }

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }

    /// Handles Enter key press (inserts newline).
    ///
    /// # Returns
    ///
    /// A `Task<Message>` that scrolls to keep the cursor visible
    pub(crate) fn handle_enter(&mut self) -> Task<Message> {
        // Standard editing treats Enter as a boundary. In Vim Insert mode the
        // newline belongs to the same insertion session and is closed by Esc.
        let keep_vim_group = self.keep_vim_insert_group();
        if !keep_vim_group {
            self.end_grouping_if_active();
        }

        // A mouse click leaves a zero-length anchor in place so a following
        // drag can extend the selection. Enter must clear that anchor before
        // moving the caret to the new line; otherwise the inserted newline
        // becomes selected and the next typed character deletes it.
        //
        // For a real selection, Enter replaces the selected text with a
        // newline. Group both commands so one undo restores the selection.
        let replaces_selection =
            self.cursors.iter().any(|cursor| cursor.has_selection());
        if replaces_selection {
            self.ensure_grouping_started();
            self.delete_selection();
        } else {
            self.clear_selection();
        }

        // Multi-cursor: process in descending document order
        let order = self.cursors.descending_order();

        for &idx in &order {
            let pos = self.cursors.as_slice()[idx].position;

            // Copy leading whitespace of the current line to the new line (if enabled)
            let indent: String = if self.auto_indent_enabled {
                self.buffer
                    .line(pos.0)
                    .chars()
                    .take_while(|c| c.is_whitespace())
                    .collect()
            } else {
                String::new()
            };
            let indent_len = indent.chars().count();

            let mut cmd =
                InsertNewlineCommand::with_indent(pos.0, pos.1, pos, indent);
            let mut cursor_pos = pos;
            cmd.execute(&mut self.buffer, &mut cursor_pos);
            self.cursors.as_mut_slice()[idx].position = cursor_pos;
            adjust_other_cursors(
                self.cursors.as_mut_slice(),
                idx,
                pos.0,
                pos.1,
                EditType::InsertNewline { indent_len },
            );
            self.history.push(Box::new(cmd));
        }

        if replaces_selection && !keep_vim_group {
            self.end_grouping_if_active();
        }

        self.finish_edit_operation();
        self.scroll_to_cursor()
    }
}

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

    #[test]
    fn test_typing_with_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        // Ensure editor has focus for character input
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('X'));
        assert_eq!(editor.buffer.line(0), "X world");
        assert_eq!(editor.cursors.primary_position(), (0, 1));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_typing_digit_with_reversed_selection_replaces_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().anchor = Some((0, 5));
        editor.cursors.primary_mut().position = (0, 0);

        let _ = editor.update(&Message::CharacterInput('7'));
        assert_eq!(editor.buffer.line(0), "7 world");
        assert_eq!(editor.cursors.primary_position(), (0, 1));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_typing_with_selection_undoes_as_single_edit() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('X'));
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello world");
    }

    #[test]
    fn test_matching_close_pairs() {
        assert_eq!(matching_close('('), Some(')'));
        assert_eq!(matching_close('['), Some(']'));
        assert_eq!(matching_close('{'), Some('}'));
        assert_eq!(matching_close('"'), Some('"'));
        assert_eq!(matching_close('\''), Some('\''));
        assert_eq!(matching_close('a'), None);
    }

    #[test]
    fn test_is_closing_char() {
        for ch in [')', ']', '}', '"', '\''] {
            assert!(is_closing_char(ch), "{ch} should be a closing char");
        }
        assert!(!is_closing_char('('));
        assert!(!is_closing_char('a'));
    }

    #[test]
    fn test_auto_close_inserts_pair_with_cursor_between() {
        for &(open, close) in
            &[('(', ')'), ('[', ']'), ('{', '}'), ('"', '"'), ('\'', '\'')]
        {
            let mut editor = CodeEditor::new("", "py");
            editor.focus_for_test();

            let _ = editor.update(&Message::CharacterInput(open));
            assert_eq!(
                editor.buffer.line(0),
                format!("{open}{close}"),
                "pair {open}{close}"
            );
            assert_eq!(
                editor.cursors.primary_position(),
                (0, 1),
                "pair {open}{close}"
            );
            assert!(!editor.cursors.primary().has_selection());
        }
    }

    #[test]
    fn test_auto_close_types_through_existing_closer() {
        let mut editor = CodeEditor::new("()", "py");
        editor.focus_for_test();
        editor.cursors.primary_mut().position = (0, 1);

        let _ = editor.update(&Message::CharacterInput(')'));
        assert_eq!(editor.buffer.line(0), "()");
        assert_eq!(editor.cursors.primary_position(), (0, 2));
    }

    #[test]
    fn test_auto_close_suppressed_before_word_char() {
        let mut editor = CodeEditor::new("snake", "py");
        editor.focus_for_test();
        editor.cursors.primary_mut().position = (0, 2); // sn|ake

        let _ = editor.update(&Message::CharacterInput('\''));
        assert_eq!(editor.buffer.line(0), "sn'ake");
        assert_eq!(editor.cursors.primary_position(), (0, 3));
    }

    #[test]
    fn test_auto_close_undo_removes_pair_in_one_step() {
        let mut editor = CodeEditor::new("", "py");
        editor.focus_for_test();

        let _ = editor.update(&Message::CharacterInput('('));
        assert_eq!(editor.buffer.line(0), "()");
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "");
    }

    #[test]
    fn test_auto_close_multi_cursor_shifts_other_cursors() {
        let mut editor = CodeEditor::new("a b", "py");
        editor.focus_for_test();
        editor.cursors.set_single((0, 1));
        editor.cursors.add_cursor((0, 3));

        let _ = editor.update(&Message::CharacterInput('('));
        assert_eq!(editor.buffer.line(0), "a() b()");
        assert_eq!(editor.cursors.as_slice()[0].position, (0, 2));
        assert_eq!(editor.cursors.as_slice()[1].position, (0, 6));
    }

    #[test]
    fn test_auto_close_brackets_disabled_inserts_plain_char() {
        let mut editor = CodeEditor::new("", "py");
        editor.focus_for_test();
        editor.set_auto_close_brackets(false);

        let _ = editor.update(&Message::CharacterInput('('));
        assert_eq!(editor.buffer.line(0), "(");
        assert_eq!(editor.cursors.primary_position(), (0, 1));
    }

    #[test]
    fn test_surround_selection_wraps_forward_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.focus_for_test();
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('"'));
        assert_eq!(editor.buffer.line(0), "\"hello\" world");
        assert_eq!(editor.cursors.primary().anchor, Some((0, 1)));
        assert_eq!(editor.cursors.primary_position(), (0, 6));
        assert!(editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_surround_selection_wraps_reversed_selection() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.focus_for_test();
        editor.cursors.primary_mut().anchor = Some((0, 5));
        editor.cursors.primary_mut().position = (0, 0);

        let _ = editor.update(&Message::CharacterInput('('));
        assert_eq!(editor.buffer.line(0), "(hello) world");
        assert_eq!(editor.cursors.primary().anchor, Some((0, 6)));
        assert_eq!(editor.cursors.primary_position(), (0, 1));
    }

    #[test]
    fn test_surround_selection_wraps_multiline_selection() {
        let mut editor = CodeEditor::new("foo\nbar", "py");
        editor.focus_for_test();
        editor.cursors.primary_mut().anchor = Some((0, 1));
        editor.cursors.primary_mut().position = (1, 2);

        let _ = editor.update(&Message::CharacterInput('['));
        assert_eq!(editor.buffer.line(0), "f[oo");
        assert_eq!(editor.buffer.line(1), "ba]r");
        assert_eq!(editor.cursors.primary().anchor, Some((0, 2)));
        assert_eq!(editor.cursors.primary_position(), (1, 2));
    }

    #[test]
    fn test_surround_selection_undoes_as_single_group() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.focus_for_test();
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('"'));
        assert_eq!(editor.buffer.line(0), "\"hello\" world");
        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line(0), "hello world");
    }

    #[test]
    fn test_surround_selection_disabled_replaces_selection_instead() {
        let mut editor = CodeEditor::new("hello world", "py");
        editor.focus_for_test();
        editor.set_auto_close_brackets(false);
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::CharacterInput('"'));
        assert_eq!(editor.buffer.line(0), "\" world");
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_enter_no_indent() {
        let mut editor = CodeEditor::new("hello", "rs");
        editor.cursors.primary_mut().position = (0, 5);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.buffer.line(1), "");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
    }

    #[test]
    fn test_typing_after_enter_does_not_delete_newline_from_click_anchor() {
        let mut editor = CodeEditor::new("hello", "rs");
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;

        // A regular click starts drag tracking with a zero-length anchor.
        editor.cursors.primary_mut().position = (0, 5);
        editor.cursors.primary_mut().anchor = Some((0, 5));

        let _ = editor.update(&Message::Enter);
        let _ = editor.update(&Message::CharacterInput('X'));

        assert_eq!(editor.buffer.line_count(), 2);
        assert_eq!(editor.buffer.line(0), "hello");
        assert_eq!(editor.buffer.line(1), "X");
        assert_eq!(editor.cursors.primary_position(), (1, 1));
        assert!(!editor.cursors.primary().has_selection());
    }

    #[test]
    fn test_enter_replaces_selection_and_undo_restores_text() {
        let mut editor = CodeEditor::new("hello world", "rs");
        editor.set_auto_indent_enabled(false);
        editor.cursors.primary_mut().anchor = Some((0, 0));
        editor.cursors.primary_mut().position = (0, 5);

        let _ = editor.update(&Message::Enter);

        assert_eq!(editor.buffer.line(0), "");
        assert_eq!(editor.buffer.line(1), " world");
        assert_eq!(editor.cursors.primary_position(), (1, 0));
        assert!(!editor.cursors.primary().has_selection());

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.content(), "hello world");
    }

    #[test]
    fn test_enter_auto_indent_spaces() {
        let mut editor = CodeEditor::new("    hello", "rs");
        editor.cursors.primary_mut().position = (0, 9);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "    hello");
        assert_eq!(editor.buffer.line(1), "    ");
        assert_eq!(editor.cursors.primary_position(), (1, 4));
    }

    #[test]
    fn test_enter_auto_indent_tab() {
        let mut editor = CodeEditor::new("\thello", "rs");
        editor.cursors.primary_mut().position = (0, 6);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line(0), "\thello");
        assert_eq!(editor.buffer.line(1), "\t");
        assert_eq!(editor.cursors.primary_position(), (1, 1));
    }

    #[test]
    fn test_enter_auto_indent_undo() {
        let mut editor = CodeEditor::new("    hello", "rs");
        editor.cursors.primary_mut().position = (0, 9);
        let _ = editor.update(&Message::Enter);
        assert_eq!(editor.buffer.line_count(), 2);

        let _ = editor.update(&Message::Undo);
        assert_eq!(editor.buffer.line_count(), 1);
        assert_eq!(editor.buffer.line(0), "    hello");
        assert_eq!(editor.cursors.primary_position(), (0, 9));
    }

    // =========================================================================
    // Multi-cursor tests
    // =========================================================================

    #[test]
    fn test_focus_navigation_shift_tab_keeps_focus_when_search_closed() {
        let mut editor = CodeEditor::new("hello", "txt");
        editor.focus_for_test();
        assert!(editor.has_canvas_focus);

        let _ = editor.update(&Message::Backtab);
        assert!(editor.has_canvas_focus);
    }

    #[test]
    fn test_focus_navigation_shift_tab_keeps_focus_when_search_open() {
        let mut editor = CodeEditor::new("hello", "txt");
        editor.focus_for_test();
        editor.search_state.open_search();

        let _ = editor.update(&Message::Backtab);
        assert!(editor.has_canvas_focus);
    }
}