vim-line 7.7.3

A line-oriented vim motions library for TUI 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
use super::*;

#[test]
fn test_basic_motion() {
    let mut editor = VimLineEditor::new();
    let text = "hello world";

    // Move right with 'l'
    editor.handle_key(Key::char('l'), text);
    assert_eq!(editor.cursor(), 1);

    // Move right with 'w'
    editor.handle_key(Key::char('w'), text);
    assert_eq!(editor.cursor(), 6); // Start of "world"

    // Move to end with '$' - cursor should be ON the last char, not past it
    editor.handle_key(Key::char('$'), text);
    assert_eq!(editor.cursor(), 10); // 'd' is at index 10

    // Move to start with '0'
    editor.handle_key(Key::char('0'), text);
    assert_eq!(editor.cursor(), 0);
}

#[test]
fn test_mode_switching() {
    let mut editor = VimLineEditor::new();
    let text = "hello";

    assert_eq!(editor.mode(), Mode::Normal);

    editor.handle_key(Key::char('i'), text);
    assert_eq!(editor.mode(), Mode::Insert);

    editor.handle_key(Key::code(KeyCode::Escape), text);
    assert_eq!(editor.mode(), Mode::Normal);
}

#[test]
fn test_delete_word() {
    let mut editor = VimLineEditor::new();
    let text = "hello world";

    // dw should delete "hello "
    editor.handle_key(Key::char('d'), text);
    editor.handle_key(Key::char('w'), text);

    // Check we're back in Normal mode
    assert_eq!(editor.mode(), Mode::Normal);
}

#[test]
fn test_insert_char() {
    let mut editor = VimLineEditor::new();
    let text = "";

    editor.handle_key(Key::char('i'), text);
    let result = editor.handle_key(Key::char('x'), text);

    assert_eq!(result.edits.len(), 1);
    match &result.edits[0] {
        TextEdit::Insert { at, text } => {
            assert_eq!(*at, 0);
            assert_eq!(text, "x");
        }
        _ => panic!("Expected Insert"),
    }
}

#[test]
fn test_visual_mode() {
    let mut editor = VimLineEditor::new();
    let text = "hello world";

    // Enter visual mode
    editor.handle_key(Key::char('v'), text);
    assert_eq!(editor.mode(), Mode::Visual);

    // Extend selection
    editor.handle_key(Key::char('w'), text);

    // Selection should cover from 0 to cursor
    let sel = editor.selection().unwrap();
    assert_eq!(sel.start, 0);
    assert!(sel.end > 0);
}

#[test]
fn test_backspace_ascii() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("abc");

    // Enter insert mode and go to end
    editor.handle_key(Key::char('i'), &text);
    editor.handle_key(Key::code(KeyCode::End), &text);
    assert_eq!(editor.cursor(), 3);

    // Backspace should delete 'c'
    let result = editor.handle_key(Key::code(KeyCode::Backspace), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "ab");
    assert_eq!(editor.cursor(), 2);
}

#[test]
fn test_backspace_unicode() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("a😀b");

    // Enter insert mode and position after emoji (byte position 5: 1 + 4)
    editor.handle_key(Key::char('i'), &text);
    editor.handle_key(Key::code(KeyCode::End), &text);
    editor.handle_key(Key::code(KeyCode::Left), &text); // Move before 'b'
    assert_eq!(editor.cursor(), 5); // After the 4-byte emoji

    // Backspace should delete entire emoji (4 bytes), not just 1 byte
    let result = editor.handle_key(Key::code(KeyCode::Backspace), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "ab");
    assert_eq!(editor.cursor(), 1);
}

#[test]
fn test_yank_and_paste() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("hello world");

    // Yank word with yw
    editor.handle_key(Key::char('y'), &text);
    let result = editor.handle_key(Key::char('w'), &text);
    assert!(result.yanked.is_some());
    assert_eq!(result.yanked.unwrap(), "hello ");

    // Move to end and paste
    editor.handle_key(Key::char('$'), &text);
    let result = editor.handle_key(Key::char('p'), &text);

    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "hello worldhello ");
}

