2022-04-21 23:14:58 +00:00
|
|
|
#[cfg(test)]
|
2022-10-10 21:46:07 +00:00
|
|
|
mod test;
|
2022-04-21 23:14:58 +00:00
|
|
|
|
2022-03-25 02:24:36 +00:00
|
|
|
mod editor_events;
|
|
|
|
mod insert;
|
2022-04-15 23:00:44 +00:00
|
|
|
mod motion;
|
2022-03-25 02:24:36 +00:00
|
|
|
mod normal;
|
2022-10-06 03:19:30 +00:00
|
|
|
mod object;
|
2022-04-15 23:00:44 +00:00
|
|
|
mod state;
|
2022-05-20 00:42:30 +00:00
|
|
|
mod utils;
|
2022-05-03 17:29:57 +00:00
|
|
|
mod visual;
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
use collections::HashMap;
|
2022-06-28 20:35:43 +00:00
|
|
|
use command_palette::CommandPaletteFilter;
|
2022-10-15 22:08:21 +00:00
|
|
|
use editor::{Bias, Cancel, Editor};
|
2022-05-18 18:10:24 +00:00
|
|
|
use gpui::{impl_actions, MutableAppContext, Subscription, ViewContext, WeakViewHandle};
|
2022-10-15 22:08:21 +00:00
|
|
|
use language::CursorShape;
|
2022-04-08 22:32:56 +00:00
|
|
|
use serde::Deserialize;
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2022-04-06 00:10:17 +00:00
|
|
|
use settings::Settings;
|
2022-04-15 23:00:44 +00:00
|
|
|
use state::{Mode, Operator, VimState};
|
2022-04-06 00:10:17 +00:00
|
|
|
use workspace::{self, Workspace};
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2022-06-06 07:18:44 +00:00
|
|
|
#[derive(Clone, Deserialize, PartialEq)]
|
2022-04-07 23:20:49 +00:00
|
|
|
pub struct SwitchMode(pub Mode);
|
|
|
|
|
2022-06-06 07:18:44 +00:00
|
|
|
#[derive(Clone, Deserialize, PartialEq)]
|
2022-04-15 23:00:44 +00:00
|
|
|
pub struct PushOperator(pub Operator);
|
|
|
|
|
2022-10-09 04:20:47 +00:00
|
|
|
#[derive(Clone, Deserialize, PartialEq)]
|
|
|
|
struct Number(u8);
|
|
|
|
|
|
|
|
impl_actions!(vim, [Number, SwitchMode, PushOperator]);
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
editor_events::init(cx);
|
2022-04-21 23:14:58 +00:00
|
|
|
normal::init(cx);
|
2022-05-03 17:29:57 +00:00
|
|
|
visual::init(cx);
|
2022-03-25 02:24:36 +00:00
|
|
|
insert::init(cx);
|
2022-10-06 03:19:30 +00:00
|
|
|
object::init(cx);
|
2022-04-15 23:00:44 +00:00
|
|
|
motion::init(cx);
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2022-06-30 19:32:53 +00:00
|
|
|
// Vim Actions
|
2022-04-15 23:00:44 +00:00
|
|
|
cx.add_action(|_: &mut Workspace, &SwitchMode(mode): &SwitchMode, cx| {
|
2022-07-18 20:33:55 +00:00
|
|
|
Vim::update(cx, |vim, cx| vim.switch_mode(mode, false, cx))
|
2022-03-26 20:30:55 +00:00
|
|
|
});
|
2022-04-15 23:00:44 +00:00
|
|
|
cx.add_action(
|
|
|
|
|_: &mut Workspace, &PushOperator(operator): &PushOperator, cx| {
|
|
|
|
Vim::update(cx, |vim, cx| vim.push_operator(operator, cx))
|
|
|
|
},
|
|
|
|
);
|
2022-10-09 04:20:47 +00:00
|
|
|
cx.add_action(|_: &mut Workspace, n: &Number, cx: _| {
|
|
|
|
Vim::update(cx, |vim, cx| vim.push_number(n, cx));
|
|
|
|
});
|
2022-06-30 19:32:53 +00:00
|
|
|
|
|
|
|
// Editor Actions
|
|
|
|
cx.add_action(|_: &mut Editor, _: &Cancel, cx| {
|
|
|
|
// If we are in a non normal mode or have an active operator, swap to normal mode
|
|
|
|
// Otherwise forward cancel on to the editor
|
|
|
|
let vim = Vim::read(cx);
|
|
|
|
if vim.state.mode != Mode::Normal || vim.active_operator().is_some() {
|
|
|
|
MutableAppContext::defer(cx, |cx| {
|
|
|
|
Vim::update(cx, |state, cx| {
|
2022-07-18 20:33:55 +00:00
|
|
|
state.switch_mode(Mode::Normal, false, cx);
|
2022-06-30 19:32:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cx.propagate_action();
|
|
|
|
}
|
|
|
|
});
|
2022-03-25 02:24:36 +00:00
|
|
|
|
2022-06-29 18:58:12 +00:00
|
|
|
// Sync initial settings with the rest of the app
|
|
|
|
Vim::update(cx, |state, cx| state.sync_vim_settings(cx));
|
|
|
|
|
|
|
|
// Any time settings change, update vim mode to match
|
2022-05-23 16:23:25 +00:00
|
|
|
cx.observe_global::<Settings, _>(|cx| {
|
|
|
|
Vim::update(cx, |state, cx| {
|
|
|
|
state.set_enabled(cx.global::<Settings>().vim_mode, cx)
|
|
|
|
})
|
2022-03-26 20:30:55 +00:00
|
|
|
})
|
|
|
|
.detach();
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 00:39:32 +00:00
|
|
|
// Any keystrokes not mapped to vim should clar the active operator
|
|
|
|
pub fn observe_keypresses(window_id: usize, cx: &mut MutableAppContext) {
|
|
|
|
cx.observe_keystrokes(window_id, |_keystroke, _result, handled_by, cx| {
|
|
|
|
dbg!(_keystroke);
|
|
|
|
dbg!(_result);
|
|
|
|
if let Some(handled_by) = handled_by {
|
|
|
|
dbg!(handled_by.name());
|
|
|
|
if handled_by.namespace() == "vim" {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vim::update(cx, |vim, cx| {
|
|
|
|
if vim.active_operator().is_some() {
|
|
|
|
vim.clear_operator(cx);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
true
|
|
|
|
})
|
|
|
|
.detach()
|
|
|
|
}
|
|
|
|
|
2022-03-25 02:24:36 +00:00
|
|
|
#[derive(Default)]
|
2022-04-15 23:00:44 +00:00
|
|
|
pub struct Vim {
|
2022-03-25 02:24:36 +00:00
|
|
|
editors: HashMap<usize, WeakViewHandle<Editor>>,
|
|
|
|
active_editor: Option<WeakViewHandle<Editor>>,
|
2022-05-18 18:10:24 +00:00
|
|
|
selection_subscription: Option<Subscription>,
|
2022-03-25 02:24:36 +00:00
|
|
|
|
|
|
|
enabled: bool,
|
2022-04-15 23:00:44 +00:00
|
|
|
state: VimState,
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 23:00:44 +00:00
|
|
|
impl Vim {
|
|
|
|
fn read(cx: &mut MutableAppContext) -> &Self {
|
|
|
|
cx.default_global()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update<F, S>(cx: &mut MutableAppContext, update: F) -> S
|
2022-03-26 20:30:55 +00:00
|
|
|
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-07-18 20:33:55 +00:00
|
|
|
fn switch_mode(&mut self, mode: Mode, leave_selections: bool, cx: &mut MutableAppContext) {
|
2022-04-15 23:00:44 +00:00
|
|
|
self.state.mode = mode;
|
|
|
|
self.state.operator_stack.clear();
|
2022-06-30 19:32:53 +00:00
|
|
|
|
|
|
|
// Sync editor settings like clip mode
|
2022-06-29 18:58:12 +00:00
|
|
|
self.sync_vim_settings(cx);
|
2022-06-30 19:32:53 +00:00
|
|
|
|
2022-07-18 20:33:55 +00:00
|
|
|
if leave_selections {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-30 19:32:53 +00:00
|
|
|
// Adjust selections
|
|
|
|
for editor in self.editors.values() {
|
|
|
|
if let Some(editor) = editor.upgrade(cx) {
|
|
|
|
editor.update(cx, |editor, cx| {
|
|
|
|
editor.change_selections(None, cx, |s| {
|
|
|
|
s.move_with(|map, selection| {
|
|
|
|
if self.state.empty_selections_only() {
|
|
|
|
let new_head = map.clip_point(selection.head(), Bias::Left);
|
|
|
|
selection.collapse_to(new_head, selection.goal)
|
|
|
|
} else {
|
|
|
|
selection.set_head(
|
|
|
|
map.clip_point(selection.head(), Bias::Left),
|
|
|
|
selection.goal,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 23:00:44 +00:00
|
|
|
fn push_operator(&mut self, operator: Operator, cx: &mut MutableAppContext) {
|
|
|
|
self.state.operator_stack.push(operator);
|
2022-06-29 18:58:12 +00:00
|
|
|
self.sync_vim_settings(cx);
|
2022-04-15 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 04:20:47 +00:00
|
|
|
fn push_number(&mut self, Number(number): &Number, cx: &mut MutableAppContext) {
|
|
|
|
if let Some(Operator::Number(current_number)) = self.active_operator() {
|
|
|
|
self.pop_operator(cx);
|
|
|
|
self.push_operator(Operator::Number(current_number * 10 + *number as usize), cx);
|
|
|
|
} else {
|
|
|
|
self.push_operator(Operator::Number(*number as usize), cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 23:00:44 +00:00
|
|
|
fn pop_operator(&mut self, cx: &mut MutableAppContext) -> Operator {
|
2022-10-06 03:19:30 +00:00
|
|
|
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");
|
2022-06-29 18:58:12 +00:00
|
|
|
self.sync_vim_settings(cx);
|
2022-04-15 23:00:44 +00:00
|
|
|
popped_operator
|
|
|
|
}
|
|
|
|
|
2022-10-09 04:20:47 +00:00
|
|
|
fn pop_number_operator(&mut self, cx: &mut MutableAppContext) -> usize {
|
|
|
|
let mut times = 1;
|
|
|
|
if let Some(Operator::Number(number)) = self.active_operator() {
|
|
|
|
times = number;
|
|
|
|
self.pop_operator(cx);
|
|
|
|
}
|
|
|
|
times
|
|
|
|
}
|
|
|
|
|
2022-04-15 23:00:44 +00:00
|
|
|
fn clear_operator(&mut self, cx: &mut MutableAppContext) {
|
|
|
|
self.state.operator_stack.clear();
|
2022-06-29 18:58:12 +00:00
|
|
|
self.sync_vim_settings(cx);
|
2022-04-15 23:00:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 18:04:26 +00:00
|
|
|
fn active_operator(&self) -> Option<Operator> {
|
2022-04-15 23:00:44 +00:00
|
|
|
self.state.operator_stack.last().copied()
|
|
|
|
}
|
|
|
|
|
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-04-15 23:00:44 +00:00
|
|
|
self.state = Default::default();
|
2022-03-27 00:38:00 +00:00
|
|
|
if enabled {
|
2022-07-18 20:33:55 +00:00
|
|
|
self.switch_mode(Mode::Normal, false, cx);
|
2022-03-27 00:38:00 +00:00
|
|
|
}
|
2022-06-29 18:58:12 +00:00
|
|
|
self.sync_vim_settings(cx);
|
2022-03-26 20:30:55 +00:00
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 18:58:12 +00:00
|
|
|
fn sync_vim_settings(&self, cx: &mut MutableAppContext) {
|
2022-04-15 23:00:44 +00:00
|
|
|
let state = &self.state;
|
|
|
|
let cursor_shape = state.cursor_shape();
|
2022-05-23 18:04:26 +00:00
|
|
|
|
2022-06-29 18:58:12 +00:00
|
|
|
cx.update_default_global::<CommandPaletteFilter, _, _>(|filter, _| {
|
|
|
|
if self.enabled {
|
|
|
|
filter.filtered_namespaces.remove("vim");
|
|
|
|
} else {
|
|
|
|
filter.filtered_namespaces.insert("vim");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-26 20:30:55 +00:00
|
|
|
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);
|
2022-05-23 18:04:26 +00:00
|
|
|
editor.set_clip_at_line_ends(state.clip_at_line_end(), cx);
|
2022-04-15 23:00:44 +00:00
|
|
|
editor.set_input_enabled(!state.vim_controlled());
|
2022-05-24 20:35:57 +00:00
|
|
|
editor.selections.line_mode =
|
|
|
|
matches!(state.mode, Mode::Visual { line: true });
|
2022-04-15 23:00:44 +00:00
|
|
|
let context_layer = state.keymap_context_layer();
|
2022-03-26 20:30:55 +00:00
|
|
|
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);
|
2022-05-19 00:41:26 +00:00
|
|
|
editor.selections.line_mode = false;
|
2022-03-26 20:30:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|