Fix remaining vim failures

This commit is contained in:
Nathan Sobo 2023-04-20 15:25:11 -06:00
parent 137d9384b5
commit 0bce80b6f8
10 changed files with 331 additions and 313 deletions

View File

@ -104,6 +104,10 @@ pub fn marked_text_ranges_by(
/// ```text /// ```text
/// one «ˇreversed» selection and one «forwardˇ» selection /// one «ˇreversed» selection and one «forwardˇ» selection
/// ``` /// ```
///
/// Any • characters in the input string will be replaced with spaces. This makes
/// it easier to test cases with trailing spaces, which tend to get trimmed from the
/// source code.
pub fn marked_text_ranges( pub fn marked_text_ranges(
marked_text: &str, marked_text: &str,
ranges_are_directed: bool, ranges_are_directed: bool,
@ -114,6 +118,7 @@ pub fn marked_text_ranges(
let mut current_range_start = None; let mut current_range_start = None;
let mut current_range_cursor = None; let mut current_range_cursor = None;
let marked_text = marked_text.replace("", " ");
for (marked_ix, marker) in marked_text.match_indices(&['«', '»', 'ˇ']) { for (marked_ix, marker) in marked_text.match_indices(&['«', '»', 'ˇ']) {
unmarked_text.push_str(&marked_text[prev_marked_ix..marked_ix]); unmarked_text.push_str(&marked_text[prev_marked_ix..marked_ix]);
let unmarked_len = unmarked_text.len(); let unmarked_len = unmarked_text.len();

View File

@ -1,7 +1,6 @@
use editor::{EditorBlurred, EditorFocused, EditorMode, EditorReleased, Event}; use crate::Vim;
use gpui::{AppContext, WindowContext}; use editor::{EditorBlurred, EditorFocused, EditorReleased};
use gpui::AppContext;
use crate::{state::Mode, Vim};
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.subscribe_global(focused).detach(); cx.subscribe_global(focused).detach();
@ -15,33 +14,7 @@ fn focused(EditorFocused(editor): &EditorFocused, cx: &mut AppContext) {
vim.update_active_editor(cx, |previously_active_editor, cx| { vim.update_active_editor(cx, |previously_active_editor, cx| {
Vim::unhook_vim_settings(previously_active_editor, cx); Vim::unhook_vim_settings(previously_active_editor, cx);
}); });
vim.set_active_editor(editor.clone(), cx);
vim.active_editor = Some(editor.downgrade());
vim.editor_subscription = Some(cx.subscribe(editor, |editor, event, cx| match event {
Event::SelectionsChanged { local: true } => {
let editor = editor.read(cx);
if editor.leader_replica_id().is_none() {
let newest_empty = editor.selections.newest::<usize>(cx).is_empty();
local_selections_changed(newest_empty, cx);
}
}
Event::InputIgnored { text } => {
Vim::active_editor_input_ignored(text.clone(), cx);
}
_ => {}
}));
if vim.enabled {
let editor = editor.read(cx);
let editor_mode = editor.mode();
let newest_selection_empty = editor.selections.newest::<usize>(cx).is_empty();
if editor_mode == EditorMode::Full && !newest_selection_empty {
vim.switch_mode(Mode::Visual { line: false }, true, cx);
}
}
vim.sync_vim_settings(cx);
}); });
}); });
} }
@ -73,11 +46,3 @@ fn released(EditorReleased(editor): &EditorReleased, cx: &mut AppContext) {
}); });
}); });
} }
fn local_selections_changed(newest_empty: bool, cx: &mut WindowContext) {
Vim::update(cx, |vim, cx| {
if vim.enabled && vim.state.mode == Mode::Normal && !newest_empty {
vim.switch_mode(Mode::Visual { line: false }, false, cx)
}
})
}

View File

