1use crate::{Vim, state::Mode};
2use editor::{Bias, Editor};
3use gpui::{Action, Context, Window, actions};
4use language::SelectionGoal;
5use settings::Settings;
6use text::Point;
7use vim_mode_setting::HelixModeSetting;
8use workspace::searchable::Direction;
9
10actions!(
11 vim,
12 [
13 /// Switches to normal mode with cursor positioned before the current character.
14 NormalBefore,
15 /// Temporarily switches to normal mode for one command.
16 TemporaryNormal,
17 /// Inserts the next character from the line above into the current line.
18 InsertFromAbove,
19 /// Inserts the next character from the line below into the current line.
20 InsertFromBelow
21 ]
22);
23
24pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
25 Vim::action(editor, cx, Vim::normal_before);
26 Vim::action(editor, cx, Vim::temporary_normal);
27 Vim::action(editor, cx, |vim, _: &InsertFromAbove, window, cx| {
28 vim.insert_around(Direction::Prev, window, cx)
29 });
30 Vim::action(editor, cx, |vim, _: &InsertFromBelow, window, cx| {
31 vim.insert_around(Direction::Next, window, cx)
32 })
33}
34
35impl Vim {
36 pub(crate) fn normal_before(
37 &mut self,
38 action: &NormalBefore,
39 window: &mut Window,
40 cx: &mut Context<Self>,
41 ) {
42 if self.active_operator().is_some() {
43 self.operator_stack.clear();
44 self.sync_vim_settings(window, cx);
45 return;
46 }
47 let count = Vim::take_count(cx).unwrap_or(1);
48 Vim::take_forced_motion(cx);
49 self.stop_recording_immediately(action.boxed_clone(), cx);
50 if count <= 1 || Vim::globals(cx).dot_replaying {
51 self.create_mark("^".into(), window, cx);
52
53 if HelixModeSetting::get_global(cx).0 {
54 self.update_editor(cx, |_, editor, cx| {
55 editor.dismiss_menus_and_popups(false, window, cx);
56 });
57 self.switch_mode(Mode::HelixNormal, false, window, cx);
58 return;
59 }
60
61 self.update_editor(cx, |_, editor, cx| {
62 editor.dismiss_menus_and_popups(false, window, cx);
63
64 editor.change_selections(Default::default(), window, cx, |s| {
65 s.move_cursors_with(|map, mut cursor, _| {
66 *cursor.column_mut() = cursor.column().saturating_sub(1);
67 (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
68 });
69 });
70 });
71
72 self.switch_mode(Mode::Normal, false, window, cx);
73 return;
74 }
75
76 self.repeat(true, window, cx)
77 }
78
79 fn temporary_normal(
80 &mut self,
81 _: &TemporaryNormal,
82 window: &mut Window,
83 cx: &mut Context<Self>,
84 ) {
85 self.switch_mode(Mode::Normal, true, window, cx);
86 self.temp_mode = true;
87 }
88
89 fn insert_around(&mut self, direction: Direction, _: &mut Window, cx: &mut Context<Self>) {
90 self.update_editor(cx, |_, editor, cx| {
91 let snapshot = editor.buffer().read(cx).snapshot(cx);
92 let mut edits = Vec::new();
93 for selection in editor.selections.all::<Point>(&editor.display_snapshot(cx)) {
94 let point = selection.head();
95 let new_row = match direction {
96 Direction::Next => point.row + 1,
97 Direction::Prev if point.row > 0 => point.row - 1,
98 _ => continue,
99 };
100 let source = snapshot.clip_point(Point::new(new_row, point.column), Bias::Left);
101 if let Some(c) = snapshot.chars_at(source).next()
102 && c != '\n'
103 {
104 edits.push((point..point, c.to_string()))
105 }
106 }
107
108 editor.edit(edits, cx);
109 });
110 }
111}
112
113#[cfg(test)]
114mod test {
115 use crate::{
116 state::Mode,
117 test::{NeovimBackedTestContext, VimTestContext},
118 };
119
120 #[gpui::test]
121 async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
122 let mut cx = VimTestContext::new(cx, true).await;
123 cx.simulate_keystrokes("i");
124 assert_eq!(cx.mode(), Mode::Insert);
125 cx.simulate_keystrokes("T e s t");
126 cx.assert_editor_state("Testˇ");
127 cx.simulate_keystrokes("escape");
128 assert_eq!(cx.mode(), Mode::Normal);
129 cx.assert_editor_state("Tesˇt");
130 }
131
132 #[gpui::test]
133 async fn test_insert_with_counts(cx: &mut gpui::TestAppContext) {
134 let mut cx = NeovimBackedTestContext::new(cx).await;
135
136 cx.set_shared_state("ˇhello\n").await;
137 cx.simulate_shared_keystrokes("5 i - escape").await;
138 cx.shared_state().await.assert_eq("----ˇ-hello\n");
139
140 cx.set_shared_state("ˇhello\n").await;
141 cx.simulate_shared_keystrokes("5 a - escape").await;
142 cx.shared_state().await.assert_eq("h----ˇ-ello\n");
143
144 cx.simulate_shared_keystrokes("4 shift-i - escape").await;
145 cx.shared_state().await.assert_eq("---ˇ-h-----ello\n");
146
147 cx.simulate_shared_keystrokes("3 shift-a - escape").await;
148 cx.shared_state().await.assert_eq("----h-----ello--ˇ-\n");
149
150 cx.set_shared_state("ˇhello\n").await;
151 cx.simulate_shared_keystrokes("3 o o i escape").await;
152 cx.shared_state().await.assert_eq("hello\noi\noi\noˇi\n");
153
154 cx.set_shared_state("ˇhello\n").await;
155 cx.simulate_shared_keystrokes("3 shift-o o i escape").await;
156 cx.shared_state().await.assert_eq("oi\noi\noˇi\nhello\n");
157 }
158
159 #[gpui::test]
160 async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) {
161 let mut cx = NeovimBackedTestContext::new(cx).await;
162
163 cx.set_shared_state("ˇhello\n").await;
164 cx.simulate_shared_keystrokes("3 i - escape").await;
165 cx.shared_state().await.assert_eq("--ˇ-hello\n");
166 cx.simulate_shared_keystrokes(".").await;
167 cx.shared_state().await.assert_eq("----ˇ--hello\n");
168 cx.simulate_shared_keystrokes("2 .").await;
169 cx.shared_state().await.assert_eq("-----ˇ---hello\n");
170
171 cx.set_shared_state("ˇhello\n").await;
172 cx.simulate_shared_keystrokes("2 o k k escape").await;
173 cx.shared_state().await.assert_eq("hello\nkk\nkˇk\n");
174 cx.simulate_shared_keystrokes(".").await;
175 cx.shared_state()
176 .await
177 .assert_eq("hello\nkk\nkk\nkk\nkˇk\n");
178 cx.simulate_shared_keystrokes("1 .").await;
179 cx.shared_state()
180 .await
181 .assert_eq("hello\nkk\nkk\nkk\nkk\nkˇk\n");
182 }
183
184 #[gpui::test]
185 async fn test_insert_ctrl_r(cx: &mut gpui::TestAppContext) {
186 let mut cx = NeovimBackedTestContext::new(cx).await;
187
188 cx.set_shared_state("heˇllo\n").await;
189 cx.simulate_shared_keystrokes("y y i ctrl-r \"").await;
190 cx.shared_state().await.assert_eq("hehello\nˇllo\n");
191
192 cx.simulate_shared_keystrokes("ctrl-r x ctrl-r escape")
193 .await;
194 cx.shared_state().await.assert_eq("hehello\nˇllo\n");
195 }
196
197 #[gpui::test]
198 async fn test_insert_ctrl_y(cx: &mut gpui::TestAppContext) {
199 let mut cx = NeovimBackedTestContext::new(cx).await;
200
201 cx.set_shared_state("hello\nˇ\nworld").await;
202 cx.simulate_shared_keystrokes("i ctrl-y ctrl-e").await;
203 cx.shared_state().await.assert_eq("hello\nhoˇ\nworld");
204 }
205}