#[test]
fn test_visual_mode_delete() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("hello world");

    // Enter visual mode at position 0
    editor.handle_key(Key::char('v'), &text);
    assert_eq!(editor.mode(), Mode::Visual);

    // Extend selection with 'e' motion to end of word (stays on 'o' of hello)
    editor.handle_key(Key::char('e'), &text);

    // Delete selection with d - deletes "hello"
    let result = editor.handle_key(Key::char('d'), &text);

    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, " world");
    assert_eq!(editor.mode(), Mode::Normal);
}

#[test]
fn test_operator_pending_escape() {
    let mut editor = VimLineEditor::new();
    let text = "hello world";

    // Start delete operator
    editor.handle_key(Key::char('d'), text);
    assert!(matches!(editor.mode(), Mode::OperatorPending(_)));

    // Cancel with Escape
    editor.handle_key(Key::code(KeyCode::Escape), text);
    assert_eq!(editor.mode(), Mode::Normal);
}

#[test]
fn test_replace_char() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("hello");

    // Press 'r' then 'x' to replace 'h' with 'x'
    editor.handle_key(Key::char('r'), &text);
    assert_eq!(editor.mode(), Mode::ReplaceChar);

    let result = editor.handle_key(Key::char('x'), &text);
    assert_eq!(editor.mode(), Mode::Normal);

    // Apply edits
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "xello");
}

#[test]
fn test_replace_char_escape() {
    let mut editor = VimLineEditor::new();
    let text = "hello";

    // Press 'r' then Escape should cancel
    editor.handle_key(Key::char('r'), text);
    assert_eq!(editor.mode(), Mode::ReplaceChar);

    editor.handle_key(Key::code(KeyCode::Escape), text);
    assert_eq!(editor.mode(), Mode::Normal);
}

#[test]
fn test_cw_no_trailing_space() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("hello world");

    // cw should delete "hello" (not "hello ") and enter insert mode
    editor.handle_key(Key::char('c'), &text);
    let result = editor.handle_key(Key::char('w'), &text);

    assert_eq!(editor.mode(), Mode::Insert);

    // Apply edits
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    // Should preserve the space before "world"
    assert_eq!(text, " world");
}

#[test]
fn test_dw_includes_trailing_space() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("hello world");

    // dw should delete "hello " (including trailing space)
    editor.handle_key(Key::char('d'), &text);
    let result = editor.handle_key(Key::char('w'), &text);

    assert_eq!(editor.mode(), Mode::Normal);

    // Apply edits
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "world");
}

#[test]
fn test_paste_at_empty_buffer() {
    let mut editor = VimLineEditor::new();

    // First yank something from non-empty text
    let yank_text = String::from("test");
    editor.handle_key(Key::char('y'), &yank_text);
    editor.handle_key(Key::char('w'), &yank_text);

    // Now paste into empty buffer
    let mut text = String::new();
    editor.set_cursor(0, &text);
    let result = editor.handle_key(Key::char('p'), &text);

    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "test");
}

#[test]
fn test_dollar_cursor_on_last_char() {
    let mut editor = VimLineEditor::new();
    let text = "abc";

    // $ should place cursor ON 'c' (index 2), not past it (index 3)
    editor.handle_key(Key::char('$'), text);
    assert_eq!(editor.cursor(), 2);

    // Single character line
    let text = "x";
    editor.set_cursor(0, text);
    editor.handle_key(Key::char('$'), text);
    assert_eq!(editor.cursor(), 0); // Stay on the only char
}

#[test]
fn test_x_delete_last_char_moves_cursor_left() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("abc");

    // Move to last char
    editor.handle_key(Key::char('$'), &text);
    assert_eq!(editor.cursor(), 2); // On 'c'

    // Delete with x
    let result = editor.handle_key(Key::char('x'), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }

    assert_eq!(text, "ab");
    // Cursor should move left to stay on valid char
    assert_eq!(editor.cursor(), 1); // On 'b'
}

#[test]
fn test_x_delete_middle_char_cursor_stays() {
    let mut editor = VimLineEditor::new();
    let mut text = String::from("abc");

    // Position on 'b' (index 1)
    editor.handle_key(Key::char('l'), &text);
    assert_eq!(editor.cursor(), 1);

    // Delete with x
    let result = editor.handle_key(Key::char('x'), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }

    assert_eq!(text, "ac");
    // Cursor stays at same position (now on 'c')
    assert_eq!(editor.cursor(), 1);
}

