fresh-editor 0.3.0

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
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
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};
use tempfile::TempDir;

/// Test that cursor position stays in sync when editing lines with non-ASCII characters
/// This reproduces the bug where visual cursor position drifts from actual position
/// when a line contains Unicode box-drawing characters or other multi-byte UTF-8 characters
#[test]
fn test_cursor_sync_with_non_ascii_box_drawing_chars() {
    let mut harness = EditorTestHarness::new(120, 30).unwrap();

    // Type a line with box-drawing characters like in the bug report
    // Example: │  ┌──────────────┐  ┌──────────────┐  ┌─────────────┐  │
    let text_with_boxes = "   17 │ │  ┌──────────────┐  ┌──────────────┐  ┌─────────────┐  │";
    harness.type_text(text_with_boxes).unwrap();
    harness.render().unwrap();

    // Verify buffer content is correct
    harness.assert_buffer_content(text_with_boxes);

    // Get the buffer position (should be at end)
    let buffer_pos = harness.cursor_position();
    let expected_buffer_pos = text_with_boxes.len();
    assert_eq!(
        buffer_pos, expected_buffer_pos,
        "Cursor should be at end of text (byte position {}), but is at {}",
        expected_buffer_pos, buffer_pos
    );

    // Move cursor to the beginning of the line.
    // Press Home twice: first goes to first non-whitespace (smart home),
    // second goes to column 0.
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();

    // Cursor should now be at position 0
    let buffer_pos_after_home = harness.cursor_position();
    assert_eq!(
        buffer_pos_after_home, 0,
        "Cursor should be at position 0 after Home"
    );

    // Now move cursor right character by character and verify screen position matches
    // The key insight: when moving through multi-byte UTF-8 characters,
    // the buffer position advances by the number of bytes in the character,
    // but the screen column should advance by 1

    // First, let's move right 10 times (through "   17 │ │ ")
    for i in 1..=10 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();

        let buffer_pos = harness.cursor_position();
        let (screen_x, _screen_y) = harness.screen_cursor_position();

        // The screen cursor position depends on gutter width
        // For this test, we're mainly checking that the screen cursor advances properly
        // The gutter width varies based on line numbers, so we'll focus on relative movement

        println!(
            "After {} right arrows: buffer_pos={}, screen_x={}",
            i, buffer_pos, screen_x
        );
    }

    // Now test: type a character and verify it appears at the visual cursor position
    // Move to somewhere in the middle of the line (Home twice to ensure column 0)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();

    // Move right 20 characters
    for _ in 0..20 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }

    let buffer_pos_before_insert = harness.cursor_position();
    let (screen_x_before, screen_y_before) = harness.screen_cursor_position();

    println!(
        "Before insert: buffer_pos={}, screen=({}, {})",
        buffer_pos_before_insert, screen_x_before, screen_y_before
    );

    // Insert a marker character 'X' at this position
    harness.type_text("X").unwrap();

    // Verify that 'X' appears at the expected position in the buffer
    let buffer_content_after = harness.get_buffer_content().unwrap();
    println!("Buffer after insert: {:?}", buffer_content_after);

    // The 'X' should be inserted at buffer_pos_before_insert
    // and should appear visually at screen_x_before

    // Get the screen position where 'X' appears
    harness.render().unwrap();

    // This is where the bug manifests: if cursor tracking is broken,
    // the 'X' will not appear at screen_x_before
}

/// Test cursor movement with simple multi-byte UTF-8 characters (emojis)
#[test]
fn test_cursor_sync_with_emoji() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type a line with emojis
    let text = "Hello 😀 World 🌍";
    harness.type_text(text).unwrap();

    // Move to beginning
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();

    // The text has these characters:
    // H e l l o   😀   W o r l d   🌍
    // 0 1 2 3 4 5 [6-9] 10 11 12 13 14 15 [16-19]
    // Note: 😀 is 4 bytes (U+1F600), 🌍 is 4 bytes (U+1F30D)

    // Move right 7 times should position us after the emoji
    for _ in 0..7 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }

    let buffer_pos = harness.cursor_position();
    // "Hello " = 6 bytes, "😀" = 4 bytes, so position should be 10
    assert_eq!(
        buffer_pos, 10,
        "After moving through 'Hello 😀', cursor should be at byte 10"
    );

    // Type 'X' and verify it's inserted correctly
    harness.type_text("X").unwrap();
    let expected = "Hello 😀X World 🌍";
    harness.assert_buffer_content(expected);
}

