hjkl-vim 0.38.0

Vim modal state types and grammar primitives for the hjkl editor stack. Pre-1.0 churn.
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
/// Phase 6.6c integration tests: validate that `hjkl_vim::dispatch_input`
/// correctly routes the search-prompt FSM through `hjkl-vim` rather than
/// the deprecated engine shim.
use hjkl_engine::{Editor, Input, Key};
use hjkl_vim::VimEditorExt;

fn editor_with(content: &str) -> Editor {
    let opts = hjkl_engine::Options::default();
    let mut e = hjkl_vim::vim_editor(
        hjkl_buffer::View::new(),
        hjkl_engine::DefaultHost::new(),
        opts,
    );
    e.set_content(content);
    e
}

fn inp(key: Key) -> Input {
    Input {
        key,
        ctrl: false,
        alt: false,
        shift: false,
    }
}

fn ctrl(key: Key) -> Input {
    Input {
        key,
        ctrl: true,
        alt: false,
        shift: false,
    }
}

/// Run a string of keys through `dispatch_input` (not the deprecated shim).
/// Supports the same `<tag>` notation the engine tests use.
fn dispatch_keys(e: &mut Editor, keys: &str) {
    let mut iter = keys.chars();
    while let Some(c) = iter.next() {
        if c == '<' {
            let mut tag = String::new();
            for ch in iter.by_ref() {
                if ch == '>' {
                    break;
                }
                tag.push(ch);
            }
            let input = match tag.as_str() {
                "Esc" => inp(Key::Esc),
                "CR" => inp(Key::Enter),
                "BS" => inp(Key::Backspace),
                "Up" => inp(Key::Up),
                "Down" => inp(Key::Down),
                s if s.starts_with("C-") => {
                    let ch = s.chars().nth(2).unwrap();
                    ctrl(Key::Char(ch))
                }
                _ => continue,
            };
            hjkl_vim::dispatch_input(e, input);
        } else {
            hjkl_vim::dispatch_input(e, inp(Key::Char(c)));
        }
    }
}

// ── insert-mode dispatch tests (Phase 6.6d) ───────────────────────────────────

#[test]
fn insert_char_appends_to_buffer() {
    // Enter insert mode via the public API, then dispatch a Char key through
    // `dispatch_input`. The buffer should contain the typed character.
    let mut e = editor_with("");
    e.enter_insert_i(1);
    hjkl_vim::dispatch_input(&mut e, inp(Key::Char('x')));
    hjkl_vim::dispatch_input(&mut e, inp(Key::Esc));
    // View always has a trailing newline.
    assert!(
        e.content().starts_with('x'),
        "dispatch_input should type 'x' in insert mode; got: {:?}",
        e.content()
    );
}

#[test]
fn insert_mode_esc_returns_to_normal() {
    use hjkl_engine::VimMode;
    let mut e = editor_with("hello");
    e.enter_insert_i(1);
    assert_eq!(e.vim_mode(), VimMode::Insert);
    hjkl_vim::dispatch_input(&mut e, inp(Key::Esc));
    assert_eq!(
        e.vim_mode(),
        VimMode::Normal,
        "Esc via dispatch_input should exit insert mode"
    );
}

#[test]
fn insert_backspace_deletes_char() {
    let mut e = editor_with("");
    e.enter_insert_i(1);
    hjkl_vim::dispatch_input(&mut e, inp(Key::Char('a')));
    hjkl_vim::dispatch_input(&mut e, inp(Key::Char('b')));
    hjkl_vim::dispatch_input(&mut e, inp(Key::Backspace));
    hjkl_vim::dispatch_input(&mut e, inp(Key::Esc));
    // View content starts with 'a'; trailing newline is expected.
    assert!(
        e.content().starts_with('a') && !e.content().starts_with("ab"),
        "Backspace via dispatch_input should delete last char; got: {:?}",
        e.content()
    );
}

