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.change_selections(Some(Autoscroll::fit()), cx, |s| {
20 s.move_cursors_with(|map, mut cursor, _| {
21 *cursor.column_mut() = cursor.column().saturating_sub(1);
22 (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
23 });
24 });
25 });
26 vim.switch_mode(Mode::Normal, false, cx);
27 false
28 } else {
29 true
30 }
31 });
32
33 if should_repeat {
34 repeat::repeat(cx, true)
35 }
36}
37
38#[cfg(test)]
39mod test {
40 use std::sync::Arc;
41
42 use gpui::executor::Deterministic;
43
44 use crate::{
45 state::Mode,
46 test::{NeovimBackedTestContext, VimTestContext},
47 };
48
49 #[gpui::test]
50 async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
51 let mut cx = VimTestContext::new(cx, true).await;
52 cx.simulate_keystroke("i");
53 assert_eq!(cx.mode(), Mode::Insert);
54 cx.simulate_keystrokes(["T", "e", "s", "t"]);
55 cx.assert_editor_state("Testˇ");
56 cx.simulate_keystroke("escape");
57 assert_eq!(cx.mode(), Mode::Normal);
58 cx.assert_editor_state("Tesˇt");
59 }
60
61 #[gpui::test]
62 async fn test_insert_with_counts(
63 deterministic: Arc<Deterministic>,
64 cx: &mut gpui::TestAppContext,
65 ) {
66 let mut cx = NeovimBackedTestContext::new(cx).await;
67
68 cx.set_shared_state("ˇhello\n").await;
69 cx.simulate_shared_keystrokes(["5", "i", "-", "escape"])
70 .await;
71 deterministic.run_until_parked();
72 cx.assert_shared_state("----ˇ-hello\n").await;
73
74 cx.set_shared_state("ˇhello\n").await;
75 cx.simulate_shared_keystrokes(["5", "a", "-", "escape"])
76 .await;
77 deterministic.run_until_parked();
78 cx.assert_shared_state("h----ˇ-ello\n").await;
79
80 cx.simulate_shared_keystrokes(["4", "shift-i", "-", "escape"])
81 .await;
82 deterministic.run_until_parked();
83 cx.assert_shared_state("---ˇ-h-----ello\n").await;
84
85 cx.simulate_shared_keystrokes(["3", "shift-a", "-", "escape"])
86 .await;
87 deterministic.run_until_parked();
88 cx.assert_shared_state("----h-----ello--ˇ-\n").await;
89
90 cx.set_shared_state("ˇhello\n").await;
91 cx.simulate_shared_keystrokes(["3", "o", "o", "i", "escape"])
92 .await;
93 deterministic.run_until_parked();
94 cx.assert_shared_state("hello\noi\noi\noˇi\n").await;
95
96 cx.set_shared_state("ˇhello\n").await;
97 cx.simulate_shared_keystrokes(["3", "shift-o", "o", "i", "escape"])
98 .await;
99 deterministic.run_until_parked();
100 cx.assert_shared_state("oi\noi\noˇi\nhello\n").await;
101 }
102
103 #[gpui::test]
104 async fn test_insert_with_repeat(
105 deterministic: Arc<Deterministic>,
106 cx: &mut gpui::TestAppContext,
107 ) {
108 let mut cx = NeovimBackedTestContext::new(cx).await;
109
110 cx.set_shared_state("ˇhello\n").await;
111 cx.simulate_shared_keystrokes(["3", "i", "-", "escape"])
112 .await;
113 deterministic.run_until_parked();
114 cx.assert_shared_state("--ˇ-hello\n").await;
115 cx.simulate_shared_keystrokes(["."]).await;
116 deterministic.run_until_parked();
117 cx.assert_shared_state("----ˇ--hello\n").await;
118 cx.simulate_shared_keystrokes(["2", "."]).await;
119 deterministic.run_until_parked();
120 cx.assert_shared_state("-----ˇ---hello\n").await;
121
122 cx.set_shared_state("ˇhello\n").await;
123 cx.simulate_shared_keystrokes(["2", "o", "k", "k", "escape"])
124 .await;
125 deterministic.run_until_parked();
126 cx.assert_shared_state("hello\nkk\nkˇk\n").await;
127 cx.simulate_shared_keystrokes(["."]).await;
128 deterministic.run_until_parked();
129 cx.assert_shared_state("hello\nkk\nkk\nkk\nkˇk\n").await;
130 cx.simulate_shared_keystrokes(["1", "."]).await;
131 deterministic.run_until_parked();
132 cx.assert_shared_state("hello\nkk\nkk\nkk\nkk\nkˇk\n").await;
133 }
134}