gpuikit 0.9.0

A UI toolkit for GPUI applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
#[allow(unused)]
use super::super::*;

#[test]
fn test_empty_buffer_has_one_line() {
    let editor = Editor::new("test", vec![]);
    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(editor.get_buffer().get_line(0), Some("".to_string()));
}

#[test]
fn test_empty_buffer_cursor_starts_at_origin() {
    let editor = Editor::new("test", vec![]);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));
}

#[test]
fn test_empty_buffer_backspace_does_nothing() {
    let mut editor = Editor::new("test", vec![]);
    editor.backspace();
    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));
}

#[test]
fn test_empty_buffer_delete_does_nothing() {
    let mut editor = Editor::new("test", vec![]);
    editor.delete();
    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));
}

#[test]
fn test_empty_buffer_movement_stays_at_origin() {
    let mut editor = Editor::new("test", vec![]);

    editor.move_up(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));

    editor.move_down(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));

    editor.move_left(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));

    editor.move_right(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));
}

#[test]
fn test_empty_buffer_insert_character() {
    let mut editor = Editor::new("test", vec![]);
    editor.insert_char('A');
    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(editor.get_buffer().get_line(0), Some("A".to_string()));
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 1));
}

#[test]
fn test_empty_buffer_select_all() {
    let mut editor = Editor::new("test", vec![]);
    editor.select_all();
    assert_eq!(editor.get_selected_text(), "");
}

#[test]
fn test_single_char_delete_at_end_does_nothing() {
    let mut editor = Editor::new("test", vec!["X".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 1));
    editor.delete();
    assert_eq!(editor.get_buffer().get_line(0), Some("X".to_string()));
}

#[test]
fn test_single_char_backspace_deletes_character() {
    let mut editor = Editor::new("test", vec!["X".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 1));
    editor.backspace();
    assert_eq!(editor.get_buffer().get_line(0), Some("".to_string()));
}

#[test]
fn test_chinese_characters() {
    let mut editor = Editor::new("test", vec![]);
    editor.insert_char('ä½ ');
    editor.insert_char('儽');
    assert_eq!(editor.get_buffer().get_line(0), Some("你儽".to_string()));
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 2));
}

#[test]
fn test_arabic_text() {
    let mut editor = Editor::new("test", vec![]);
    let arabic = "Ł…Ų±Ų­ŲØŲ§";
    for ch in arabic.chars() {
        editor.insert_char(ch);
    }
    assert_eq!(editor.get_buffer().get_line(0), Some(arabic.to_string()));
}

// TODO: This test requires Unicode normalization feature
// #[test]
// fn test_combining_diacritics() {
//     let mut editor = Editor::new("test", vec![]);
//     // e + combining acute accent
//     editor.insert_char('e');
//     editor.insert_char('\u{0301}'); // combining acute accent
//
//     // The buffer should normalize to the composed form
//     assert_eq!(
//         editor.get_buffer().get_line(0),
//         Some("Ć©".to_string()), // U+00E9 - composed form
//         "Combining characters should be normalized to their composed form"
//     );
//
//     // Cursor should be at position 1 since it's one grapheme cluster
//     assert_eq!(
//         editor.cursor_position(),
//         CursorPosition::new(0, 1),
//         "Cursor should treat normalized character as single position"
//     );
// }

#[test]
fn test_cursor_at_start_of_line() {
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 0));

    editor.move_left(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));

    editor.backspace();
    assert_eq!(editor.get_buffer().get_line(0), Some("Hello".to_string()));
}

#[test]
fn test_cursor_at_end_of_line() {
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 5));

    editor.move_right(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 5));

    editor.delete();
    assert_eq!(editor.get_buffer().get_line(0), Some("Hello".to_string()));
}

#[test]
fn test_cursor_wrapping_to_next_line() {
    let mut editor = Editor::new("test", vec!["First".to_string(), "Second".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 5)); // End of first line

    // Moving right from end of line should wrap to beginning of next line
    editor.move_right(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(1, 0));
}

#[test]
fn test_cursor_wrapping_to_previous_line() {
    let mut editor = Editor::new("test", vec!["First".to_string(), "Second".to_string()]);
    editor.set_cursor_position(CursorPosition::new(1, 0)); // Beginning of second line

    // Moving left from beginning of line should wrap to end of previous line
    editor.move_left(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 5));
}