/// Test that cursor position is correct when clicking on text with non-ASCII characters
#[test]
fn test_mouse_click_on_non_ascii_text() {
    let mut harness = EditorTestHarness::new(120, 30).unwrap();

    // Type a line with box-drawing characters
    let text = "│  ┌──────────────┐  ┌──────────────┐  │";
    harness.type_text(text).unwrap();
    harness.render().unwrap();

    // Now click on various positions in the line and verify cursor position

    // Get the gutter width first by checking where line 1 starts
    // The tab bar is at row 0, first line of text is at row 1
    let _line_row = 1;

    // Click at the beginning of the text (after gutter)
    // We need to figure out where the gutter ends
    // Let's assume standard gutter of 8 chars for now: " " + "   1" + " │ "

    // This test may need adjustment based on actual gutter rendering
}

/// Test that backspace properly deletes entire UTF-8 characters, not just bytes
/// This reproduces the bug where backspace removes only the last byte of a multi-byte character
#[test]
fn test_backspace_deletes_entire_utf8_character() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Test 1: Euro sign (3 bytes: 0xE2 0x82 0xAC)
    harness.type_text("").unwrap();
    harness.assert_buffer_content("");

    // Backspace should delete the entire euro sign, not just one byte
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("");

    // Test 2: Norwegian characters (2 bytes each: æ=0xC3 0xA6, ø=0xC3 0xB8, å=0xC3 0xA5)
    harness.type_text("æøå").unwrap();
    harness.assert_buffer_content("æøå");

    // Backspace should delete 'å' entirely
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("æø");

    // Another backspace should delete 'ø' entirely
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("æ");

    // Another backspace should delete 'æ' entirely
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("");

    // Test 3: Emoji (4 bytes: 😀 = U+1F600)
    harness.type_text("a😀b").unwrap();
    harness.assert_buffer_content("a😀b");

    // Backspace should delete 'b'
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("a😀");

    // Backspace should delete the entire emoji (4 bytes), not just one byte
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("a");
}

/// Test that delete (forward) properly removes entire UTF-8 characters
#[test]
fn test_delete_forward_removes_entire_utf8_character() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type text with multi-byte characters
    harness.type_text("a€b").unwrap();
    harness.assert_buffer_content("a€b");

    // Move to beginning
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();

    // Delete 'a' - this should work fine (ASCII)
    harness
        .send_key(KeyCode::Delete, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("€b");

    // Delete '€' - should delete entire 3-byte euro sign, not just one byte
    harness
        .send_key(KeyCode::Delete, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("b");
}

/// Test that selecting and deleting/replacing UTF-8 characters works correctly
#[test]
fn test_selection_delete_with_utf8_characters() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type text with multi-byte characters: a + æ(2) + ø(2) + å(2) + b
    harness.type_text("aæøåb").unwrap();
    harness.assert_buffer_content("aæøåb");

    // Move to beginning
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();

    // Move right once (past 'a')
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();

    // Select the three Norwegian characters by shift+right 3 times
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();

    // Delete the selection with backspace
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    harness.assert_buffer_content("ab");
}

/// Test that selecting and replacing UTF-8 characters works correctly
#[test]
fn test_selection_replace_with_utf8_characters() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type text with emoji
    harness.type_text("hello😀world").unwrap();
    harness.assert_buffer_content("hello😀world");

    // Move to beginning
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();

    // Move right 5 times (past "hello")
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }

    // Select the emoji (1 character, 4 bytes)
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();

    // Replace with a different character
    harness.type_text("X").unwrap();
    harness.assert_buffer_content("helloXworld");
}