#[test]
fn insert_ctrl_r_pastes_register() {
    // Write "hi" into the 'z' named register directly, then paste via Ctrl-R z.
    let mut e = editor_with("");
    e.with_registers_mut(|r| r.record_yank("hi".to_string(), false, Some('z')));
    e.enter_insert_i(1);
    // Ctrl-R arms the register wait, then 'z' pastes.
    hjkl_vim::dispatch_input(&mut e, ctrl(Key::Char('r')));
    hjkl_vim::dispatch_input(&mut e, inp(Key::Char('z')));
    hjkl_vim::dispatch_input(&mut e, inp(Key::Esc));
    let content = e.content();
    assert!(
        content.contains("hi"),
        "Ctrl-R z should paste register contents; got: {content:?}"
    );
}

// ── search-prompt dispatch tests ──────────────────────────────────────────────

#[test]
fn search_forward_commit_moves_cursor() {
    // View: "alpha beta" — cursor at col 0.
    // `/beta<CR>` should advance the cursor to col 6 (start of "beta").
    let mut e = editor_with("alpha beta");
    dispatch_keys(&mut e, "/beta<CR>");
    assert_eq!(e.cursor(), (0, 6), "cursor should land on 'beta'");
}

#[test]
fn search_commit_no_match_does_not_push_jump_via_dispatch() {
    // A search for a pattern that doesn't exist should leave the jumplist
    // unchanged — same invariant as the engine's own test, but exercised
    // through `dispatch_input`.
    let mut e = editor_with("alpha beta\nfoo end");
    e.jump_cursor(0, 3);
    let pre_len = e.jump_back_list().len();
    dispatch_keys(&mut e, "/zzznotfound<CR>");
    assert_eq!(
        e.jump_back_list().len(),
        pre_len,
        "no match → jumplist should not grow"
    );
}

#[test]
fn search_esc_cancels_without_moving_cursor() {
    let mut e = editor_with("alpha beta");
    let pre = e.cursor();
    dispatch_keys(&mut e, "/beta<Esc>");
    assert_eq!(e.cursor(), pre, "Esc should not move the cursor");
}

#[test]
fn search_backspace_trims_pattern() {
    // Open `/`, type "beta", backspace once → pattern is "bet",
    // then Enter — "bet" matches at col 6.
    let mut e = editor_with("alpha beta");
    dispatch_keys(&mut e, "/beta<BS><CR>");
    // "bet" still matches start of "beta" at col 6.
    assert_eq!(e.cursor(), (0, 6));
}

// ── vim-sneak FSM tests ───────────────────────────────────────────────────────

/// `sba` from [0,0] on "foo bar baz qux\n" → cursor [0,4] (start of "ba" in "bar").
#[test]
fn sneak_forward_fsm_jumps_to_digraph() {
    let mut e = editor_with("foo bar baz qux");
    dispatch_keys(&mut e, "sba");
    assert_eq!(e.cursor(), (0, 4), "s+ba should land on 'ba' in 'bar'");
}

/// `Sba` from [0,12] → cursor [0,8] (backward to "baz").
#[test]
fn sneak_backward_fsm_s_uppercase() {
    let mut e = editor_with("foo bar baz qux");
    e.jump_cursor(0, 12);
    dispatch_keys(&mut e, "Sba");
    assert_eq!(e.cursor(), (0, 8), "S+ba backward should land on 'baz'");
}

/// After `sba`, `;` should repeat forward (sneak-repeat, not f-repeat).
#[test]
fn sneak_fsm_semicolon_repeats_forward() {
    let mut e = editor_with("foo bar baz qux");
    dispatch_keys(&mut e, "sba");
    assert_eq!(e.cursor(), (0, 4));
    dispatch_keys(&mut e, ";");
    assert_eq!(
        e.cursor(),
        (0, 8),
        "semicolon after sneak should jump to next 'ba'"
    );
}

