First round of vim tests
This commit is contained in:
parent
f35453caad
commit
d1805d8ada
16 changed files with 938 additions and 884 deletions
|
@ -1,42 +1,40 @@
|
|||
use gpui::{div, AnyElement, Element, IntoElement, Render, ViewContext};
|
||||
use settings::{Settings, SettingsStore};
|
||||
use gpui::{div, AnyElement, Element, IntoElement, Render, Subscription, ViewContext};
|
||||
use settings::SettingsStore;
|
||||
use workspace::{item::ItemHandle, ui::Label, StatusItemView};
|
||||
|
||||
use crate::{state::Mode, Vim, VimModeSetting};
|
||||
use crate::{state::Mode, Vim};
|
||||
|
||||
pub struct ModeIndicator {
|
||||
pub mode: Option<Mode>,
|
||||
// _subscription: Subscription,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
impl ModeIndicator {
|
||||
pub fn new(cx: &mut ViewContext<Self>) -> Self {
|
||||
cx.observe_global::<Vim>(|this, cx| this.set_mode(Vim::read(cx).state().mode, cx))
|
||||
.detach();
|
||||
let _subscriptions = vec![
|
||||
cx.observe_global::<Vim>(|this, cx| this.update_mode(cx)),
|
||||
cx.observe_global::<SettingsStore>(|this, cx| this.update_mode(cx)),
|
||||
];
|
||||
|
||||
cx.observe_global::<SettingsStore>(move |mode_indicator, cx| {
|
||||
if VimModeSetting::get_global(cx).0 {
|
||||
mode_indicator.mode = cx
|
||||
.has_global::<Vim>()
|
||||
.then(|| cx.global::<Vim>().state().mode);
|
||||
} else {
|
||||
mode_indicator.mode.take();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
let mut this = Self {
|
||||
mode: None,
|
||||
_subscriptions,
|
||||
};
|
||||
this.update_mode(cx);
|
||||
this
|
||||
}
|
||||
|
||||
fn update_mode(&mut self, cx: &mut ViewContext<Self>) {
|
||||
// Vim doesn't exist in some tests
|
||||
let mode = cx
|
||||
.has_global::<Vim>()
|
||||
.then(|| {
|
||||
let vim = cx.global::<Vim>();
|
||||
vim.enabled.then(|| vim.state().mode)
|
||||
})
|
||||
.flatten();
|
||||
if !cx.has_global::<Vim>() {
|
||||
return;
|
||||
}
|
||||
|
||||
Self {
|
||||
mode,
|
||||
// _subscription,
|
||||
let vim = Vim::read(cx);
|
||||
if vim.enabled {
|
||||
self.mode = Some(vim.state().mode);
|
||||
} else {
|
||||
self.mode = None;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -203,7 +203,6 @@ fn insert_after(_: &mut Workspace, _: &InsertAfter, cx: &mut ViewContext<Workspa
|
|||
}
|
||||
|
||||
fn insert_before(_: &mut Workspace, _: &InsertBefore, cx: &mut ViewContext<Workspace>) {
|
||||
dbg!("insert before!");
|
||||
Vim::update(cx, |vim, cx| {
|
||||
vim.start_recording(cx);
|
||||
vim.switch_mode(Mode::Insert, false, cx);
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,4 +1,4 @@
|
|||
use editor::scroll::VERTICAL_SCROLL_MARGIN;
|
||||
use editor::{scroll::VERTICAL_SCROLL_MARGIN, test::editor_test_context::ContextHandle};
|
||||
use indoc::indoc;
|
||||
use settings::SettingsStore;
|
||||
use std::{
|
||||
|
@ -7,7 +7,6 @@ use std::{
|
|||
};
|
||||
|
||||
use collections::{HashMap, HashSet};
|
||||
use gpui::{geometry::vector::vec2f, ContextHandle};
|
||||
use language::language_settings::{AllLanguageSettings, SoftWrap};
|
||||
use util::test::marked_text_offsets;
|
||||
|
||||
|
@ -151,19 +150,20 @@ impl<'a> NeovimBackedTestContext<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn set_scroll_height(&mut self, rows: u32) {
|
||||
// match Zed's scrolling behavior
|
||||
self.neovim
|
||||
.set_option(&format!("scrolloff={}", VERTICAL_SCROLL_MARGIN))
|
||||
.await;
|
||||
// +2 to account for the vim command UI at the bottom.
|
||||
self.neovim.set_option(&format!("lines={}", rows + 2)).await;
|
||||
let window = self.window;
|
||||
let line_height =
|
||||
self.editor(|editor, cx| editor.style().text.line_height(cx.font_cache()));
|
||||
// todo!()
|
||||
// pub async fn set_scroll_height(&mut self, rows: u32) {
|
||||
// // match Zed's scrolling behavior
|
||||
// self.neovim
|
||||
// .set_option(&format!("scrolloff={}", VERTICAL_SCROLL_MARGIN))
|
||||
// .await;
|
||||
// // +2 to account for the vim command UI at the bottom.
|
||||
// self.neovim.set_option(&format!("lines={}", rows + 2)).await;
|
||||
// let window = self.window;
|
||||
// let line_height =
|
||||
// self.editor(|editor, cx| editor.style().text.line_height(cx.font_cache()));
|
||||
|
||||
window.simulate_resize(vec2f(1000., (rows as f32) * line_height), &mut self.cx);
|
||||
}
|
||||
// window.simulate_resize(vec2f(1000., (rows as f32) * line_height), &mut self.cx);
|
||||
// }
|
||||
|
||||
pub async fn set_neovim_option(&mut self, option: &str) {
|
||||
self.neovim.set_option(option).await;
|
||||
|
@ -211,12 +211,7 @@ impl<'a> NeovimBackedTestContext<'a> {
|
|||
|
||||
pub async fn assert_shared_clipboard(&mut self, text: &str) {
|
||||
let neovim = self.neovim.read_register('"').await;
|
||||
let editor = self
|
||||
.platform()
|
||||
.read_from_clipboard()
|
||||
.unwrap()
|
||||
.text()
|
||||
.clone();
|
||||
let editor = self.read_from_clipboard().unwrap().text().clone();
|
||||
|
||||
if text == neovim && text == editor {
|
||||
return;
|
||||
|
|
|
@ -10,7 +10,7 @@ use async_compat::Compat;
|
|||
#[cfg(feature = "neovim")]
|
||||
use async_trait::async_trait;
|
||||
#[cfg(feature = "neovim")]
|
||||
use gpui::keymap_matcher::Keystroke;
|
||||
use gpui::Keystroke;
|
||||
|
||||
#[cfg(feature = "neovim")]
|
||||
use language::Point;
|
||||
|
@ -116,16 +116,24 @@ impl NeovimConnection {
|
|||
keystroke.key = "lt".to_string()
|
||||
}
|
||||
|
||||
let special = keystroke.shift
|
||||
|| keystroke.ctrl
|
||||
|| keystroke.alt
|
||||
|| keystroke.cmd
|
||||
let special = keystroke.modifiers.shift
|
||||
|| keystroke.modifiers.control
|
||||
|| keystroke.modifiers.alt
|
||||
|| keystroke.modifiers.command
|
||||
|| keystroke.key.len() > 1;
|
||||
let start = if special { "<" } else { "" };
|
||||
let shift = if keystroke.shift { "S-" } else { "" };
|
||||
let ctrl = if keystroke.ctrl { "C-" } else { "" };
|
||||
let alt = if keystroke.alt { "M-" } else { "" };
|
||||
let cmd = if keystroke.cmd { "D-" } else { "" };
|
||||
let shift = if keystroke.modifiers.shift { "S-" } else { "" };
|
||||
let ctrl = if keystroke.modifiers.control {
|
||||
"C-"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let alt = if keystroke.modifiers.alt { "M-" } else { "" };
|
||||
let cmd = if keystroke.modifiers.command {
|
||||
"D-"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let end = if special { ">" } else { "" };
|
||||
|
||||
let key = format!("{start}{shift}{ctrl}{alt}{cmd}{}{end}", keystroke.key);
|
||||
|
|
|
@ -16,11 +16,25 @@ pub struct VimTestContext<'a> {
|
|||
|
||||
impl<'a> VimTestContext<'a> {
|
||||
pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
|
||||
cx.update(|cx| {
|
||||
search::init(cx);
|
||||
let settings = SettingsStore::test(cx);
|
||||
cx.set_global(settings);
|
||||
command_palette::init(cx);
|
||||
crate::init(cx);
|
||||
});
|
||||
let lsp = EditorLspTestContext::new_rust(Default::default(), cx).await;
|
||||
Self::new_with_lsp(lsp, enabled)
|
||||
}
|
||||
|
||||
pub async fn new_typescript(cx: &'a mut gpui::TestAppContext) -> VimTestContext<'a> {
|
||||
cx.update(|cx| {
|
||||
search::init(cx);
|
||||
let settings = SettingsStore::test(cx);
|
||||
cx.set_global(settings);
|
||||
command_palette::init(cx);
|
||||
crate::init(cx);
|
||||
});
|
||||
Self::new_with_lsp(
|
||||
EditorLspTestContext::new_typescript(Default::default(), cx).await,
|
||||
true,
|
||||
|
@ -28,12 +42,6 @@ impl<'a> VimTestContext<'a> {
|
|||
}
|
||||
|
||||
pub fn new_with_lsp(mut cx: EditorLspTestContext<'a>, enabled: bool) -> VimTestContext<'a> {
|
||||
cx.update(|cx| {
|
||||
search::init(cx);
|
||||
crate::init(cx);
|
||||
command_palette::init(cx);
|
||||
});
|
||||
|
||||
cx.update(|cx| {
|
||||
cx.update_global(|store: &mut SettingsStore, cx| {
|
||||
store.update_user_settings::<VimModeSetting>(cx, |s| *s = Some(enabled));
|
||||
|
@ -65,9 +73,11 @@ impl<'a> VimTestContext<'a> {
|
|||
|
||||
pub fn update_view<F, T, R>(&mut self, view: View<T>, update: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut T, &mut ViewContext<T>) -> R,
|
||||
T: 'static,
|
||||
F: FnOnce(&mut T, &mut ViewContext<T>) -> R + 'static,
|
||||
{
|
||||
self.update_window(self.window, |_, cx| view.update(cx, update))
|
||||
let window = self.window.clone();
|
||||
self.update_window(window, move |_, cx| view.update(cx, update))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
|
@ -75,8 +85,7 @@ impl<'a> VimTestContext<'a> {
|
|||
where
|
||||
F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
|
||||
{
|
||||
self.update_window(self.window, |_, cx| self.cx.workspace.update(cx, update))
|
||||
.unwrap()
|
||||
self.cx.update_workspace(update)
|
||||
}
|
||||
|
||||
pub fn enable_vim(&mut self) {
|
||||
|
|
|
@ -116,45 +116,43 @@ fn register(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
|
|||
visual::register(workspace, cx);
|
||||
}
|
||||
|
||||
pub fn observe_keystrokes(_: &mut WindowContext) {
|
||||
// todo!()
|
||||
pub fn observe_keystrokes(cx: &mut WindowContext) {
|
||||
cx.observe_keystrokes(|keystroke_event, cx| {
|
||||
if let Some(action) = keystroke_event
|
||||
.action
|
||||
.as_ref()
|
||||
.map(|action| action.boxed_clone())
|
||||
{
|
||||
Vim::update(cx, |vim, _| {
|
||||
if vim.workspace_state.recording {
|
||||
vim.workspace_state
|
||||
.recorded_actions
|
||||
.push(ReplayableAction::Action(action.boxed_clone()));
|
||||
|
||||
// cx.observe_keystrokes(|_keystroke, result, handled_by, cx| {
|
||||
// if result == &MatchResult::Pending {
|
||||
// return true;
|
||||
// }
|
||||
// if let Some(handled_by) = handled_by {
|
||||
// Vim::update(cx, |vim, _| {
|
||||
// if vim.workspace_state.recording {
|
||||
// vim.workspace_state
|
||||
// .recorded_actions
|
||||
// .push(ReplayableAction::Action(handled_by.boxed_clone()));
|
||||
if vim.workspace_state.stop_recording_after_next_action {
|
||||
vim.workspace_state.recording = false;
|
||||
vim.workspace_state.stop_recording_after_next_action = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// if vim.workspace_state.stop_recording_after_next_action {
|
||||
// vim.workspace_state.recording = false;
|
||||
// vim.workspace_state.stop_recording_after_next_action = false;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// Keystroke is handled by the vim system, so continue forward
|
||||
if action.name().starts_with("vim::") {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// // Keystroke is handled by the vim system, so continue forward
|
||||
// if handled_by.namespace() == "vim" {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Vim::update(cx, |vim, cx| match vim.active_operator() {
|
||||
// Some(
|
||||
// Operator::FindForward { .. } | Operator::FindBackward { .. } | Operator::Replace,
|
||||
// ) => {}
|
||||
// Some(_) => {
|
||||
// vim.clear_operator(cx);
|
||||
// }
|
||||
// _ => {}
|
||||
// });
|
||||
// true
|
||||
// })
|
||||
// .detach()
|
||||
Vim::update(cx, |vim, cx| match vim.active_operator() {
|
||||
Some(
|
||||
Operator::FindForward { .. } | Operator::FindBackward { .. } | Operator::Replace,
|
||||
) => {}
|
||||
Some(_) => {
|
||||
vim.clear_operator(cx);
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
})
|
||||
.detach()
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue