Encapsulate CommandPaletteFilter and CommandPaletteInterceptor (#9402)

This PR refactors the `CommandPaletteFilter` and
`CommandPaletteInterceptor` to better encapsulate their internals.

Previously these globals and their fields were publicly accessible,
which meant that there was a lot of reaching in and making
modifications.

These changes should make it easier to add additional consumers of these
hooks (right now they're primarily used by Vim mode).

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-03-15 12:37:25 -04:00 committed by GitHub
parent d311a4b840
commit 55f4c8e51b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 164 additions and 56 deletions

View file

@ -87,8 +87,8 @@ pub fn init(cx: &mut AppContext) {
// Any time settings change, update vim mode to match. The Vim struct
// will be initialized as disabled by default, so we filter its commands
// out when starting up.
cx.update_global::<CommandPaletteFilter, _>(|filter, _| {
filter.hidden_namespaces.insert("vim");
CommandPaletteFilter::update_global(cx, |filter, _| {
filter.hide_namespace(Vim::NAMESPACE);
});
cx.update_global(|vim: &mut Vim, cx: &mut AppContext| {
vim.set_enabled(VimModeSetting::get_global(cx).0, cx)
@ -191,6 +191,9 @@ struct Vim {
impl Global for Vim {}
impl Vim {
/// The namespace for Vim actions.
const NAMESPACE: &'static str = "vim";
fn read(cx: &mut AppContext) -> &Self {
cx.global::<Self>()
}
@ -628,21 +631,23 @@ impl Vim {
return;
}
if !enabled {
let _ = cx.remove_global::<CommandPaletteInterceptor>();
cx.update_global::<CommandPaletteFilter, _>(|filter, _| {
filter.hidden_namespaces.insert("vim");
CommandPaletteInterceptor::update_global(cx, |interceptor, _| {
interceptor.clear();
});
CommandPaletteFilter::update_global(cx, |filter, _| {
filter.hide_namespace(Self::NAMESPACE);
});
*self = Default::default();
return;
}
self.enabled = true;
cx.update_global::<CommandPaletteFilter, _>(|filter, _| {
filter.hidden_namespaces.remove("vim");
CommandPaletteFilter::update_global(cx, |filter, _| {
filter.show_namespace(Self::NAMESPACE);
});
CommandPaletteInterceptor::update_global(cx, |interceptor, _| {
interceptor.set(Box::new(command::command_interceptor));
});
cx.set_global::<CommandPaletteInterceptor>(CommandPaletteInterceptor(Box::new(
command::command_interceptor,
)));
if let Some(active_window) = cx
.active_window()