vim.rs

  1mod editor_events;
  2mod insert;
  3mod mode;
  4mod normal;
  5#[cfg(test)]
  6mod vim_test_context;
  7
  8use collections::HashMap;
  9use editor::{CursorShape, Editor};
 10use gpui::{impl_actions, MutableAppContext, ViewContext, WeakViewHandle};
 11
 12use mode::Mode;
 13use settings::Settings;
 14use workspace::{self, Workspace};
 15
 16#[derive(Clone)]
 17pub struct SwitchMode(pub Mode);
 18
 19impl_actions!(vim, [SwitchMode]);
 20
 21pub fn init(cx: &mut MutableAppContext) {
 22    editor_events::init(cx);
 23    insert::init(cx);
 24    normal::init(cx);
 25
 26    cx.add_action(|_: &mut Workspace, action: &SwitchMode, cx| {
 27        VimState::update_global(cx, |state, cx| state.switch_mode(action, cx))
 28    });
 29
 30    cx.observe_global::<Settings, _>(|settings, cx| {
 31        VimState::update_global(cx, |state, cx| state.set_enabled(settings.vim_mode, cx))
 32    })
 33    .detach();
 34}
 35
 36#[derive(Default)]
 37pub struct VimState {
 38    editors: HashMap<usize, WeakViewHandle<Editor>>,
 39    active_editor: Option<WeakViewHandle<Editor>>,
 40
 41    enabled: bool,
 42    mode: Mode,
 43}
 44
 45impl VimState {
 46    fn update_global<F, S>(cx: &mut MutableAppContext, update: F) -> S
 47    where
 48        F: FnOnce(&mut Self, &mut MutableAppContext) -> S,
 49    {
 50        cx.update_default_global(update)
 51    }
 52
 53    fn update_active_editor<S>(
 54        &self,
 55        cx: &mut MutableAppContext,
 56        update: impl FnOnce(&mut Editor, &mut ViewContext<Editor>) -> S,
 57    ) -> Option<S> {
 58        self.active_editor
 59            .clone()
 60            .and_then(|ae| ae.upgrade(cx))
 61            .map(|ae| ae.update(cx, update))
 62    }
 63
 64    fn switch_mode(&mut self, SwitchMode(mode): &SwitchMode, cx: &mut MutableAppContext) {
 65        self.mode = *mode;
 66        self.sync_editor_options(cx);
 67    }
 68
 69    fn set_enabled(&mut self, enabled: bool, cx: &mut MutableAppContext) {
 70        if self.enabled != enabled {
 71            self.enabled = enabled;
 72            self.mode = Default::default();
 73            if enabled {
 74                self.mode = Mode::normal();
 75            }
 76            self.sync_editor_options(cx);
 77        }
 78    }
 79
 80    fn sync_editor_options(&self, cx: &mut MutableAppContext) {
 81        let mode = self.mode;
 82        let cursor_shape = mode.cursor_shape();
 83        for editor in self.editors.values() {
 84            if let Some(editor) = editor.upgrade(cx) {
 85                editor.update(cx, |editor, cx| {
 86                    if self.enabled {
 87                        editor.set_cursor_shape(cursor_shape, cx);
 88                        editor.set_clip_at_line_ends(cursor_shape == CursorShape::Block, cx);
 89                        editor.set_input_enabled(mode == Mode::Insert);
 90                        let context_layer = mode.keymap_context_layer();
 91                        editor.set_keymap_context_layer::<Self>(context_layer);
 92                    } else {
 93                        editor.set_cursor_shape(CursorShape::Bar, cx);
 94                        editor.set_clip_at_line_ends(false, cx);
 95                        editor.set_input_enabled(true);
 96                        editor.remove_keymap_context_layer::<Self>();
 97                    }
 98                });
 99            }
100        }
101    }
102}
103
104#[cfg(test)]
105mod test {
106    use crate::{mode::Mode, vim_test_context::VimTestContext};
107
108    #[gpui::test]
109    async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
110        let mut cx = VimTestContext::new(cx, false, "").await;
111        cx.simulate_keystrokes(&["h", "j", "k", "l"]);
112        cx.assert_editor_state("hjkl|");
113    }
114
115    #[gpui::test]
116    async fn test_toggle_through_settings(cx: &mut gpui::TestAppContext) {
117        let mut cx = VimTestContext::new(cx, true, "").await;
118
119        cx.simulate_keystroke("i");
120        assert_eq!(cx.mode(), Mode::Insert);
121
122        // Editor acts as though vim is disabled
123        cx.disable_vim();
124        cx.simulate_keystrokes(&["h", "j", "k", "l"]);
125        cx.assert_editor_state("hjkl|");
126
127        // Enabling dynamically sets vim mode again and restores normal mode
128        cx.enable_vim();
129        assert_eq!(cx.mode(), Mode::normal());
130        cx.simulate_keystrokes(&["h", "h", "h", "l"]);
131        assert_eq!(cx.editor_text(), "hjkl".to_owned());
132        cx.assert_editor_state("hj|kl");
133        cx.simulate_keystrokes(&["i", "T", "e", "s", "t"]);
134        cx.assert_editor_state("hjTest|kl");
135
136        // Disabling and enabling resets to normal mode
137        assert_eq!(cx.mode(), Mode::Insert);
138        cx.disable_vim();
139        cx.enable_vim();
140        assert_eq!(cx.mode(), Mode::normal());
141    }
142}