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

@ -27,7 +27,7 @@ actions!(command_palette, [Toggle]);
pub fn init(cx: &mut AppContext) {
client::init_settings(cx);
cx.set_global(HitCounts::default());
cx.set_global(CommandPaletteFilter::default());
command_palette_hooks::init(cx);
cx.observe_new_views(CommandPalette::register).detach();
}
@ -73,23 +73,18 @@ impl CommandPalette {
telemetry: Arc<Telemetry>,
cx: &mut ViewContext<Self>,
) -> Self {
let filter = cx.try_global::<CommandPaletteFilter>();
let filter = CommandPaletteFilter::try_global(cx);
let commands = cx
.available_actions()
.into_iter()
.filter_map(|action| {
let name = action.name();
let namespace = name.split("::").next().unwrap_or("malformed action name");
if filter.is_some_and(|f| {
f.hidden_namespaces.contains(namespace)
|| f.hidden_action_types.contains(&action.type_id())
}) {
if filter.is_some_and(|filter| filter.is_hidden(&*action)) {
return None;
}
Some(Command {
name: humanize_action_name(&name),
name: humanize_action_name(action.name()),
action,
})
})
@ -185,12 +180,8 @@ impl CommandPaletteDelegate {
) {
self.updating_matches.take();
let mut intercept_result =
if let Some(interceptor) = cx.try_global::<CommandPaletteInterceptor>() {
(interceptor.0)(&query, cx)
} else {
None
};
let mut intercept_result = CommandPaletteInterceptor::try_global(cx)
.and_then(|interceptor| interceptor.intercept(&query, cx));
if parse_zed_link(&query, cx).is_some() {
intercept_result = Some(CommandInterceptResult {
@ -523,10 +514,9 @@ mod tests {
// Add namespace filter, and redeploy the palette
cx.update(|cx| {
cx.set_global(CommandPaletteFilter::default());
cx.update_global::<CommandPaletteFilter, _>(|filter, _| {
filter.hidden_namespaces.insert("editor");
})
CommandPaletteFilter::update_global(cx, |filter, _| {
filter.hide_namespace("editor");
});
});
cx.simulate_keystrokes("cmd-shift-p");