reovim_plugin_which_key/
commands.rs

1//! Which-key commands
2
3use std::{any::Any, sync::Arc};
4
5use reovim_core::{
6    command::{CommandResult, CommandTrait, ExecutionContext},
7    declare_event_command,
8    event_bus::{DynEvent, Event},
9    keystroke::KeySequence,
10};
11
12/// Event to open the which-key panel with a prefix filter
13///
14/// When dispatched, the which-key plugin will show keybindings that start
15/// with the given prefix. For example, if prefix is `[g]`, it will show
16/// all bindings starting with `g` (like `gg`, `ge`, `gd`, etc.).
17#[derive(Debug, Clone)]
18pub struct WhichKeyOpen {
19    /// The prefix keys to filter bindings (e.g., [g] for g? trigger)
20    pub prefix: KeySequence,
21}
22
23impl WhichKeyOpen {
24    /// Create a new `WhichKeyOpen` event with the given prefix
25    #[must_use]
26    pub const fn new(prefix: KeySequence) -> Self {
27        Self { prefix }
28    }
29
30    /// Create a `WhichKeyOpen` event with no prefix (show all bindings)
31    #[must_use]
32    pub fn all() -> Self {
33        Self {
34            prefix: KeySequence::default(),
35        }
36    }
37}
38
39impl Event for WhichKeyOpen {
40    fn priority(&self) -> u32 {
41        100
42    }
43}
44
45/// Command to trigger which-key with a specific prefix
46///
47/// This is used as an inline command for keybindings like `g?`, `z?`, etc.
48/// Each binding carries its own prefix so which-key knows what to filter.
49#[derive(Debug, Clone)]
50pub struct WhichKeyTrigger {
51    pub prefix: KeySequence,
52}
53
54impl WhichKeyTrigger {
55    #[must_use]
56    pub const fn new(prefix: KeySequence) -> Self {
57        Self { prefix }
58    }
59
60    #[must_use]
61    pub fn all() -> Self {
62        Self {
63            prefix: KeySequence::default(),
64        }
65    }
66
67    /// Create an Arc-wrapped command for use as inline command
68    #[must_use]
69    pub fn arc(prefix: KeySequence) -> Arc<dyn CommandTrait> {
70        Arc::new(Self::new(prefix))
71    }
72}
73
74impl CommandTrait for WhichKeyTrigger {
75    fn name(&self) -> &'static str {
76        "which_key_trigger"
77    }
78
79    fn description(&self) -> &'static str {
80        "Show available keybindings"
81    }
82
83    fn execute(&self, _ctx: &mut ExecutionContext) -> CommandResult {
84        // Emit WhichKeyOpen event with this command's prefix
85        CommandResult::EmitEvent(DynEvent::new(WhichKeyOpen::new(self.prefix.clone())))
86    }
87
88    fn clone_box(&self) -> Box<dyn CommandTrait> {
89        Box::new(self.clone())
90    }
91
92    fn as_any(&self) -> &dyn Any {
93        self
94    }
95}
96
97declare_event_command! {
98    WhichKeyClose,
99    id: "which_key_close",
100    description: "Close which-key panel",
101}
102
103declare_event_command! {
104    WhichKeyBackspace,
105    id: "which_key_backspace",
106    description: "Remove last key from filter",
107}