Skip to main content

reovim_module_commands/
lib.rs

1#![cfg_attr(coverage_nightly, allow(unused_features))]
2#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
3//! Ex-commands module - POLICY.
4//!
5//! This module implements the standard ex-commands (colon commands):
6//! - `:q` / `:quit` - Quit the editor
7//! - `:w` / `:write` - Write the buffer to disk
8//! - `:wq` - Write and quit
9//! - `:e` / `:edit` - Open a file
10//! - `:set` - Set editor options
11//! - `:colorscheme` - Switch color theme
12//! - `:detach` - Detach from server
13//! - `:servers` - List server instances
14//! - `:kill-server` - Kill the server
15
16mod buffer;
17mod colorscheme;
18mod edit;
19mod quit;
20mod session;
21mod set;
22mod substitute;
23mod write;
24
25use {
26    reovim_driver_command::{CommandHandler, CommandHandlerStore},
27    reovim_kernel::api::v1::{Module, ModuleContext, ModuleError, ModuleId, ProbeResult, Version},
28};
29
30pub use {
31    buffer::{BdeleteCommand, BnextCommand, BpreviousCommand, QuitAllCommand},
32    colorscheme::ColorschemeCommand,
33    edit::EditCommand,
34    quit::QuitCommand,
35    session::{DetachCommand, KillServerCommand, ServersCommand},
36    set::SetCommand,
37    substitute::SubstituteCommand,
38    write::{WriteCommand, WriteQuitCommand},
39};
40
41/// Returns all command handlers provided by this module.
42#[must_use]
43pub fn command_handlers() -> Vec<Box<dyn CommandHandler>> {
44    let mut cmds: Vec<Box<dyn CommandHandler>> = vec![
45        Box::new(EditCommand),
46        Box::new(QuitCommand),
47        Box::new(WriteCommand),
48        Box::new(WriteQuitCommand),
49        Box::new(ColorschemeCommand),
50        Box::new(SetCommand),
51        Box::new(SubstituteCommand),
52    ];
53    cmds.extend(session::command_handlers());
54    cmds.extend(buffer::command_handlers());
55    cmds
56}
57
58// ============================================================================
59// Module trait implementation
60// ============================================================================
61
62/// Commands module instance.
63pub struct CommandsModule;
64
65impl CommandsModule {
66    /// Create a new commands module.
67    #[must_use]
68    pub const fn new() -> Self {
69        Self
70    }
71}
72
73impl Default for CommandsModule {
74    fn default() -> Self {
75        Self::new()
76    }
77}
78
79impl Module for CommandsModule {
80    fn id(&self) -> ModuleId {
81        ModuleId::new("commands")
82    }
83
84    fn name(&self) -> &'static str {
85        "Ex-Commands"
86    }
87
88    fn version(&self) -> Version {
89        Version::new(0, 9, 0)
90    }
91
92    fn init(&mut self, ctx: &ModuleContext) -> ProbeResult {
93        // Register command handlers (#547)
94        let store = ctx.services.get_or_create::<CommandHandlerStore>();
95        for handler in command_handlers() {
96            store.add(handler);
97        }
98
99        ProbeResult::Success
100    }
101
102    fn exit(&mut self) -> Result<(), ModuleError> {
103        Ok(())
104    }
105
106    fn provides(&self) -> &[&'static str] {
107        &[reovim_capabilities::COMMAND_DISPATCH]
108    }
109}
110
111// Generate FFI entry points for dynamic loading (only when building standalone cdylib)
112#[cfg(feature = "dynamic")]
113reovim_module_macros::declare_module!(CommandsModule);
114
115#[cfg(test)]
116#[path = "lib_tests.rs"]
117mod tests;