/// Test loading a file with UTF-8 characters, backspacing, saving, and verifying file content
/// This reproduces the exact bug where backspace removes only a byte, corrupting the file on save
#[test]
fn test_backspace_utf8_file_save_roundtrip() {
    let temp_dir = TempDir::new().unwrap();

    // Test 1: Euro sign (3 bytes: 0xE2 0x82 0xAC)
    let euro_path = temp_dir.path().join("euro.txt");
    std::fs::write(&euro_path, "\n").unwrap();

    let mut harness = EditorTestHarness::new(80, 24).unwrap();
    harness.open_file(&euro_path).unwrap();
    harness.render().unwrap();

    // Move to end of line (after €, before newline)
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();

    // Backspace should delete the entire euro sign
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();

    // Save with Ctrl+S
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Verify the file contains only a newline (euro sign fully deleted)
    let saved = std::fs::read(&euro_path).unwrap();
    assert_eq!(
        saved, b"\n",
        "Euro sign should be fully deleted, file should contain only newline. Got: {:?}",
        saved
    );

    // Test 2: Norwegian characters (æøå)
    let norwegian_path = temp_dir.path().join("norwegian.txt");
    std::fs::write(&norwegian_path, "æøå\n").unwrap();

    let mut harness2 = EditorTestHarness::new(80, 24).unwrap();
    harness2.open_file(&norwegian_path).unwrap();
    harness2.render().unwrap();

    // Move to end of line
    harness2.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();

    // Backspace should delete 'å' entirely (2 bytes)
    harness2
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();

    // Save
    harness2
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness2.render().unwrap();

    // Verify
    let saved2 = std::fs::read(&norwegian_path).unwrap();
    assert_eq!(
        saved2,
        "æø\n".as_bytes(),
        "Only 'å' should be deleted, leaving 'æø'. Got: {:?}",
        String::from_utf8_lossy(&saved2)
    );
}

