fix issue with single line editors in vim not properly unhooking vim mode bindings

This commit is contained in:
Kay Simmons 2023-02-08 15:01:42 -08:00
parent 0a50d271b7
commit c1812ddc27
4 changed files with 88 additions and 72 deletions

View file

@ -10,13 +10,12 @@ mod state;
mod utils;
mod visual;
use collections::HashMap;
use command_palette::CommandPaletteFilter;
use editor::{Bias, Cancel, Editor, EditorMode};
use gpui::{
impl_actions,
keymap_matcher::{KeyPressed, Keystroke},
MutableAppContext, Subscription, ViewContext, WeakViewHandle,
MutableAppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
};
use language::CursorShape;
use motion::Motion;
@ -117,9 +116,8 @@ pub fn observe_keypresses(window_id: usize, cx: &mut MutableAppContext) {
#[derive(Default)]
pub struct Vim {
editors: HashMap<usize, WeakViewHandle<Editor>>,
active_editor: Option<WeakViewHandle<Editor>>,
selection_subscription: Option<Subscription>,
editor_subscription: Option<Subscription>,
enabled: bool,
state: VimState,
@ -160,24 +158,27 @@ impl Vim {
}
// 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,
);
}
});
})
if let Some(editor) = self
.active_editor
.as_ref()
.and_then(|editor| editor.upgrade(cx))
{
editor.update(cx, |editor, cx| {
dbg!(&mode, editor.mode());
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,
);
}
});
})
}
})
}
}
@ -264,26 +265,33 @@ impl Vim {
}
});
for editor in self.editors.values() {
if let Some(editor) = editor.upgrade(cx) {
if let Some(editor) = self
.active_editor
.as_ref()
.and_then(|editor| editor.upgrade(cx))
{
if self.enabled && editor.read(cx).mode() == EditorMode::Full {
editor.update(cx, |editor, cx| {
if self.enabled && editor.mode() == EditorMode::Full {
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);
} else {
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>();
}
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);
});
} else {
self.unhook_vim_settings(editor, cx);
}
}
}
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>();
});
}
}