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(false, 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_keystrokes("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_keystrokes("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").await;
64 cx.run_until_parked();
65 cx.shared_state().await.assert_eq("----ˇ-hello\n");
66
67 cx.set_shared_state("ˇhello\n").await;
68 cx.simulate_shared_keystrokes("5 a - escape").await;
69 cx.run_until_parked();
70 cx.shared_state().await.assert_eq("h----ˇ-ello\n");
71
72 cx.simulate_shared_keystrokes("4 shift-i - escape").await;
73 cx.run_until_parked();
74 cx.shared_state().await.assert_eq("---ˇ-h-----ello\n");
75
76 cx.simulate_shared_keystrokes("3 shift-a - escape").await;
77 cx.run_until_parked();
78 cx.shared_state().await.assert_eq("----h-----ello--ˇ-\n");
79
80 cx.set_shared_state("ˇhello\n").await;
81 cx.simulate_shared_keystrokes("3 o o i escape").await;
82 cx.run_until_parked();
83 cx.shared_state().await.assert_eq("hello\noi\noi\noˇi\n");
84
85 cx.set_shared_state("ˇhello\n").await;
86 cx.simulate_shared_keystrokes("3 shift-o o i escape").await;
87 cx.run_until_parked();
88 cx.shared_state().await.assert_eq("oi\noi\noˇi\nhello\n");
89 }
90
91 #[gpui::test]
92 async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) {
93 let mut cx = NeovimBackedTestContext::new(cx).await;
94
95 cx.set_shared_state("ˇhello\n").await;
96 cx.simulate_shared_keystrokes("3 i - escape").await;
97 cx.run_until_parked();
98 cx.shared_state().await.assert_eq("--ˇ-hello\n");
99 cx.simulate_shared_keystrokes(".").await;
100 cx.run_until_parked();
101 cx.shared_state().await.assert_eq("----ˇ--hello\n");
102 cx.simulate_shared_keystrokes("2 .").await;
103 cx.run_until_parked();
104 cx.shared_state().await.assert_eq("-----ˇ---hello\n");
105
106 cx.set_shared_state("ˇhello\n").await;
107 cx.simulate_shared_keystrokes("2 o k k escape").await;
108 cx.run_until_parked();
109 cx.shared_state().await.assert_eq("hello\nkk\nkˇk\n");
110 cx.simulate_shared_keystrokes(".").await;
111 cx.run_until_parked();
112 cx.shared_state()
113 .await
114 .assert_eq("hello\nkk\nkk\nkk\nkˇk\n");
115 cx.simulate_shared_keystrokes("1 .").await;
116 cx.run_until_parked();
117 cx.shared_state()
118 .await
119 .assert_eq("hello\nkk\nkk\nkk\nkk\nkˇk\n");
120 }
121}