insert.rs

  1use crate::{Vim, state::Mode};
  2use editor::{Bias, Editor};
  3use gpui::{Action, Context, Window, actions};
  4use language::SelectionGoal;
  5use settings::Settings;
  6use text::Point;
  7use vim_mode_setting::HelixModeSetting;
  8use workspace::searchable::Direction;
  9
 10actions!(
 11    vim,
 12    [
 13        /// Switches to normal mode with cursor positioned before the current character.
 14        NormalBefore,
 15        /// Temporarily switches to normal mode for one command.
 16        TemporaryNormal,
 17        /// Inserts the next character from the line above into the current line.
 18        InsertFromAbove,
 19        /// Inserts the next character from the line below into the current line.
 20        InsertFromBelow
 21    ]
 22);
 23
 24pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
 25    Vim::action(editor, cx, Vim::normal_before);
 26    Vim::action(editor, cx, Vim::temporary_normal);
 27    Vim::action(editor, cx, |vim, _: &InsertFromAbove, window, cx| {
 28        vim.insert_around(Direction::Prev, window, cx)
 29    });
 30    Vim::action(editor, cx, |vim, _: &InsertFromBelow, window, cx| {
 31        vim.insert_around(Direction::Next, window, cx)
 32    })
 33}
 34
 35impl Vim {
 36    pub(crate) fn normal_before(
 37        &mut self,
 38        action: &NormalBefore,
 39        window: &mut Window,
 40        cx: &mut Context<Self>,
 41    ) {
 42        if self.active_operator().is_some() {
 43            self.operator_stack.clear();
 44            self.sync_vim_settings(window, cx);
 45            return;
 46        }
 47        let count = Vim::take_count(cx).unwrap_or(1);
 48        Vim::take_forced_motion(cx);
 49        self.stop_recording_immediately(action.boxed_clone(), cx);
 50        if count <= 1 || Vim::globals(cx).dot_replaying {
 51            self.create_mark("^".into(), window, cx);
 52
 53            self.update_editor(cx, |_, editor, cx| {
 54                editor.dismiss_menus_and_popups(false, window, cx);
 55
 56                if !HelixModeSetting::get_global(cx).0 {
 57                    editor.change_selections(Default::default(), window, cx, |s| {
 58                        s.move_cursors_with(|map, mut cursor, _| {
 59                            *cursor.column_mut() = cursor.column().saturating_sub(1);
 60                            (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
 61                        });
 62                    });
 63                }
 64            });
 65
 66            if HelixModeSetting::get_global(cx).0 {
 67                self.switch_mode(Mode::HelixNormal, false, window, cx);
 68            } else {
 69                self.switch_mode(Mode::Normal, false, window, cx);
 70            }
 71            return;
 72        }
 73
 74        self.repeat(true, window, cx)
 75    }
 76
 77    fn temporary_normal(
 78        &mut self,
 79        _: &TemporaryNormal,
 80        window: &mut Window,
 81        cx: &mut Context<Self>,
 82    ) {
 83        self.switch_mode(Mode::Normal, true, window, cx);
 84        self.temp_mode = true;
 85    }
 86
 87    fn insert_around(&mut self, direction: Direction, _: &mut Window, cx: &mut Context<Self>) {
 88        self.update_editor(cx, |_, editor, cx| {
 89            let snapshot = editor.buffer().read(cx).snapshot(cx);
 90            let mut edits = Vec::new();
 91            for selection in editor.selections.all::<Point>(cx) {
 92                let point = selection.head();
 93                let new_row = match direction {
 94                    Direction::Next => point.row + 1,
 95                    Direction::Prev if point.row > 0 => point.row - 1,
 96                    _ => continue,
 97                };
 98                let source = snapshot.clip_point(Point::new(new_row, point.column), Bias::Left);
 99                if let Some(c) = snapshot.chars_at(source).next()
100                    && c != '\n'
101                {
102                    edits.push((point..point, c.to_string()))
103                }
104            }
105
106            editor.edit(edits, cx);
107        });
108    }
109}
110
111#[cfg(test)]
112mod test {
113    use crate::{
114        state::Mode,
115        test::{NeovimBackedTestContext, VimTestContext},
116    };
117
118    #[gpui::test]
119    async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
120        let mut cx = VimTestContext::new(cx, true).await;
121        cx.simulate_keystrokes("i");
122        assert_eq!(cx.mode(), Mode::Insert);
123        cx.simulate_keystrokes("T e s t");
124        cx.assert_editor_state("Testˇ");
125        cx.simulate_keystrokes("escape");
126        assert_eq!(cx.mode(), Mode::Normal);
127        cx.assert_editor_state("Tesˇt");
128    }
129
130    #[gpui::test]
131    async fn test_insert_with_counts(cx: &mut gpui::TestAppContext) {
132        let mut cx = NeovimBackedTestContext::new(cx).await;
133
134        cx.set_shared_state("ˇhello\n").await;
135        cx.simulate_shared_keystrokes("5 i - escape").await;
136        cx.shared_state().await.assert_eq("----ˇ-hello\n");
137
138        cx.set_shared_state("ˇhello\n").await;
139        cx.simulate_shared_keystrokes("5 a - escape").await;
140        cx.shared_state().await.assert_eq("h----ˇ-ello\n");
141
142        cx.simulate_shared_keystrokes("4 shift-i - escape").await;
143        cx.shared_state().await.assert_eq("---ˇ-h-----ello\n");
144
145        cx.simulate_shared_keystrokes("3 shift-a - escape").await;
146        cx.shared_state().await.assert_eq("----h-----ello--ˇ-\n");
147
148        cx.set_shared_state("ˇhello\n").await;
149        cx.simulate_shared_keystrokes("3 o o i escape").await;
150        cx.shared_state().await.assert_eq("hello\noi\noi\noˇi\n");
151
152        cx.set_shared_state("ˇhello\n").await;
153        cx.simulate_shared_keystrokes("3 shift-o o i escape").await;
154        cx.shared_state().await.assert_eq("oi\noi\noˇi\nhello\n");
155    }
156
157    #[gpui::test]
158    async fn test_insert_with_repeat(cx: &mut gpui::TestAppContext) {
159        let mut cx = NeovimBackedTestContext::new(cx).await;
160
161        cx.set_shared_state("ˇhello\n").await;
162        cx.simulate_shared_keystrokes("3 i - escape").await;
163        cx.shared_state().await.assert_eq("--ˇ-hello\n");
164        cx.simulate_shared_keystrokes(".").await;
165        cx.shared_state().await.assert_eq("----ˇ--hello\n");
166        cx.simulate_shared_keystrokes("2 .").await;
167        cx.shared_state().await.assert_eq("-----ˇ---hello\n");
168
169        cx.set_shared_state("ˇhello\n").await;
170        cx.simulate_shared_keystrokes("2 o k k escape").await;
171        cx.shared_state().await.assert_eq("hello\nkk\nkˇk\n");
172        cx.simulate_shared_keystrokes(".").await;
173        cx.shared_state()
174            .await
175            .assert_eq("hello\nkk\nkk\nkk\nkˇk\n");
176        cx.simulate_shared_keystrokes("1 .").await;
177        cx.shared_state()
178            .await
179            .assert_eq("hello\nkk\nkk\nkk\nkk\nkˇk\n");
180    }
181
182    #[gpui::test]
183    async fn test_insert_ctrl_r(cx: &mut gpui::TestAppContext) {
184        let mut cx = NeovimBackedTestContext::new(cx).await;
185
186        cx.set_shared_state("heˇllo\n").await;
187        cx.simulate_shared_keystrokes("y y i ctrl-r \"").await;
188        cx.shared_state().await.assert_eq("hehello\nˇllo\n");
189
190        cx.simulate_shared_keystrokes("ctrl-r x ctrl-r escape")
191            .await;
192        cx.shared_state().await.assert_eq("hehello\nˇllo\n");
193    }
194
195    #[gpui::test]
196    async fn test_insert_ctrl_y(cx: &mut gpui::TestAppContext) {
197        let mut cx = NeovimBackedTestContext::new(cx).await;
198
199        cx.set_shared_state("hello\nˇ\nworld").await;
200        cx.simulate_shared_keystrokes("i ctrl-y ctrl-e").await;
201        cx.shared_state().await.assert_eq("hello\nhoˇ\nworld");
202    }
203}