reovim_plugin_which_key/
commands.rs1use 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#[derive(Debug, Clone)]
18pub struct WhichKeyOpen {
19 pub prefix: KeySequence,
21}
22
23impl WhichKeyOpen {
24 #[must_use]
26 pub const fn new(prefix: KeySequence) -> Self {
27 Self { prefix }
28 }
29
30 #[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#[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 #[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 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}