1use crate::{normal::repeat, state::Mode, Vim};
2use editor::{scroll::Autoscroll, Bias};
3use gpui::{actions, Action, ViewContext};
4use language::SelectionGoal;
5use workspace::Workspace;
6
7actions!(vim, [NormalBefore]);
8
9pub fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
10 workspace.register_action(normal_before);
11}
12
13fn normal_before(_: &mut Workspace, action: &NormalBefore, cx: &mut ViewContext<Workspace>) {
14 let should_repeat = Vim::update(cx, |vim, cx| {
15 let count = vim.take_count(cx).unwrap_or(1);
16 vim.stop_recording_immediately(action.boxed_clone());
17 if count <= 1 || vim.workspace_state.replaying {
18 vim.update_active_editor(cx, |_, editor, cx| {
19 editor.dismiss_menus_and_popups(cx);
20 editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
21 s.move_cursors_with(|map, mut cursor, _| {
22 *cursor.column_mut() = cursor.column().saturating_sub(1);
23 (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
24 });
25 });
26 });
27 vim.switch_mode(Mode::Normal, false, cx);
28 false
29 } else {
30 true
31 }
32 });
33
34 if should_repeat {
35 repeat::repeat(cx, true)
36 }
37}
38
39#[cfg(test)]
40mod test {
41 use crate::{
42 state::Mode,
43 test::{NeovimBackedTestContext, VimTestContext},
44 };
45
46 #[gpui::test]
47 async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
48 let mut cx = VimTestContext::new(cx, true).await;
49 cx.simulate_keystroke("i");
50 assert_eq!(cx.mode(), Mode::Insert);
51 cx.simulate_keystrokes(["T", "e", "s", "t"]);
52 cx.assert_editor_state("Testˇ");
53 cx.simulate_keystroke("escape");
54 assert_eq!(cx.mode(), Mode::Normal);
55 cx.assert_editor_state("Tesˇt");
56 }
57
58 #[gpui::test]
59 async fn test_insert_with_counts(cx: &mut gpui::TestAppContext) {
60 let mut cx = NeovimBackedTestContext::new(cx).await;
61
62 cx.set_shared_state("ˇhello\n").await;
63 cx.simulate_shared_keystrokes(["5", "i", "-", "escape"])
64 .await;
65 cx.run_until_parked();
66 cx.assert_shared_state("----ˇ-hello\n").await;
67
68 cx.set_shared_state("ˇhello\n").await;
69 cx.simulate_shared_keystrokes(["5", "a", "-", "escape"])
70 .await;
71 cx.run_until_parked();
72 cx.assert_shared_state("h----ˇ-ello\n").await;
73
74 cx.simulate_shared_keystrokes(["4", "shift-i", "-", "escape"])
75 .await;
76 cx.run_until_parked();
77 cx.assert_shared_state("---ˇ-h-----ello\n").await;
78
79 cx.simulate_shared_keystrokes(["3", "shift-a", "-", "escape"])
80 .await;
81 cx.run_until_parked();
82 cx.assert_shared_state("----h-----ello--ˇ-\n").await;
83
84 cx.set_shared_state("ˇhello\n").await;
85 cx.simulate_shared_keystrokes(["3", "o", "o", "i", "escape"])
86 .await;
87 cx.run_until_parked();
88 cx.assert_shared_state("hello\noi\noi\noˇi\n").await;
89
90 cx.set_shared_state("ˇhello\n").await;
91 cx.simulate_shared_keystrokes(["3", "shift-o", "o", "i", "escape"])
92 .await;
93 cx.run_until_parked();
94 cx.assert_shared_state("oi\noi\noˇi\nhello\n").await;
95 }
96
97 #[gpui::test]
98 async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) {
99 let mut cx = NeovimBackedTestContext::new(cx).await;
100
101 cx.set_shared_state("ˇhello\n").await;
102 cx.simulate_shared_keystrokes(["3", "i", "-", "escape"])
103 .await;
104 cx.run_until_parked();
105 cx.assert_shared_state("--ˇ-hello\n").await;
106 cx.simulate_shared_keystrokes(["."]).await;
107 cx.run_until_parked();
108 cx.assert_shared_state("----ˇ--hello\n").await;
109 cx.simulate_shared_keystrokes(["2", "."]).await;
110 cx.run_until_parked();
111 cx.assert_shared_state("-----ˇ---hello\n").await;
112
113 cx.set_shared_state("ˇhello\n").await;
114 cx.simulate_shared_keystrokes(["2", "o", "k", "k", "escape"])
115 .await;
116 cx.run_until_parked();
117 cx.assert_shared_state("hello\nkk\nkˇk\n").await;
118 cx.simulate_shared_keystrokes(["."]).await;
119 cx.run_until_parked();
120 cx.assert_shared_state("hello\nkk\nkk\nkk\nkˇk\n").await;
121 cx.simulate_shared_keystrokes(["1", "."]).await;
122 cx.run_until_parked();
123 cx.assert_shared_state("hello\nkk\nkk\nkk\nkk\nkˇk\n").await;
124 }
125}