#[test]
fn test_percent_bracket_matching() {
    let mut editor = VimLineEditor::new();
    let text = "(hello world)";

    // Cursor starts at position 0, on '('
    assert_eq!(editor.cursor(), 0);

    // Press '%' to jump to matching ')'
    editor.handle_key(Key::char('%'), text);
    assert_eq!(editor.cursor(), 12); // Position of ')'

    // Press '%' again to jump back to '('
    editor.handle_key(Key::char('%'), text);
    assert_eq!(editor.cursor(), 0);
}

#[test]
fn test_percent_nested_brackets() {
    let mut editor = VimLineEditor::new();
    let text = "([{<>}])";

    // Start on '('
    editor.handle_key(Key::char('%'), text);
    assert_eq!(editor.cursor(), 7); // Matching ')'

    // Move to '[' at position 1
    editor.set_cursor(1, text);
    editor.handle_key(Key::char('%'), text);
    assert_eq!(editor.cursor(), 6); // Matching ']'

    // Move to '{' at position 2
    editor.set_cursor(2, text);
    editor.handle_key(Key::char('%'), text);
    assert_eq!(editor.cursor(), 5); // Matching '}'

    // Move to '<' at position 3
    editor.set_cursor(3, text);
    editor.handle_key(Key::char('%'), text);
    assert_eq!(editor.cursor(), 4); // Matching '>'
}

#[test]
fn test_percent_on_non_bracket() {
    let mut editor = VimLineEditor::new();
    let text = "hello";

    // Start at position 0 on 'h'
    let orig_cursor = editor.cursor();
    editor.handle_key(Key::char('%'), text);
    // Cursor should not move when not on a bracket
    assert_eq!(editor.cursor(), orig_cursor);
}

// ----- Mode::CommandLine (Ex-style `:` command line, opt-in) -----

#[test]
fn test_command_line_enter_clean() {
    // Checkpoint 1: flag-on `:` enters CommandLine with empty buffer; main
    // text untouched; status reports the new mode.
    let text = String::from("preserved");
    let mut editor = VimLineEditor::new().with_command_mode(true);
    editor.set_cursor(0, &text);

    let result = editor.handle_key(Key::char(':'), &text);

    assert_eq!(editor.mode(), Mode::CommandLine);
    assert_eq!(editor.command_line_buffer(), Some(""));
    assert_eq!(editor.command_line_cursor(), 0);
    assert!(result.edits.is_empty(), "no edits should touch main text");
    assert!(result.action.is_none());
    assert_eq!(editor.status(), "COMMAND");
    // Main buffer is the caller's; verify we returned no instructions to
    // mutate it.
    assert_eq!(text, "preserved");
}

#[test]
fn test_command_line_typing_submit_escape() {
    // Checkpoint 2: typing appends to the command buffer, not the main
    // buffer; Enter emits SubmitCommand without the leading `:`; Escape
    // clears the buffer with no side effects.
    let text = String::from("main");
    let mut editor = VimLineEditor::new().with_command_mode(true);

    editor.handle_key(Key::char(':'), &text);
    for c in "ir".chars() {
        let r = editor.handle_key(Key::char(c), &text);
        assert!(r.edits.is_empty(), "char '{}' must not edit main text", c);
        assert!(r.action.is_none(), "char '{}' must not emit an action", c);
    }
    assert_eq!(editor.command_line_buffer(), Some("ir"));
    assert_eq!(editor.command_line_cursor(), 2);

    let submit = editor.handle_key(Key::code(KeyCode::Enter), &text);
    match submit.action {
        Some(Action::SubmitCommand(s)) => assert_eq!(s, "ir"),
        other => panic!("expected SubmitCommand(\"ir\"), got {:?}", other),
    }
    assert!(submit.edits.is_empty(), "submit must not edit main text");
    assert_eq!(editor.mode(), Mode::Normal);
    assert_eq!(editor.command_line_buffer(), None);

    // Now exercise Escape: enter again, type, escape -> Normal, no action.
    editor.handle_key(Key::char(':'), &text);
    editor.handle_key(Key::char('q'), &text);
    let esc = editor.handle_key(Key::code(KeyCode::Escape), &text);
    assert!(esc.action.is_none());
    assert!(esc.edits.is_empty());
    assert_eq!(editor.mode(), Mode::Normal);
    assert_eq!(editor.command_line_buffer(), None);
    assert_eq!(text, "main");
}

#[test]
fn test_command_line_disabled_by_default() {
    // Checkpoint 3: with the flag off (default), Normal-mode `:` is a
    // no-op so other consumers of vim-line are unaffected.
    let text = String::from("hello");
    let mut editor = VimLineEditor::new();
    let result = editor.handle_key(Key::char(':'), &text);

    assert_eq!(editor.mode(), Mode::Normal);
    assert!(result.edits.is_empty());
    assert!(result.action.is_none());
    assert_eq!(editor.command_line_buffer(), None);
}

#[test]
fn test_command_line_does_not_steal_r_replace() {
    // Checkpoint 4: `r:` (replace char with `:`) still works with the
    // command-mode flag enabled — ReplaceChar swallows the next key
    // before Normal-mode dispatch can see it.
    let mut text = String::from("abc");
    let mut editor = VimLineEditor::new().with_command_mode(true);

    editor.handle_key(Key::char('r'), &text);
    assert_eq!(editor.mode(), Mode::ReplaceChar);
    let result = editor.handle_key(Key::char(':'), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, ":bc");
    assert_eq!(editor.mode(), Mode::Normal);
}

#[test]
fn test_command_line_does_not_steal_d_operator() {
    // Checkpoint 4 (cont.): `d:` cancels the delete operator like before
    // (`:` is not a known motion), without entering CommandLine.
    let text = String::from("hello");
    let mut editor = VimLineEditor::new().with_command_mode(true);

    editor.handle_key(Key::char('d'), &text);
    assert!(matches!(editor.mode(), Mode::OperatorPending(_)));
    let result = editor.handle_key(Key::char(':'), &text);
    assert_eq!(editor.mode(), Mode::Normal);
    assert!(result.edits.is_empty());
    assert!(result.action.is_none());
    assert_eq!(editor.command_line_buffer(), None);
}

#[test]
fn test_command_line_insert_mode_literal_colon_unchanged() {
    // Checkpoint 4 (cont.): Insert-mode `:` is still a literal char insert
    // even with the flag on. This is the path word definitions (`: foo …`)
    // flow through.
    let mut text = String::new();
    let mut editor = VimLineEditor::new().with_command_mode(true);

    editor.handle_key(Key::char('i'), &text);
    let result = editor.handle_key(Key::char(':'), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, ":");
    assert_eq!(editor.mode(), Mode::Insert);
}

#[test]
fn test_command_line_backspace_and_arrows() {
    // Backspace pops from the command buffer; arrows move the command
    // cursor; mid-buffer typing inserts at the cursor — none of which
    // should touch the host's main text.
    let text = String::from("ignored");
    let mut editor = VimLineEditor::new().with_command_mode(true);

    editor.handle_key(Key::char(':'), &text);
    for c in "abd".chars() {
        editor.handle_key(Key::char(c), &text);
    }
    assert_eq!(editor.command_line_buffer(), Some("abd"));

    editor.handle_key(Key::code(KeyCode::Backspace), &text);
    assert_eq!(editor.command_line_buffer(), Some("ab"));
    assert_eq!(editor.command_line_cursor(), 2);

    editor.handle_key(Key::code(KeyCode::Left), &text);
    assert_eq!(editor.command_line_cursor(), 1);
    editor.handle_key(Key::char('X'), &text);
    assert_eq!(editor.command_line_buffer(), Some("aXb"));

    editor.handle_key(Key::code(KeyCode::Home), &text);
    assert_eq!(editor.command_line_cursor(), 0);
    editor.handle_key(Key::code(KeyCode::End), &text);
    assert_eq!(editor.command_line_cursor(), 3);

    assert_eq!(text, "ignored");
}