/// Test that arrow keys move by grapheme clusters for Thai text
///
/// Thai "ที่" is 3 Unicode code points but 1 grapheme cluster:
/// - ท (U+0E17) base consonant
/// - ี (U+0E35) vowel mark
/// - ่ (U+0E48) tone mark
///
/// Pressing Right arrow once should skip the entire cluster.
/// Also verifies the screen cursor moves correctly (visual position).
#[test]
fn test_thai_grapheme_cluster_movement() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type Thai text: "aที่b" (a + Thai cluster + b)
    // This gives us: 1 byte (a) + 9 bytes (Thai) + 1 byte (b) = 11 bytes
    // Visual width: 1 (a) + 1 (Thai cluster) + 1 (b) = 3 columns
    let text = "aที่b";
    harness.type_text(text).unwrap();
    harness.render().unwrap();

    // Verify the text was typed correctly
    harness.assert_buffer_content(text);

    // Cursor should be at end (byte 11)
    let pos_at_end = harness.cursor_position();
    assert_eq!(
        pos_at_end, 11,
        "Cursor should be at byte 11 after typing text"
    );

    // Move to start
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    assert_eq!(
        harness.cursor_position(),
        0,
        "Cursor should be at start after Home"
    );

    // Get initial screen cursor position (at start of text, after gutter)
    let (initial_x, initial_y) = harness.screen_cursor_position();
    println!("Initial screen cursor: ({}, {})", initial_x, initial_y);

    // Press Right arrow - should move past 'a' (byte 0->1, visual 0->1)
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let pos1 = harness.cursor_position();
    let (x1, y1) = harness.screen_cursor_position();
    println!(
        "After 1st Right: buffer pos={}, screen=({}, {})",
        pos1, x1, y1
    );
    assert_eq!(pos1, 1, "After 1st Right, should be at byte 1 (after 'a')");
    assert_eq!(
        x1,
        initial_x + 1,
        "Screen cursor should advance by 1 column (past 'a')"
    );

    // Press Right arrow - should skip entire Thai cluster (byte 1->10, visual 1->2)
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let pos2 = harness.cursor_position();
    let (x2, y2) = harness.screen_cursor_position();
    println!(
        "After 2nd Right: buffer pos={}, screen=({}, {})",
        pos2, x2, y2
    );
    assert_eq!(
        pos2, 10,
        "After 2nd Right, should be at byte 10 (after Thai cluster 'ที่'). Got {}",
        pos2
    );
    assert_eq!(
        x2,
        initial_x + 2,
        "Screen cursor should advance by 1 column (Thai cluster has visual width 1). Got {}",
        x2
    );

    // Press Right arrow - should move past 'b' (byte 10->11, visual 2->3)
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let pos3 = harness.cursor_position();
    let (x3, y3) = harness.screen_cursor_position();
    println!(
        "After 3rd Right: buffer pos={}, screen=({}, {})",
        pos3, x3, y3
    );
    assert_eq!(
        pos3, 11,
        "After 3rd Right, should be at byte 11 (after 'b')"
    );
    assert_eq!(
        x3,
        initial_x + 3,
        "Screen cursor should advance by 1 column (past 'b')"
    );

    // Now go back with Left arrows
    // Press Left - should move before 'b' (byte 11->10, visual 3->2)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos_l1 = harness.cursor_position();
    let (xl1, _) = harness.screen_cursor_position();
    println!("After 1st Left: buffer pos={}, screen x={}", pos_l1, xl1);
    assert_eq!(pos_l1, 10, "After 1st Left, should be at byte 10");
    assert_eq!(xl1, initial_x + 2, "Screen cursor should be at column 2");

    // Press Left - should skip entire Thai cluster back (byte 10->1, visual 2->1)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos_l2 = harness.cursor_position();
    let (xl2, _) = harness.screen_cursor_position();
    println!("After 2nd Left: buffer pos={}, screen x={}", pos_l2, xl2);
    assert_eq!(
        pos_l2, 1,
        "After 2nd Left, should be at byte 1 (before Thai cluster). Got {}",
        pos_l2
    );
    assert_eq!(
        xl2,
        initial_x + 1,
        "Screen cursor should be at column 1 (after 'a'). Got {}",
        xl2
    );

    // Press Left - should move before 'a' (byte 1->0, visual 1->0)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos_l3 = harness.cursor_position();
    let (xl3, _) = harness.screen_cursor_position();
    println!("After 3rd Left: buffer pos={}, screen x={}", pos_l3, xl3);
    assert_eq!(pos_l3, 0, "After 3rd Left, should be at byte 0");
    assert_eq!(
        xl3, initial_x,
        "Screen cursor should be back at initial column"
    );
}

/// Test that backspace deletes Thai combining marks layer-by-layer
///
/// This is the "pro" behavior: backspace removes one code point at a time,
/// allowing users to fix a typo in a tone mark without retyping the whole character.
#[test]
fn test_thai_backspace_layer_by_layer() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type Thai text: "ที่" (base + vowel + tone = 3 code points)
    let thai = "ที่";
    harness.type_text(thai).unwrap();
    harness.render().unwrap();

    // Cursor is at end (byte 9)
    assert_eq!(harness.cursor_position(), 9);

    // First backspace: should delete tone mark (่) only, leaving "ที"
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    let content1 = harness.get_buffer_content().unwrap();
    assert_eq!(
        content1, "ที",
        "First backspace should delete only the tone mark. Got: {:?}",
        content1
    );

    // Second backspace: should delete vowel mark (ี) only, leaving "ท"
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    let content2 = harness.get_buffer_content().unwrap();
    assert_eq!(
        content2, "",
        "Second backspace should delete only the vowel mark. Got: {:?}",
        content2
    );

    // Third backspace: should delete base consonant (ท), leaving empty
    harness
        .send_key(KeyCode::Backspace, KeyModifiers::NONE)
        .unwrap();
    let content3 = harness.get_buffer_content().unwrap();
    assert_eq!(
        content3, "",
        "Third backspace should delete the base consonant. Got: {:?}",
        content3
    );
}

