hjkl 0.34.1

Vim-modal terminal editor: standalone TUI built on the hjkl engine.
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
use super::*;

// ── Phase 4e: visual-mode operator dispatch via keymap + range-mutation ──────
//
// These tests verify that `d` / `y` / `c` in Visual / VisualLine mode are
// consumed by the app keymap (dispatching `AppAction::VisualOp`) and produce
// the correct buffer / mode state via the range-mutation primitives.

#[test]
fn visual_d_deletes_selection_via_keymap() {
    // Enter Visual, select 5 chars ("hello"), d → " world" remains.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "hello world");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    // Enter Visual mode via engine FSM.
    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('v'));
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::Visual,
        "must be in Visual after v"
    );

    // Extend right 4: cursor on col 4, anchor at col 0 → "hello" selected.
    for _ in 0..4 {
        let consumed = app.route_chord_key(ck('l'));
        assert!(consumed, "l in Visual must be consumed by keymap");
    }

    // Dispatch d via keymap.
    let consumed = app.route_chord_key(ck('d'));
    assert!(
        consumed,
        "d in Visual must be consumed by keymap (VisualOp)"
    );

    // View should have " world" (the chars after the deleted selection).
    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec![" world"],
        "vd must delete selected chars; got {lines:?}"
    );

    // Must have returned to Normal mode.
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::Normal,
        "must exit Visual mode after d"
    );
    assert_window_synced_to_engine(&app);
}

#[test]
fn visual_y_yanks_selection_via_keymap() {
    // Enter Visual, select "hello", y → unnamed register has "hello", buffer unchanged.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "hello world");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('v'));

    // Extend right 4: covers "hello".
    for _ in 0..4 {
        app.route_chord_key(ck('l'));
    }

    // Dispatch y via keymap.
    let consumed = app.route_chord_key(ck('y'));
    assert!(
        consumed,
        "y in Visual must be consumed by keymap (VisualOp)"
    );

    // View must be unchanged.
    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec!["hello world"],
        "vy must not modify the buffer; got {lines:?}"
    );

    // Unnamed register must contain the yanked text.
    let reg = app.active_editor().yank();
    assert!(
        reg.contains("hello"),
        "unnamed register must contain 'hello' after vy; got {reg:?}"
    );

    // Must have returned to Normal mode.
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::Normal,
        "must exit Visual mode after y"
    );
    assert_window_synced_to_engine(&app);
}

#[test]
fn visual_line_d_deletes_line_via_keymap() {
    // Enter VisualLine (V), d → first line deleted.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "first line\nsecond line");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    // Enter VisualLine via engine FSM (Shift-V).
    hjkl_vim_tui::handle_key(
        app.active_editor_mut(),
        KeyEvent::new(KeyCode::Char('V'), KeyModifiers::NONE),
    );
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::VisualLine,
        "must be in VisualLine after V"
    );

    // Dispatch d via keymap.
    let consumed = app.route_chord_key(ck('d'));
    assert!(
        consumed,
        "d in VisualLine must be consumed by keymap (VisualOp)"
    );

    // First line should be gone.
    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec!["second line"],
        "Vd must delete first line; got {lines:?}"
    );

    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::Normal,
        "must exit VisualLine mode after d"
    );
    assert_window_synced_to_engine(&app);
}

#[test]
fn visual_c_enters_insert_mode_via_keymap() {
    // Enter Visual, select "hello", c → Insert mode, selection deleted.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "hello world");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('v'));

    // Extend right 4: covers "hello".
    for _ in 0..4 {
        app.route_chord_key(ck('l'));
    }

    // Dispatch c via keymap.
    let consumed = app.route_chord_key(ck('c'));
    assert!(
        consumed,
        "c in Visual must be consumed by keymap (VisualOp)"
    );

    // Must be in Insert mode.
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::Insert,
        "vc must enter Insert mode; got {:?}",
        app.active_editor().vim_mode()
    );

    // View should have "hello" deleted, leaving " world".
    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec![" world"],
        "vc must delete selected chars; got {lines:?}"
    );

    assert_window_synced_to_engine(&app);
}

#[test]
fn gg_full_sequence_in_normal_mode_via_keymap() {
    // Sanity-check coverage for the previously-working Normal path.
    // Confirms route_chord_key handles Normal mode and that gg still works.
    let mut app = App::new(None, false, None, None).unwrap();
    let lines: Vec<String> = (0..30).map(|i| format!("line{i:02}")).collect();
    seed_buffer(&mut app, &lines.join("\n"));
    app.active_editor_mut().jump_cursor(20, 0);
    app.sync_viewport_from_editor();

    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::Normal,
        "must be in Normal mode"
    );

    use crossterm::event::{KeyCode, KeyEvent as CtKeyEvent, KeyModifiers};
    let g_key = CtKeyEvent::new(KeyCode::Char('g'), KeyModifiers::NONE);

    // First `g` — Normal keymap sets pending_state to AfterG.
    let consumed = app.route_chord_key(g_key);
    assert!(consumed, "first g must be consumed");
    assert!(
        matches!(
            app.pending_state,
            Some(hjkl_vim::PendingState::AfterG { .. })
        ),
        "first g must set pending_state to AfterG; got {:?}",
        app.pending_state
    );

    // Second `g` — reducer commits gg.
    let consumed = app.route_chord_key(g_key);
    assert!(consumed, "second g must be consumed");
    assert!(
        app.pending_state.is_none(),
        "after gg the reducer must clear pending_state"
    );
    assert_eq!(
        app.active_editor().cursor().0,
        0,
        "gg must move engine cursor to row 0 from row 20"
    );
    assert_window_synced_to_engine(&app);
}

