insert.rs

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