insert.rs

  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            self.update_editor(cx, |_, editor, cx| {
 54                editor.dismiss_menus_and_popups(false, window, cx);
 55
 56                if !HelixModeSetting::get_global(cx).0 {
 57                    editor.change_selections(Default::default(), window, cx, |s| {
 58                        s.move_cursors_with(|map, mut cursor, _| {
 59                            *cursor.column_mut() = cursor.column().saturating_sub(1);
 60                            (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
 61                        });
 62                    });
 63                }
 64            });
 65
 66            self.switch_mode(Mode::Normal, false, window, cx);
 67            return;
 68        }
 69
 70        self.repeat(true, window, cx)
 71    }
 72
 73    fn temporary_normal(
 74        &mut self,
 75        _: &TemporaryNormal,
 76        window: &mut Window,
 77        cx: &mut Context<Self>,
 78    ) {
 79        self.switch_mode(Mode::Normal, true, window, cx);
 80        self.temp_mode = true;
 81    }
 82
 83    fn insert_around(&mut self, direction: Direction, _: &mut Window, cx: &mut Context<Self>) {
 84        self.update_editor(cx, |_, editor, cx| {
 85            let snapshot = editor.buffer().read(cx).snapshot(cx);
 86            let mut edits = Vec::new();
 87            for selection in editor.selections.all::<Point>(cx) {
 88                let point = selection.head();
 89                let new_row = match direction {
 90                    Direction::Next => point.row + 1,
 91                    Direction::Prev if point.row > 0 => point.row - 1,
 92                    _ => continue,
 93                };
 94                let source = snapshot.clip_point(Point::new(new_row, point.column), Bias::Left);
 95                if let Some(c) = snapshot.chars_at(source).next()
 96                    && c != '\n'
 97                {
 98                    edits.push((point..point, c.to_string()))
 99                }
100            }
101
102            editor.edit(edits, cx);
103        });
104    }
105}
106
107#[cfg(test)]
108mod test {
109    use crate::{
110        state::Mode,
111        test::{NeovimBackedTestContext, VimTestContext},
112    };
113
114    #[gpui::test]
115    async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
116        let mut cx = VimTestContext::new(cx, true).await;
117        cx.simulate_keystrokes("i");
118        assert_eq!(cx.mode(), Mode::Insert);
119        cx.simulate_keystrokes("T e s t");
120        cx.assert_editor_state("Testˇ");
121        cx.simulate_keystrokes("escape");
122        assert_eq!(cx.mode(), Mode::Normal);
123        cx.assert_editor_state("Tesˇt");
124    }
125
126    #[gpui::test]
127    async fn test_insert_with_counts(cx: &mut gpui::TestAppContext) {
128        let mut cx = NeovimBackedTestContext::new(cx).await;
129
130        cx.set_shared_state("ˇhello\n").await;
131        cx.simulate_shared_keystrokes("5 i - escape").await;
132        cx.shared_state().await.assert_eq("----ˇ-hello\n");
133
134        cx.set_shared_state("ˇhello\n").await;
135        cx.simulate_shared_keystrokes("5 a - escape").await;
136        cx.shared_state().await.assert_eq("h----ˇ-ello\n");
137
138        cx.simulate_shared_keystrokes("4 shift-i - escape").await;
139        cx.shared_state().await.assert_eq("---ˇ-h-----ello\n");
140
141        cx.simulate_shared_keystrokes("3 shift-a - escape").await;
142        cx.shared_state().await.assert_eq("----h-----ello--ˇ-\n");
143
144        cx.set_shared_state("ˇhello\n").await;
145        cx.simulate_shared_keystrokes("3 o o i escape").await;
146        cx.shared_state().await.assert_eq("hello\noi\noi\noˇi\n");
147
148        cx.set_shared_state("ˇhello\n").await;
149        cx.simulate_shared_keystrokes("3 shift-o o i escape").await;
150        cx.shared_state().await.assert_eq("oi\noi\noˇi\nhello\n");
151    }
152
153    #[gpui::test]
154    async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) {
155        let mut cx = NeovimBackedTestContext::new(cx).await;
156
157        cx.set_shared_state("ˇhello\n").await;
158        cx.simulate_shared_keystrokes("3 i - escape").await;
159        cx.shared_state().await.assert_eq("--ˇ-hello\n");
160        cx.simulate_shared_keystrokes(".").await;
161        cx.shared_state().await.assert_eq("----ˇ--hello\n");
162        cx.simulate_shared_keystrokes("2 .").await;
163        cx.shared_state().await.assert_eq("-----ˇ---hello\n");
164
165        cx.set_shared_state("ˇhello\n").await;
166        cx.simulate_shared_keystrokes("2 o k k escape").await;
167        cx.shared_state().await.assert_eq("hello\nkk\nkˇk\n");
168        cx.simulate_shared_keystrokes(".").await;
169        cx.shared_state()
170            .await
171            .assert_eq("hello\nkk\nkk\nkk\nkˇk\n");
172        cx.simulate_shared_keystrokes("1 .").await;
173        cx.shared_state()
174            .await
175            .assert_eq("hello\nkk\nkk\nkk\nkk\nkˇk\n");
176    }
177
178    #[gpui::test]
179    async fn test_insert_ctrl_r(cx: &mut gpui::TestAppContext) {
180        let mut cx = NeovimBackedTestContext::new(cx).await;
181
182        cx.set_shared_state("heˇllo\n").await;
183        cx.simulate_shared_keystrokes("y y i ctrl-r \"").await;
184        cx.shared_state().await.assert_eq("hehello\nˇllo\n");
185
186        cx.simulate_shared_keystrokes("ctrl-r x ctrl-r escape")
187            .await;
188        cx.shared_state().await.assert_eq("hehello\nˇllo\n");
189    }
190
191    #[gpui::test]
192    async fn test_insert_ctrl_y(cx: &mut gpui::TestAppContext) {
193        let mut cx = NeovimBackedTestContext::new(cx).await;
194
195        cx.set_shared_state("hello\nˇ\nworld").await;
196        cx.simulate_shared_keystrokes("i ctrl-y ctrl-e").await;
197        cx.shared_state().await.assert_eq("hello\nhoˇ\nworld");
198    }
199}