diff --git a/Cargo.lock b/Cargo.lock index 40fb5ee91e..373f0f11bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -917,6 +917,7 @@ checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" name = "command_palette" version = "0.1.0" dependencies = [ + "collections", "ctor", "editor", "env_logger", @@ -5601,6 +5602,7 @@ version = "0.1.0" dependencies = [ "assets", "collections", + "command_palette", "editor", "gpui", "indoc", diff --git a/crates/command_palette/Cargo.toml b/crates/command_palette/Cargo.toml index 25260f4390..85f5b36ed6 100644 --- a/crates/command_palette/Cargo.toml +++ b/crates/command_palette/Cargo.toml @@ -8,6 +8,7 @@ path = "src/command_palette.rs" doctest = false [dependencies] +collections = { path = "../collections" } editor = { path = "../editor" } fuzzy = { path = "../fuzzy" } gpui = { path = "../gpui" } diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index 0708826ea7..ec4468b463 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -1,3 +1,4 @@ +use collections::HashSet; use fuzzy::{StringMatch, StringMatchCandidate}; use gpui::{ actions, @@ -10,6 +11,11 @@ use settings::Settings; use std::cmp; use workspace::Workspace; +#[derive(Default)] +pub struct CommandPaletteFilter { + pub filtered_namespaces: HashSet<&'static str>, +} + pub fn init(cx: &mut MutableAppContext) { cx.add_action(CommandPalette::toggle); Picker::::init(cx); @@ -45,14 +51,24 @@ impl CommandPalette { let this = cx.weak_handle(); let actions = cx .available_actions(cx.window_id(), focused_view_id) - .map(|(name, action, bindings)| Command { - name: humanize_action_name(name), - action, - keystrokes: bindings - .last() - .map_or(Vec::new(), |binding| binding.keystrokes().to_vec()), + .filter_map(|(name, action, bindings)| { + if cx.has_global::() { + let filter = cx.global::(); + if filter.filtered_namespaces.contains(action.namespace()) { + return None; + } + } + + Some(Command { + name: humanize_action_name(name), + action, + keystrokes: bindings + .last() + .map_or(Vec::new(), |binding| binding.keystrokes().to_vec()), + }) }) .collect(); + let picker = cx.add_view(|cx| Picker::new(this, cx)); Self { picker, diff --git a/crates/gpui/src/app/action.rs b/crates/gpui/src/app/action.rs index 069cdebd5d..538f99a586 100644 --- a/crates/gpui/src/app/action.rs +++ b/crates/gpui/src/app/action.rs @@ -2,6 +2,7 @@ use std::any::{Any, TypeId}; pub trait Action: 'static { fn id(&self) -> TypeId; + fn namespace(&self) -> &'static str; fn name(&self) -> &'static str; fn as_any(&self) -> &dyn Any; fn boxed_clone(&self) -> Box; @@ -80,6 +81,10 @@ macro_rules! impl_internal_actions { macro_rules! __impl_action { ($namespace:path, $name:ident, $from_json_fn:item) => { impl $crate::action::Action for $name { + fn namespace(&self) -> &'static str { + stringify!($namespace) + } + fn name(&self) -> &'static str { stringify!($name) } diff --git a/crates/vim/Cargo.toml b/crates/vim/Cargo.toml index deb8294d5c..56b0aec8cc 100644 --- a/crates/vim/Cargo.toml +++ b/crates/vim/Cargo.toml @@ -10,6 +10,7 @@ doctest = false [dependencies] assets = { path = "../assets" } collections = { path = "../collections" } +command_palette = { path = "../command_palette" } editor = { path = "../editor" } gpui = { path = "../gpui" } language = { path = "../language" } diff --git a/crates/vim/src/vim.rs b/crates/vim/src/vim.rs index 3f54954ed5..0ee1abc3ab 100644 --- a/crates/vim/src/vim.rs +++ b/crates/vim/src/vim.rs @@ -10,6 +10,7 @@ mod utils; mod visual; use collections::HashMap; +use command_palette::CommandPaletteFilter; use editor::{Bias, CursorShape, Editor, Input}; use gpui::{impl_actions, MutableAppContext, Subscription, ViewContext, WeakViewHandle}; use serde::Deserialize; @@ -124,6 +125,13 @@ 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); } }