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
|
|
|
|
2023-02-10 22:41:22 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-28 20:35:43 +00:00
|
|
|
use command_palette::CommandPaletteFilter;
|
2023-01-18 01:32:10 +00:00
|
|
|
use editor::{Bias, Cancel, Editor, EditorMode};
|
2023-01-09 23:44:40 +00:00
|
|
|
use gpui::{
|
2023-02-10 23:41:19 +00:00
|
|
|
actions, impl_actions, MutableAppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
|
2023-01-09 23:44:40 +00:00
|
|
|
};
|
2022-10-15 22:08:21 +00:00
|
|
|
use language::CursorShape;
|
2023-01-09 23:44:40 +00:00
|
|
|
use motion::Motion;
|
|
|
|
use normal::normal_replace;
|
2022-04-08 22:32:56 +00:00
|
|
|
use serde::Deserialize;
|
2022-04-06 00:10:17 +00:00
|
|
|
use settings::Settings;
|
2022-04-15 23:00:44 +00:00
|
|
|
use state::{Mode, Operator, VimState};
|
2023-01-09 23:44:40 +00:00
|
|
|
use visual::visual_replace;
|
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);
|
|
|
|
|
2023-02-10 23:41:19 +00:00
|
|
|
actions!(vim, [Tab, Enter]);
|
2022-10-09 04:20:47 +00:00
|
|
|
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| {
|
2023-01-06 22:03:01 +00:00
|
|
|
// If we are in aren't in normal mode or have an active operator, swap to normal mode
|
2022-06-30 19:32:53 +00:00
|
|
|
// 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
|
|
|
|
2023-02-10 23:41:19 +00:00
|
|
|
cx.add_action(|_: &mut Workspace, _: &Tab, cx| {
|
|
|
|
Vim::active_editor_input_ignored(" ".into(), cx)
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.add_action(|_: &mut Workspace, _: &Enter, cx| {
|
|
|
|
Vim::active_editor_input_ignored("\n".into(), cx)
|
|
|
|
});
|
|
|
|
|
2022-06-29 18:58:12 +00:00
|
|
|
// Sync initial settings with the rest of the app
|
2023-02-10 23:41:19 +00:00
|
|
|
Vim::update(cx, |vim, cx| vim.sync_vim_settings(cx));
|
2022-06-29 18:58:12 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2023-02-10 22:41:22 +00:00
|
|
|
pub fn observe_keystrokes(window_id: usize, cx: &mut MutableAppContext) {
|
2022-12-08 00:39:32 +00:00
|
|
|
cx.observe_keystrokes(window_id, |_keystroke, _result, handled_by, cx| {
|
|
|
|
if let Some(handled_by) = handled_by {
|
2023-01-06 22:03:01 +00:00
|
|
|
// Keystroke is handled by the vim system, so continue forward
|
|
|
|
// Also short circuit if it is the special cancel action
|
|
|
|
if handled_by.namespace() == "vim"
|
|
|
|
|| (handled_by.namespace() == "editor" && handled_by.name() == "Cancel")
|
|
|
|
{
|
2022-12-08 00:39:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 22:41:22 +00:00
|
|
|
Vim::update(cx, |vim, cx| match vim.active_operator() {
|
2023-02-10 23:41:19 +00:00
|
|
|
Some(
|
|
|
|
Operator::FindForward { .. } | Operator::FindBackward { .. } | Operator::Replace,
|
|
|
|
) => {}
|
2023-02-10 22:41:22 +00:00
|
|
|
Some(_) => {
|
2022-12-08 00:39:32 +00:00
|
|
|
vim.clear_operator(cx);
|
|
|
|
}
|
2023-02-10 22:41:22 +00:00
|
|
|
_ => {}
|
2022-12-08 00:39:32 +00:00
|
|
|
});
|
|
|
|
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
|
|
|
active_editor: Option<WeakViewHandle<Editor>>,
|
2023-02-08 23:01:42 +00:00
|
|
|
editor_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
|
2023-02-08 23:01:42 +00:00
|
|
|
if let Some(editor) = self
|
|
|
|
.active_editor
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|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-06-30 19:32:53 +00:00
|
|
|
})
|
2023-02-08 23:01:42 +00:00
|
|
|
})
|
2022-06-30 19:32:53 +00:00
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
|
2023-02-10 22:41:22 +00:00
|
|
|
fn active_editor_input_ignored(text: Arc<str>, cx: &mut MutableAppContext) {
|
|
|
|
if text.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-09 23:44:40 +00:00
|
|
|
match Vim::read(cx).active_operator() {
|
|
|
|
Some(Operator::FindForward { before }) => {
|
2023-02-10 22:41:22 +00:00
|
|
|
motion::motion(Motion::FindForward { before, text }, cx)
|
2023-01-09 23:44:40 +00:00
|
|
|
}
|
|
|
|
Some(Operator::FindBackward { after }) => {
|
2023-02-10 22:41:22 +00:00
|
|
|
motion::motion(Motion::FindBackward { after, text }, cx)
|
2023-01-09 23:44:40 +00:00
|
|
|
}
|
|
|
|
Some(Operator::Replace) => match Vim::read(cx).state.mode {
|
2023-02-10 22:41:22 +00:00
|
|
|
Mode::Normal => normal_replace(text, cx),
|
|
|
|
Mode::Visual { line } => visual_replace(text, line, cx),
|
2023-01-09 23:44:40 +00:00
|
|
|
_ => Vim::update(cx, |vim, cx| vim.clear_operator(cx)),
|
|
|
|
},
|
2023-02-10 22:41:22 +00:00
|
|
|
_ => {}
|
2023-01-09 23:44:40 +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-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");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-08 23:01:42 +00:00
|
|
|
if let Some(editor) = self
|
|
|
|
.active_editor
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|editor| editor.upgrade(cx))
|
|
|
|
{
|
|
|
|
if self.enabled && editor.read(cx).mode() == EditorMode::Full {
|
2022-03-26 20:30:55 +00:00
|
|
|
editor.update(cx, |editor, cx| {
|
2023-02-08 23:01:42 +00:00
|
|
|
editor.set_cursor_shape(cursor_shape, cx);
|
|
|
|
editor.set_clip_at_line_ends(state.clip_at_line_end(), cx);
|
|
|
|
editor.set_input_enabled(!state.vim_controlled());
|
|
|
|
editor.selections.line_mode = matches!(state.mode, Mode::Visual { line: true });
|
|
|
|
let context_layer = state.keymap_context_layer();
|
|
|
|
editor.set_keymap_context_layer::<Self>(context_layer);
|
2022-03-26 20:30:55 +00:00
|
|
|
});
|
2023-02-08 23:01:42 +00:00
|
|
|
} else {
|
|
|
|
self.unhook_vim_settings(editor, cx);
|
2022-03-26 20:30:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|
2023-02-08 23:01:42 +00:00
|
|
|
|
|
|
|
fn unhook_vim_settings(&self, editor: ViewHandle<Editor>, cx: &mut MutableAppContext) {
|
|
|
|
editor.update(cx, |editor, cx| {
|
|
|
|
editor.set_cursor_shape(CursorShape::Bar, cx);
|
|
|
|
editor.set_clip_at_line_ends(false, cx);
|
|
|
|
editor.set_input_enabled(true);
|
|
|
|
editor.selections.line_mode = false;
|
|
|
|
editor.remove_keymap_context_layer::<Self>();
|
|
|
|
});
|
|
|
|
}
|
2022-03-25 02:24:36 +00:00
|
|
|
}
|