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(
129        _: &mut schemars::r#gen::SchemaGenerator,
130    ) -> Option<schemars::schema::Schema>
131    where
132        Self: Sized,
133    {
134        None
135    }
136
137    /// A list of alternate, deprecated names for this action. These names can still be used to
138    /// invoke the action. In Zed, the keymap JSON schema will accept these old names and provide
139    /// warnings.
140    fn deprecated_aliases() -> &'static [&'static str]
141    where
142        Self: Sized,
143    {
144        &[]
145    }
146
147    /// Returns the deprecation message for this action, if any. In Zed, the keymap JSON schema will
148    /// cause this to be displayed as a warning.
149    fn deprecation_message() -> Option<&'static str>
150    where
151        Self: Sized,
152    {
153        None
154    }
155}
156
157impl std::fmt::Debug for dyn Action {
158    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
159        f.debug_struct("dyn Action")
160            .field("name", &self.name())
161            .finish()
162    }
163}
164
165impl dyn Action {
166    /// Type-erase Action type.
167    pub fn as_any(&self) -> &dyn Any {
168        self as &dyn Any
169    }
170}
171
172/// Error type for `Keystroke::parse`. This is used instead of `anyhow::Error` so that Zed can use
173/// markdown to display it.
174#[derive(Debug)]
175pub enum ActionBuildError {
176    /// Indicates that an action with this name has not been registered.
177    NotFound {
178        /// Name of the action that was not found.
179        name: String,
180    },
181    /// Indicates that an error occurred while building the action, typically a JSON deserialization
182    /// error.
183    BuildError {
184        /// Name of the action that was attempting to be built.
185        name: String,
186        /// Error that occurred while building the action.
187        error: anyhow::Error,
188    },
189}
190
191impl std::error::Error for ActionBuildError {
192    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
193        match self {
194            ActionBuildError::NotFound { .. } => None,
195            ActionBuildError::BuildError { error, .. } => error.source(),
196        }
197    }
198}
199
200impl Display for ActionBuildError {
201    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202        match self {
203            ActionBuildError::NotFound { name } => {
204                write!(f, "Didn't find an action named \"{name}\"")
205            }
206            ActionBuildError::BuildError { name, error } => {
207                write!(f, "Error while building action \"{name}\": {error}")
208            }
209        }
210    }
211}
212
213type ActionBuilder = fn(json: serde_json::Value) -> anyhow::Result<Box<dyn Action>>;
214
215pub(crate) struct ActionRegistry {
216    by_name: HashMap<&'static str, ActionData>,
217    names_by_type_id: HashMap<TypeId, &'static str>,
218    all_names: Vec<&'static str>, // So we can return a static slice.
219    deprecated_aliases: HashMap<&'static str, &'static str>, // deprecated name -> preferred name
220    deprecation_messages: HashMap<&'static str, &'static str>, // action name -> deprecation message
221}
222
223impl Default for ActionRegistry {
224    fn default() -> Self {
225        let mut this = ActionRegistry {
226            by_name: Default::default(),
227            names_by_type_id: Default::default(),
228            all_names: Default::default(),
229            deprecated_aliases: Default::default(),
230            deprecation_messages: Default::default(),
231        };
232
233        this.load_actions();
234
235        this
236    }
237}
238
239struct ActionData {
240    pub build: ActionBuilder,
241    pub json_schema: fn(&mut schemars::r#gen::SchemaGenerator) -> Option<schemars::schema::Schema>,
242}
243
244/// This type must be public so that our macros can build it in other crates.
245/// But this is an implementation detail and should not be used directly.
246#[doc(hidden)]
247pub struct MacroActionBuilder(pub fn() -> MacroActionData);
248
249/// This type must be public so that our macros can build it in other crates.
250/// But this is an implementation detail and should not be used directly.
251#[doc(hidden)]
252pub struct MacroActionData {
253    pub name: &'static str,
254    pub type_id: TypeId,
255    pub build: ActionBuilder,
256    pub json_schema: fn(&mut schemars::r#gen::SchemaGenerator) -> Option<schemars::schema::Schema>,
257    pub deprecated_aliases: &'static [&'static str],
258    pub deprecation_message: Option<&'static str>,
259}
260
261inventory::collect!(MacroActionBuilder);
262
263impl ActionRegistry {
264    /// Load all registered actions into the registry.
265    pub(crate) fn load_actions(&mut self) {
266        for builder in inventory::iter::<MacroActionBuilder> {
267            let action = builder.0();
268            self.insert_action(action);
269        }
270    }
271
272    #[cfg(test)]
273    pub(crate) fn load_action<A: Action>(&mut self) {
274        self.insert_action(MacroActionData {
275            name: A::name_for_type(),
276            type_id: TypeId::of::<A>(),
277            build: A::build,
278            json_schema: A::action_json_schema,
279            deprecated_aliases: A::deprecated_aliases(),
280            deprecation_message: A::deprecation_message(),
281        });
282    }
283
284    fn insert_action(&mut self, action: MacroActionData) {
285        let name = action.name;
286        if self.by_name.contains_key(name) {
287            panic!(
288                "Action with name `{name}` already registered \
289                (might be registered in `#[action(deprecated_aliases = [...])]`."
290            );
291        }
292        self.by_name.insert(
293            name,
294            ActionData {
295                build: action.build,
296                json_schema: action.json_schema,
297            },
298        );
299        for &alias in action.deprecated_aliases {
300            if self.by_name.contains_key(alias) {
301                panic!(
302                    "Action with name `{alias}` already registered. \
303                    `{alias}` is specified in `#[action(deprecated_aliases = [...])]` for action `{name}`."
304                );
305            }
306            self.by_name.insert(
307                alias,
308                ActionData {
309                    build: action.build,
310                    json_schema: action.json_schema,
311                },
312            );
313            self.deprecated_aliases.insert(alias, name);
314            self.all_names.push(alias);
315        }
316        self.names_by_type_id.insert(action.type_id, name);
317        self.all_names.push(name);
318        if let Some(deprecation_msg) = action.deprecation_message {
319            self.deprecation_messages.insert(name, deprecation_msg);
320        }
321    }
322
323    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
324    pub fn build_action_type(&self, type_id: &TypeId) -> Result<Box<dyn Action>> {
325        let name = self
326            .names_by_type_id
327            .get(type_id)
328            .with_context(|| format!("no action type registered for {type_id:?}"))?;
329
330        Ok(self.build_action(name, None)?)
331    }
332
333    /// Construct an action based on its name and optional JSON parameters sourced from the keymap.
334    pub fn build_action(
335        &self,
336        name: &str,
337        params: Option<serde_json::Value>,
338    ) -> std::result::Result<Box<dyn Action>, ActionBuildError> {
339        let build_action = self
340            .by_name
341            .get(name)
342            .ok_or_else(|| ActionBuildError::NotFound {
343                name: name.to_owned(),
344            })?
345            .build;
346        (build_action)(params.unwrap_or_else(|| json!({}))).map_err(|e| {
347            ActionBuildError::BuildError {
348                name: name.to_owned(),
349                error: e,
350            }
351        })
352    }
353
354    pub fn all_action_names(&self) -> &[&'static str] {
355        self.all_names.as_slice()
356    }
357
358    pub fn action_schemas(
359        &self,
360        generator: &mut schemars::r#gen::SchemaGenerator,
361    ) -> Vec<(&'static str, Option<schemars::schema::Schema>)> {
362        // Use the order from all_names so that the resulting schema has sensible order.
363        self.all_names
364            .iter()
365            .map(|name| {
366                let action_data = self
367                    .by_name
368                    .get(name)
369                    .expect("All actions in all_names should be registered");
370                (*name, (action_data.json_schema)(generator))
371            })
372            .collect::<Vec<_>>()
373    }
374
375    pub fn deprecated_aliases(&self) -> &HashMap<&'static str, &'static str> {
376        &self.deprecated_aliases
377    }
378
379    pub fn deprecation_messages(&self) -> &HashMap<&'static str, &'static str> {
380        &self.deprecation_messages
381    }
382}
383
384/// Generate a list of all the registered actions.
385/// Useful for transforming the list of available actions into a
386/// format suited for static analysis such as in validating keymaps, or
387/// generating documentation.
388pub fn generate_list_of_all_registered_actions() -> Vec<MacroActionData> {
389    let mut actions = Vec::new();
390    for builder in inventory::iter::<MacroActionBuilder> {
391        actions.push(builder.0());
392    }
393    actions
394}
395
396mod no_action {
397    use crate as gpui;
398    use std::any::Any as _;
399
400    actions!(
401        zed,
402        [
403            /// Action with special handling which unbinds the keybinding this is associated with,
404            /// if it is the highest precedence match.
405            NoAction
406        ]
407    );
408
409    /// Returns whether or not this action represents a removed key binding.
410    pub fn is_no_action(action: &dyn gpui::Action) -> bool {
411        action.as_any().type_id() == (NoAction {}).type_id()
412    }
413}