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

@ -1,4 +1,4 @@
use gpui::{actions, impl_actions};
use gpui::{Action, actions};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
@ -11,20 +11,20 @@ use serde::{Deserialize, Serialize};
// https://github.com/mmastrac/rust-ctor/issues/280
pub fn init() {}
#[derive(Clone, PartialEq, Deserialize, JsonSchema)]
#[derive(Clone, PartialEq, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
#[serde(deny_unknown_fields)]
pub struct OpenBrowser {
pub url: String,
}
#[derive(Clone, PartialEq, Deserialize, JsonSchema)]
#[derive(Clone, PartialEq, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
#[serde(deny_unknown_fields)]
pub struct OpenZedUrl {
pub url: String,
}
impl_actions!(zed, [OpenBrowser, OpenZedUrl]);
actions!(
zed,
[
@ -56,62 +56,56 @@ pub enum ExtensionCategoryFilter {
DebugAdapters,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct Extensions {
/// Filters the extensions page down to extensions that are in the specified category.
#[serde(default)]
pub category_filter: Option<ExtensionCategoryFilter>,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct DecreaseBufferFontSize {
#[serde(default)]
pub persist: bool,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct IncreaseBufferFontSize {
#[serde(default)]
pub persist: bool,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct ResetBufferFontSize {
#[serde(default)]
pub persist: bool,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct DecreaseUiFontSize {
#[serde(default)]
pub persist: bool,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct IncreaseUiFontSize {
#[serde(default)]
pub persist: bool,
}
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = zed)]
pub struct ResetUiFontSize {
#[serde(default)]
pub persist: bool,
}
impl_actions!(
zed,
[
Extensions,
DecreaseBufferFontSize,
IncreaseBufferFontSize,
ResetBufferFontSize,
DecreaseUiFontSize,
IncreaseUiFontSize,
ResetUiFontSize,
]
);
pub mod dev {
use gpui::actions;
@ -119,34 +113,32 @@ pub mod dev {
}
pub mod workspace {
use gpui::action_with_deprecated_aliases;
use gpui::actions;
action_with_deprecated_aliases!(
actions!(
workspace,
CopyPath,
[
"editor::CopyPath",
"outline_panel::CopyPath",
"project_panel::CopyPath"
]
);
action_with_deprecated_aliases!(
workspace,
CopyRelativePath,
[
"editor::CopyRelativePath",
"outline_panel::CopyRelativePath",
"project_panel::CopyRelativePath"
#[action(deprecated_aliases = ["editor::CopyPath", "outline_panel::CopyPath", "project_panel::CopyPath"])]
CopyPath,
#[action(deprecated_aliases = ["editor::CopyRelativePath", "outline_panel::CopyRelativePath", "project_panel::CopyRelativePath"])]
CopyRelativePath
]
);
}
pub mod git {
use gpui::{action_with_deprecated_aliases, actions};
use gpui::actions;
actions!(git, [CheckoutBranch, Switch, SelectRepo]);
action_with_deprecated_aliases!(git, Branch, ["branches::OpenRecent"]);
actions!(
git,
[
CheckoutBranch,
Switch,
SelectRepo,
#[action(deprecated_aliases = ["branches::OpenRecent"])]
Branch
]
);
}
pub mod jj {
@ -174,33 +166,31 @@ pub mod feedback {
}
pub mod theme_selector {
use gpui::impl_actions;
use gpui::Action;
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = theme_selector)]
#[serde(deny_unknown_fields)]
pub struct Toggle {
/// A list of theme names to filter the theme selector down to.
pub themes_filter: Option<Vec<String>>,
}
impl_actions!(theme_selector, [Toggle]);
}
pub mod icon_theme_selector {
use gpui::impl_actions;
use gpui::Action;
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = icon_theme_selector)]
#[serde(deny_unknown_fields)]
pub struct Toggle {
/// A list of icon theme names to filter the theme selector down to.
pub themes_filter: Option<Vec<String>>,
}
impl_actions!(icon_theme_selector, [Toggle]);
}
pub mod agent {
@ -213,40 +203,35 @@ pub mod agent {
}
pub mod assistant {
use gpui::{
action_with_deprecated_aliases, actions, impl_action_with_deprecated_aliases, impl_actions,
};
use gpui::{Action, actions};
use schemars::JsonSchema;
use serde::Deserialize;
use uuid::Uuid;
action_with_deprecated_aliases!(agent, ToggleFocus, ["assistant::ToggleFocus"]);
actions!(
agent,
[
#[action(deprecated_aliases = ["assistant::ToggleFocus"])]
ToggleFocus
]
);
actions!(assistant, [ShowConfiguration]);
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema)]
#[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)]
#[action(namespace = agent, deprecated_aliases = ["assistant::OpenRulesLibrary", "assistant::DeployPromptLibrary"])]
#[serde(deny_unknown_fields)]
pub struct OpenRulesLibrary {
#[serde(skip)]
pub prompt_to_select: Option<Uuid>,
}
impl_action_with_deprecated_aliases!(
agent,
OpenRulesLibrary,
[
"assistant::OpenRulesLibrary",
"assistant::DeployPromptLibrary"
]
);
#[derive(Clone, Default, Deserialize, PartialEq, JsonSchema)]
#[derive(Clone, Default, Deserialize, PartialEq, JsonSchema, Action)]
#[action(namespace = assistant)]
#[serde(deny_unknown_fields)]
pub struct InlineAssist {
pub prompt: Option<String>,
}
impl_actions!(assistant, [InlineAssist]);
}
pub mod debugger {
@ -255,14 +240,16 @@ pub mod debugger {
actions!(debugger, [OpenOnboardingModal, ResetOnboarding]);
}
#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema)]
#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema, Action)]
#[action(namespace = projects)]
#[serde(deny_unknown_fields)]
pub struct OpenRecent {
#[serde(default)]
pub create_new_window: bool,
}
#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema)]
#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema, Action)]
#[action(namespace = projects)]
#[serde(deny_unknown_fields)]
pub struct OpenRemote {
#[serde(default)]
@ -271,8 +258,6 @@ pub struct OpenRemote {
pub create_new_window: bool,
}
impl_actions!(projects, [OpenRecent, OpenRemote]);
/// Where to spawn the task in the UI.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
@ -285,7 +270,8 @@ pub enum RevealTarget {
}
/// Spawn a task with name or open tasks modal.
#[derive(Debug, PartialEq, Clone, Deserialize, JsonSchema)]
#[derive(Debug, PartialEq, Clone, Deserialize, JsonSchema, Action)]
#[action(namespace = task)]
#[serde(untagged)]
pub enum Spawn {
/// Spawns a task by the name given.
@ -317,7 +303,8 @@ impl Spawn {
}
/// Rerun the last task.
#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema)]
#[derive(PartialEq, Clone, Deserialize, Default, JsonSchema, Action)]
#[action(namespace = task)]
#[serde(deny_unknown_fields)]
pub struct Rerun {
/// Controls whether the task context is reevaluated prior to execution of a task.
@ -340,14 +327,18 @@ pub struct Rerun {
pub task_id: Option<String>,
}
impl_actions!(task, [Spawn, Rerun]);
pub mod outline {
use std::sync::OnceLock;
use gpui::{AnyView, App, Window, action_as};
use gpui::{AnyView, App, Window, actions};
action_as!(outline, ToggleOutline as Toggle);
actions!(
outline,
[
#[action(name = "Toggle")]
ToggleOutline
]
);
/// A pointer to outline::toggle function, exposed here to sewer the breadcrumbs <-> outline dependency.
pub static TOGGLE_OUTLINE: OnceLock<fn(AnyView, &mut Window, &mut App)> = OnceLock::new();
}