insert.rs

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