2022-03-25 02:24:36 +00:00
|
|
|
mod editor_events;
|
|
|
|
mod insert;
|
|
|
|
mod mode;
|
|
|
|
mod normal;
|
|
|
|
#[cfg(test)]
|
2022-03-28 00:58:28 +00:00
|
|
|
mod vim_test_context;
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
use collections::HashMap;
|
2022-03-26 01:09:37 +00:00
|
|
|
use editor::{CursorShape, Editor};
|
2022-04-07 23:20:49 +00:00
|
|
|
use gpui::{impl_actions, MutableAppContext, ViewContext, WeakViewHandle};
|
2022-04-08 22:32:56 +00:00
|
|
|
use serde::Deserialize;
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
use mode::Mode;
|
2022-04-06 00:10:17 +00:00
|
|
|
use settings::Settings;
|
|
|
|
use workspace::{self, Workspace};
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
#[derive(Clone, Deserialize)]
|
2022-04-07 23:20:49 +00:00
|
|
|
pub struct SwitchMode(pub Mode);
|
|
|
|
|
|
|
|
impl_actions!(vim, [SwitchMode]);
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
editor_events::init(cx);
|
|
|
|
insert::init(cx);
|
|
|
|
normal::init(cx);
|
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
cx.add_action(|_: &mut Workspace, action: &SwitchMode, cx| {
|
|
|
|
VimState::update_global(cx, |state, cx| state.switch_mode(action, cx))
|
|
|
|
});
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
cx.observe_global::<Settings, _>(|settings, cx| {
|
|
|
|
VimState::update_global(cx, |state, cx| state.set_enabled(settings.vim_mode, cx))
|
|
|
|
})
|
|
|
|
.detach();
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct VimState {
|
|
|
|
editors: HashMap<usize, WeakViewHandle<Editor>>,
|
|
|
|
active_editor: Option<WeakViewHandle<Editor>>,
|
|
|
|
|
|
|
|
enabled: bool,
|
|
|
|
mode: Mode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VimState {
|
2022-03-26 20:30:55 +00:00
|
|
|
fn update_global<F, S>(cx: &mut MutableAppContext, update: F) -> S
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self, &mut MutableAppContext) -> S,
|
|
|
|
{
|
|
|
|
cx.update_default_global(update)
|
|
|
|
}
|
|
|
|
|
2022-03-25 02:24:36 +00:00
|
|
|
fn update_active_editor<S>(
|
2022-03-26 20:30:55 +00:00
|
|
|
&self,
|
2022-03-25 02:24:36 +00:00
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
update: impl FnOnce(&mut Editor, &mut ViewContext<Editor>) -> S,
|
|
|
|
) -> Option<S> {
|
2022-03-26 20:30:55 +00:00
|
|
|
self.active_editor
|
2022-03-25 02:24:36 +00:00
|
|
|
.clone()
|
|
|
|
.and_then(|ae| ae.upgrade(cx))
|
|
|
|
.map(|ae| ae.update(cx, update))
|
|
|
|
}
|
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
fn switch_mode(&mut self, SwitchMode(mode): &SwitchMode, cx: &mut MutableAppContext) {
|
|
|
|
self.mode = *mode;
|
|
|
|
self.sync_editor_options(cx);
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
fn set_enabled(&mut self, enabled: bool, cx: &mut MutableAppContext) {
|
|
|
|
if self.enabled != enabled {
|
|
|
|
self.enabled = enabled;
|
2022-03-28 00:58:28 +00:00
|
|
|
self.mode = Default::default();
|
2022-03-27 00:38:00 +00:00
|
|
|
if enabled {
|
2022-03-28 00:58:28 +00:00
|
|
|
self.mode = Mode::normal();
|
2022-03-27 00:38:00 +00:00
|
|
|
}
|
2022-03-26 20:30:55 +00:00
|
|
|
self.sync_editor_options(cx);
|
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
fn sync_editor_options(&self, cx: &mut MutableAppContext) {
|
|
|
|
let mode = self.mode;
|
|
|
|
let cursor_shape = mode.cursor_shape();
|
|
|
|
for editor in self.editors.values() {
|
|
|
|
if let Some(editor) = editor.upgrade(cx) {
|
|
|
|
editor.update(cx, |editor, cx| {
|
|
|
|
if self.enabled {
|
|
|
|
editor.set_cursor_shape(cursor_shape, cx);
|
|
|
|
editor.set_clip_at_line_ends(cursor_shape == CursorShape::Block, cx);
|
|
|
|
editor.set_input_enabled(mode == Mode::Insert);
|
|
|
|
let context_layer = mode.keymap_context_layer();
|
|
|
|
editor.set_keymap_context_layer::<Self>(context_layer);
|
|
|
|
} else {
|
|
|
|
editor.set_cursor_shape(CursorShape::Bar, cx);
|
|
|
|
editor.set_clip_at_line_ends(false, cx);
|
|
|
|
editor.set_input_enabled(true);
|
|
|
|
editor.remove_keymap_context_layer::<Self>();
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
2022-03-26 20:30:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 00:58:28 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::{mode::Mode, vim_test_context::VimTestContext};
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_initially_disabled(cx: &mut gpui::TestAppContext) {
|
|
|
|
let mut cx = VimTestContext::new(cx, false, "").await;
|
|
|
|
cx.simulate_keystrokes(&["h", "j", "k", "l"]);
|
|
|
|
cx.assert_editor_state("hjkl|");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_toggle_through_settings(cx: &mut gpui::TestAppContext) {
|
|
|
|
let mut cx = VimTestContext::new(cx, true, "").await;
|
|
|
|
|
|
|
|
cx.simulate_keystroke("i");
|
|
|
|
assert_eq!(cx.mode(), Mode::Insert);
|
|
|
|
|
|
|
|
// Editor acts as though vim is disabled
|
|
|
|
cx.disable_vim();
|
|
|
|
cx.simulate_keystrokes(&["h", "j", "k", "l"]);
|
|
|
|
cx.assert_editor_state("hjkl|");
|
|
|
|
|
|
|
|
// Enabling dynamically sets vim mode again and restores normal mode
|
|
|
|
cx.enable_vim();
|
|
|
|
assert_eq!(cx.mode(), Mode::normal());
|
|
|
|
cx.simulate_keystrokes(&["h", "h", "h", "l"]);
|
|
|
|
assert_eq!(cx.editor_text(), "hjkl".to_owned());
|
|
|
|
cx.assert_editor_state("hj|kl");
|
|
|
|
cx.simulate_keystrokes(&["i", "T", "e", "s", "t"]);
|
|
|
|
cx.assert_editor_state("hjTest|kl");
|
|
|
|
|
|
|
|
// Disabling and enabling resets to normal mode
|
|
|
|
assert_eq!(cx.mode(), Mode::Insert);
|
|
|
|
cx.disable_vim();
|
|
|
|
cx.enable_vim();
|
|
|
|
assert_eq!(cx.mode(), Mode::normal());
|
|
|
|
}
|
|
|
|
}
|