Define vim_mode setting in vim crate
This commit is contained in:
parent
926d7b356d
commit
9b06be2aa2
5 changed files with 70 additions and 21 deletions
|
@ -18,8 +18,8 @@ impl<'a> VimTestContext<'a> {
|
|||
pub async fn new(cx: &'a mut gpui::TestAppContext, enabled: bool) -> VimTestContext<'a> {
|
||||
let mut cx = EditorLspTestContext::new_rust(Default::default(), cx).await;
|
||||
cx.update(|cx| {
|
||||
cx.update_global(|settings: &mut Settings, _| {
|
||||
settings.vim_mode = enabled;
|
||||
cx.update_global(|store: &mut SettingsStore, _| {
|
||||
store.replace_value(VimModeSetting(enabled));
|
||||
});
|
||||
search::init(cx);
|
||||
crate::init(cx);
|
||||
|
@ -52,16 +52,16 @@ impl<'a> VimTestContext<'a> {
|
|||
|
||||
pub fn enable_vim(&mut self) {
|
||||
self.cx.update(|cx| {
|
||||
cx.update_global(|settings: &mut Settings, _| {
|
||||
settings.vim_mode = true;
|
||||
cx.update_global(|store: &mut SettingsStore, _| {
|
||||
store.replace_value(VimModeSetting(true))
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
pub fn disable_vim(&mut self) {
|
||||
self.cx.update(|cx| {
|
||||
cx.update_global(|settings: &mut Settings, _| {
|
||||
settings.vim_mode = false;
|
||||
cx.update_global(|store: &mut SettingsStore, _| {
|
||||
store.replace_value(VimModeSetting(false))
|
||||
});
|
||||
})
|
||||
}
|
||||
|
|
|
@ -22,11 +22,13 @@ use language::CursorShape;
|
|||
use motion::Motion;
|
||||
use normal::normal_replace;
|
||||
use serde::Deserialize;
|
||||
use settings::Settings;
|
||||
use settings::{Setting, SettingsStore};
|
||||
use state::{Mode, Operator, VimState};
|
||||
use visual::visual_replace;
|
||||
use workspace::{self, Workspace};
|
||||
|
||||
struct VimModeSetting(bool);
|
||||
|
||||
#[derive(Clone, Deserialize, PartialEq)]
|
||||
pub struct SwitchMode(pub Mode);
|
||||
|
||||
|
@ -40,6 +42,8 @@ actions!(vim, [Tab, Enter]);
|
|||
impl_actions!(vim, [Number, SwitchMode, PushOperator]);
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
settings::register_setting::<VimModeSetting>(cx);
|
||||
|
||||
editor_events::init(cx);
|
||||
normal::init(cx);
|
||||
visual::init(cx);
|
||||
|
@ -91,11 +95,11 @@ pub fn init(cx: &mut AppContext) {
|
|||
filter.filtered_namespaces.insert("vim");
|
||||
});
|
||||
cx.update_default_global(|vim: &mut Vim, cx: &mut AppContext| {
|
||||
vim.set_enabled(cx.global::<Settings>().vim_mode, cx)
|
||||
vim.set_enabled(settings::get_setting::<VimModeSetting>(None, cx).0, cx)
|
||||
});
|
||||
cx.observe_global::<Settings, _>(|cx| {
|
||||
cx.observe_global::<SettingsStore, _>(|cx| {
|
||||
cx.update_default_global(|vim: &mut Vim, cx: &mut AppContext| {
|
||||
vim.set_enabled(cx.global::<Settings>().vim_mode, cx)
|
||||
vim.set_enabled(settings::get_setting::<VimModeSetting>(None, cx).0, cx)
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
|
@ -330,6 +334,26 @@ impl Vim {
|
|||
}
|
||||
}
|
||||
|
||||
impl Setting for VimModeSetting {
|
||||
const KEY: Option<&'static str> = Some("vim_mode");
|
||||
|
||||
type FileContent = Option<bool>;
|
||||
|
||||
fn load(
|
||||
default_value: &Self::FileContent,
|
||||
user_values: &[&Self::FileContent],
|
||||
_: &AppContext,
|
||||
) -> Self {
|
||||
Self(
|
||||
user_values
|
||||
.first()
|
||||
.map(|e| **e)
|
||||
.flatten()
|
||||
.unwrap_or(default_value.unwrap()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue