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