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