1use crate::{normal::repeat, state::Mode, Vim};
2use editor::{scroll::autoscroll::Autoscroll, Bias};
3use gpui::{actions, Action, AppContext, ViewContext};
4use language::SelectionGoal;
5use workspace::Workspace;
6
7actions!(vim, [NormalBefore]);
8
9pub fn init(cx: &mut AppContext) {
10 cx.add_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.cancel(&Default::default(), 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 std::sync::Arc;
42
43 use gpui::executor::Deterministic;
44
45 use crate::{
46 state::Mode,
47 test::{NeovimBackedTestContext, VimTestContext},
48 };
49
50 #[gpui::test]
51 async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
52 let mut cx = VimTestContext::new(cx, true).await;
53 cx.simulate_keystroke("i");
54 assert_eq!(cx.mode(), Mode::Insert);
55 cx.simulate_keystrokes(["T", "e", "s", "t"]);
56 cx.assert_editor_state("Testˇ");
57 cx.simulate_keystroke("escape");
58 assert_eq!(cx.mode(), Mode::Normal);
59 cx.assert_editor_state("Tesˇt");
60 }
61
62 #[gpui::test]
63 async fn test_insert_with_counts(
64 deterministic: Arc<Deterministic>,
65 cx: &mut gpui::TestAppContext,
66 ) {
67 let mut cx = NeovimBackedTestContext::new(cx).await;
68
69 cx.set_shared_state("ˇhello\n").await;
70 cx.simulate_shared_keystrokes(["5", "i", "-", "escape"])
71 .await;
72 deterministic.run_until_parked();
73 cx.assert_shared_state("----ˇ-hello\n").await;
74
75 cx.set_shared_state("ˇhello\n").await;
76 cx.simulate_shared_keystrokes(["5", "a", "-", "escape"])
77 .await;
78 deterministic.run_until_parked();
79 cx.assert_shared_state("h----ˇ-ello\n").await;
80
81 cx.simulate_shared_keystrokes(["4", "shift-i", "-", "escape"])
82 .await;
83 deterministic.run_until_parked();
84 cx.assert_shared_state("---ˇ-h-----ello\n").await;
85
86 cx.simulate_shared_keystrokes(["3", "shift-a", "-", "escape"])
87 .await;
88 deterministic.run_until_parked();
89 cx.assert_shared_state("----h-----ello--ˇ-\n").await;
90
91 cx.set_shared_state("ˇhello\n").await;
92 cx.simulate_shared_keystrokes(["3", "o", "o", "i", "escape"])
93 .await;
94 deterministic.run_until_parked();
95 cx.assert_shared_state("hello\noi\noi\noˇi\n").await;
96
97 cx.set_shared_state("ˇhello\n").await;
98 cx.simulate_shared_keystrokes(["3", "shift-o", "o", "i", "escape"])
99 .await;
100 deterministic.run_until_parked();
101 cx.assert_shared_state("oi\noi\noˇi\nhello\n").await;
102 }
103
104 #[gpui::test]
105 async fn test_insert_with_repeat(
106 deterministic: Arc<Deterministic>,
107 cx: &mut gpui::TestAppContext,
108 ) {
109 let mut cx = NeovimBackedTestContext::new(cx).await;
110
111 cx.set_shared_state("ˇhello\n").await;
112 cx.simulate_shared_keystrokes(["3", "i", "-", "escape"])
113 .await;
114 deterministic.run_until_parked();
115 cx.assert_shared_state("--ˇ-hello\n").await;
116 cx.simulate_shared_keystrokes(["."]).await;
117 deterministic.run_until_parked();
118 cx.assert_shared_state("----ˇ--hello\n").await;
119 cx.simulate_shared_keystrokes(["2", "."]).await;
120 deterministic.run_until_parked();
121 cx.assert_shared_state("-----ˇ---hello\n").await;
122
123 cx.set_shared_state("ˇhello\n").await;
124 cx.simulate_shared_keystrokes(["2", "o", "k", "k", "escape"])
125 .await;
126 deterministic.run_until_parked();
127 cx.assert_shared_state("hello\nkk\nkˇk\n").await;
128 cx.simulate_shared_keystrokes(["."]).await;
129 deterministic.run_until_parked();
130 cx.assert_shared_state("hello\nkk\nkk\nkk\nkˇk\n").await;
131 cx.simulate_shared_keystrokes(["1", "."]).await;
132 deterministic.run_until_parked();
133 cx.assert_shared_state("hello\nkk\nkk\nkk\nkk\nkˇk\n").await;
134 }
135}