/// After `sba` from [0,0], `,` (reverse) — no prior "ba" → stays at [0,4].
#[test]
fn sneak_fsm_comma_reverse_no_prior_match() {
    let mut e = editor_with("foo bar baz qux");
    dispatch_keys(&mut e, "sba");
    assert_eq!(e.cursor(), (0, 4));
    let pre = e.cursor();
    dispatch_keys(&mut e, ",");
    assert_eq!(e.cursor(), pre, "comma with no prior 'ba' should not move");
}

/// `dsab` on "hello ab world" from [0,0] → "ab world".
#[test]
fn sneak_fsm_operator_pending_delete() {
    let mut e = editor_with("hello ab world");
    dispatch_keys(&mut e, "dsab");
    let content = e.content();
    assert!(
        content.starts_with("ab world"),
        "dsab should delete up to 'ab' leaving 'ab world'; got: {content:?}"
    );
}

/// `sneak_disabled_falls_through_to_substitute_char`:
/// `:set nomotion_sneak` (via settings_mut) then `sx<Esc>` → substitute char.
#[test]
fn sneak_disabled_falls_through_to_substitute_char() {
    let mut e = editor_with("foo");
    e.settings_mut().motion_sneak = false;
    // `s` with sneak disabled should substitute char (enter insert, delete 'f', type 'x').
    dispatch_keys(&mut e, "sx<Esc>");
    // View starts with 'x' (substitute-char path was taken).
    let content = e.content();
    assert!(
        content.starts_with('x'),
        "with motion_sneak=false, s should substitute char; got: {content:?}"
    );
    // Cursor should be at col 0 after Esc.
    assert_eq!(e.cursor().1, 0, "cursor should be col 0 after s+char+Esc");
}

// ── count-threading regression tests ──────────────────────────────────────────

/// Completing a sneak jump must not leave a stale count in the editor: after
/// `sba`, `0` is the LineStart motion, not a count digit.
#[test]
fn sneak_does_not_leak_count_into_next_command() {
    let mut e = editor_with("foo bar baz qux");
    dispatch_keys(&mut e, "sba");
    assert_eq!(e.cursor(), (0, 4), "s+ba should land on 'ba' in 'bar'");
    assert_eq!(e.count(), 0, "sneak must not leave a stale count behind");
    dispatch_keys(&mut e, "0");
    assert_eq!(e.cursor(), (0, 0), "0 after a sneak must be LineStart");
}

/// Cancelling `f` with Esc must drop the stashed count: `3f<Esc>x` deletes
/// one char, not three.
#[test]
fn cancelled_find_drops_count() {
    let mut e = editor_with("abcdef");
    dispatch_keys(&mut e, "3f<Esc>x");
    assert!(
        e.content().starts_with("bcdef"),
        "3f<Esc> must discard the count; got: {:?}",
        e.content()
    );
}

/// Cancelling `r` with Esc must drop the stashed count: `3r<Esc>x` deletes
/// one char, not three.
#[test]
fn cancelled_replace_drops_count() {
    let mut e = editor_with("abcdef");
    dispatch_keys(&mut e, "3r<Esc>x");
    assert!(
        e.content().starts_with("bcdef"),
        "3r<Esc> must discard the count; got: {:?}",
        e.content()
    );
}

// Note: the pathological `count1 * count2` saturation is covered by the
// `op_total_count_saturates_instead_of_overflowing` unit test in
// `pending.rs`. It is NOT exercised end-to-end here because feeding a
// `usize::MAX` count into the real engine makes the engine's operator-apply
// loop iterate that many times (a separate, engine-side unbounded-work
// concern outside this crate's slice).

/// `@1` plays register 1 — a digit after `@` names a register, it is not a
/// count prefix (mirrors `q1` / `"1`).
#[test]
fn at_digit_plays_numbered_register() {
    let mut e = editor_with("ab");
    // Register `"1` is the head of the delete ring. Seed it with a line-sized
    // delete — small (sub-line) deletes go to `"-`, not the numbered ring.
    e.with_registers_mut(|r| r.record_delete("x".to_string(), true, None));
    dispatch_keys(&mut e, "@1");
    assert!(
        e.content().starts_with('b'),
        "@1 should play the `x` macro in register 1; got: {:?}",
        e.content()
    );
}

