insert.rs

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