// ── Phase 4e follow-up regression tests ──────────────────────────────────────

#[test]
fn visual_d_with_named_register_writes_to_register() {
    // "ad on a visual selection → register 'a' contains the deleted text.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "hello world");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    // "a — set pending register to 'a' via engine FSM.
    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('"'));
    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('a'));
    assert_eq!(
        app.active_editor().pending_register(),
        Some('a'),
        "pending_register must be Some('a') after \"a chord"
    );

    // Enter Visual mode.
    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('v'));
    // Extend right 4 to select "hello".
    for _ in 0..4 {
        app.route_chord_key(ck('l'));
    }

    // d — should use register 'a' from pending_register().
    let consumed = app.route_chord_key(ck('d'));
    assert!(consumed, "d in Visual must be consumed");

    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec![" world"],
        "\"ad must delete selection; got {lines:?}"
    );

    // Named register 'a' must contain the deleted text.
    let reg_a = &app.active_editor().registers().named[0]; // 'a' - 'a' = 0
    assert!(
        reg_a.text.contains("hello"),
        "register 'a' must contain 'hello' after \"ad; got {:?}",
        reg_a.text
    );

    assert_eq!(app.active_editor().vim_mode(), hjkl_engine::VimMode::Normal);
    assert_window_synced_to_engine(&app);
}

#[test]
fn visual_line_d_deletes_single_line_via_range_mutation() {
    // Vd on a single-line VisualLine selection. Previously fell to the engine
    // FSM (run_operator_over_range bailed on top==bot Linewise). With the
    // guard fix it flows through delete_range + MotionKind::Linewise.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "only line\nsecond line");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    // Enter VisualLine.
    hjkl_vim_tui::handle_key(
        app.active_editor_mut(),
        KeyEvent::new(KeyCode::Char('V'), KeyModifiers::NONE),
    );
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::VisualLine
    );

    // d — single-line VisualLine delete via range-mutation primitive.
    let consumed = app.route_chord_key(ck('d'));
    assert!(consumed, "d in VisualLine must be consumed");

    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec!["second line"],
        "Vd on single line must delete it; got {lines:?}"
    );

    assert_eq!(app.active_editor().vim_mode(), hjkl_engine::VimMode::Normal);
    assert_window_synced_to_engine(&app);
}

#[test]
fn visual_block_d_deletes_rectangle_via_range_mutation() {
    // <C-v>lljjd — flows through delete_block primitive. Each affected line
    // has cols 0..=2 removed.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "abcde\nfghij\nklmno");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    // Enter VisualBlock.
    hjkl_vim_tui::handle_key(
        app.active_editor_mut(),
        KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL),
    );
    assert_eq!(
        app.active_editor().vim_mode(),
        hjkl_engine::VimMode::VisualBlock
    );

    // Extend right 2 (cols 0..=2), down 2 (rows 0..=2).
    for _ in 0..2 {
        app.route_chord_key(ck('l'));
    }
    for _ in 0..2 {
        app.route_chord_key(ck('j'));
    }

    let consumed = app.route_chord_key(ck('d'));
    assert!(consumed, "d in VisualBlock must be consumed");

    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec!["de", "ij", "no"],
        "VisualBlock d must remove cols 0..=2 on each row; got {lines:?}"
    );

    assert_eq!(app.active_editor().vim_mode(), hjkl_engine::VimMode::Normal);
    assert_window_synced_to_engine(&app);
}

#[test]
fn visual_block_y_yanks_rectangle_to_register() {
    // <C-v>lj"ay — yank a 2-col block into register 'a'. View unchanged.
    let mut app = App::new(None, false, None, None).unwrap();
    seed_buffer(&mut app, "abcde\nfghij\nklmno");
    app.active_editor_mut().jump_cursor(0, 0);
    app.sync_viewport_from_editor();

    // Set pending register 'a'.
    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('"'));
    hjkl_vim_tui::handle_key(app.active_editor_mut(), ck('a'));

    // Enter VisualBlock.
    hjkl_vim_tui::handle_key(
        app.active_editor_mut(),
        KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL),
    );

    // Extend right 1 (cols 0..=1), down 1 (rows 0..=1).
    app.route_chord_key(ck('l'));
    app.route_chord_key(ck('j'));

    let consumed = app.route_chord_key(ck('y'));
    assert!(consumed, "y in VisualBlock must be consumed");

    // View must be unchanged.
    let lines = app
        .active_editor()
        .buffer()
        .rope()
        .lines()
        .map(|s| {
            let s = s.to_string();
            s.strip_suffix('\n').map(str::to_string).unwrap_or(s)
        })
        .collect::<Vec<_>>();
    assert_eq!(
        lines,
        vec!["abcde", "fghij", "klmno"],
        "VisualBlock y must not modify buffer"
    );

    // Register 'a' must contain the yanked block text.
    let reg_a = &app.active_editor().registers().named[0];
    assert!(
        !reg_a.text.is_empty(),
        "register 'a' must be non-empty after block yank"
    );
    assert!(
        reg_a.text.contains("ab") && reg_a.text.contains("fg"),
        "register 'a' must contain block text 'ab'/'fg'; got {:?}",
        reg_a.text
    );

    assert_eq!(app.active_editor().vim_mode(), hjkl_engine::VimMode::Normal);
    assert_window_synced_to_engine(&app);
}