/// A self-recursive macro (register `a` containing `@a`) must terminate at
/// the replay-depth cap instead of overflowing the stack.
#[test]
fn recursive_macro_terminates() {
    let mut e = editor_with("hello");
    e.with_registers_mut(|r| r.record_yank("@a".to_string(), false, Some('a')));
    dispatch_keys(&mut e, "@a");
    // Reaching here (no stack overflow) is the regression assertion.
}

/// A huge numeric search offset (`/pat/e+N`) must saturate instead of
/// overflowing isize arithmetic (panic in debug builds).
#[test]
fn search_offset_huge_value_does_not_panic() {
    let mut e = editor_with("abx");
    dispatch_keys(&mut e, "/x/e+9223372036854775807<CR>");
    assert_eq!(e.cursor().0, 0, "cursor stays on the matched row");
    let mut e2 = editor_with("foo\nbar x baz");
    e2.jump_cursor(1, 0);
    dispatch_keys(&mut e2, "/x/-9223372036854775808<CR>");
    // Reaching here without a panic is the regression assertion.
}

/// `esc_exits_blame_view`: BLAME is an FSM-owned read-only view; Esc in Normal
/// leaves it (the host no longer intercepts the key).
#[test]
fn esc_exits_blame_view() {
    let mut e = editor_with("hello\nworld");
    e.enter_blame();
    assert!(e.is_blame());
    dispatch_keys(&mut e, "<Esc>");
    assert!(!e.is_blame(), "Esc must exit BLAME via the FSM");
}

/// `mode_entry_key_exits_blame_view`: pressing `v` (or any mode-entry key) in
/// BLAME drops the overlay and enters that mode, all inside the FSM.
#[test]
fn mode_entry_key_exits_blame_view() {
    let mut e = editor_with("hello\nworld");
    e.enter_blame();
    dispatch_keys(&mut e, "v");
    assert!(!e.is_blame());
    assert_eq!(e.vim_mode(), hjkl_engine::VimMode::Visual);
}

// ── dot-repeat count override (audit A2) ──────────────────────────────────

/// `3ihello<Esc>` then `5.` — an explicit `[count]` before `.` must
/// *override* the count recorded on the insert-mode change (`:h .`), not be
/// ignored in favour of the recorded count. `LastChange::InsertAt`'s replay
/// loop used to iterate on the raw recorded `count`, skipping the same
/// `scaled(...)` override every other `LastChange` arm applies.
///
/// Each "hello" contributes exactly one 'h', so counting 'h' characters in
/// the result is a splice-position-independent way to count insertions
/// (avoids asserting the exact string, which depends on where the cursor
/// sits after `Esc`).
#[test]
fn dot_repeat_count_override_applies_to_insert_mode_change() {
    let mut e = editor_with("");
    dispatch_keys(&mut e, "3ihello<Esc>");
    let after_insert = e.content();
    assert_eq!(
        after_insert.matches('h').count(),
        3,
        "3ihello<Esc> must insert 'hello' 3 times; got {after_insert:?}"
    );

    dispatch_keys(&mut e, "5.");
    let after_repeat = e.content();
    assert_eq!(
        after_repeat.matches('h').count(),
        8,
        "5. must override the recorded count 3 with 5 (3 + 5 = 8 total 'hello' \
         insertions), got {after_repeat:?}"
    );
}

/// `2iX<Esc>` then `.` with NO explicit count must reuse the recorded count
/// of 2 (regression guard: the override must default to the recorded count,
/// not to 1 or 0, when the user types no count before `.`).
#[test]
fn dot_repeat_without_count_reuses_recorded_count() {
    let mut e = editor_with("");
    dispatch_keys(&mut e, "2iX<Esc>");
    assert!(
        e.content().starts_with("XX"),
        "2iX<Esc> must insert 'X' twice; got {:?}",
        e.content()
    );

    dispatch_keys(&mut e, ".");
    assert!(
        e.content().starts_with("XXXX"),
        "bare . must reuse the recorded count 2 (2 + 2 = 4 'X's); got {:?}",
        e.content()
    );
}