/// Test that Delete key removes entire Thai grapheme cluster
///
/// Unlike backspace (layer-by-layer), Delete removes the whole cluster at once
/// because if you delete the base consonant, the marks have nothing to sit on.
#[test]
fn test_thai_delete_entire_cluster() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();

    // Type Thai text: "ที่นี่" (2 grapheme clusters)
    let thai = "ที่นี่";
    harness.type_text(thai).unwrap();
    harness.render().unwrap();

    // Move to start
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    assert_eq!(harness.cursor_position(), 0);

    // Press Delete once - should remove entire first grapheme cluster "ที่"
    harness
        .send_key(KeyCode::Delete, KeyModifiers::NONE)
        .unwrap();
    let content = harness.get_buffer_content().unwrap();
    assert_eq!(
        content, "นี่",
        "Delete should remove entire grapheme cluster 'ที่', leaving 'นี่'. Got: {:?}",
        content
    );
}

/// Test Thai text loaded from file - movement and rendering
///
/// This tests the full flow: open file with Thai text, verify rendering,
/// test cursor movement by grapheme clusters.
#[test]
fn test_thai_file_open_and_movement() {
    // Create temp file with Thai text
    let temp_dir = TempDir::new().unwrap();
    let thai_path = temp_dir.path().join("thai.txt");

    // Write Thai text: "ที่นี่คือที่ติดตั้งระบบ" (typical Thai sentence)
    // This text has 13 grapheme clusters but 23 code points
    let thai_content = "ที่นี่คือที่ติดตั้งระบบ\n";
    std::fs::write(&thai_path, thai_content).unwrap();

    let mut harness = EditorTestHarness::new(120, 30).unwrap();
    harness.open_file(&thai_path).unwrap();
    harness.render().unwrap();

    // Verify content was loaded
    let loaded = harness.get_buffer_content().unwrap();
    assert_eq!(
        loaded.trim(),
        thai_content.trim(),
        "Thai content should be loaded correctly"
    );

    // Move to start of file
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Get initial position
    let initial_pos = harness.cursor_position();
    let (initial_x, initial_y) = harness.screen_cursor_position();
    println!(
        "Initial: buffer pos={}, screen=({}, {})",
        initial_pos, initial_x, initial_y
    );

    // Press Right arrow - should skip entire first grapheme cluster "ที่"
    // The first grapheme "ที่" is 9 bytes (3 code points × 3 bytes each)
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    let pos1 = harness.cursor_position();
    let (x1, _) = harness.screen_cursor_position();
    println!("After 1st Right: buffer pos={}, screen x={}", pos1, x1);

    // The first grapheme cluster "ที่" should be skipped entirely
    assert_eq!(
        pos1, 9,
        "After 1st Right, cursor should be at byte 9 (after first Thai cluster 'ที่'). Got {}",
        pos1
    );

    // Screen cursor should advance by 1 column (Thai grapheme has visual width 1)
    assert_eq!(
        x1,
        initial_x + 1,
        "Screen cursor should advance by 1 column. Got {} (initial was {})",
        x1,
        initial_x
    );

    // Press Right arrow again - should skip second grapheme cluster "นี่"
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    let pos2 = harness.cursor_position();
    let (x2, _) = harness.screen_cursor_position();
    println!("After 2nd Right: buffer pos={}, screen x={}", pos2, x2);

    // Second cluster "นี่" is also 9 bytes
    assert_eq!(
        pos2, 18,
        "After 2nd Right, cursor should be at byte 18. Got {}",
        pos2
    );
    assert_eq!(
        x2,
        initial_x + 2,
        "Screen cursor should be at initial+2. Got {}",
        x2
    );

    // Now go back with Left arrow - should skip back over "นี่"
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let pos_l1 = harness.cursor_position();
    let (xl1, _) = harness.screen_cursor_position();
    println!("After 1st Left: buffer pos={}, screen x={}", pos_l1, xl1);

    assert_eq!(
        pos_l1, 9,
        "After 1st Left, cursor should be at byte 9. Got {}",
        pos_l1
    );
    assert_eq!(
        xl1,
        initial_x + 1,
        "Screen cursor should be at initial+1. Got {}",
        xl1
    );

    // Left again - should go back to start
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let pos_l2 = harness.cursor_position();
    let (xl2, _) = harness.screen_cursor_position();
    println!("After 2nd Left: buffer pos={}, screen x={}", pos_l2, xl2);

    assert_eq!(
        pos_l2, 0,
        "After 2nd Left, cursor should be at byte 0. Got {}",
        pos_l2
    );
    assert_eq!(
        xl2, initial_x,
        "Screen cursor should be back at initial. Got {}",
        xl2
    );
}

