reovim_module_textobjects/
word.rs1use {
15 reovim_driver_command::{
16 ArgKind, ArgSpec, Command, CommandContext, CommandHandler, CommandResult,
17 },
18 reovim_driver_session::{
19 OperatorPendingState, SessionRuntime, TextObjRange,
20 api::{ExtensionApi, ModeApi, Selection, SelectionMode},
21 },
22 reovim_kernel::api::v1::{CommandId, Position, TextObject, TextObjectEngine, WordBoundary},
23};
24
25use crate::ids;
26
27fn is_visual_mode(runtime: &SessionRuntime<'_>) -> bool {
33 let mode = runtime.current_mode();
34 let mode_name = mode.name();
35 mode_name == "visual" || mode_name == "visual-line" || mode_name == "visual-block"
36}
37
38fn visual_selection_mode(runtime: &SessionRuntime<'_>) -> SelectionMode {
40 let mode = runtime.current_mode();
41 match mode.name() {
42 "visual-line" => SelectionMode::Line,
43 "visual-block" => SelectionMode::Block,
44 _ => SelectionMode::Character,
45 }
46}
47
48#[cfg_attr(coverage_nightly, coverage(off))]
51fn execute_word_textobj(
52 runtime: &mut SessionRuntime<'_>,
53 args: &CommandContext,
54 text_object: TextObject,
55) -> CommandResult {
56 let Some(buffer_id) = args.buffer_id() else {
57 return CommandResult::error("No active buffer");
58 };
59
60 let count = args.count().unwrap_or(1);
61
62 let Some(window) = runtime.windows().active() else {
64 return CommandResult::error("No active window");
65 };
66 let pos = Position::new(window.cursor.line, window.cursor.column);
67
68 let range_result = runtime.with_buffer_read(buffer_id, |buffer| {
70 TextObjectEngine::range(buffer, pos, text_object, count)
71 });
72
73 let Some(range_result) = range_result else {
74 return CommandResult::error("Buffer not found");
75 };
76
77 let Some((start, end)) = range_result else {
78 return CommandResult::Success; };
80
81 let end_exclusive = Position::new(end.line, end.column + 1);
84
85 if is_visual_mode(runtime) {
87 let sel_mode = visual_selection_mode(runtime);
89 if let Some(window) = runtime.windows_mut().active_mut() {
90 window.selection = Some(Selection::new(start, end_exclusive, sel_mode));
91 window.cursor = end.into();
93 }
94 } else {
95 let state = runtime.ext_mut::<OperatorPendingState>();
98 state.set_textobj_range(TextObjRange::characterwise(start, end_exclusive));
99 }
100
101 CommandResult::Success
102}
103
104#[derive(Debug, Clone, Copy, Default)]
112pub struct InnerWord;
113
114impl Command for InnerWord {
115 fn id(&self) -> CommandId {
116 ids::INNER_WORD
117 }
118
119 fn description(&self) -> &'static str {
120 "Inner word text object"
121 }
122
123 fn args(&self) -> Vec<ArgSpec> {
124 vec![ArgSpec::optional(
125 "count",
126 ArgKind::Count,
127 "Number of words",
128 )]
129 }
130}
131
132impl CommandHandler for InnerWord {
133 fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
134 execute_word_textobj(runtime, args, TextObject::InnerWord(WordBoundary::Word))
135 }
136}
137
138#[derive(Debug, Clone, Copy, Default)]
146pub struct AWord;
147
148impl Command for AWord {
149 fn id(&self) -> CommandId {
150 ids::AROUND_WORD
151 }
152
153 fn description(&self) -> &'static str {
154 "A word text object (including whitespace)"
155 }
156
157 fn args(&self) -> Vec<ArgSpec> {
158 vec![ArgSpec::optional(
159 "count",
160 ArgKind::Count,
161 "Number of words",
162 )]
163 }
164}
165
166impl CommandHandler for AWord {
167 fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
168 execute_word_textobj(runtime, args, TextObject::AWord(WordBoundary::Word))
169 }
170}
171
172#[derive(Debug, Clone, Copy, Default)]
180pub struct InnerWordBig;
181
182impl Command for InnerWordBig {
183 fn id(&self) -> CommandId {
184 ids::INNER_WORD_BIG
185 }
186
187 fn description(&self) -> &'static str {
188 "Inner WORD text object"
189 }
190
191 fn args(&self) -> Vec<ArgSpec> {
192 vec![ArgSpec::optional(
193 "count",
194 ArgKind::Count,
195 "Number of WORDs",
196 )]
197 }
198}
199
200impl CommandHandler for InnerWordBig {
201 fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
202 execute_word_textobj(runtime, args, TextObject::InnerWord(WordBoundary::BigWord))
203 }
204}
205
206#[derive(Debug, Clone, Copy, Default)]
214pub struct AWordBig;
215
216impl Command for AWordBig {
217 fn id(&self) -> CommandId {
218 ids::AROUND_WORD_BIG
219 }
220
221 fn description(&self) -> &'static str {
222 "A WORD text object (including whitespace)"
223 }
224
225 fn args(&self) -> Vec<ArgSpec> {
226 vec![ArgSpec::optional(
227 "count",
228 ArgKind::Count,
229 "Number of WORDs",
230 )]
231 }
232}
233
234impl CommandHandler for AWordBig {
235 fn execute(&self, runtime: &mut SessionRuntime<'_>, args: &CommandContext) -> CommandResult {
236 execute_word_textobj(runtime, args, TextObject::AWord(WordBoundary::BigWord))
237 }
238}
239
240#[must_use]
246pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
247 vec![
248 Box::new(InnerWord),
249 Box::new(AWord),
250 Box::new(InnerWordBig),
251 Box::new(AWordBig),
252 ]
253}
254
255