#[test]
fn test_select_single_character() {
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 0));
    editor.move_right(true);
    assert_eq!(editor.get_selected_text(), "H");
}

#[test]
fn test_select_word() {
    let mut editor = Editor::new("test", vec!["Hello World".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 0));
    for _ in 0..5 {
        editor.move_right(true);
    }
    assert_eq!(editor.get_selected_text(), "Hello");
}

#[test]
fn test_select_across_lines() {
    let mut editor = Editor::new("test", vec!["First".to_string(), "Second".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 3)); // After "Fir"
    editor.move_down(true); // Selects to (1, 3) - includes "st\nSec"
    editor.move_right(true); // Selects to (1, 4)
    editor.move_right(true); // Selects to (1, 5)

    // Selection from (0, 3) to (1, 5) should be "st\nSecon"
    assert_eq!(editor.get_selected_text(), "st\nSecon");
}

#[test]
fn test_selection_replacement() {
    let mut editor = Editor::new("test", vec!["Hello World".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 0));
    for _ in 0..5 {
        editor.move_right(true);
    }
    editor.insert_char('G');
    assert_eq!(editor.get_buffer().get_line(0), Some("G World".to_string()));
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 1));
}

#[test]
fn test_select_emoji() {
    let mut editor = Editor::new("test", vec!["Hello šŸ˜€ World".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 6)); // After "Hello "
    editor.move_right(true); // Select the emoji

    // Should have selected just the emoji
    assert_eq!(editor.get_selected_text(), "šŸ˜€");
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 7));

    editor.move_right(true); // Select the space after emoji
    assert_eq!(editor.get_selected_text(), "šŸ˜€ ");
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 8));
}

// ==================== Goal Column Tests ====================

#[test]
fn test_goal_column_maintains_on_shorter_line() {
    let mut editor = Editor::new(
        "test",
        vec![
            "Long line here".to_string(),
            "Short".to_string(),
            "Another long line".to_string(),
        ],
    );

    editor.set_cursor_position(CursorPosition::new(0, 10));
    editor.move_down(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(1, 5)); // End of short line

    editor.move_down(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(2, 10)); // Back to column 10
}

#[test]
fn test_goal_column_resets_on_horizontal_movement() {
    let mut editor = Editor::new(
        "test",
        vec!["Long line here".to_string(), "Short".to_string()],
    );

    editor.set_cursor_position(CursorPosition::new(0, 10));
    editor.move_down(false);
    editor.move_left(false); // Horizontal movement should reset goal column
    editor.move_up(false);
    assert_eq!(editor.cursor_position().col, 4); // Should be at column 4, not 10
}

#[test]
fn test_backspace_at_line_start_merges_lines() {
    let mut editor = Editor::new("test", vec!["First".to_string(), "Second".to_string()]);
    editor.set_cursor_position(CursorPosition::new(1, 0));
    editor.backspace();
    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(
        editor.get_buffer().get_line(0),
        Some("FirstSecond".to_string())
    );
    // Cursor should be at merge point (0, 5) where the lines were joined
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 5));
}

#[test]
fn test_delete_at_line_end_merges_lines() {
    let mut editor = Editor::new("test", vec!["First".to_string(), "Second".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 5));
    editor.delete();
    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(
        editor.get_buffer().get_line(0),
        Some("FirstSecond".to_string())
    );
}

#[test]
fn test_insert_newline_splits_line() {
    let mut editor = Editor::new("test", vec!["HelloWorld".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 5));
    editor.insert_newline();
    assert_eq!(editor.get_buffer().line_count(), 2);
    assert_eq!(editor.get_buffer().get_line(0), Some("Hello".to_string()));
    assert_eq!(editor.get_buffer().get_line(1), Some("World".to_string()));
}

#[test]
fn test_very_long_line() {
    let long_line = "x".repeat(10000);
    let mut editor = Editor::new("test", vec![long_line.clone()]);

    editor.set_cursor_position(CursorPosition::new(0, 10000));
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 10000));

    editor.insert_char('!');
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 10001));
}

#[test]
fn test_tab_character_handling() {
    let mut editor = Editor::new("test", vec![]);
    editor.insert_char('\t');
    assert_eq!(editor.get_buffer().get_line(0), Some("\t".to_string()));
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 1));
}

