reovim_module_textobjects/
paragraph.rs1use {
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
19fn 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 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 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; };
53
54 let end_exclusive = Position::new(end.line, end.column + 1);
56
57 let state = runtime.ext_mut::<OperatorPendingState>();
60 state.set_textobj_range(TextObjRange::linewise(start, end_exclusive));
61
62 CommandResult::Success
63}
64
65#[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#[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#[must_use]
139pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
140 vec![Box::new(InnerParagraph), Box::new(AroundParagraph)]
141}