#[test]
fn normal_k_on_single_line_emits_history_prev() {
    let mut editor = VimLineEditor::new();
    let text = "hello";
    let result = editor.handle_key(Key::char('k'), text);
    assert_eq!(result.action, Some(Action::HistoryPrev));
}

#[test]
fn normal_j_on_single_line_emits_history_next() {
    let mut editor = VimLineEditor::new();
    let text = "hello";
    let result = editor.handle_key(Key::char('j'), text);
    assert_eq!(result.action, Some(Action::HistoryNext));
}

#[test]
fn normal_kj_in_middle_of_multiline_moves_between_lines() {
    let mut editor = VimLineEditor::new();
    let text = "alpha\nbeta\ngamma";

    // Cursor on the middle line ('b' of "beta", index 6).
    editor.set_cursor(6, text);
    let result = editor.handle_key(Key::char('k'), text);
    assert!(
        result.action.is_none(),
        "k off-boundary is a motion, not history"
    );
    assert_eq!(editor.cursor(), 0, "moved up to 'a' of \"alpha\"");

    // From "alpha", k again IS at the first line → history.
    let result = editor.handle_key(Key::char('k'), text);
    assert_eq!(result.action, Some(Action::HistoryPrev));

    // Cursor back to middle line, then j off-boundary moves down.
    editor.set_cursor(6, text);
    let result = editor.handle_key(Key::char('j'), text);
    assert!(
        result.action.is_none(),
        "j off-boundary is a motion, not history"
    );
    assert_eq!(editor.cursor(), 11, "moved down to 'g' of \"gamma\"");

    // From "gamma", j again IS at the last line → history.
    let result = editor.handle_key(Key::char('j'), text);
    assert_eq!(result.action, Some(Action::HistoryNext));
}

#[test]
fn normal_arrows_still_emit_history_unchanged() {
    let mut editor = VimLineEditor::new();
    let text = "alpha\nbeta\ngamma";
    editor.set_cursor(6, text);
    // Up/Down arrows in Normal mode are always history nav regardless of
    // line position — preserving existing behavior.
    let up = editor.handle_key(Key::code(KeyCode::Up), text);
    assert_eq!(up.action, Some(Action::HistoryPrev));
    let down = editor.handle_key(Key::code(KeyCode::Down), text);
    assert_eq!(down.action, Some(Action::HistoryNext));
}

#[test]
fn visual_kj_remains_motion_only() {
    let mut editor = VimLineEditor::new();
    let text = "one\ntwo";
    editor.handle_key(Key::char('v'), text);
    // In Visual mode, j/k still move the cursor and never emit a history
    // intent — only Normal mode has the boundary fall-through.
    let result = editor.handle_key(Key::char('j'), text);
    assert!(result.action.is_none());
    assert_eq!(editor.cursor(), 4);
}

// ----- vim word vs WORD motions (issue #540) ---------------------------
//
// `w`/`b`/`e` now use vim *word* semantics: a word is a maximal run of
// one class (keyword / punctuation / whitespace). `W`/`B`/`E` keep the
// classic whitespace-only WORD semantics.

/// Helper: drive a sequence of single-char Normal-mode keys and return the
/// resulting cursor. Starts each editor fresh on `text` at column 0.
fn walk(text: &str, keys: &str) -> usize {
    let mut editor = VimLineEditor::new();
    editor.set_cursor(0, text);
    for c in keys.chars() {
        editor.handle_key(Key::char(c), text);
    }
    editor.cursor()
}

#[test]
fn w_stops_on_punctuation_run() {
    // foo,bar — from `f`, w lands on `,` then on `b` of bar.
    let text = "foo,bar";
    assert_eq!(walk(text, "w"), 3, "w from f lands on the comma");
    assert_eq!(walk(text, "ww"), 4, "second w lands on b of bar");
}

#[test]
fn w_walks_foo_comma_space_bar_baz() {
    // `foo, bar baz` — w hops foo -> , -> (skip space) bar -> baz.
    let text = "foo, bar baz";
    assert_eq!(walk(text, "w"), 3, "on comma");
    assert_eq!(walk(text, "ww"), 5, "on b of bar");
    assert_eq!(walk(text, "www"), 9, "on b of baz");
}