#[test]
fn test_windows_line_endings() {
    // Test CRLF handling - buffer treats \n as line separator but preserves \r
    let editor = Editor::new("test", vec!["Line1\r\n".to_string(), "Line2".to_string()]);

    // The buffer splits on \n, so "Line1\r\n" becomes two lines: "Line1\r" and ""
    assert_eq!(editor.get_buffer().line_count(), 3);

    // First line keeps the \r
    assert_eq!(editor.get_buffer().get_line(0), Some("Line1\r".to_string()));

    // Second line is empty (the part after \n)
    assert_eq!(editor.get_buffer().get_line(1), Some("".to_string()));

    // Third line is the second input string
    assert_eq!(editor.get_buffer().get_line(2), Some("Line2".to_string()));
}

#[test]
fn test_undo_after_emoji_insertion() {
    let mut editor = Editor::new("test", vec![]);
    editor.insert_char('A');
    editor.insert_char('šŸ˜€');
    editor.insert_char('B');

    // If undo is implemented
    // editor.undo();
    // assert_eq!(editor.get_buffer().get_line(0), Some("AšŸ˜€".to_string()));
}

#[test]
fn test_zero_width_characters() {
    let mut editor = Editor::new("test", vec![]);
    editor.insert_char('a');
    editor.insert_char('\u{200B}'); // Zero-width space
    editor.insert_char('b');

    // Should have all three characters in order
    let line = editor.get_buffer().get_line(0).unwrap();
    assert_eq!(line, "a\u{200B}b");

    // Cursor should be after all 3 characters
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 3));
}

#[test]
fn test_surrogate_pairs() {
    let mut editor = Editor::new("test", vec![]);
    // Mathematical bold capital A (U+1D400)
    let surrogate = "š€";
    for ch in surrogate.chars() {
        editor.insert_char(ch);
    }
    assert_eq!(editor.get_buffer().get_line(0), Some(surrogate.to_string()));
}

#[test]
fn test_right_to_left_text() {
    let mut editor = Editor::new("test", vec![]);
    let hebrew = "שלום";
    for ch in hebrew.chars() {
        editor.insert_char(ch);
    }
    assert_eq!(editor.get_buffer().get_line(0), Some(hebrew.to_string()));
}

#[gpui::test]
async fn test_cursor_position_after_paste(cx: &mut gpui::TestAppContext) {
    use gpui::ClipboardItem;

    // Create an editor with initial content
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);

    editor.set_cursor_position(CursorPosition::new(0, 5));

    // Write to clipboard
    cx.write_to_clipboard(ClipboardItem::new_string(" World".to_string()));

    // Simulate paste by reading from clipboard and inserting
    let clipboard_content = cx.read_from_clipboard();
    if let Some(item) = clipboard_content {
        if let Some(text) = item.text() {
            for ch in text.chars() {
                editor.insert_char(ch);
            }
        }
    }

    assert_eq!(
        editor.get_buffer().get_line(0),
        Some("Hello World".to_string())
    );
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 11));
}

#[gpui::test]
async fn test_copy_and_paste_selection(cx: &mut gpui::TestAppContext) {
    use gpui::ClipboardItem;

    // Create an editor with initial content
    let mut editor = Editor::new("test", vec!["Hello World".to_string()]);

    // Select "World"
    editor.set_cursor_position(CursorPosition::new(0, 6));
    for _ in 0..5 {
        editor.move_right(true);
    }

    // Copy the selection to clipboard
    let selected_text = editor.get_selected_text();
    cx.write_to_clipboard(ClipboardItem::new_string(selected_text));

    // Clear selection and move cursor to beginning
    editor.clear_selection();
    editor.set_cursor_position(CursorPosition::new(0, 0));

    // Read from clipboard and insert
    let clipboard_content = cx.read_from_clipboard();
    if let Some(item) = clipboard_content {
        if let Some(text) = item.text() {
            for ch in text.chars() {
                editor.insert_char(ch);
            }
        }
    }

    // Should have "World" pasted at the beginning
    assert_eq!(
        editor.get_buffer().get_line(0),
        Some("WorldHello World".to_string())
    );
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 5));
}

