1mod editor_events;
2mod insert;
3mod motion;
4mod normal;
5mod state;
6#[cfg(test)]
7mod vim_test_context;
8
9use collections::HashMap;
10use editor::{CursorShape, Editor};
11use gpui::{impl_actions, MutableAppContext, ViewContext, WeakViewHandle};
12use serde::Deserialize;
13
14use settings::Settings;
15use state::{Mode, Operator, VimState};
16use workspace::{self, Workspace};
17
18#[derive(Clone, Deserialize)]
19pub struct SwitchMode(pub Mode);
20
21#[derive(Clone, Deserialize)]
22pub struct PushOperator(pub Operator);
23
24impl_actions!(vim, [SwitchMode, PushOperator]);
25
26pub fn init(cx: &mut MutableAppContext) {
27 editor_events::init(cx);
28 insert::init(cx);
29 motion::init(cx);
30
31 cx.add_action(|_: &mut Workspace, &SwitchMode(mode): &SwitchMode, cx| {
32 Vim::update(cx, |vim, cx| vim.switch_mode(mode, cx))
33 });
34 cx.add_action(
35 |_: &mut Workspace, &PushOperator(operator): &PushOperator, cx| {
36 Vim::update(cx, |vim, cx| vim.push_operator(operator, cx))
37 },
38 );
39
40 cx.observe_global::<Settings, _>(|settings, cx| {
41 Vim::update(cx, |state, cx| state.set_enabled(settings.vim_mode, cx))
42 })
43 .detach();
44}
45
46#[derive(Default)]
47pub struct Vim {
48 editors: HashMap<usize, WeakViewHandle<Editor>>,
49 active_editor: Option<WeakViewHandle<Editor>>,
50
51 enabled: bool,
52 state: VimState,
53}
54
55impl Vim {
56 fn read(cx: &mut MutableAppContext) -> &Self {
57 cx.default_global()
58 }
59
60 fn update<F, S>(cx: &mut MutableAppContext, update: F) -> S
61 where
62 F: FnOnce(&mut Self, &mut MutableAppContext) -> S,
63 {
64 cx.update_default_global(update)
65 }
66
67 fn update_active_editor<S>(
68 &self,
69 cx: &mut MutableAppContext,
70 update: impl FnOnce(&mut Editor, &mut ViewContext<Editor>) -> S,
71 ) -> Option<S> {
72 self.active_editor
73 .clone()
74 .and_then(|ae| ae.upgrade(cx))
75 .map(|ae| ae.update(cx, update))
76 }
77
78 fn switch_mode(&mut self, mode: Mode, cx: &mut MutableAppContext) {
79 self.state.mode = mode;
80 self.state.operator_stack.clear();
81 self.sync_editor_options(cx);
82 }
83
84 fn push_operator(&mut self, operator: Operator, cx: &mut MutableAppContext) {
85 self.state.operator_stack.push(operator);
86 self.sync_editor_options(cx);
87 }
88
89 fn pop_operator(&mut self, cx: &mut MutableAppContext) -> Operator {
90 let popped_operator = self.state.operator_stack.pop().expect("Operator popped when no operator was on the stack. This likely means there is an invalid keymap config");
91 self.sync_editor_options(cx);
92 popped_operator
93 }
94
95 fn clear_operator(&mut self, cx: &mut MutableAppContext) {
96 self.state.operator_stack.clear();
97 self.sync_editor_options(cx);
98 }
99
100 fn active_operator(&mut self) -> Option<Operator> {
101 self.state.operator_stack.last().copied()
102 }
103
104 fn set_enabled(&mut self, enabled: bool, cx: &mut MutableAppContext) {
105 if self.enabled != enabled {
106 self.enabled = enabled;
107 self.state = Default::default();
108 if enabled {
109 self.state.mode = Mode::Normal;
110 }
111 self.sync_editor_options(cx);
112 }
113 }
114
115 fn sync_editor_options(&self, cx: &mut MutableAppContext) {
116 let state = &self.state;
117 let cursor_shape = state.cursor_shape();
118 for editor in self.editors.values() {
119 if let Some(editor) = editor.upgrade(cx) {
120 editor.update(cx, |editor, cx| {
121 if self.enabled {
122 editor.set_cursor_shape(cursor_shape, cx);
123 editor.set_clip_at_line_ends(cursor_shape == CursorShape::Block, cx);
124 editor.set_input_enabled(!state.vim_controlled());
125 let context_layer = state.keymap_context_layer();
126 editor.set_keymap_context_layer::<Self>(context_layer);
127 } else {
128 editor.set_cursor_shape(CursorShape::Bar, cx);
129 editor.set_clip_at_line_ends(false, cx);
130 editor.set_input_enabled(true);
131 editor.remove_keymap_context_layer::<Self>();
132 }
133 });
134 }
135 }
136 }
137}
138
139#[cfg(test)]
140mod test {
141 use crate::{state::Mode, vim_test_context::VimTestContext};
142
143 #[gpui::test]
144 async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
145 let mut cx = VimTestContext::new(cx, false, "").await;
146 cx.simulate_keystrokes(["h", "j", "k", "l"]);
147 cx.assert_editor_state("hjkl|");
148 }
149
150 #[gpui::test]
151 async fn test_toggle_through_settings(cx: &mut gpui::TestAppContext) {
152 let mut cx = VimTestContext::new(cx, true, "").await;
153
154 cx.simulate_keystroke("i");
155 assert_eq!(cx.mode(), Mode::Insert);
156
157 // Editor acts as though vim is disabled
158 cx.disable_vim();
159 cx.simulate_keystrokes(["h", "j", "k", "l"]);
160 cx.assert_editor_state("hjkl|");
161
162 // Enabling dynamically sets vim mode again and restores normal mode
163 cx.enable_vim();
164 assert_eq!(cx.mode(), Mode::Normal);
165 cx.simulate_keystrokes(["h", "h", "h", "l"]);
166 assert_eq!(cx.editor_text(), "hjkl".to_owned());
167 cx.assert_editor_state("hj|kl");
168 cx.simulate_keystrokes(["i", "T", "e", "s", "t"]);
169 cx.assert_editor_state("hjTest|kl");
170
171 // Disabling and enabling resets to normal mode
172 assert_eq!(cx.mode(), Mode::Insert);
173 cx.disable_vim();
174 cx.enable_vim();
175 assert_eq!(cx.mode(), Mode::Normal);
176 }
177}