Skip to main content

reovim_module_textobjects/
lib.rs

1#![cfg_attr(coverage_nightly, allow(unused_features))]
2#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
3//! Vim text objects module - POLICY.
4//!
5//! This module implements text object commands for operator-pending mode:
6//! - Word objects: `iw`, `aw`, `iW`, `aW`
7//! - Quote objects: `i"`, `a"`, `i'`, `a'`, `` i` ``, `` a` ``
8//! - Bracket objects: `i(`, `a(`, `i[`, `a[`, `i{`, `a{`, `i<`, `a<`
9//! - Paragraph objects: `ip`, `ap`
10//!
11//! # Mechanism vs Policy
12//!
13//! - **Mechanism (Kernel)**: `TextObjectEngine::range()` computes text ranges
14//! - **Policy (This Module)**: Commands wire keys to specific text objects
15//!
16//! # Example
17//!
18//! ```ignore
19//! use reovim_module_textobjects::word;
20//!
21//! // Get all word text object commands
22//! let cmds = word::all_commands();
23//! for cmd in &cmds {
24//!     println!("{}: {}", cmd.id(), cmd.description());
25//! }
26//! ```
27
28pub mod bracket;
29pub mod ids;
30pub mod paragraph;
31pub mod quote;
32pub mod semantic;
33pub mod word;
34
35use {
36    reovim_driver_command::{CommandHandler, CommandHandlerStore, CommandProvider},
37    reovim_kernel::api::v1::{Module, ModuleContext, ModuleError, ModuleId, ProbeResult, Version},
38};
39
40/// Module identifier for text objects module.
41pub const TEXTOBJECTS_MODULE: ModuleId = ModuleId::new("textobjects");
42
43/// Text objects module instance.
44pub struct TextObjectsModule;
45
46impl TextObjectsModule {
47    /// Create a new text objects module.
48    #[must_use]
49    pub const fn new() -> Self {
50        Self
51    }
52}
53
54impl Default for TextObjectsModule {
55    fn default() -> Self {
56        Self::new()
57    }
58}
59
60impl Module for TextObjectsModule {
61    fn id(&self) -> ModuleId {
62        TEXTOBJECTS_MODULE
63    }
64
65    fn name(&self) -> &'static str {
66        "Vim Text Objects"
67    }
68
69    fn version(&self) -> Version {
70        Version::new(0, 9, 0)
71    }
72
73    fn init(&mut self, ctx: &ModuleContext) -> ProbeResult {
74        // Self-register commands (same pattern as EditorModule)
75        let command_store = ctx.services.get_or_create::<CommandHandlerStore>();
76        for handler in self.command_handlers() {
77            command_store.add(handler);
78        }
79        ProbeResult::Success
80    }
81
82    fn exit(&mut self) -> Result<(), ModuleError> {
83        Ok(())
84    }
85}
86
87impl CommandProvider for TextObjectsModule {
88    fn command_handlers(&self) -> Vec<Box<dyn CommandHandler>> {
89        all_commands()
90    }
91}
92
93/// Get all text object commands.
94#[must_use]
95pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
96    let mut cmds = Vec::new();
97    cmds.extend(word::all_commands());
98    cmds.extend(quote::all_commands());
99    cmds.extend(bracket::all_commands());
100    cmds.extend(paragraph::all_commands());
101    cmds.extend(semantic::all_commands());
102    cmds
103}
104
105// Generate FFI entry points for dynamic loading (only when building standalone cdylib)
106#[cfg(feature = "dynamic")]
107reovim_module_macros::declare_module!(TextObjectsModule);
108
109#[cfg(test)]
110mod bracket_tests;
111#[cfg(test)]
112mod lib_tests;
113#[cfg(test)]
114mod paragraph_tests;
115#[cfg(test)]
116mod quote_tests;
117#[cfg(test)]
118mod semantic_tests;
119#[cfg(test)]
120mod word_tests;