/// Test grapheme cluster movement in the search prompt
///
/// When typing Thai text in the search prompt, arrow keys should move
/// by grapheme cluster, not by individual code points.
#[test]
fn test_search_prompt_grapheme_movement() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();
    harness.render().unwrap();

    // Open search prompt with Ctrl+F
    harness
        .send_key(KeyCode::Char('f'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Verify search prompt is open
    harness.assert_screen_contains("Search:");

    // Type Thai text: "aที่b" (a + Thai cluster + b)
    // The Thai cluster "ที่" is 3 code points but 1 grapheme
    let thai_text = "aที่b";
    harness.type_text(thai_text).unwrap();
    harness.render().unwrap();

    // Verify text appears in prompt
    harness.assert_screen_contains(thai_text);

    // Get screen cursor position at end
    let (end_x, end_y) = harness.screen_cursor_position();
    println!("Cursor at end: ({}, {})", end_x, end_y);

    // Press Left once - should move back by 1 (past 'b')
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x1, _) = harness.screen_cursor_position();
    println!("After 1st Left: x={}", x1);
    assert_eq!(
        x1,
        end_x - 1,
        "1st Left should move back 1 column (past 'b')"
    );

    // Press Left again - should skip entire Thai cluster
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x2, _) = harness.screen_cursor_position();
    println!("After 2nd Left: x={}", x2);
    assert_eq!(
        x2,
        end_x - 2,
        "2nd Left should skip entire Thai cluster (visual width 1)"
    );

    // Press Left again - should move before 'a'
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x3, _) = harness.screen_cursor_position();
    println!("After 3rd Left: x={}", x3);
    assert_eq!(x3, end_x - 3, "3rd Left should move before 'a'");

    // Now press Right 3 times and verify we end up back at the end
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let (final_x, _) = harness.screen_cursor_position();
    assert_eq!(
        final_x, end_x,
        "3 Right arrows should return cursor to end position"
    );

    // Close search prompt
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
}

/// Test grapheme cluster movement in the file open prompt
///
/// When typing Thai text in the file open prompt, arrow keys should move
/// by grapheme cluster, not by individual code points.
#[test]
fn test_file_open_prompt_grapheme_movement() {
    let mut harness = EditorTestHarness::new(100, 30).unwrap();
    harness.render().unwrap();

    // Open file prompt with Ctrl+O
    harness
        .send_key(KeyCode::Char('o'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Verify file open prompt is open (prompt shows "Open:" at bottom)
    harness.assert_screen_contains("Open file:");

    // Clear any prefilled text by going to start and selecting all then deleting
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::End, KeyModifiers::SHIFT).unwrap();
    harness
        .send_key(KeyCode::Delete, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Type Thai text: "ที่นี่" (2 grapheme clusters, 6 code points)
    // Each cluster is 3 code points (base + vowel + tone)
    let thai_text = "ที่นี่";
    harness.type_text(thai_text).unwrap();
    harness.render().unwrap();

    // Verify text appears in prompt
    harness.assert_screen_contains(thai_text);

    // Get screen cursor position at end
    let (end_x, end_y) = harness.screen_cursor_position();
    println!("Cursor at end: ({}, {})", end_x, end_y);

    // Press Left once - should skip entire second Thai cluster "นี่"
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x1, _) = harness.screen_cursor_position();
    println!("After 1st Left: x={}", x1);
    assert_eq!(
        x1,
        end_x - 1,
        "1st Left should skip entire Thai cluster 'นี่' (visual width 1)"
    );

    // Press Left again - should skip entire first Thai cluster "ที่"
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x2, _) = harness.screen_cursor_position();
    println!("After 2nd Left: x={}", x2);
    assert_eq!(
        x2,
        end_x - 2,
        "2nd Left should skip entire Thai cluster 'ที่' (visual width 1)"
    );

    // Now at the start. Press Right twice to return to end
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let (final_x, _) = harness.screen_cursor_position();
    assert_eq!(
        final_x, end_x,
        "2 Right arrows should return cursor to end position"
    );

    // Close file open prompt
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
}

