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