#[gpui::test]
async fn test_cut_and_paste(cx: &mut gpui::TestAppContext) {
    use gpui::ClipboardItem;

    // Create an editor with initial content
    let mut editor = Editor::new("test", vec!["Hello World".to_string()]);

    // Select "Hello "
    editor.set_cursor_position(CursorPosition::new(0, 0));
    for _ in 0..6 {
        editor.move_right(true);
    }

    // Cut the selection (copy to clipboard and delete)
    let selected_text = editor.get_selected_text();
    cx.write_to_clipboard(ClipboardItem::new_string(selected_text));
    editor.delete_selection();

    // Should have only "World" left
    assert_eq!(editor.get_buffer().get_line(0), Some("World".to_string()));

    // Move to end and paste
    editor.set_cursor_position(CursorPosition::new(0, 5));

    // Read from clipboard and insert
    let clipboard_content = cx.read_from_clipboard();
    if let Some(item) = clipboard_content {
        if let Some(text) = item.text() {
            for ch in text.chars() {
                editor.insert_char(ch);
            }
        }
    }

    // Should have "WorldHello "
    assert_eq!(
        editor.get_buffer().get_line(0),
        Some("WorldHello ".to_string())
    );
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 11));
}

#[test]
fn test_selection_with_zero_width_joiner() {
    let mut editor = Editor::new("test", vec![]);
    // Emoji with ZWJ: man (šŸ‘Ø) + ZWJ + computer (šŸ’») = man technologist
    let emoji = "šŸ‘Øā€šŸ’»"; // Man technologist (with ZWJ)
    for ch in emoji.chars() {
        editor.insert_char(ch);
    }

    // Should have inserted all characters (man, ZWJ, computer)
    let line = editor.get_buffer().get_line(0).unwrap();
    assert_eq!(line, emoji);

    // Without grapheme clustering, cursor should be at position 3 (3 chars: šŸ‘Ø + ZWJ + šŸ’»)
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 3));

    // Select all should select the entire emoji sequence
    editor.select_all();
    let selected = editor.get_selected_text();
    assert_eq!(selected, emoji);
}

#[test]
fn test_syntax_highlighting_language_detection() {
    let _editor = Editor::new("test.rs", vec!["fn main() {}".to_string()]);
    // Test that language is detected from file extension
    // assert_eq!(editor.get_language(), Some("rust"));
}

#[test]
fn test_syntax_highlighting_state_after_edit() {
    let mut editor = Editor::new("test.rs", vec!["fn main() {".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 11));
    editor.insert_char('}');
    // Verify highlighting state is updated
}

#[test]
fn test_navigate_through_empty_lines() {
    let mut editor = Editor::new("test", vec!["".to_string(), "".to_string(), "".to_string()]);
    assert_eq!(editor.get_buffer().line_count(), 3);

    editor.move_down(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(1, 0));

    editor.move_down(false);
    assert_eq!(editor.cursor_position(), CursorPosition::new(2, 0));
}

#[test]
fn test_delete_empty_line() {
    let mut editor = Editor::new("test", vec!["".to_string(), "".to_string(), "".to_string()]);
    editor.set_cursor_position(CursorPosition::new(1, 0));
    editor.backspace();
    assert_eq!(editor.get_buffer().line_count(), 2);
}

#[test]
fn test_cursor_clamping_to_valid_position() {
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);
    editor.set_cursor_position(CursorPosition::new(100, 100));
    // Cursor should be clamped to the last valid position: end of "Hello" (row 0, col 5)
    let pos = editor.cursor_position();
    assert_eq!(pos, CursorPosition::new(0, 5));
}

// Note: This test is commented out because CursorPosition uses usize for row/col,
// which cannot represent negative values. The Rust type system prevents this issue.
// If the API changes to accept signed integers, this test should verify clamping to (0, 0).
// #[test]
// fn test_cursor_clamping_negative_values() {
//     let mut editor = Editor::new("test", vec!["Hello".to_string()]);
//     // If this were possible with signed integers:
//     // editor.set_cursor_position(CursorPosition::new(-1, -1));
//     // assert_eq!(editor.cursor_position(), CursorPosition::new(0, 0));
// }

#[test]
fn test_consecutive_emoji_deletion() {
    let mut editor = Editor::new("test", vec!["šŸ‘šŸ‘ŽšŸ‘Œ".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 3)); // After all three emoji

    // Delete the last emoji (šŸ‘Œ)
    editor.backspace();
    let line = editor.get_buffer().get_line(0).unwrap();
    assert_eq!(line, "šŸ‘šŸ‘Ž");
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 2));
}

