insert.rs

  1use crate::{
  2    normal::{mark::create_mark, repeat},
  3    state::Mode,
  4    Vim,
  5};
  6use editor::{scroll::Autoscroll, Bias};
  7use gpui::{actions, Action, ViewContext};
  8use language::SelectionGoal;
  9use workspace::Workspace;
 10
 11actions!(vim, [NormalBefore]);
 12
 13pub fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
 14    workspace.register_action(normal_before);
 15}
 16
 17fn normal_before(_: &mut Workspace, action: &NormalBefore, cx: &mut ViewContext<Workspace>) {
 18    let should_repeat = Vim::update(cx, |vim, cx| {
 19        let count = vim.take_count(cx).unwrap_or(1);
 20        vim.stop_recording_immediately(action.boxed_clone());
 21        if count <= 1 || vim.workspace_state.replaying {
 22            create_mark(vim, "^".into(), false, cx);
 23            vim.update_active_editor(cx, |_, editor, cx| {
 24                editor.dismiss_menus_and_popups(false, cx);
 25                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 26                    s.move_cursors_with(|map, mut cursor, _| {
 27                        *cursor.column_mut() = cursor.column().saturating_sub(1);
 28                        (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
 29                    });
 30                });
 31            });
 32            vim.switch_mode(Mode::Normal, false, cx);
 33            false
 34        } else {
 35            true
 36        }
 37    });
 38
 39    if should_repeat {
 40        repeat::repeat(cx, true)
 41    }
 42}
 43
 44#[cfg(test)]
 45mod test {
 46    use crate::{
 47        state::Mode,
 48        test::{NeovimBackedTestContext, VimTestContext},
 49    };
 50
 51    #[gpui::test]
 52    async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
 53        let mut cx = VimTestContext::new(cx, true).await;
 54        cx.simulate_keystrokes("i");
 55        assert_eq!(cx.mode(), Mode::Insert);
 56        cx.simulate_keystrokes("T e s t");
 57        cx.assert_editor_state("Testˇ");
 58        cx.simulate_keystrokes("escape");
 59        assert_eq!(cx.mode(), Mode::Normal);
 60        cx.assert_editor_state("Tesˇt");
 61    }
 62
 63    #[gpui::test]
 64    async fn test_insert_with_counts(cx: &mut gpui::TestAppContext) {
 65        let mut cx = NeovimBackedTestContext::new(cx).await;
 66
 67        cx.set_shared_state("ˇhello\n").await;
 68        cx.simulate_shared_keystrokes("5 i - escape").await;
 69        cx.run_until_parked();
 70        cx.shared_state().await.assert_eq("----ˇ-hello\n");
 71
 72        cx.set_shared_state("ˇhello\n").await;
 73        cx.simulate_shared_keystrokes("5 a - escape").await;
 74        cx.run_until_parked();
 75        cx.shared_state().await.assert_eq("h----ˇ-ello\n");
 76
 77        cx.simulate_shared_keystrokes("4 shift-i - escape").await;
 78        cx.run_until_parked();
 79        cx.shared_state().await.assert_eq("---ˇ-h-----ello\n");
 80
 81        cx.simulate_shared_keystrokes("3 shift-a - escape").await;
 82        cx.run_until_parked();
 83        cx.shared_state().await.assert_eq("----h-----ello--ˇ-\n");
 84
 85        cx.set_shared_state("ˇhello\n").await;
 86        cx.simulate_shared_keystrokes("3 o o i escape").await;
 87        cx.run_until_parked();
 88        cx.shared_state().await.assert_eq("hello\noi\noi\noˇi\n");
 89
 90        cx.set_shared_state("ˇhello\n").await;
 91        cx.simulate_shared_keystrokes("3 shift-o o i escape").await;
 92        cx.run_until_parked();
 93        cx.shared_state().await.assert_eq("oi\noi\noˇi\nhello\n");
 94    }
 95
 96    #[gpui::test]
 97    async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) {
 98        let mut cx = NeovimBackedTestContext::new(cx).await;
 99
100        cx.set_shared_state("ˇhello\n").await;
101        cx.simulate_shared_keystrokes("3 i - escape").await;
102        cx.run_until_parked();
103        cx.shared_state().await.assert_eq("--ˇ-hello\n");
104        cx.simulate_shared_keystrokes(".").await;
105        cx.run_until_parked();
106        cx.shared_state().await.assert_eq("----ˇ--hello\n");
107        cx.simulate_shared_keystrokes("2 .").await;
108        cx.run_until_parked();
109        cx.shared_state().await.assert_eq("-----ˇ---hello\n");
110
111        cx.set_shared_state("ˇhello\n").await;
112        cx.simulate_shared_keystrokes("2 o k k escape").await;
113        cx.run_until_parked();
114        cx.shared_state().await.assert_eq("hello\nkk\nkˇk\n");
115        cx.simulate_shared_keystrokes(".").await;
116        cx.run_until_parked();
117        cx.shared_state()
118            .await
119            .assert_eq("hello\nkk\nkk\nkk\nkˇk\n");
120        cx.simulate_shared_keystrokes("1 .").await;
121        cx.run_until_parked();
122        cx.shared_state()
123            .await
124            .assert_eq("hello\nkk\nkk\nkk\nkk\nkˇk\n");
125    }
126}