From c9d23dba6c94eeb014aaf1436c6bf43649ebd488 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Wed, 29 Jun 2022 11:58:12 -0700 Subject: [PATCH] Update command palette filter from vim mode more proactively --- crates/gpui/src/app.rs | 8 ++------ crates/vim/src/editor_events.rs | 4 ++-- crates/vim/src/vim.rs | 31 ++++++++++++++++++------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 5fa5544808..218bf08949 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -1634,14 +1634,10 @@ impl MutableAppContext { pub fn default_global(&mut self) -> &T { let type_id = TypeId::of::(); 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() } diff --git a/crates/vim/src/editor_events.rs b/crates/vim/src/editor_events.rs index 8837f264d3..6ad0cb77c4 100644 --- a/crates/vim/src/editor_events.rs +++ b/crates/vim/src/editor_events.rs @@ -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); }) } diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index 0ee1abc3ab..82567f9829 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -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::(|cx| { Vim::update(cx, |state, cx| { state.set_enabled(cx.global::().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 { @@ -125,21 +129,22 @@ impl Vim { if enabled { self.state.mode = Mode::Normal; } - cx.update_default_global::(|filter, _| { - if enabled { - filter.filtered_namespaces.remove("vim"); - } else { - filter.filtered_namespaces.insert("vim"); - } - }); - self.sync_editor_options(cx); + self.sync_vim_settings(cx); } } - fn sync_editor_options(&self, cx: &mut MutableAppContext) { + fn sync_vim_settings(&self, cx: &mut MutableAppContext) { let state = &self.state; let cursor_shape = state.cursor_shape(); + cx.update_default_global::(|filter, _| { + if self.enabled { + filter.filtered_namespaces.remove("vim"); + } else { + filter.filtered_namespaces.insert("vim"); + } + }); + for editor in self.editors.values() { if let Some(editor) = editor.upgrade(cx) { editor.update(cx, |editor, cx| {