#[test]
fn test_mixed_width_characters() {
    let mut editor = Editor::new("test", vec![]);
    // Mix of ASCII, full-width, and emoji
    editor.insert_char('A');
    editor.insert_char('ļ¼”'); // Full-width A
    editor.insert_char('šŸ˜€');
    editor.insert_char('B');

    let line = editor.get_buffer().get_line(0).unwrap();
    // Should have all characters in order
    assert_eq!(line, "Aļ¼”šŸ˜€B");
    // Cursor should be after all 4 characters
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 4));
}

#[test]
fn test_consecutive_newlines() {
    let mut editor = Editor::new("test", vec![]);
    editor.insert_newline();
    editor.insert_newline();
    editor.insert_newline();

    assert_eq!(editor.get_buffer().line_count(), 4);
    assert_eq!(editor.cursor_position(), CursorPosition::new(3, 0));
}

#[test]
fn test_rapid_insertions() {
    let mut editor = Editor::new("test", vec![]);

    // Simulate rapid typing
    for c in "The quick brown fox jumps over the lazy dog".chars() {
        editor.insert_char(c);
    }

    let line = editor.get_buffer().get_line(0).unwrap();
    assert_eq!(line, "The quick brown fox jumps over the lazy dog");
}

#[test]
fn test_alternating_insert_delete() {
    let mut editor = Editor::new("test", vec![]);

    editor.insert_char('A');
    editor.insert_char('B');
    editor.backspace();
    editor.insert_char('C');
    editor.insert_char('D');
    editor.backspace();

    assert_eq!(editor.get_buffer().get_line(0), Some("AC".to_string()));
}

#[test]
fn test_selection_past_end_of_line() {
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);
    editor.set_cursor_position(CursorPosition::new(0, 0));

    // Try to select past end of line
    for _ in 0..10 {
        editor.move_right(true);
    }

    let selected = editor.get_selected_text();
    assert_eq!(selected, "Hello");
}

#[test]
fn test_delete_across_empty_lines() {
    let mut editor = Editor::new(
        "test",
        vec![
            "First".to_string(),
            "".to_string(),
            "".to_string(),
            "Last".to_string(),
        ],
    );

    // Delete from end of first line
    editor.set_cursor_position(CursorPosition::new(0, 5));
    editor.delete();
    editor.delete();
    editor.delete();

    assert_eq!(editor.get_buffer().line_count(), 1);
    assert_eq!(
        editor.get_buffer().get_line(0),
        Some("FirstLast".to_string())
    );
}

#[test]
fn test_invisible_characters() {
    let mut editor = Editor::new("test", vec![]);

    // Insert various invisible characters
    editor.insert_char('\u{00A0}'); // Non-breaking space
    editor.insert_char('\u{2000}'); // En quad
    editor.insert_char('\u{2001}'); // Em quad
    editor.insert_char('\u{200B}'); // Zero-width space
    editor.insert_char('\u{FEFF}'); // Zero-width no-break space

    let line = editor.get_buffer().get_line(0).unwrap();
    assert_eq!(line.chars().count(), 5);
}

#[test]
fn test_grapheme_cluster_navigation() {
    let mut editor = Editor::new("test", vec![]);

    // Insert grapheme clusters
    let clusters = "Ć©"; // e + combining accent
    for ch in clusters.chars() {
        editor.insert_char(ch);
    }
    editor.insert_char(' ');

    let flag = "šŸ‡ŗšŸ‡ø"; // Regional indicator symbols
    for ch in flag.chars() {
        editor.insert_char(ch);
    }

    let line = editor.get_buffer().get_line(0).unwrap();
    assert!(!line.is_empty());
}

#[test]
fn test_selection_collapse_on_typing() {
    let mut editor = Editor::new("test", vec!["Hello World".to_string()]);

    // Create selection
    editor.set_cursor_position(CursorPosition::new(0, 0));
    for _ in 0..5 {
        editor.move_right(true);
    }
    assert!(editor.has_selection());

    // Type should replace selection
    editor.insert_char('X');
    assert!(!editor.has_selection());
    assert_eq!(editor.get_buffer().get_line(0), Some("X World".to_string()));
}