#[test]
fn w_past_open_paren_lands_on_arg() {
    // The motivating case: jump to the first non-blank after `(` or `,`.
    // `foo(bar)` from `f`: w -> ( -> b -> ) .
    let text = "foo(bar)";
    assert_eq!(walk(text, "w"), 3, "on (");
    assert_eq!(walk(text, "ww"), 4, "on b of bar");
    assert_eq!(walk(text, "www"), 7, "on )");
}

#[test]
fn w_on_colon_dash_punctuation() {
    // `:-x` — `:-` is a single punctuation run; w from `:` skips the
    // whole run and lands on `x`. A second w is clamped onto `x` (#541),
    // it can't move past the buffer.
    let text = ":-x";
    assert_eq!(walk(text, "w"), 2, "w lands on x");
    assert_eq!(walk(text, "ww"), 2, "second w stays clamped on x");
}

#[test]
fn b_walks_back_through_punctuation() {
    // `foo,bar` cursor on `b` (index 4): b -> , -> f.
    let text = "foo,bar";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(4, text);
    editor.handle_key(Key::char('b'), text);
    assert_eq!(editor.cursor(), 3, "b lands on comma");
    editor.handle_key(Key::char('b'), text);
    assert_eq!(editor.cursor(), 0, "b lands on f");
}

#[test]
fn b_from_within_word_goes_to_start() {
    // Cursor mid-keyword lands on the keyword's start.
    let text = "hello world";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(3, text); // on 2nd l
    editor.handle_key(Key::char('b'), text);
    assert_eq!(editor.cursor(), 0);
}

#[test]
fn e_lands_on_last_char_of_word() {
    // `foo,bar` from `f`: e -> o (idx 2) -> , (idx 3) -> r (idx 6).
    let text = "foo,bar";
    assert_eq!(walk(text, "e"), 2, "on last o of foo");
    assert_eq!(walk(text, "ee"), 3, "on comma");
    assert_eq!(walk(text, "eee"), 6, "on last r of bar");
}

#[test]
fn e_skips_leading_whitespace() {
    // Cursor on whitespace: e jumps to end of next word.
    let text = "foo bar";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(3, text); // on the space
    editor.handle_key(Key::char('e'), text);
    assert_eq!(editor.cursor(), 6, "on r of bar");
}

#[test]
fn w_at_end_of_buffer_lands_on_last_char() {
    // vim Normal-mode invariant (#541): `w` on the final word can't move
    // past the buffer, so it lands ON the last character, not at len.
    let text = "foo";
    assert_eq!(walk(text, "w"), 2, "lands on last 'o', not past it");
    assert_eq!(walk(text, "ww"), 2, "stays clamped on the last char");
}

#[test]
fn b_at_start_of_buffer_stays_put() {
    let text = "foo";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(0, text);
    editor.handle_key(Key::char('b'), text);
    assert_eq!(editor.cursor(), 0);
}

#[test]
fn b_from_eob_skips_trailing_punctuation() {
    // Regression: vim-line lets the normal-mode cursor sit at EOB (e.g.
    // after `w` eats a final word). `b` from there must behave like `b`
    // from the last char — skipping a trailing punctuation word back to
    // the previous keyword — instead of landing on the `,`.
    //
    // Note: #541 makes EOB unreachable via Normal-mode input (forward
    // motions clamp onto the last char), so we force the cursor to len
    // with `set_cursor` to exercise the defensive clamp in `motions.rs`,
    // which protects non-Normal / host-driven callers.
    let text = "foo,";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(text.len(), text);
    assert_eq!(editor.cursor(), text.len());
    // b from a forced-EOB position lands on `foo` start, not the comma.
    editor.handle_key(Key::char('b'), text);
    assert_eq!(editor.cursor(), 0);
}

#[test]
fn b_from_eob_skips_trailing_punctuation_in_phrase() {
    // `foo bar,` — from a forced-EOB position, b lands on `bar`, not the `,`.
    // (EOB is unreachable via Normal input after #541; we force it to keep
    // coverage of the defensive clamp in `motions.rs`.)
    let text = "foo bar,";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(text.len(), text);
    assert_eq!(editor.cursor(), text.len());
    editor.handle_key(Key::char('b'), text);
    assert_eq!(editor.cursor(), 4, "lands on start of bar, not the comma");
}

