Skip to main content

reovim_module_textobjects/
paragraph.rs

1//! Paragraph text object commands.
2//!
3//! Implements text objects: `ip`, `ap`.
4//!
5//! These commands select paragraph regions (contiguous non-blank lines).
6
7use {
8    reovim_driver_command::{
9        ArgKind, ArgSpec, Command, CommandContext, CommandHandler, CommandResult,
10    },
11    reovim_driver_session::{
12        OperatorPendingState, SessionRuntime, TextObjRange, api::ExtensionApi,
13    },
14    reovim_kernel::api::v1::{CommandId, Position, TextObject, TextObjectEngine},
15};
16
17use crate::ids;
18
19// =============================================================================
20// Helper function
21// =============================================================================
22
23/// Execute a paragraph text object and store the range for operator consumption.
24fn execute_paragraph_textobj(
25    runtime: &mut SessionRuntime<'_>,
26    args: &CommandContext,
27    text_object: TextObject,
28) -> CommandResult {
29    let Some(buffer_id) = args.buffer_id() else {
30        return CommandResult::error("No active buffer");
31    };
32
33    let count = args.count().unwrap_or(1);
34
35    // Get cursor from per-client Window (#471)
36    let Some(window) = runtime.windows().active() else {
37        return CommandResult::error("No active window");
38    };
39    let pos = Position::new(window.cursor.line, window.cursor.column);
40
41    // Calculate text object range using with_buffer_read callback
42    let range_result = runtime.with_buffer_read(buffer_id, |buffer| {
43        TextObjectEngine::range(buffer, pos, text_object, count)
44    });
45
46    let Some(range_result) = range_result else {
47        return CommandResult::error("Buffer not found");
48    };
49
50    let Some((start, end)) = range_result else {
51        return CommandResult::Success; // No-op if no paragraph found
52    };
53
54    // Kernel returns inclusive end, convert to exclusive
55    let end_exclusive = Position::new(end.line, end.column + 1);
56
57    // Store range in OperatorPendingState for operator consumption (Epic #465)
58    // Paragraph operations are linewise - they affect entire lines
59    let state = runtime.ext_mut::<OperatorPendingState>();
60    state.set_textobj_range(TextObjRange::linewise(start, end_exclusive));
61
62    CommandResult::Success
63}
64
65// =============================================================================
66// Inner Paragraph (ip)
67// =============================================================================
68
69/// Inner paragraph text object.
70///
71/// Selects contiguous non-blank lines.
72#[derive(Debug, Clone, Copy, Default)]
73pub struct InnerParagraph;
74
75impl Command for InnerParagraph {
76    fn id(&self) -> CommandId {
77        ids::INNER_PARAGRAPH
78    }
79
80    fn description(&self) -> &'static str {
81        "Inner paragraph text object"
82    }
83
84    fn args(&self) -> Vec<ArgSpec> {
85        vec![ArgSpec::optional(
86            "count",
87            ArgKind::Count,
88            "Number of paragraphs",
89        )]
90    }
91}
92
93impl CommandHandler for InnerParagraph {
94    fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
95        execute_paragraph_textobj(runtime, args, TextObject::InnerParagraph)
96    }
97}
98
99// =============================================================================
100// Around Paragraph (ap)
101// =============================================================================
102
103/// Around paragraph text object.
104///
105/// Selects contiguous non-blank lines plus trailing blank lines.
106#[derive(Debug, Clone, Copy, Default)]
107pub struct AroundParagraph;
108
109impl Command for AroundParagraph {
110    fn id(&self) -> CommandId {
111        ids::AROUND_PARAGRAPH
112    }
113
114    fn description(&self) -> &'static str {
115        "Around paragraph text object"
116    }
117
118    fn args(&self) -> Vec<ArgSpec> {
119        vec![ArgSpec::optional(
120            "count",
121            ArgKind::Count,
122            "Number of paragraphs",
123        )]
124    }
125}
126
127impl CommandHandler for AroundParagraph {
128    fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
129        execute_paragraph_textobj(runtime, args, TextObject::AParagraph)
130    }
131}
132
133// =============================================================================
134// Command Registration
135// =============================================================================
136
137/// Get all paragraph text object commands as boxed trait objects.
138#[must_use]
139pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
140    vec![Box::new(InnerParagraph), Box::new(AroundParagraph)]
141}