#[test]
fn test_backspace_multiple_graphemes() {
    let mut editor = Editor::new("test", vec![]);

    // Insert multiple complex characters
    editor.insert_char('A');
    editor.insert_char('šŸ‘Ø');
    editor.insert_char('\u{200D}'); // ZWJ
    editor.insert_char('šŸ’»'); // Forms "man technologist"
    editor.insert_char('B');

    // Backspace from end
    editor.backspace();
    let line = editor.get_buffer().get_line(0).unwrap();
    assert!(line.contains('A'));
}

#[test]
fn test_cursor_at_grapheme_boundaries() {
    let mut editor = Editor::new("test", vec!["AšŸ‘Øā€šŸ‘©ā€šŸ‘¦B".to_string()]);

    // Navigate through complex emoji
    editor.set_cursor_position(CursorPosition::new(0, 0));
    editor.move_right(false); // Past 'A'
    let pos1 = editor.cursor_position().col;

    editor.move_right(false); // Past emoji
    let pos2 = editor.cursor_position().col;

    // Should have moved past the complex emoji
    assert!(pos2 > pos1);
}

#[test]
fn test_line_wrapping_behavior() {
    let very_long_word = "a".repeat(200);
    let mut editor = Editor::new("test", vec![very_long_word.clone()]);

    // Navigate to middle
    editor.set_cursor_position(CursorPosition::new(0, 100));

    // Insert newline to break the line
    editor.insert_newline();

    assert_eq!(editor.get_buffer().line_count(), 2);
    assert_eq!(editor.get_buffer().get_line(0), Some("a".repeat(100)));
    assert_eq!(editor.get_buffer().get_line(1), Some("a".repeat(100)));
}

#[test]
fn test_selection_with_mixed_line_endings() {
    // Test selection across lines with different ending styles
    let mut editor = Editor::new("test", vec!["Line 1".to_string(), "Line 2".to_string()]);

    editor.set_cursor_position(CursorPosition::new(0, 4));
    editor.move_down(true);
    editor.move_right(true);
    editor.move_right(true);

    // Selection from (0, 4) to (1, 6) should be " 1\nLine 2"
    let selected = editor.get_selected_text();
    assert_eq!(selected, " 1\nLine 2");
}

#[test]
fn test_empty_selection() {
    let mut editor = Editor::new("test", vec!["Hello".to_string()]);

    // Create zero-width selection
    editor.set_cursor_position(CursorPosition::new(0, 2)); // After "He"
    editor.move_right(true); // Select "l" (position 2 to 3)
    editor.move_left(true); // Move back to position 2, collapsing selection

    // Selection should be empty after moving back to start position
    let selected = editor.get_selected_text();
    assert_eq!(selected, "");
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 2));
}

#[test]
fn test_normalization_inconsistencies() {
    let mut editor = Editor::new("test", vec![]);

    // Insert pre-composed character
    editor.insert_char('Ć©'); // U+00E9
    editor.insert_char(' ');

    // Insert decomposed equivalent
    editor.insert_char('e');
    editor.insert_char('\u{0301}'); // combining acute

    let line = editor.get_buffer().get_line(0).unwrap();
    // Without normalization, both forms are preserved exactly as entered
    assert_eq!(line, "Ć© e\u{0301}");

    // Verify they are indeed different representations
    let chars: Vec<char> = line.chars().collect();
    assert_eq!(chars.len(), 4); // Ć©(1) + space(1) + e(1) + combining(1) = 4
    assert_eq!(chars[0], 'Ć©'); // Pre-composed
    assert_eq!(chars[2], 'e'); // Base character
    assert_eq!(chars[3], '\u{0301}'); // Combining accent
}

#[test]
fn test_cursor_position_after_bulk_delete() {
    let mut editor = Editor::new("test", vec!["1234567890".to_string()]);

    // Select middle portion (positions 3-7, characters "4567")
    editor.set_cursor_position(CursorPosition::new(0, 3));
    for _ in 0..4 {
        editor.move_right(true);
    }

    // Delete selection should remove "4567"
    editor.delete();

    // Should have removed "4567", leaving "123890"
    let result = editor.get_buffer().get_line(0).unwrap();
    assert_eq!(result, "123890");

    // Cursor should be at the deletion point (position 3)
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 3));
}

