1use anyhow::{Context as _, Result};
  2use collections::HashMap;
  3pub use gpui_macros::Action;
  4pub use no_action::{NoAction, is_no_action};
  5use serde_json::json;
  6use std::{
  7    any::{Any, TypeId},
  8    fmt::Display,
  9};
 10
 11/// Defines and registers unit structs that can be used as actions. For more complex data types, derive `Action`.
 12///
 13/// For example:
 14///
 15/// ```
 16/// use gpui::actions;
 17/// actions!(editor, [MoveUp, MoveDown, MoveLeft, MoveRight, Newline]);
 18/// ```
 19///
 20/// This will create actions with names like `editor::MoveUp`, `editor::MoveDown`, etc.
 21///
 22/// The namespace argument `editor` can also be omitted, though it is required for Zed actions.
 23#[macro_export]
 24macro_rules! actions {
 25    ($namespace:path, [ $( $(#[$attr:meta])* $name:ident),* $(,)? ]) => {
 26        $(
 27            #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug, gpui::Action)]
 28            #[action(namespace = $namespace)]
 29            $(#[$attr])*
 30            pub struct $name;
 31        )*
 32    };
 33    ([ $( $(#[$attr:meta])* $name:ident),* $(,)? ]) => {
 34        $(
 35            #[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug, gpui::Action)]
 36            $(#[$attr])*
 37            pub struct $name;
 38        )*
 39    };
 40}
 41
 42/// Actions are used to implement keyboard-driven UI. When you declare an action, you can bind keys
 43/// to the action in the keymap and listeners for that action in the element tree.
 44///
 45/// To declare a list of simple actions, you can use the actions! macro, which defines a simple unit
 46/// struct action for each listed action name in the given namespace.
 47///
 48/// ```
 49/// use gpui::actions;
 50/// actions!(editor, [MoveUp, MoveDown, MoveLeft, MoveRight, Newline]);
 51/// ```
 52///
 53/// Registering the actions with the same name will result in a panic during  `App` creation.
 54///
 55/// # Derive Macro
 56///
 57/// More complex data types can also be actions, by using the derive macro for `Action`:
 58///
 59/// ```
 60/// use gpui::Action;
 61/// #[derive(Clone, PartialEq, serde::Deserialize, schemars::JsonSchema, Action)]
 62/// #[action(namespace = editor)]
 63/// pub struct SelectNext {
 64///     pub replace_newest: bool,
 65/// }
 66/// ```
 67///
 68/// The derive macro for `Action` requires that the type implement `Clone` and `PartialEq`. It also
 69/// requires `serde::Deserialize` and `schemars::JsonSchema` unless `#[action(no_json)]` is
 70/// specified. In Zed these trait impls are used to load keymaps from JSON.
 71///
 72/// Multiple arguments separated by commas may be specified in `#[action(...)]`:
 73///
 74/// - `namespace = some_namespace` sets the namespace. In Zed this is required.
 75///
 76/// - `name = "ActionName"` overrides the action's name. This must not contain `::`.
 77///
 78/// - `no_json` causes the `build` method to always error and `action_json_schema` to return `None`,
 79///   and allows actions not implement `serde::Serialize` and `schemars::JsonSchema`.
 80///
 81/// - `no_register` skips registering the action. This is useful for implementing the `Action` trait
 82///   while not supporting invocation by name or JSON deserialization.
 83///
 84/// - `deprecated_aliases = ["editor::SomeAction"]` specifies deprecated old names for the action.
 85///   These action names should *not* correspond to any actions that are registered. These old names
 86///   can then still be used to refer to invoke this action. In Zed, the keymap JSON schema will
 87///   accept these old names and provide warnings.
 88///
 89/// - `deprecated = "Message about why this action is deprecation"` specifies a deprecation message.
 90///   In Zed, the keymap JSON schema will cause this to be displayed as a warning.
 91///
 92/// # Manual Implementation
 93///
 94/// If you want to control the behavior of the action trait manually, you can use the lower-level
 95/// `#[register_action]` macro, which only generates the code needed to register your action before
 96/// `main`.
 97///
 98/// ```
 99/// use gpui::{SharedString, register_action};
100/// #[derive(Clone, PartialEq, Eq, serde::Deserialize, schemars::JsonSchema)]
101/// pub struct Paste {
102///     pub content: SharedString,
103/// }
104///
105/// impl gpui::Action for Paste {
106///     # fn boxed_clone(&self) -> Box<dyn gpui::Action> { unimplemented!()}
107///     # fn partial_eq(&self, other: &dyn gpui::Action) -> bool { unimplemented!() }
108///     # fn name(&self) -> &'static str { "Paste" }
109///     # fn name_for_type() -> &'static str { "Paste" }
110///     # fn build(value: serde_json::Value) -> anyhow::Result<Box<dyn gpui::Action>> {
111///     #     unimplemented!()
112///     # }
113/// }
114///
115/// register_action!(Paste);
116/// ```
117pub trait Action: Any + Send {
118    /// Clone the action into a new box
119    fn boxed_clone(&self) -> Box<dyn Action>;
120
121    /// Do a partial equality check on this action and the other
122    fn partial_eq(&self, action: &dyn Action) -> bool;
123
124    /// Get the name of this action, for displaying in UI
125    fn name(&self) -> &'static str;
126
127    /// Get the name of this action type (static)
128    fn name_for_type() -> &'static str
129    where
130        Self: Sized;
131
132    /// Build this action from a JSON value. This is used to construct actions from the keymap.
133    /// A value of `{}` will be passed for actions that don't have any parameters.
134    fn build(value: serde_json::Value) -> Result<Box<dyn Action>>
135    where
136        Self: Sized;
137
138    /// Optional JSON schema for the action's input data.
139    fn action_json_schema(_: &mut schemars::SchemaGenerator) -> Option<schemars::Schema>
140    where
141        Self: Sized,
142    {
143        None
144    }
145
146    /// A list of alternate, deprecated names for this action. These names can still be used to
147    /// invoke the action. In Zed, the keymap JSON schema will accept these old names and provide
148    /// warnings.
149    fn deprecated_aliases() -> &'static [&'static str]
150    where
151        Self: Sized,
152    {
153        &[]
154    }
155
156    /// Returns the deprecation message for this action, if any. In Zed, the keymap JSON schema will
157    /// cause this to be displayed as a warning.
158    fn deprecation_message() -> Option<&'static str>
159    where
160        Self: Sized,
161    {
162        None
163    }
164
165    /// The documentation for this action, if any. When using the derive macro for actions
166    /// this will be automatically generated from the doc comments on the action struct.
167    fn documentation() -> Option<&'static str>
168    where
169        Self: Sized,
170    {
171        None
172    }
173}
174
175impl std::fmt::Debug for dyn Action {
176    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177        f.debug_struct("dyn Action")
178            .field("name", &self.name())
179            .finish()
180    }
181}
182
183impl dyn Action {
184    /// Type-erase Action type.
185    pub fn as_any(&self) -> &dyn Any {
186        self as &dyn Any
187    }
188}
189
190/// Error type for `Keystroke::parse`. This is used instead of `anyhow::Error` so that Zed can use
191/// markdown to display it.
192#[derive(Debug)]
193pub enum ActionBuildError {
194    /// Indicates that an action with this name has not been registered.
195    NotFound {
196        /// Name of the action that was not found.
197        name: String,
198    },
199    /// Indicates that an error occurred while building the action, typically a JSON deserialization
200    /// error.
201    BuildError {
202        /// Name of the action that was attempting to be built.
203        name: String,
204        /// Error that occurred while building the action.
205        error: anyhow::Error,
206    },
207}
208
209impl std::error::Error for ActionBuildError {
210    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
211        match self {
212            ActionBuildError::NotFound { .. } => None,
213            ActionBuildError::BuildError { error, .. } => error.source(),
214        }
215    }
216}
217
218impl Display for ActionBuildError {
219    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
220        match self {
221            ActionBuildError::NotFound { name } => {
222                write!(f, "Didn't find an action named \"{name}\"")
223            }
224            ActionBuildError::BuildError { name, error } => {
225                write!(f, "Error while building action \"{name}\": {error}")
226            }
227        }
228    }
229}
230
231type ActionBuilder = fn(json: serde_json::Value) -> anyhow::Result<Box<dyn Action>>;
232
233pub(crate) struct ActionRegistry {
234    by_name: HashMap<&'static str, ActionData>,
235    names_by_type_id: HashMap<TypeId, &'static str>,
236    all_names: Vec<&'static str>, // So we can return a static slice.
237    deprecated_aliases: HashMap<&'static str, &'static str>, // deprecated name -> preferred name
238    deprecation_messages: HashMap<&'static str, &'static str>, // action name -> deprecation message
239    documentation: HashMap<&'static str, &'static str>, // action name -> documentation
240}
241
242impl Default for ActionRegistry {
243    fn default() -> Self {
244        let mut this = ActionRegistry {
245            by_name: Default::default(),
246            names_by_type_id: Default::default(),
247            documentation: Default::default(),
248            all_names: Default::default(),
249            deprecated_aliases: Default::default(),
250            deprecation_messages: Default::default(),
251        };
252
253        this.load_actions();
254
255        this
256    }
257}
258
259struct ActionData {
260    pub build: ActionBuilder,
261    pub json_schema: fn(&mut schemars::SchemaGenerator) -> Option<schemars::Schema>,
262}
263
264/// This type must be public so that our macros can build it in other crates.
265/// But this is an implementation detail and should not be used directly.
266#[doc(hidden)]
267pub struct MacroActionBuilder(pub fn() -> MacroActionData);
268
269/// This type must be public so that our macros can build it in other crates.
270/// But this is an implementation detail and should not be used directly.
271#[doc(hidden)]
272pub struct MacroActionData {
273    pub name: &'static str,
274    pub type_id: TypeId,
275    pub build: ActionBuilder,
276    pub json_schema: fn(&mut schemars::SchemaGenerator) -> Option<schemars::Schema>,
277    pub deprecated_aliases: &'static [&'static str],
278    pub deprecation_message: Option<&'static str>,
279    pub documentation: Option<&'static str>,
280}
281
282inventory::collect!(MacroActionBuilder);
283
284impl ActionRegistry {
285    /// Load all registered actions into the registry.
286    pub(crate) fn load_actions(&mut self) {
287        for builder in inventory::iter::<MacroActionBuilder> {
288            let action = builder.0();
289            self.insert_action(action);
290        }
291    }
292
293    #[cfg(test)]
294    pub(crate) fn load_action<A: Action>(&mut self) {
295        self.insert_action(MacroActionData {
296            name: A::name_for_type(),
297            type_id: TypeId::of::<A>(),
298            build: A::build,
299            json_schema: A::action_json_schema,
300            deprecated_aliases: A::deprecated_aliases(),
301            deprecation_message: A::deprecation_message(),
302            documentation: A::documentation(),
303        });
304    }
305
306    fn insert_action(&mut self, action: MacroActionData) {
307        let name = action.name;
308        if self.by_name.contains_key(name) {
309            panic!(
310                "Action with name `{name}` already registered \
311                (might be registered in `#[action(deprecated_aliases = [...])]`."
312            );
313        }
314        self.by_name.insert(
315            name,
316            ActionData {
317                build: action.build,
318                json_schema: action.json_schema,
319            },
320        );
321        for &alias in action.deprecated_aliases {
322            if self.by_name.contains_key(alias) {
323                panic!(
324                    "Action with name `{alias}` already registered. \
325                    `{alias}` is specified in `#[action(deprecated_aliases = [...])]` for action `{name}`."
326                );
327            }
328            self.by_name.insert(
329                alias,
330                ActionData {
331                    build: action.build,
332                    json_schema: action.json_schema,
333                },
334            );
335            self.deprecated_aliases.insert(alias, name);
336            self.all_names.push(alias);
337        }
338        self.names_by_type_id.insert(action.type_id, name);
339        self.all_names.push(name);
340        if let Some(deprecation_msg) = action.deprecation_message {
341            self.deprecation_messages.insert(name, deprecation_msg);
342        }
343        if let Some(documentation) = action.documentation {
344            self.documentation.insert(name, documentation);
345        }
346    }
347
348    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
349    pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
350        let name = self
351            .names_by_type_id
352            .get(type_id)
353            .with_context(|| format!("no action type registered for {type_id:?}"))?;
354
355        Ok(self.build_action(name, None)?)
356    }
357
358    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
359    pub fn build_action(
360        &self,
361        name: &str,
362        params: Option<serde_json::Value>,
363    ) -> std::result::Result<Box<dyn Action>, ActionBuildError> {
364        let build_action = self
365            .by_name
366            .get(name)
367            .ok_or_else(|| ActionBuildError::NotFound {
368                name: name.to_owned(),
369            })?
370            .build;
371        (build_action)(params.unwrap_or_else(|| json!({}))).map_err(|e| {
372            ActionBuildError::BuildError {
373                name: name.to_owned(),
374                error: e,
375            }
376        })
377    }
378
379    pub const fn all_action_names(&self) -> &[&'static str] {
380        self.all_names.as_slice()
381    }
382
383    pub fn action_schemas(
384        &self,
385        generator: &mut schemars::SchemaGenerator,
386    ) -> Vec<(&'static str, Option<schemars::Schema>)> {
387        // Use the order from all_names so that the resulting schema has sensible order.
388        self.all_names
389            .iter()
390            .map(|name| {
391                let action_data = self
392                    .by_name
393                    .get(name)
394                    .expect("All actions in all_names should be registered");
395                (*name, (action_data.json_schema)(generator))
396            })
397            .collect::<Vec<_>>()
398    }
399
400    pub const fn deprecated_aliases(&self) -> &HashMap<&'static str, &'static str> {
401        &self.deprecated_aliases
402    }
403
404    pub const fn deprecation_messages(&self) -> &HashMap<&'static str, &'static str> {
405        &self.deprecation_messages
406    }
407
408    pub const fn documentation(&self) -> &HashMap<&'static str, &'static str> {
409        &self.documentation
410    }
411}
412
413/// Generate a list of all the registered actions.
414/// Useful for transforming the list of available actions into a
415/// format suited for static analysis such as in validating keymaps, or
416/// generating documentation.
417pub fn generate_list_of_all_registered_actions() -> impl Iterator<Item = MacroActionData> {
418    inventory::iter::<MacroActionBuilder>
419        .into_iter()
420        .map(|builder| builder.0())
421}
422
423mod no_action {
424    use crate as gpui;
425    use std::any::Any as _;
426
427    actions!(
428        zed,
429        [
430            /// Action with special handling which unbinds the keybinding this is associated with,
431            /// if it is the highest precedence match.
432            NoAction
433        ]
434    );
435
436    /// Returns whether or not this action represents a removed key binding.
437    pub fn is_no_action(action: &dyn gpui::Action) -> bool {
438        action.as_any().type_id() == (NoAction {}).type_id()
439    }
440}