// ── VisualBlock `A` / `I` / `c` edge-column resolution (audit-r2) ──────────

fn lines_of(e: &Editor) -> Vec<String> {
    e.buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect()
}

#[test]
fn block_append_pads_rows_shorter_than_the_top_row_to_the_block_edge() {
    // Fix 1: block `A`'s append column used to be clamped by the TOP row's
    // length alone, so on rows LONGER than the top row the typed text
    // landed inside the block instead of past its right edge. vim `v_b_A`
    // (`:h v_b_A`) pads every row shorter than the block's right edge to
    // reach it, then appends there — verified against `nvim --headless`.
    let mut e = editor_with("ab\nabcdef");
    e.jump_cursor(1, 5);
    dispatch_keys(&mut e, "<C-v>k");
    dispatch_keys(&mut e, "A");
    dispatch_keys(&mut e, "X<Esc>");
    assert_eq!(
        lines_of(&e),
        &["ab    X".to_string(), "abcdefX".to_string()]
    );
}

#[test]
fn block_highlight_delete_bridge_honors_ragged_flag() {
    // Regression: the REAL app never calls `apply_visual_operator` for
    // VisualBlock `d`/`y`/`c` — its keymap intercepts those keys with
    // `AppAction::VisualOp`, which reads `block_highlight()` (a static
    // (top,bot,left,right) snapshot) and calls `delete_block` /
    // `yank_block` / `change_block` (see apps/hjkl/src/app/engine_
    // actions.rs). Those bridges must still resolve a ragged (`$`) block
    // per row rather than reusing the snapshotted `right_col` — this is
    // the exact call shape `engine_actions.rs` uses.
    let mut e = editor_with("short\nmuchlongerline");
    dispatch_keys(&mut e, "l<C-v>$j");
    let (top, bot, left, right) = e.block_highlight().expect("in VisualBlock mode");
    e.delete_block(top, bot, left, right, '"');
    assert_eq!(lines_of(&e), &["s".to_string(), "m".to_string()]);
}

#[test]
fn block_dollar_delete_removes_to_each_rows_own_eol() {
    // Fix 3: `$` in VisualBlock makes the block ragged (`:h v_b_$`) — every
    // row deletes to ITS OWN EOL, not a fixed-width rectangle capped by
    // whichever row the cursor was on when `$` was pressed. Verified
    // against `nvim --headless`.
    let mut e = editor_with("short\nmuchlongerline");
    dispatch_keys(&mut e, "l<C-v>$jd");
    assert_eq!(lines_of(&e), &["s".to_string(), "m".to_string()]);
}

#[test]
fn block_insert_skips_rows_shorter_than_the_block_column() {
    // Fix 2: block `I` used to pad rows shorter than the block's left
    // column (same padding `A` uses). vim `v_b_I` (`:h v_b_I`) SKIPS those
    // rows entirely instead — no padding, no insert — verified against
    // `nvim --headless`.
    let mut e = editor_with("aaaa\nx\nbbbb");
    e.jump_cursor(0, 2);
    dispatch_keys(&mut e, "<C-v>jj");
    dispatch_keys(&mut e, "I");
    dispatch_keys(&mut e, "Z<Esc>");
    assert_eq!(
        lines_of(&e),
        &["aaZaa".to_string(), "x".to_string(), "bbZbb".to_string()]
    );
}

/// B1: an insert-mode ctrl-key combo with no dedicated binding (e.g.
/// `<C-b>`) must be a no-op, NOT insert the literal letter (the pre-fix
/// bug — `<C-a>` used to insert "a"). This intentionally diverges from
/// real nvim, which inserts the raw control byte for most unbound ctrl
/// keys (verified: `<C-b>` in nvim inserts a literal ^B) — logged in
/// DIVERGE.md since reproducing an unprintable control byte in the buffer
/// has no user-facing benefit and no-op is the safer choice.
#[test]
fn insert_unhandled_ctrl_key_is_noop_not_literal_letter() {
    let mut e = editor_with("hello\n");
    dispatch_keys(&mut e, "i<C-b>x<Esc>");
    assert_eq!(
        lines_of(&e),
        &["xhello".to_string(), String::new()],
        "unhandled ctrl key must not insert its letter literally"
    );
}