// #[test]
// fn test_repeated_undo_redo() {
//     // Test that repeated undo/redo operations don't corrupt state
//     // This is a placeholder since undo might not be implemented
//     let mut editor = Editor::new("test", vec![]);

//     editor.insert_char('A');
//     editor.insert_char('B');
//     editor.insert_char('C');

//     // If undo were implemented:
//     // editor.undo();
//     // editor.undo();
//     // editor.redo();
//     // editor.undo();

//     // For now, just verify current state
//     assert_eq!(editor.get_buffer().get_line(0), Some("ABC".to_string()));
// }

#[test]
fn test_control_characters() {
    let mut editor = Editor::new("test", vec![]);

    // Insert various control characters
    editor.insert_char('\x00'); // NULL
    editor.insert_char('\x01'); // SOH
    editor.insert_char('\x1F'); // Unit separator
    editor.insert_char('\x7F'); // DEL

    let line = editor.get_buffer().get_line(0).unwrap();
    // All control characters should be preserved
    assert_eq!(line.len(), 4);
    assert_eq!(line.chars().next(), Some('\x00'));
    assert_eq!(line.chars().nth(1), Some('\x01'));
    assert_eq!(line.chars().nth(2), Some('\x1F'));
    assert_eq!(line.chars().nth(3), Some('\x7F'));

    // Cursor should be after all 4 characters
    assert_eq!(editor.cursor_position(), CursorPosition::new(0, 4));
}

/// Paint every line through `Editor::highlight_line`, in order, exactly as
/// `EditorElement::paint_line` does, and return the colour of each *painted*
/// byte.
///
/// It also asserts the invariant the paint loop depends on: the runs must sum
/// to exactly the display line, because that same string is what
/// `shape_line` is handed. `Editor::highlight_line` parses the line with the
/// newline the grammar wants and trims back to here, and this is what catches
/// it if the trim is ever dropped.
#[cfg(test)]
fn painted_colors(editor: &mut Editor, lines: &[String]) -> Vec<Vec<Option<gpui::Hsla>>> {
    lines
        .iter()
        .enumerate()
        .map(|(index, line)| {
            let runs = editor.highlight_line(line, index, "Courier".into(), 14.0);
            let painted: usize = runs.iter().map(|run| run.len).sum();
            assert_eq!(
                painted,
                line.len(),
                "line {index} ({line:?}) got runs covering {painted} bytes, but \
                 `shape_line` is given the {} bytes of the line itself",
                line.len(),
            );
            runs.iter()
                .flat_map(|run| std::iter::repeat_n(Some(run.color), run.len))
                .collect()
        })
        .collect()
}

/// The same per-byte colours from the stateless whole-document pass, split back
/// up by display line.
///
/// This is the oracle: same grammar, same theme, same `HighlightIterator`,
/// differing only in how the cross-line state is carried. It is only a valid
/// comparison because both sides now feed syntect lines that carry their `\n`.
#[cfg(test)]
fn block_colors_per_line(text: &str, language: &str) -> Vec<Vec<Option<gpui::Hsla>>> {
    let highlighter = SyntaxHighlighter::new();
    let mut colors: Vec<Option<gpui::Hsla>> = vec![None; text.len()];
    for (range, style) in highlighter.highlight_block(text, language) {
        for slot in &mut colors[range] {
            *slot = style.color;
        }
    }

    let mut per_line = Vec::new();
    let mut offset = 0;
    for line in text.split('\n') {
        per_line.push(colors[offset..offset + line.len()].to_vec());
        offset += line.len() + 1; // the separator this line was split on
    }
    per_line
}

