action.rs

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