// ── macro recorder literal q (audit B1) ─────────────────────────────────

/// Recording must not drop literal `q` keys — the recorder's
/// `input.key != Key::Char('q')` clause was filtering every `q`,
/// including insert-mode text (`iquick<Esc>` recorded as `iuick<Esc>`)
/// and pending-operator targets (`fq` recorded as `f`).
#[test]
fn macro_records_literal_q_in_insert_mode() {
    let mut e = editor_with("\n");
    dispatch_keys(&mut e, "qaiquick<Esc>q");
    // Register "a must hold the encoded recording.
    let text = e
        .with_registers(|r| r.read('a').map(|slot| slot.text.clone()))
        .expect("recording must populate register a");
    assert_eq!(
        text, "iquick<Esc>",
        "insert-mode literal q must survive recording; got {text:?}"
    );
    // Replay on a fresh empty buffer — copy the register to a new editor
    // so the replay has the macro text available.
    let mut e2 = editor_with("\n");
    e2.with_registers_mut(|r| r.record_yank(text, false, Some('a')));
    dispatch_keys(&mut e2, "@a");
    assert_eq!(
        e2.content().trim_end_matches('\n'),
        "quick",
        "replay of register a must produce 'quick'; got {:?}",
        e2.content()
    );
}

/// When `f`/`t`/`F`/`T` targets the letter `q`, that target must be
/// recorded so the macro replays the correct search character instead of
/// desynchronising by reusing the next recorded key.
#[test]
fn macro_records_q_as_pending_target() {
    // `q` must sit off col 0 so `fq` has somewhere to move to — with the
    // cursor already on the `q`, a no-op `fq` would satisfy the cursor
    // assertion below vacuously.
    let mut e = editor_with("xq\n");
    dispatch_keys(&mut e, "qb0fqq");
    let text = e
        .with_registers(|r| r.read('b').map(|slot| slot.text.clone()))
        .expect("recording must populate register b");
    assert_eq!(
        text, "0fq",
        "pending-target q must survive recording; got {text:?}"
    );
    // The cursor should have landed on 'q' during recording.
    assert_eq!(
        e.cursor(),
        (0, 1),
        "fq must land on the q at col 1 while recording; got {:?}",
        e.cursor()
    );
}

// ── J join no-space exceptions (audit B8) ──────────────────────────────

/// `J` with a first line ending in whitespace must not insert an extra
/// space (vim `:h J` exception #1).
#[test]
fn join_trailing_whitespace_no_extra_space() {
    let mut e = editor_with("hello \nworld");
    dispatch_keys(&mut e, "J");
    assert_eq!(e.content(), "hello world\n", "trailing space on first line");
}

/// `J` with a first line ending in tab must not insert an extra space.
#[test]
fn join_trailing_tab_no_extra_space() {
    let mut e = editor_with("hello\t\nworld");
    dispatch_keys(&mut e, "J");
    assert_eq!(e.content(), "hello\tworld\n", "trailing tab on first line");
}

/// `J` with a second line starting with `)` must not insert a space
/// (vim `:h J` exception #2).
#[test]
fn join_leading_paren_no_space() {
    let mut e = editor_with("hello\n)world");
    dispatch_keys(&mut e, "J");
    assert_eq!(e.content(), "hello)world\n", "leading ) on second line");
}

/// `J` with both lines non-empty and no exception conditions inserts a
/// single space (the baseline behaviour must remain).
#[test]
fn join_plain_inserts_space() {
    let mut e = editor_with("hello\nworld");
    dispatch_keys(&mut e, "J");
    assert_eq!(e.content(), "hello world\n");
}