insert.rs

  1use crate::{normal::repeat, state::Mode, Vim};
  2use editor::{scroll::autoscroll::Autoscroll, Bias};
  3use gpui::{actions, Action, AppContext, ViewContext};
  4use language::SelectionGoal;
  5use workspace::Workspace;
  6
  7actions!(NormalBefore);
  8
  9pub fn init(cx: &mut AppContext) {
 10    // todo!()
 11    // cx.add_action(normal_before);
 12}
 13
 14fn normal_before(_: &mut Workspace, action: &NormalBefore, cx: &mut ViewContext<Workspace>) {
 15    let should_repeat = Vim::update(cx, |vim, cx| {
 16        let count = vim.take_count(cx).unwrap_or(1);
 17        vim.stop_recording_immediately(action.boxed_clone());
 18        if count <= 1 || vim.workspace_state.replaying {
 19            vim.update_active_editor(cx, |editor, cx| {
 20                editor.cancel(&Default::default(), cx);
 21                editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
 22                    s.move_cursors_with(|map, mut cursor, _| {
 23                        *cursor.column_mut() = cursor.column().saturating_sub(1);
 24                        (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
 25                    });
 26                });
 27            });
 28            vim.switch_mode(Mode::Normal, false, cx);
 29            false
 30        } else {
 31            true
 32        }
 33    });
 34
 35    if should_repeat {
 36        repeat::repeat(cx, true)
 37    }
 38}
 39
 40// #[cfg(test)]
 41// mod test {
 42//     use std::sync::Arc;
 43
 44//     use gpui::executor::Deterministic;
 45
 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_keystroke("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_keystroke("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(
 65//         deterministic: Arc<Deterministic>,
 66//         cx: &mut gpui::TestAppContext,
 67//     ) {
 68//         let mut cx = NeovimBackedTestContext::new(cx).await;
 69
 70//         cx.set_shared_state("ˇhello\n").await;
 71//         cx.simulate_shared_keystrokes(["5", "i", "-", "escape"])
 72//             .await;
 73//         deterministic.run_until_parked();
 74//         cx.assert_shared_state("----ˇ-hello\n").await;
 75
 76//         cx.set_shared_state("ˇhello\n").await;
 77//         cx.simulate_shared_keystrokes(["5", "a", "-", "escape"])
 78//             .await;
 79//         deterministic.run_until_parked();
 80//         cx.assert_shared_state("h----ˇ-ello\n").await;
 81
 82//         cx.simulate_shared_keystrokes(["4", "shift-i", "-", "escape"])
 83//             .await;
 84//         deterministic.run_until_parked();
 85//         cx.assert_shared_state("---ˇ-h-----ello\n").await;
 86
 87//         cx.simulate_shared_keystrokes(["3", "shift-a", "-", "escape"])
 88//             .await;
 89//         deterministic.run_until_parked();
 90//         cx.assert_shared_state("----h-----ello--ˇ-\n").await;
 91
 92//         cx.set_shared_state("ˇhello\n").await;
 93//         cx.simulate_shared_keystrokes(["3", "o", "o", "i", "escape"])
 94//             .await;
 95//         deterministic.run_until_parked();
 96//         cx.assert_shared_state("hello\noi\noi\noˇi\n").await;
 97
 98//         cx.set_shared_state("ˇhello\n").await;
 99//         cx.simulate_shared_keystrokes(["3", "shift-o", "o", "i", "escape"])
100//             .await;
101//         deterministic.run_until_parked();
102//         cx.assert_shared_state("oi\noi\noˇi\nhello\n").await;
103//     }
104
105//     #[gpui::test]
106//     async fn test_insert_with_repeat(
107//         deterministic: Arc<Deterministic>,
108//         cx: &mut gpui::TestAppContext,
109//     ) {
110//         let mut cx = NeovimBackedTestContext::new(cx).await;
111
112//         cx.set_shared_state("ˇhello\n").await;
113//         cx.simulate_shared_keystrokes(["3", "i", "-", "escape"])
114//             .await;
115//         deterministic.run_until_parked();
116//         cx.assert_shared_state("--ˇ-hello\n").await;
117//         cx.simulate_shared_keystrokes(["."]).await;
118//         deterministic.run_until_parked();
119//         cx.assert_shared_state("----ˇ--hello\n").await;
120//         cx.simulate_shared_keystrokes(["2", "."]).await;
121//         deterministic.run_until_parked();
122//         cx.assert_shared_state("-----ˇ---hello\n").await;
123
124//         cx.set_shared_state("ˇhello\n").await;
125//         cx.simulate_shared_keystrokes(["2", "o", "k", "k", "escape"])
126//             .await;
127//         deterministic.run_until_parked();
128//         cx.assert_shared_state("hello\nkk\nkˇk\n").await;
129//         cx.simulate_shared_keystrokes(["."]).await;
130//         deterministic.run_until_parked();
131//         cx.assert_shared_state("hello\nkk\nkk\nkk\nkˇk\n").await;
132//         cx.simulate_shared_keystrokes(["1", "."]).await;
133//         deterministic.run_until_parked();
134//         cx.assert_shared_state("hello\nkk\nkk\nkk\nkk\nkˇk\n").await;
135//     }
136// }