/// Test grapheme cluster movement in settings search box
///
/// Note: The settings search box is a simple filter field that doesn't support
/// cursor movement (Left/Right arrows) - it only supports typing at the end
/// and backspace. This is a limitation of the simple filter design, not a bug.
///
/// This test is marked ignore since cursor movement isn't supported in this field.
#[test]
#[ignore = "Settings search is a simple filter without cursor movement support"]
fn test_settings_search_grapheme_movement() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    harness.render().unwrap();

    // Open settings with Ctrl+,
    harness.open_settings().unwrap();

    // Verify settings is open
    harness.assert_screen_contains("Settings");

    // Open search box with '/'
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Type Thai text: "aที่b" (a + Thai cluster + b)
    let thai_text = "aที่b";
    for c in thai_text.chars() {
        harness
            .send_key(KeyCode::Char(c), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Verify text appears in the search box
    harness.assert_screen_contains(thai_text);

    // Get screen cursor position at end
    let (end_x, end_y) = harness.screen_cursor_position();
    println!("Cursor at end: ({}, {})", end_x, end_y);

    // Press Left once - should move back by 1 (past 'b')
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x1, _) = harness.screen_cursor_position();
    println!("After 1st Left: x={}", x1);
    assert_eq!(
        x1,
        end_x - 1,
        "1st Left should move back 1 column (past 'b')"
    );

    // Press Left again - should skip entire Thai cluster
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x2, _) = harness.screen_cursor_position();
    println!("After 2nd Left: x={}", x2);
    assert_eq!(
        x2,
        end_x - 2,
        "2nd Left should skip entire Thai cluster (visual width 1)"
    );

    // Press Left again - should move before 'a'
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let (x3, _) = harness.screen_cursor_position();
    println!("After 3rd Left: x={}", x3);
    assert_eq!(x3, end_x - 3, "3rd Left should move before 'a'");

    // Press Right 3 times to return to end
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let (final_x, _) = harness.screen_cursor_position();
    assert_eq!(
        final_x, end_x,
        "3 Right arrows should return cursor to end position"
    );

    // Close settings
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
}

/// Test that Left arrow moves by grapheme cluster in main editor buffer (typed content)
#[test]
fn test_main_editor_left_arrow_grapheme_movement() {
    let mut harness = EditorTestHarness::new(80, 24).unwrap();
    harness.render().unwrap();

    // Type Thai text directly in the editor: "ที่" (1 grapheme cluster, 3 code points)
    // Each Thai grapheme cluster consists of:
    // - Base consonant (3 bytes)
    // - Vowel mark (3 bytes)
    // - Tone mark (3 bytes)
    let thai_text = "ที่";
    harness.type_text(thai_text).unwrap();
    harness.render().unwrap();

    // Cursor should be at byte 9 (after all 3 code points = 9 bytes)
    let pos_end = harness.cursor_position();
    assert_eq!(
        pos_end, 9,
        "After typing Thai cluster, cursor should be at byte 9"
    );

    // Press Left arrow ONCE - should skip entire grapheme cluster back to position 0
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let pos_after_left = harness.cursor_position();
    println!("After 1 Left: cursor at byte {}", pos_after_left);

    // Expected: position 0 (skipped entire grapheme cluster)
    // Bug: position 6 (only skipped one code point = 3 bytes)
    assert_eq!(
        pos_after_left, 0,
        "Left arrow should move by entire grapheme cluster (from byte 9 to 0). \
         If this fails with position 6, the bug is: left arrow moves by code point instead of grapheme"
    );
}

