Skip to main content

fresh_core/
command.rs

1use serde::{Deserialize, Serialize};
2
3/// Source of a command (builtin or from a plugin)
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
5#[ts(export)]
6pub enum CommandSource {
7    /// Built-in editor command
8    Builtin,
9    /// Command registered by a plugin (contains plugin filename without extension)
10    Plugin(String),
11}
12
13/// A command registered by a plugin via the service bridge.
14/// This is a simplified version that the editor converts to its internal Command type.
15#[derive(Debug, Clone, Serialize, Deserialize, ts_rs::TS)]
16#[ts(export)]
17pub struct Command {
18    /// Command name (e.g., "Open File")
19    pub name: String,
20    /// Command description
21    pub description: String,
22    /// The action name to trigger (for plugin commands, this is the function name)
23    pub action_name: String,
24    /// Plugin that registered this command
25    pub plugin_name: String,
26    /// Custom contexts required for this command (plugin-defined contexts like "vi-mode")
27    pub custom_contexts: Vec<String>,
28    /// When `true`, a key bound to this command bypasses terminal
29    /// keyboard capture — the action fires instead of the keystroke
30    /// being forwarded to the PTY child. Use for commands the user
31    /// must always be able to reach (e.g. the orchestrator picker,
32    /// "switch to other session", a global panic-exit) so a focused
33    /// terminal pane doesn't trap them. Default `false` — keys
34    /// bound to non-bypassing commands still go to the PTY when
35    /// keyboard capture is on, matching the existing UX.
36    #[serde(default)]
37    pub terminal_bypass: bool,
38}
39
40/// A single suggestion item for autocomplete
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
42#[serde(deny_unknown_fields)]
43#[ts(export, rename = "PromptSuggestion")]
44pub struct Suggestion {
45    /// The text to display
46    pub text: String,
47    /// Optional description
48    #[serde(default)]
49    #[ts(optional)]
50    pub description: Option<String>,
51    /// The value to use when selected (defaults to text if None)
52    #[serde(default)]
53    #[ts(optional)]
54    pub value: Option<String>,
55    /// Whether this suggestion is disabled (greyed out, defaults to false)
56    #[serde(default)]
57    #[ts(optional)]
58    pub disabled: Option<bool>,
59    /// Optional styled rendering of `description`. When present, the
60    /// suggestion list renders these spans (in order) in place of the
61    /// plain `description` text — letting a plugin highlight a portion
62    /// of the row, e.g. the symbol word inside a code-line snippet.
63    #[serde(default)]
64    #[ts(optional)]
65    pub description_spans: Option<Vec<crate::api::StyledText>>,
66    /// Optional keyboard shortcut
67    #[serde(default)]
68    #[ts(optional)]
69    pub keybinding: Option<String>,
70    /// Source of the command (for command palette) - internal, not settable by plugins
71    #[serde(skip)]
72    #[ts(skip)]
73    pub source: Option<CommandSource>,
74}
75
76#[cfg(feature = "plugins")]
77impl<'js> rquickjs::FromJs<'js> for Suggestion {
78    fn from_js(_ctx: &rquickjs::Ctx<'js>, value: rquickjs::Value<'js>) -> rquickjs::Result<Self> {
79        rquickjs_serde::from_value(value).map_err(|e| rquickjs::Error::FromJs {
80            from: "object",
81            to: "Suggestion",
82            message: Some(e.to_string()),
83        })
84    }
85}
86
87impl Suggestion {
88    pub fn new(text: String) -> Self {
89        Self {
90            text,
91            description: None,
92            value: None,
93            disabled: None,
94            description_spans: None,
95            keybinding: None,
96            source: None,
97        }
98    }
99
100    /// Check if this suggestion is disabled
101    pub fn is_disabled(&self) -> bool {
102        self.disabled.unwrap_or(false)
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    /// `is_disabled` mirrors the `disabled` option and treats `None` as enabled.
111    #[test]
112    fn is_disabled_reflects_disabled_field() {
113        let mut s = Suggestion::new("foo".into());
114        assert!(!s.is_disabled(), "None defaults to enabled");
115
116        s.disabled = Some(false);
117        assert!(!s.is_disabled());
118
119        s.disabled = Some(true);
120        assert!(s.is_disabled());
121    }
122
123    /// `Suggestion::from_js` round-trips user-visible fields through
124    /// `rquickjs_serde`. A mutant that returns `Ok(Default::default())`
125    /// would drop `text` (becoming empty) and `description`.
126    #[cfg(feature = "plugins")]
127    #[test]
128    fn suggestion_from_js_decodes_distinguishing_fields() {
129        use rquickjs::{Context, FromJs, Runtime, Value};
130        let rt = Runtime::new().unwrap();
131        let ctx = Context::full(&rt).unwrap();
132        ctx.with(|ctx| {
133            let v: Value = ctx
134                .eval::<Value, _>(b"({text: 'hello', description: 'world'})".as_slice())
135                .unwrap();
136            let got = Suggestion::from_js(&ctx, v).unwrap();
137            assert_eq!(got.text, "hello");
138            assert_eq!(got.description.as_deref(), Some("world"));
139        });
140    }
141}