@ -117,6 +117,7 @@ pub fn init(cx: &mut AppContext) {
} }
pub(crate) fn motion(motion: Motion, cx: &mut WindowContext) { pub(crate) fn motion(motion: Motion, cx: &mut WindowContext) {
dbg!(&motion);
if let Some(Operator::Namespace(_)) if let Some(Operator::Namespace(_))
| Some(Operator::FindForward { .. }) | Some(Operator::FindForward { .. })
| Some(Operator::FindBackward { .. }) = Vim::read(cx).active_operator() | Some(Operator::FindBackward { .. }) = Vim::read(cx).active_operator()

View File

@ -1017,15 +1017,17 @@ mod test {
let test_case = indoc! {" let test_case = indoc! {"
ˇaaaˇbˇ ˇ ˇbˇbˇ aˇaaˇbaaa ˇaaaˇbˇ ˇ ˇbˇbˇ aˇaaˇbaaa
ˇ ˇbˇaaˇa ˇbˇbˇb ˇ ˇbˇaaˇa ˇbˇbˇb
ˇ ˇ
ˇb ˇb
"}; "
}
.replace("", " ");
for count in 1..=3 { for count in 1..=3 {
cx.assert_binding_matches_all([&count.to_string(), "shift-f", "b"], test_case) cx.assert_binding_matches_all([&count.to_string(), "shift-f", "b"], &test_case)
.await; .await;
cx.assert_binding_matches_all([&count.to_string(), "shift-t", "b"], test_case) cx.assert_binding_matches_all([&count.to_string(), "shift-t", "b"], &test_case)
.await; .await;
} }
} }

View File

@ -434,19 +434,20 @@ mod test {
use crate::test::{ExemptionFeatures, NeovimBackedTestContext}; use crate::test::{ExemptionFeatures, NeovimBackedTestContext};
const WORD_LOCATIONS: &'static str = indoc! {" const WORD_LOCATIONS: &'static str = indoc! {"
The quick ˇbrowˇnˇ The quick ˇbrowˇnˇ
fox ˇjuˇmpsˇ over fox ˇjuˇmpsˇ over
the lazy dogˇ the lazy dogˇ
ˇ ˇ
ˇ ˇ
ˇ ˇ
Thˇeˇ-ˇquˇickˇ ˇbrownˇ Thˇeˇ-ˇquˇickˇ ˇbrownˇ
ˇ ˇ
ˇ ˇ
ˇ fox-jumpˇs over ˇ fox-jumpˇs over
the lazy dogˇ the lazy dogˇ
ˇ ˇ
"}; "
};
#[gpui::test] #[gpui::test]
async fn test_change_word_object(cx: &mut gpui::TestAppContext) { async fn test_change_word_object(cx: &mut gpui::TestAppContext) {

View File

@ -153,6 +153,7 @@ impl<'a> NeovimBackedTestContext<'a> {
} }
} }
dbg!(initial_state);
let _state_context = self.set_shared_state(initial_state).await; let _state_context = self.set_shared_state(initial_state).await;
let _keystroke_context = self.simulate_shared_keystrokes(keystrokes).await; let _keystroke_context = self.simulate_shared_keystrokes(keystrokes).await;
self.assert_state_matches().await; self.assert_state_matches().await;

View File

@ -13,9 +13,10 @@ mod visual;
use std::sync::Arc; use std::sync::Arc;
use collections::CommandPaletteFilter; use collections::CommandPaletteFilter;
use editor::{Bias, Cancel, Editor, EditorMode}; use editor::{Bias, Cancel, Editor, EditorMode, Event};
use gpui::{ use gpui::{
actions, impl_actions, AppContext, Subscription, ViewContext, WeakViewHandle, WindowContext, actions, impl_actions, AppContext, Subscription, ViewContext, ViewHandle, WeakViewHandle,
WindowContext,
}; };
use language::CursorShape; use language::CursorShape;
use motion::Motion; use motion::Motion;
@ -142,6 +143,35 @@ impl Vim {
cx.update_default_global(update) cx.update_default_global(update)
} }
fn set_active_editor(&mut self, editor: ViewHandle<Editor>, cx: &mut WindowContext) {
self.active_editor = Some(editor.downgrade());
self.editor_subscription = Some(cx.subscribe(&editor, |editor, event, cx| match event {
Event::SelectionsChanged { local: true } => {
let editor = editor.read(cx);
if editor.leader_replica_id().is_none() {
let newest_empty = editor.selections.newest::<usize>(cx).is_empty();
local_selections_changed(newest_empty, cx);
}
}
Event::InputIgnored { text } => {
Vim::active_editor_input_ignored(text.clone(), cx);
}
_ => {}
}));
if self.enabled {
let editor = editor.read(cx);
let editor_mode = editor.mode();
let newest_selection_empty = editor.selections.newest::<usize>(cx).is_empty();
if editor_mode == EditorMode::Full && !newest_selection_empty {
self.switch_mode(Mode::Visual { line: false }, true, cx);
}
}
self.sync_vim_settings(cx);
}
fn update_active_editor<S>( fn update_active_editor<S>(
&self, &self,
cx: &mut WindowContext, cx: &mut WindowContext,
@ -179,6 +209,7 @@ impl Vim {
} }
fn push_operator(&mut self, operator: Operator, cx: &mut WindowContext) { fn push_operator(&mut self, operator: Operator, cx: &mut WindowContext) {
dbg!("push_operator", &operator);
self.state.operator_stack.push(operator); self.state.operator_stack.push(operator);
self.sync_vim_settings(cx); self.sync_vim_settings(cx);
} }
@ -218,6 +249,7 @@ impl Vim {
} }
fn active_editor_input_ignored(text: Arc<str>, cx: &mut WindowContext) { fn active_editor_input_ignored(text: Arc<str>, cx: &mut WindowContext) {
dbg!(&text);
if text.is_empty() { if text.is_empty() {
return; return;
} }
@ -253,11 +285,14 @@ impl Vim {
cx.update_active_window(|cx| { cx.update_active_window(|cx| {
if self.enabled { if self.enabled {
self.active_editor = cx let active_editor = cx
.root_view() .root_view()
.downcast_ref::<Workspace>() .downcast_ref::<Workspace>()
.and_then(|workspace| workspace.read(cx).active_item(cx)) .and_then(|workspace| workspace.read(cx).active_item(cx))
.and_then(|item| item.downcast::<Editor>().map(|h| h.downgrade())); .and_then(|item| item.downcast::<Editor>());
if let Some(active_editor) = active_editor {
self.set_active_editor(active_editor, cx);
}
self.switch_mode(Mode::Normal, false, cx); self.switch_mode(Mode::Normal, false, cx);
} }
self.sync_vim_settings(cx); self.sync_vim_settings(cx);
@ -291,3 +326,11 @@ impl Vim {
editor.remove_keymap_context_layer::<Self>(); editor.remove_keymap_context_layer::<Self>();
} }
} }
fn local_selections_changed(newest_empty: bool, cx: &mut WindowContext) {
Vim::update(cx, |vim, cx| {
if vim.enabled && vim.state.mode == Mode::Normal && !newest_empty {
vim.switch_mode(Mode::Visual { line: false }, false, cx)
}
})
}