/// BUG REPRODUCTION: Left arrow at position >32 bytes falls back to code point movement
///
/// When cursor is at position > 32 bytes, prev_grapheme_boundary calculates
/// start = pos - 32, which may land in the middle of a UTF-8 character.
/// This causes from_utf8 to fail and the code falls back to prev_char_boundary
/// which only moves by one code point instead of a full grapheme cluster.
///
/// This test uses the exact Thai file that triggers the bug.
#[test]
fn test_left_arrow_at_long_position_file_loaded() {
    let temp_dir = TempDir::new().unwrap();
    let thai_path = temp_dir.path().join("thai_long.txt");

    // Use the same Thai text from /tmp/thai.txt: "ที่นี่คือที่ติดตั้งระบบ"
    // This is 69 bytes total, 23 code points, ~13 grapheme clusters
    let thai_content = "ที่นี่คือที่ติดตั้งระบบ";
    std::fs::write(&thai_path, thai_content).unwrap();

    let mut harness = EditorTestHarness::new(120, 30).unwrap();
    harness.open_file(&thai_path).unwrap();
    harness.render().unwrap();

    // Move to end of line (position 69)
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let pos_end = harness.cursor_position();
    println!("At end: cursor at byte {}", pos_end);
    assert_eq!(
        pos_end, 69,
        "Cursor should be at byte 69 (end of Thai text)"
    );

    // The last two characters are "บบ" (each is a single code point, 3 bytes)
    // Position 66-69: บ (U+0E1A)
    // Position 63-66: บ (U+0E1A)
    // So pressing Left from 69 should go to 66 (correct behavior)

    // Press Left - should move to position 66 (before last บ)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos1 = harness.cursor_position();
    println!("After 1st Left: cursor at byte {}", pos1);
    assert_eq!(
        pos1, 66,
        "After Left from 69, should be at 66 (skipped บ which is 3 bytes)"
    );

    // Press Left again - should move to position 63
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos2 = harness.cursor_position();
    println!("After 2nd Left: cursor at byte {}", pos2);
    assert_eq!(
        pos2, 63,
        "After Left from 66, should be at 63 (skipped บ which is 3 bytes)"
    );

    // Press Left again - should move to position 60 (ะ is single code point)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos3 = harness.cursor_position();
    println!("After 3rd Left: cursor at byte {}", pos3);
    assert_eq!(
        pos3, 60,
        "After Left from 63, should be at 60 (skipped ะ which is 3 bytes)"
    );

    // Continue pressing Left to navigate through the text
    // At this point we're at position 60, so start = 60 - 32 = 28
    // Position 28 is in the middle of code point at 27-30
    // This is where the bug might trigger!

    // Press Left again from position 60
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos4 = harness.cursor_position();
    println!("After 4th Left: cursor at byte {}", pos4);
    assert_eq!(
        pos4, 57,
        "After Left from 60, should be at 57 (skipped ร which is 3 bytes)"
    );

    // Keep pressing to test grapheme cluster movement
    // Position 54-57: ง (U+0E07, single code point)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos5 = harness.cursor_position();
    println!("After 5th Left: cursor at byte {}", pos5);
    assert_eq!(pos5, 54, "After Left from 57, should be at 54 (skipped ง)");

    // Position 45-54: ตั้ is a grapheme cluster with base + vowel + tone = 3 code points = 9 bytes
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let pos6 = harness.cursor_position();
    println!("After 6th Left: cursor at byte {}", pos6);

    // BUG CHECK: If this fails with position 51 instead of 45, the bug is present
    // (cursor moved by 1 code point instead of full grapheme cluster)
    assert_eq!(
        pos6, 45,
        "After Left from 54, should be at 45 (skipped grapheme cluster ตั้ which is 9 bytes). \
         If this is 51, the bug is present: left arrow fell back to code point movement."
    );
}