g_prefix.rs

 1use gpui::{action, keymap::Binding, MutableAppContext, ViewContext};
 2use workspace::Workspace;
 3
 4use crate::{mode::Mode, SwitchMode, VimState};
 5
 6action!(MoveToStart);
 7
 8pub fn init(cx: &mut MutableAppContext) {
 9    let context = Some("Editor && vim_mode == normal && vim_submode == g");
10    cx.add_bindings(vec![
11        Binding::new("g", MoveToStart, context),
12        Binding::new("escape", SwitchMode(Mode::normal()), context),
13    ]);
14
15    cx.add_action(move_to_start);
16}
17
18fn move_to_start(_: &mut Workspace, _: &MoveToStart, cx: &mut ViewContext<Workspace>) {
19    VimState::update_global(cx, |state, cx| {
20        state.update_active_editor(cx, |editor, cx| {
21            editor.move_to_beginning(&editor::MoveToBeginning, cx);
22        });
23        state.switch_mode(&SwitchMode(Mode::normal()), cx);
24    })
25}
26
27#[cfg(test)]
28mod test {
29    use indoc::indoc;
30
31    use crate::{
32        mode::{Mode, NormalState},
33        vim_test_context::VimTestContext,
34    };
35
36    #[gpui::test]
37    async fn test_g_prefix_and_abort(cx: &mut gpui::TestAppContext) {
38        let mut cx = VimTestContext::new(cx, true, "").await;
39
40        // Can abort with escape to get back to normal mode
41        cx.simulate_keystroke("g");
42        assert_eq!(cx.mode(), Mode::Normal(NormalState::GPrefix));
43        cx.simulate_keystroke("escape");
44        assert_eq!(cx.mode(), Mode::normal());
45    }
46
47    #[gpui::test]
48    async fn test_move_to_start(cx: &mut gpui::TestAppContext) {
49        let initial_content = indoc! {"
50            The quick
51            
52            brown fox jumps
53            over the lazy dog"};
54        let mut cx = VimTestContext::new(cx, true, initial_content).await;
55
56        // Jump to the end to
57        cx.simulate_keystroke("shift-G");
58        cx.assert_editor_state(indoc! {"
59            The quick
60            
61            brown fox jumps
62            over the lazy do|g"});
63
64        // Jump to the start
65        cx.simulate_keystrokes(&["g", "g"]);
66        cx.assert_editor_state(indoc! {"
67            |The quick
68            
69            brown fox jumps
70            over the lazy dog"});
71        assert_eq!(cx.mode(), Mode::normal());
72
73        // Repeat action doesn't change
74        cx.simulate_keystrokes(&["g", "g"]);
75        cx.assert_editor_state(indoc! {"
76            |The quick
77            
78            brown fox jumps
79            over the lazy dog"});
80        assert_eq!(cx.mode(), Mode::normal());
81    }
82}