gpui: Simplify Action macros + support doc comments in actions! (#33263)

Instead of a menagerie of macros for implementing `Action`, now there
are just two:

* `actions!(editor, [MoveLeft, MoveRight])`
* `#[derive(..., Action)]` with `#[action(namespace = editor)]`

In both contexts, `///` doc comments can be provided and will be used in
`JsonSchema`.

In both contexts, parameters can provided in `#[action(...)]`:

- `namespace = some_namespace` sets the namespace. In Zed this is
required.

- `name = "ActionName"` overrides the action's name. This must not
contain "::".

- `no_json` causes the `build` method to always error and
`action_json_schema` to return `None`
and allows actions not implement `serde::Serialize` and
`schemars::JsonSchema`.

- `no_register` skips registering the action. This is useful for
implementing the `Action` trait
while not supporting invocation by name or JSON deserialization.

- `deprecated_aliases = ["editor::SomeAction"]` specifies deprecated old
names for the action.
These action names should *not* correspond to any actions that are
registered. These old names
can then still be used to refer to invoke this action. In Zed, the
keymap JSON schema will
accept these old names and provide warnings.

- `deprecated = "Message about why this action is deprecation"`
specifies a deprecation message.
In Zed, the keymap JSON schema will cause this to be displayed as a
warning. This is a new feature.

Also makes the following changes since this seems like a good time to
make breaking changes:

* In `zed.rs` tests adds a test with an explicit list of namespaces. The
rationale for this is that there is otherwise no checking of `namespace
= ...` attributes.

* `Action::debug_name` renamed to `name_for_type`, since its only
difference with `name` was that it

* `Action::name` now returns `&'static str` instead of `&str` to match
the return of `name_for_type`. This makes the action trait more limited,
but the code was already assuming that `name_for_type` is the same as
`name`, and it requires `&'static`. So really this just makes the trait
harder to misuse.

* Various action reflection methods now use `&'static str` instead of
`SharedString`.

Release Notes:

- N/A
This commit is contained in:
Michael Sloan 2025-06-23 22:34:51 -06:00 committed by GitHub
parent 21f985a018
commit 24c94d474e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 878 additions and 789 deletions

View file

@ -168,7 +168,9 @@ dap = { workspace = true, features = ["test-support"] }
editor = { workspace = true, features = ["test-support"] }
gpui = { workspace = true, features = ["test-support"] }
image_viewer = { workspace = true, features = ["test-support"] }
itertools.workspace = true
language = { workspace = true, features = ["test-support"] }
pretty_assertions.workspace = true
project = { workspace = true, features = ["test-support"] }
terminal_view = { workspace = true, features = ["test-support"] }
tree-sitter-md.workspace = true

View file

@ -1308,7 +1308,7 @@ fn dump_all_gpui_actions() {
.map(|action| ActionDef {
name: action.name,
human_name: command_palette::humanize_action_name(action.name),
aliases: action.aliases,
aliases: action.deprecated_aliases,
})
.collect::<Vec<ActionDef>>();

View file

@ -1762,6 +1762,7 @@ mod tests {
TestAppContext, UpdateGlobal, VisualTestContext, WindowHandle, actions,
};
use language::{LanguageMatcher, LanguageRegistry};
use pretty_assertions::{assert_eq, assert_ne};
use project::{Project, ProjectPath, WorktreeSettings, project_settings::ProjectSettings};
use serde_json::json;
use settings::{SettingsStore, watch_config_file};
@ -3926,6 +3927,8 @@ mod tests {
})
}
actions!(test_only, [ActionA, ActionB]);
#[gpui::test]
async fn test_base_keymap(cx: &mut gpui::TestAppContext) {
let executor = cx.executor();
@ -3934,7 +3937,6 @@ mod tests {
let workspace =
cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
actions!(test1, [A, B]);
// From the Atom keymap
use workspace::ActivatePreviousPane;
// From the JetBrains keymap
@ -3954,7 +3956,7 @@ mod tests {
.fs
.save(
"/keymap.json".as_ref(),
&r#"[{"bindings": {"backspace": "test1::A"}}]"#.into(),
&r#"[{"bindings": {"backspace": "test_only::ActionA"}}]"#.into(),
Default::default(),
)
.await
@ -3981,8 +3983,8 @@ mod tests {
});
workspace
.update(cx, |workspace, _, cx| {
workspace.register_action(|_, _: &A, _window, _cx| {});
workspace.register_action(|_, _: &B, _window, _cx| {});
workspace.register_action(|_, _: &ActionA, _window, _cx| {});
workspace.register_action(|_, _: &ActionB, _window, _cx| {});
workspace.register_action(|_, _: &ActivatePreviousPane, _window, _cx| {});
workspace.register_action(|_, _: &ActivatePreviousItem, _window, _cx| {});
cx.notify();
@ -3993,7 +3995,7 @@ mod tests {
assert_key_bindings_for(
workspace.into(),
cx,
vec![("backspace", &A), ("k", &ActivatePreviousPane)],
vec![("backspace", &ActionA), ("k", &ActivatePreviousPane)],
line!(),
);
@ -4002,7 +4004,7 @@ mod tests {
.fs
.save(
"/keymap.json".as_ref(),
&r#"[{"bindings": {"backspace": "test1::B"}}]"#.into(),
&r#"[{"bindings": {"backspace": "test_only::ActionB"}}]"#.into(),
Default::default(),
)
.await
@ -4013,7 +4015,7 @@ mod tests {
assert_key_bindings_for(
workspace.into(),
cx,
vec![("backspace", &B), ("k", &ActivatePreviousPane)],
vec![("backspace", &ActionB), ("k", &ActivatePreviousPane)],
line!(),
);
@ -4033,7 +4035,7 @@ mod tests {
assert_key_bindings_for(
workspace.into(),
cx,
vec![("backspace", &B), ("{", &ActivatePreviousItem)],
vec![("backspace", &ActionB), ("{", &ActivatePreviousItem)],
line!(),
);
}
@ -4046,7 +4048,6 @@ mod tests {
let workspace =
cx.add_window(|window, cx| Workspace::test_new(project.clone(), window, cx));
actions!(test2, [A, B]);
// From the Atom keymap
use workspace::ActivatePreviousPane;
// From the JetBrains keymap
@ -4054,8 +4055,8 @@ mod tests {
workspace
.update(cx, |workspace, _, _| {
workspace.register_action(|_, _: &A, _window, _cx| {});
workspace.register_action(|_, _: &B, _window, _cx| {});
workspace.register_action(|_, _: &ActionA, _window, _cx| {});
workspace.register_action(|_, _: &ActionB, _window, _cx| {});
workspace.register_action(|_, _: &Deploy, _window, _cx| {});
})
.unwrap();
@ -4072,7 +4073,7 @@ mod tests {
.fs
.save(
"/keymap.json".as_ref(),
&r#"[{"bindings": {"backspace": "test2::A"}}]"#.into(),
&r#"[{"bindings": {"backspace": "test_only::ActionA"}}]"#.into(),
Default::default(),
)
.await
@ -4106,7 +4107,7 @@ mod tests {
assert_key_bindings_for(
workspace.into(),
cx,
vec![("backspace", &A), ("k", &ActivatePreviousPane)],
vec![("backspace", &ActionA), ("k", &ActivatePreviousPane)],
line!(),
);
@ -4219,6 +4220,122 @@ mod tests {
});
}
/// Checks that action namespaces are the expected set. The purpose of this is to prevent typos
/// and let you know when introducing a new namespace.
#[gpui::test]
async fn test_action_namespaces(cx: &mut gpui::TestAppContext) {
use itertools::Itertools;
init_keymap_test(cx);
cx.update(|cx| {
let all_actions = cx.all_action_names();
let mut actions_without_namespace = Vec::new();
let all_namespaces = all_actions
.iter()
.filter_map(|action_name| {
let namespace = action_name
.split("::")
.collect::<Vec<_>>()
.into_iter()
.rev()
.skip(1)
.rev()
.join("::");
if namespace.is_empty() {
actions_without_namespace.push(*action_name);
}
if &namespace == "test_only" || &namespace == "stories" {
None
} else {
Some(namespace)
}
})
.sorted()
.dedup()
.collect::<Vec<_>>();
assert_eq!(actions_without_namespace, Vec::<&str>::new());
let expected_namespaces = vec![
"activity_indicator",
"agent",
#[cfg(not(target_os = "macos"))]
"app_menu",
"assistant",
"assistant2",
"auto_update",
"branches",
"buffer_search",
"channel_modal",
"chat_panel",
"cli",
"client",
"collab",
"collab_panel",
"command_palette",
"console",
"context_server",
"copilot",
"debug_panel",
"debugger",
"dev",
"diagnostics",
"edit_prediction",
"editor",
"feedback",
"file_finder",
"git",
"git_onboarding",
"git_panel",
"go_to_line",
"icon_theme_selector",
"jj",
"journal",
"language_selector",
"markdown",
"menu",
"notebook",
"notification_panel",
"outline",
"outline_panel",
"pane",
"panel",
"picker",
"project_panel",
"project_search",
"project_symbols",
"projects",
"repl",
"rules_library",
"search",
"snippets",
"supermaven",
"tab_switcher",
"task",
"terminal",
"terminal_panel",
"theme_selector",
"toast",
"toolchain",
"variable_list",
"vim",
"welcome",
"workspace",
"zed",
"zed_predict_onboarding",
"zeta",
];
assert_eq!(
all_namespaces,
expected_namespaces
.into_iter()
.map(|namespace| namespace.to_string())
.sorted()
.collect::<Vec<_>>()
);
});
}
#[gpui::test]
fn test_bundled_settings_and_themes(cx: &mut App) {
cx.text_system()