/// The #135 regression through the path the paint loop actually uses:
/// `Editor::highlight_line`, which runs `ensure_parse_states` first. The second
/// parse used to overwrite the correct state that call had just cached, so the
/// next `ensure_parse_states` adopted the corrupt entry as its starting point.
///
/// Now that the editor feeds syntect the same newline-carrying lines
/// `highlight_block` gets, this can assert the strong form the issue asked for:
/// the painted colours must match the stateless block pass byte for byte. The
/// structural assertions are kept underneath as documentation of what that
/// comparison stands for.
#[test]
fn test_line_after_block_comment_is_highlighted_as_code() {
    let lines: Vec<String> = [
        "const before = 1;",
        "/* opening a block comment",
        "still inside the comment */",
        "function after() { return 2; }",
    ]
    .iter()
    .map(|line| line.to_string())
    .collect();

    let mut editor = Editor::new("test_block_comment", lines.clone());
    editor.set_language("JavaScript".to_string());

    let painted = painted_colors(&mut editor, &lines);
    assert_eq!(
        painted,
        block_colors_per_line(&lines.join("\n"), "JavaScript"),
        "painting line by line disagreed with the stateless block pass"
    );

    let comment_color = painted[1][0];
    let code = &painted[3];

    assert!(
        code.iter().all(|color| *color != comment_color),
        "the line after `*/` is still painted in the comment colour"
    );

    let distinct: std::collections::HashSet<String> =
        code.iter().map(|color| format!("{color:?}")).collect();
    assert!(
        distinct.len() >= 2,
        "the line after `*/` collapsed to {} colour(s); `function` and `after` \
         should not share the plain-text colour",
        distinct.len()
    );
}

/// #142: the syntax set is `load_defaults_newlines`, so a `//` comment only
/// closes when the parser is shown the newline that closes it. Stripping the
/// separator left the comment open forever and painted every following line as
/// comment.
///
/// The language matters. Rust's `//` does *not* diverge in syntect 5.3's
/// default newline grammars — which is why the existing `("Rust", "// just a
/// line comment\n…")` fixture never caught this. JavaScript and C do.
#[test]
fn test_line_comment_terminates_at_end_of_line() {
    let lines: Vec<String> = [
        "// a line comment",
        "function after() { return 2; }",
        "const tail = 3;",
    ]
    .iter()
    .map(|line| line.to_string())
    .collect();

    let mut editor = Editor::new("test_line_comment", lines.clone());
    editor.set_language("JavaScript".to_string());

    let painted = painted_colors(&mut editor, &lines);
    assert_eq!(
        painted,
        block_colors_per_line(&lines.join("\n"), "JavaScript"),
        "the `//` comment leaked past its newline"
    );

    let comment_color = painted[0][0];
    assert!(
        painted[1].iter().all(|color| *color != comment_color),
        "the line after a `//` comment is still painted in the comment colour"
    );
}

/// The other shape of #142: a string the grammar closes at end of line. Without
/// the newline the Python grammar never terminates it, so the following line is
/// painted as string.
#[test]
fn test_unterminated_python_string_ends_at_its_newline() {
    let lines: Vec<String> = ["x = 'unterminated", "y = 2", "z = 3"]
        .iter()
        .map(|line| line.to_string())
        .collect();

    let mut editor = Editor::new("test_unterminated_string", lines.clone());
    editor.set_language("Python".to_string());

    let painted = painted_colors(&mut editor, &lines);
    assert_eq!(
        painted,
        block_colors_per_line(&lines.join("\n"), "Python"),
        "the unterminated string ran on past its newline"
    );

    let string_color = painted[0][4]; // the opening quote
    assert!(
        painted[1].iter().all(|color| *color != string_color),
        "the line after an unterminated string is still painted as string"
    );
}

/// The buffer decides which lines are newline-terminated, and the last one is
/// not — matching what `LinesWithEndings` gives `highlight_block` for the same
/// text. This guards the run-length invariant the fix could have broken: an
/// empty line must still come back with exactly one zero-length run, not an
/// empty `Vec`.
#[test]
fn test_last_line_is_not_given_a_separator_it_does_not_have() {
    let lines: Vec<String> = ["fn main() {}", "", "// trailing"]
        .iter()
        .map(|line| line.to_string())
        .collect();

    let mut editor = Editor::new("test_last_line", lines.clone());
    editor.set_language("Rust".to_string());

    // `painted_colors` asserts runs sum to the line on every line, including
    // the unterminated last one.
    let painted = painted_colors(&mut editor, &lines);
    assert_eq!(
        painted,
        block_colors_per_line(&lines.join("\n"), "Rust"),
        "the last line was parsed as if it had a separator"
    );

    let empty_line_runs = editor.highlight_line("", 1, "Courier".into(), 14.0);
    assert_eq!(
        empty_line_runs.len(),
        1,
        "an empty line must keep exactly one zero-length run"
    );
    assert_eq!(empty_line_runs[0].len, 0);
}