vim.rs

 1mod editor_events;
 2mod insert;
 3mod mode;
 4mod normal;
 5#[cfg(test)]
 6mod vim_tests;
 7
 8use collections::HashMap;
 9use editor::{CursorShape, Editor};
10use gpui::{action, MutableAppContext, ViewContext, WeakViewHandle};
11
12use mode::Mode;
13use workspace::{self, Settings, Workspace};
14
15action!(SwitchMode, Mode);
16
17pub fn init(cx: &mut MutableAppContext) {
18    editor_events::init(cx);
19    insert::init(cx);
20    normal::init(cx);
21
22    cx.add_action(|_: &mut Workspace, action: &SwitchMode, cx| {
23        VimState::update_global(cx, |state, cx| state.switch_mode(action, cx))
24    });
25
26    cx.observe_global::<Settings, _>(|settings, cx| {
27        VimState::update_global(cx, |state, cx| state.set_enabled(settings.vim_mode, cx))
28    })
29    .detach();
30}
31
32#[derive(Default)]
33pub struct VimState {
34    editors: HashMap<usize, WeakViewHandle<Editor>>,
35    active_editor: Option<WeakViewHandle<Editor>>,
36
37    enabled: bool,
38    mode: Mode,
39}
40
41impl VimState {
42    fn update_global<F, S>(cx: &mut MutableAppContext, update: F) -> S
43    where
44        F: FnOnce(&mut Self, &mut MutableAppContext) -> S,
45    {
46        cx.update_default_global(update)
47    }
48
49    fn update_active_editor<S>(
50        &self,
51        cx: &mut MutableAppContext,
52        update: impl FnOnce(&mut Editor, &mut ViewContext<Editor>) -> S,
53    ) -> Option<S> {
54        self.active_editor
55            .clone()
56            .and_then(|ae| ae.upgrade(cx))
57            .map(|ae| ae.update(cx, update))
58    }
59
60    fn switch_mode(&mut self, SwitchMode(mode): &SwitchMode, cx: &mut MutableAppContext) {
61        self.mode = *mode;
62        self.sync_editor_options(cx);
63    }
64
65    fn set_enabled(&mut self, enabled: bool, cx: &mut MutableAppContext) {
66        if self.enabled != enabled {
67            self.enabled = enabled;
68            if enabled {
69                self.mode = Mode::Normal;
70            }
71            self.sync_editor_options(cx);
72        }
73    }
74
75    fn sync_editor_options(&self, cx: &mut MutableAppContext) {
76        let mode = self.mode;
77        let cursor_shape = mode.cursor_shape();
78        for editor in self.editors.values() {
79            if let Some(editor) = editor.upgrade(cx) {
80                editor.update(cx, |editor, cx| {
81                    if self.enabled {
82                        editor.set_cursor_shape(cursor_shape, cx);
83                        editor.set_clip_at_line_ends(cursor_shape == CursorShape::Block, cx);
84                        editor.set_input_enabled(mode == Mode::Insert);
85                        let context_layer = mode.keymap_context_layer();
86                        editor.set_keymap_context_layer::<Self>(context_layer);
87                    } else {
88                        editor.set_cursor_shape(CursorShape::Bar, cx);
89                        editor.set_clip_at_line_ends(false, cx);
90                        editor.set_input_enabled(true);
91                        editor.remove_keymap_context_layer::<Self>();
92                    }
93                });
94            }
95        }
96    }
97}