1use crate::{mode::Mode, SwitchMode, VimState};
2use editor::Bias;
3use gpui::{actions, keymap::Binding, MutableAppContext, ViewContext};
4use language::SelectionGoal;
5use workspace::Workspace;
6
7actions!(vim, [NormalBefore]);
8
9pub fn init(cx: &mut MutableAppContext) {
10 let context = Some("Editor && vim_mode == insert");
11 cx.add_bindings(vec![
12 Binding::new("escape", NormalBefore, context),
13 Binding::new("ctrl-c", NormalBefore, context),
14 ]);
15
16 cx.add_action(normal_before);
17}
18
19fn normal_before(_: &mut Workspace, _: &NormalBefore, cx: &mut ViewContext<Workspace>) {
20 VimState::update_global(cx, |state, cx| {
21 state.update_active_editor(cx, |editor, cx| {
22 editor.move_cursors(cx, |map, mut cursor, _| {
23 *cursor.column_mut() = cursor.column().saturating_sub(1);
24 (map.clip_point(cursor, Bias::Left), SelectionGoal::None)
25 });
26 });
27 state.switch_mode(&SwitchMode(Mode::normal()), cx);
28 })
29}
30
31#[cfg(test)]
32mod test {
33 use crate::{mode::Mode, vim_test_context::VimTestContext};
34
35 #[gpui::test]
36 async fn test_enter_and_exit_insert_mode(cx: &mut gpui::TestAppContext) {
37 let mut cx = VimTestContext::new(cx, true, "").await;
38 cx.simulate_keystroke("i");
39 assert_eq!(cx.mode(), Mode::Insert);
40 cx.simulate_keystrokes(&["T", "e", "s", "t"]);
41 cx.assert_editor_state("Test|");
42 cx.simulate_keystroke("escape");
43 assert_eq!(cx.mode(), Mode::normal());
44 cx.assert_editor_state("Tes|t");
45 }
46}