1
//! The vim preset on the command line: modes, motions and the SSH Esc coalescing.
2

            
3
use super::tests::{alt, ctrl, focused_text, key, press, type_text};
4
use super::{Keys, PRESETS, VimState};
5
use crate::app::App;
6
use crate::event::{Intent, apply};
7
use crate::form::Form;
8
use crate::modal::Modal;
9
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
10

            
11
31
fn vim_cmdline(text: &str) -> App {
12
31
    let mut app = crate::test_support::vim_app();
13
31
    app.open_command_line();
14
31
    type_text(&mut app, text);
15
31
    app
16
31
}
17

            
18
26
fn state(app: &App) -> (String, usize, VimState) {
19
26
    let editor = &app.cmdline.editor;
20
26
    (editor.buffer().to_string(), editor.cursor(), app.keys.vim())
21
26
}
22

            
23
23
fn normal(text: &str, cursor_back: usize) -> App {
24
23
    let mut app = vim_cmdline(text);
25
23
    press(&mut app, key(KeyCode::Esc));
26
76
    (0..cursor_back).for_each(|_| press(&mut app, key(KeyCode::Char('h'))));
27
23
    app
28
23
}
29

            
30
#[test]
31
1
fn the_presets_are_emacs_and_vim() {
32
1
    assert!(
33
1
        PRESETS
34
1
            .iter()
35
3
            .all(|preset| Keys::preset(preset.name).is_ok())
36
    );
37
1
    let err = Keys::preset("nano").err();
38
1
    assert_eq!(
39
1
        err.as_deref(),
40
1
        Some("unknown keymap `nano`; known: cua, emacs, vim")
41
    );
42
1
}
43

            
44
#[test]
45
1
fn opening_the_command_line_starts_in_insert_and_esc_goes_normal() {
46
1
    let mut app = crate::test_support::vim_app();
47
1
    app.open_command_line();
48
1
    assert_eq!(app.keys.vim(), VimState::Insert);
49
1
    type_text(&mut app, "ab");
50
1
    assert_eq!(app.cmdline.editor.buffer(), "ab");
51
1
    press(&mut app, key(KeyCode::Esc));
52
1
    assert_eq!(app.keys.vim(), VimState::Normal);
53
1
    assert!(app.cmdline.active);
54
1
    press(&mut app, key(KeyCode::Esc));
55
1
    assert!(!app.cmdline.active);
56
1
}
57

            
58
#[test]
59
1
fn normal_mode_does_not_type() {
60
1
    let mut app = normal("ab", 0);
61
1
    press(&mut app, key(KeyCode::Char('z')));
62
1
    assert_eq!(state(&app), ("ab".to_string(), 2, VimState::Normal));
63
1
}
64

            
65
#[test]
66
1
fn the_insert_keys_move_the_cursor_and_start_typing() {
67
1
    let cases = [('i', 1), ('a', 2), ('I', 0), ('A', 2)];
68
1
    for (letter, cursor) in cases {
69
4
        let mut app = normal("ab", 1);
70
4
        press(&mut app, key(KeyCode::Char(letter)));
71
4
        assert_eq!(app.keys.vim(), VimState::Insert, "{letter}");
72
4
        assert_eq!(app.cmdline.editor.cursor(), cursor, "{letter}");
73
4
        press(&mut app, key(KeyCode::Char('x')));
74
4
        assert_eq!(app.cmdline.editor.buffer().len(), 3, "{letter}");
75
    }
76
1
}
77

            
78
#[test]
79
1
fn normal_keys_equal_the_plain_intents() {
80
1
    let pairs = [
81
1
        ('h', Intent::MoveLeft),
82
1
        ('l', Intent::MoveRight),
83
1
        ('0', Intent::MoveHome),
84
1
        ('$', Intent::MoveEnd),
85
1
        ('w', Intent::MoveWordForward),
86
1
        ('b', Intent::MoveWordBackward),
87
1
        ('x', Intent::DeleteForward),
88
1
        ('D', Intent::DeleteWordForward),
89
1
        ('B', Intent::KillWordBackward),
90
1
    ];
91
1
    for (letter, intent) in pairs {
92
9
        let mut by_key = normal("one two three", 4);
93
9
        press(&mut by_key, key(KeyCode::Char(letter)));
94
9
        let mut by_intent = normal("one two three", 4);
95
9
        apply(&mut by_intent, intent);
96
9
        assert_eq!(state(&by_key), state(&by_intent), "{letter}");
97
    }
98
1
}
99

            
100
#[test]
101
1
fn alt_letter_is_esc_then_the_letter() {
102
1
    let mut split = vim_cmdline("ab");
103
1
    press(&mut split, alt(KeyCode::Char('h')));
104
1
    let mut typed = vim_cmdline("ab");
105
1
    press(&mut typed, key(KeyCode::Esc));
106
1
    press(&mut typed, key(KeyCode::Char('h')));
107
1
    assert_eq!(state(&split), state(&typed));
108
1
    assert_eq!(state(&split), ("ab".to_string(), 1, VimState::Normal));
109
1
}
110

            
111
#[test]
112
1
fn alt_letter_stays_unbound_under_emacs() {
113
1
    let mut app = crate::test_support::app();
114
1
    app.open_command_line();
115
1
    type_text(&mut app, "ab");
116
1
    press(&mut app, alt(KeyCode::Char('h')));
117
1
    assert!(app.cmdline.active);
118
1
    assert_eq!(app.cmdline.editor.buffer(), "ab");
119
1
}
120

            
121
#[test]
122
1
fn the_vim_state_does_not_touch_the_emacs_preset() {
123
1
    let mut app = crate::test_support::emacs_app();
124
1
    app.keys.set_vim(VimState::Normal);
125
1
    app.open_command_line();
126
1
    assert_eq!(app.keys.vim(), VimState::Off);
127
1
    type_text(&mut app, "ab");
128
1
    press(&mut app, key(KeyCode::Esc));
129
1
    assert!(!app.cmdline.active);
130
1
    assert_eq!(app.keys.indicator(), "emacs");
131
1
}
132

            
133
#[test]
134
1
fn vim_leaves_the_view_to_emacs_bindings() {
135
1
    let mut app = crate::test_support::vim_app();
136
1
    press(&mut app, ctrl(KeyCode::Char('v')));
137
1
    assert!(!app.should_quit);
138
1
    app.close_all_windows();
139
1
    press(&mut app, key(KeyCode::Char('q')));
140
1
    press(&mut app, key(KeyCode::Char('y')));
141
1
    assert!(app.should_quit);
142
1
}
143

            
144
#[test]
145
1
fn vim_leaves_forms_to_emacs_bindings() {
146
1
    let mut app = crate::test_support::vim_app();
147
1
    app.overlays.push(Modal::Form(Form::config_set()));
148
1
    type_text(&mut app, "q1");
149
1
    assert_eq!(focused_text(&app), "q1");
150
1
    press(&mut app, key(KeyCode::Esc));
151
1
    assert_eq!(app.keys.vim(), VimState::Insert);
152
1
}
153

            
154
2
fn coalesced_equals_split(typed: KeyEvent, code: KeyCode) {
155
2
    let mut coalesced = vim_cmdline("ab");
156
2
    press(&mut coalesced, typed);
157
2
    let mut split = vim_cmdline("ab");
158
2
    press(&mut split, key(KeyCode::Esc));
159
2
    press(&mut split, key(code));
160
2
    assert_eq!(state(&coalesced), state(&split), "{typed:?}");
161
2
    assert_eq!(coalesced.cmdline.active, split.cmdline.active, "{typed:?}");
162
2
}
163

            
164
#[test]
165
1
fn alt_shift_letter_is_esc_then_the_capital() {
166
1
    let typed = KeyEvent::new(KeyCode::Char('A'), KeyModifiers::ALT | KeyModifiers::SHIFT);
167
1
    coalesced_equals_split(typed, KeyCode::Char('A'));
168
1
    let mut app = vim_cmdline("ab");
169
1
    press(&mut app, typed);
170
1
    assert_eq!(app.keys.vim(), VimState::Insert);
171
1
}
172

            
173
#[test]
174
1
fn alt_enter_is_esc_then_enter() {
175
1
    coalesced_equals_split(alt(KeyCode::Enter), KeyCode::Enter);
176
1
    let mut app = vim_cmdline("ab");
177
1
    press(&mut app, alt(KeyCode::Enter));
178
1
    assert!(!app.cmdline.active);
179
1
}
180

            
181
#[test]
182
1
fn a_chord_prefix_starting_with_alt_is_not_split() {
183
1
    let text = "vim-insert: M-x y = enter-normal-mode\ncmdline: Esc = close";
184
1
    let mut app = crate::test_support::vim_app();
185
1
    app.keys = match Keys::layered("test", VimState::Insert, &[("test.keys", text)]) {
186
1
        Ok(keys) => keys,
187
        Err(err) => panic!("should parse: {err}"),
188
    };
189
1
    app.open_command_line();
190
1
    press(&mut app, alt(KeyCode::Char('x')));
191
1
    assert!(app.cmdline.active);
192
1
    press(&mut app, key(KeyCode::Char('y')));
193
1
    assert_eq!(app.keys.vim(), VimState::Normal);
194
1
}