Update command palette filter from vim mode more proactively

Keith Simmons created

Change summary

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(-)

Detailed changes

crates/gpui/src/app.rs 🔗

@@ -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()
     }

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);
     })
 }
 

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::<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,21 +129,22 @@ impl Vim {
             if enabled {
                 self.state.mode = Mode::Normal;
             }
-            cx.update_default_global::<CommandPaletteFilter, _, _>(|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::<CommandPaletteFilter, _, _>(|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| {