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