Merge pull request #1256 from zed-industries/remove-vim-commands-when-disabled

Update command palette filter from vim mode more proactively
This commit is contained in:
Keith Simmons 2022-06-29 12:08:18 -07:00 committed by GitHub
commit 4dafe1885a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 21 deletions

View file

@ -1634,14 +1634,10 @@ impl MutableAppContext {
pub fn default_global<T: 'static + Default>(&mut self) -> &T {
let type_id = TypeId::of::<T>();
self.update(|this| {
if !this.globals.contains_key(&type_id) {
if let Entry::Vacant(entry) = this.cx.globals.entry(type_id) {
entry.insert(Box::new(T::default()));
this.notify_global(type_id);
}
this.cx
.globals
.entry(type_id)
.or_insert_with(|| Box::new(T::default()));
});
self.globals.get(&type_id).unwrap().downcast_ref().unwrap()
}

View file

@ -13,7 +13,7 @@ pub fn init(cx: &mut MutableAppContext) {
fn editor_created(EditorCreated(editor): &EditorCreated, cx: &mut MutableAppContext) {
cx.update_default_global(|vim: &mut Vim, cx| {
vim.editors.insert(editor.id(), editor.downgrade());
vim.sync_editor_options(cx);
vim.sync_vim_settings(cx);
})
}
@ -42,7 +42,7 @@ fn editor_blurred(EditorBlurred(editor): &EditorBlurred, cx: &mut MutableAppCont
vim.active_editor = None;
}
}
vim.sync_editor_options(cx);
vim.sync_vim_settings(cx);
})
}

View file

@ -51,6 +51,10 @@ pub fn init(cx: &mut MutableAppContext) {
}
});
// Sync initial settings with the rest of the app
Vim::update(cx, |state, cx| state.sync_vim_settings(cx));
// Any time settings change, update vim mode to match
cx.observe_global::<Settings, _>(|cx| {
Vim::update(cx, |state, cx| {
state.set_enabled(cx.global::<Settings>().vim_mode, cx)
@ -95,23 +99,23 @@ impl Vim {
fn switch_mode(&mut self, mode: Mode, cx: &mut MutableAppContext) {
self.state.mode = mode;
self.state.operator_stack.clear();
self.sync_editor_options(cx);
self.sync_vim_settings(cx);
}
fn push_operator(&mut self, operator: Operator, cx: &mut MutableAppContext) {
self.state.operator_stack.push(operator);
self.sync_editor_options(cx);
self.sync_vim_settings(cx);
}
fn pop_operator(&mut self, cx: &mut MutableAppContext) -> Operator {
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");
self.sync_editor_options(cx);
self.sync_vim_settings(cx);
popped_operator
}
fn clear_operator(&mut self, cx: &mut MutableAppContext) {
self.state.operator_stack.clear();
self.sync_editor_options(cx);
self.sync_vim_settings(cx);
}
fn active_operator(&self) -> Option<Operator> {
@ -125,20 +129,21 @@ impl Vim {
if enabled {
self.state.mode = Mode::Normal;
}
self.sync_vim_settings(cx);
}
}
fn sync_vim_settings(&self, cx: &mut MutableAppContext) {
let state = &self.state;
let cursor_shape = state.cursor_shape();
cx.update_default_global::<CommandPaletteFilter, _, _>(|filter, _| {
if enabled {
if self.enabled {
filter.filtered_namespaces.remove("vim");
} else {
filter.filtered_namespaces.insert("vim");
}
});
self.sync_editor_options(cx);
}
}
fn sync_editor_options(&self, cx: &mut MutableAppContext) {
let state = &self.state;
let cursor_shape = state.cursor_shape();
for editor in self.editors.values() {
if let Some(editor) = editor.upgrade(cx) {