#[test]
fn e_at_end_of_buffer_stays_put() {
    // After #541 the Normal-mode cursor can't rest past the last char, so
    // "at end" means on the last char. `e` from there stays put (vim:
    // no further word-end to move to). We force len via `set_cursor` to
    // confirm the clamp defends host-driven positions too.
    let text = "foo";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(text.len(), text);
    editor.handle_key(Key::char('e'), text);
    assert_eq!(editor.cursor(), 2, "clamped onto last 'o', not past it");
}

// --- WORD motions (W/B/E): whitespace-only, the pre-#540 behavior ---

#[test]
fn word_uppercase_w_skips_punctuation_to_next_space_token() {
    // `foo,bar` — W treats the whole run as one WORD, so a single W jumps
    // to the end; clamped onto the last char (#541), not past it.
    let text = "foo,bar";
    assert_eq!(walk(text, "W"), text.len() - 1);
}

#[test]
fn word_uppercase_wbe_on_mixed_text() {
    let text = "foo, bar baz";
    // W: foo, -> bar -> baz
    assert_eq!(walk(text, "W"), 5, "W lands on b of bar");
    assert_eq!(walk(text, "WW"), 9, "W lands on b of baz");
    // B from baz lands on bar, then on foo,
    let mut editor = VimLineEditor::new();
    editor.set_cursor(9, text);
    editor.handle_key(Key::char('B'), text);
    assert_eq!(editor.cursor(), 5, "B lands on bar");
    editor.handle_key(Key::char('B'), text);
    assert_eq!(editor.cursor(), 0, "B lands on foo,");
    // E: from start, E lands on last char of `foo,` (the comma, idx 3).
    assert_eq!(walk(text, "E"), 3, "E lands on comma");
    assert_eq!(walk(text, "EE"), 7, "E lands on r of bar");
}

#[test]
fn word_uppercase_w_from_whitespace_lands_on_next_token() {
    let text = "foo bar";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(3, text); // on the space
    editor.handle_key(Key::char('W'), text);
    assert_eq!(editor.cursor(), 4, "on b of bar");
}

#[test]
fn cw_on_punctuation_only_changes_punct_run() {
    // `foo,bar` — cw from `f` changes only `foo` (cw==ce quirk), stopping
    // before the comma.
    let mut editor = VimLineEditor::new();
    let mut text = String::from("foo,bar");
    editor.handle_key(Key::char('c'), &text);
    let result = editor.handle_key(Key::char('w'), &text);
    assert_eq!(editor.mode(), Mode::Insert);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, ",bar");
}

#[test]
fn c_uppercase_word_behaves_like_c_uppercase_e() {
    // `foo, bar` — cW from `f` changes `foo,` (to end of WORD), mirroring
    // the cw->ce quirk at the WORD level.
    let mut editor = VimLineEditor::new();
    let mut text = String::from("foo, bar");
    editor.handle_key(Key::char('c'), &text);
    let result = editor.handle_key(Key::char('W'), &text);
    assert_eq!(editor.mode(), Mode::Insert);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, " bar");
}

#[test]
fn d_uppercase_w_deletes_through_whitespace() {
    // `foo, bar` — dW from `f` deletes `foo, ` (whole WORD + trailing ws).
    let mut editor = VimLineEditor::new();
    let mut text = String::from("foo, bar");
    editor.handle_key(Key::char('d'), &text);
    let result = editor.handle_key(Key::char('W'), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "bar");
}

#[test]
fn visual_uppercase_w_extends_selection() {
    let mut editor = VimLineEditor::new();
    let text = "foo, bar baz";
    editor.handle_key(Key::char('v'), text);
    editor.handle_key(Key::char('W'), text);
    let sel = editor.selection().unwrap();
    assert_eq!(sel.start, 0);
    assert_eq!(sel.end, 6, "selection spans `foo, ` up to bar");
}

// ----- #541: Normal-mode cursor clamps to the last character ---------
//
// vim's Normal-mode cursor is always ON a character, never past the final
// one. Forward motions that exhaust the buffer (`w`/`e`/`W`/`E`/`l`/Right)
// must snap back onto the last char. Edit-producing commands and Insert
// transitions are intentionally excluded.

#[test]
fn normal_l_at_last_char_does_not_overshoot() {
    let text = "abc";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(2, text); // on 'c'
    editor.handle_key(Key::char('l'), text);
    assert_eq!(editor.cursor(), 2, "l stays clamped on the last char");

    editor.handle_key(Key::code(KeyCode::Right), text);
    assert_eq!(editor.cursor(), 2, "Right stays clamped on the last char");
}

#[test]
fn normal_e_from_last_char_stays_put() {
    let text = "foo";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(2, text); // on last 'o'
    editor.handle_key(Key::char('e'), text);
    assert_eq!(editor.cursor(), 2, "e on last word-end stays on last char");
}

#[test]
fn normal_uppercase_e_at_end_lands_on_last_char() {
    let text = "foo bar";
    // E moves one WORD-end per press: end of `foo` (2), then end of `bar` (6).
    assert_eq!(walk(text, "E"), 2, "E lands on end of first WORD");
    assert_eq!(walk(text, "EE"), 6, "EE lands on end of last WORD");
    // No further WORD: a third E is clamped onto 'r' (6), not past it.
    assert_eq!(walk(text, "EEE"), 6);
}

#[test]
fn normal_w_clamps_on_multibyte_last_char() {
    // Char-boundary safety: last char is 4-byte emoji; clamp must land on
    // the emoji's start byte (index 1), not a mid-codepoint position.
    let text = "a\u{1F600}"; // 'a' (1 byte) + emoji (4 bytes) => len 5
    let mut editor = VimLineEditor::new();
    editor.set_cursor(0, text);
    editor.handle_key(Key::char('w'), text);
    assert_eq!(editor.cursor(), 1, "clamped to start of the emoji");
    assert!(text.is_char_boundary(editor.cursor()));
}

#[test]
fn normal_w_on_empty_buffer_stays_at_zero() {
    let text = "";
    let mut editor = VimLineEditor::new();
    editor.handle_key(Key::char('w'), text);
    assert_eq!(editor.cursor(), 0);
}

#[test]
fn dw_on_last_word_still_deletes_whole_word() {
    // Operator semantics must NOT regress: `dw` on the final word deletes
    // the entire word even though the motion target is clamped for pure
    // cursor movement. The operator uses the unclamped target for its range.
    let mut editor = VimLineEditor::new();
    let mut text = String::from("foo bar");
    // cursor on 'b' of bar (index 4)
    editor.set_cursor(4, &text);
    editor.handle_key(Key::char('d'), &text);
    let result = editor.handle_key(Key::char('w'), &text);
    for edit in result.edits.into_iter().rev() {
        edit.apply(&mut text);
    }
    assert_eq!(text, "foo ", "dw deleted the whole last word 'bar'");
    assert_eq!(editor.mode(), Mode::Normal);
    // cursor clamped onto the trailing space (last char of the *old* text
    // representation at the delete start region).
    assert!(editor.cursor() <= 4);
}

#[test]
fn append_at_last_char_keeps_past_end_for_insert() {
    // `a` at the last char enters Insert with cursor past the end so the
    // host can append — the clamp must NOT affect Insert transitions.
    let text = "abc";
    let mut editor = VimLineEditor::new();
    editor.set_cursor(2, text); // on 'c'
    editor.handle_key(Key::char('a'), text);
    assert_eq!(editor.mode(), Mode::Insert);
    assert_eq!(editor.cursor(), 3, "append sits past the last char");
}

#[test]
fn insert_mode_cursor_can_rest_at_len() {
    // Insert mode legitimately uses cursor == len (e.g. after `A`); the
    // #541 clamp is Normal-mode only.
    let text = "abc";
    let mut editor = VimLineEditor::new();
    editor.handle_key(Key::char('A'), text);
    assert_eq!(editor.mode(), Mode::Insert);
    assert_eq!(editor.cursor(), 3);
    // moving right in Insert at the end stays at len (no clamp here).
    editor.handle_key(Key::code(KeyCode::Right), text);
    assert_eq!(editor.cursor(), 3);
}