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