返回 CodeWhale
vim_mode.rs
根目录 / crates / tui / src / tui / vim_mode.rs
1 //! Composer vim Normal-mode keybindings.
2
3 use crate::tui::app::App;
4
5 /// Handle a plain character key press when the composer is in vim Normal mode.
6 ///
7 /// Implements the core set of normal-mode bindings:
8 /// - `h` / `l` — left / right by character
9 /// - `j` / `k` — down / up by logical line (falls back to prev/next history)
10 /// - `w` / `b` — word forward / backward
11 /// - `0` / `$` — line start / end
12 /// - `x` — delete character under cursor
13 /// - `d` (×2) — delete current line (`dd`)
14 /// - `i` — enter Insert before cursor
15 /// - `a` — enter Insert after cursor
16 /// - `o` — open new line below and enter Insert
17 /// - `G` — move to end of buffer
18 pub(super) fn handle_vim_normal_key(app: &mut App, c: char) {
19 // Handle pending `d` (waiting for second `d` to complete `dd`).
20 if app.composer.vim_pending_d {
21 app.composer.vim_pending_d = false;
22 if c == 'd' {
23 app.vim_delete_line();
24 }
25 // Any other key cancels the pending operator.
26 return;
27 }
28
29 match c {
30 'h' => app.move_cursor_left(),
31 'l' => app.move_cursor_right(),
32 'j' => app.vim_move_down(),
33 'k' => app.vim_move_up(),
34 'w' => app.vim_move_word_forward(),
35 'b' => app.vim_move_word_backward(),
36 '0' => app.vim_move_line_start(),
37 '$' => app.vim_move_line_end(),
38 'x' => app.vim_delete_char_under_cursor(),
39 'd' => {
40 // Start the `dd` operator sequence.
41 app.composer.vim_pending_d = true;
42 }
43 'i' => app.vim_enter_insert(),
44 'a' => app.vim_enter_append(),
45 'o' => app.vim_open_line_below(),
46 'G' => app.move_cursor_end(),
47 _ => {
48 // Unknown normal-mode key — silently ignored in Normal mode.
49 }
50 }
51 }
52
52 lines RUST