reovim_module_textobjects/
semantic.rs1use {
17 reovim_driver_command::{Command, CommandContext, CommandHandler, CommandResult},
18 reovim_driver_session::{
19 OperatorPendingState, SessionRuntime, TextObjRange,
20 api::{ExtensionApi, ModeApi, Selection, SelectionMode},
21 },
22 reovim_driver_syntax::{SyntaxSessionState, TextObjectKind, TextObjectScope},
23 reovim_kernel::api::v1::{CommandId, Position},
24};
25
26use crate::ids;
27
28#[cfg_attr(coverage_nightly, coverage(off))]
36#[allow(clippy::cast_possible_truncation)]
37fn execute_semantic_textobj(
38 runtime: &mut SessionRuntime<'_>,
39 args: &CommandContext,
40 kind: TextObjectKind,
41 scope: TextObjectScope,
42 linewise: bool,
43) -> CommandResult {
44 let Some(buffer_id) = args.buffer_id() else {
45 return CommandResult::Success;
46 };
47 let Some(window) = runtime.windows().active() else {
48 return CommandResult::Success;
49 };
50 let line = window.cursor.line as u32;
51 let col = window.cursor.column as u32;
52
53 let range = runtime
55 .ext::<SyntaxSessionState>()
56 .and_then(|s| s.get(buffer_id))
57 .and_then(|driver| driver.textobject_range(kind, scope, line, col));
58
59 let Some(range) = range else {
60 return CommandResult::Success;
61 };
62
63 let start = Position::new(range.start_row as usize, range.start_col as usize);
64 let end_exclusive = Position::new(range.end_row as usize, range.end_col as usize);
65
66 if is_visual_mode(runtime) {
67 let sel_mode = visual_selection_mode(runtime);
68 if let Some(window) = runtime.windows_mut().active_mut() {
69 window.selection = Some(Selection::new(start, end_exclusive, sel_mode));
70 window.cursor.line = range.end_row as usize;
72 window.cursor.column = range.end_col.saturating_sub(1) as usize;
73 }
74 } else {
75 let state = runtime.ext_mut::<OperatorPendingState>();
76 if linewise {
77 state.set_textobj_range(TextObjRange::linewise(start, end_exclusive));
78 } else {
79 state.set_textobj_range(TextObjRange::characterwise(start, end_exclusive));
80 }
81 }
82
83 CommandResult::Success
84}
85
86fn is_visual_mode(runtime: &SessionRuntime<'_>) -> bool {
87 let mode = runtime.current_mode();
88 let name = mode.name();
89 name == "visual" || name == "visual-line" || name == "visual-block"
90}
91
92fn visual_selection_mode(runtime: &SessionRuntime<'_>) -> SelectionMode {
93 let mode = runtime.current_mode();
94 match mode.name() {
95 "visual-line" => SelectionMode::Line,
96 "visual-block" => SelectionMode::Block,
97 _ => SelectionMode::Character,
98 }
99}
100
101macro_rules! semantic_command {
106 ($name:ident, $id:expr, $desc:expr, $kind:expr, $scope:expr, $linewise:expr) => {
107 #[derive(Debug, Clone, Copy, Default)]
108 pub struct $name;
109
110 impl Command for $name {
111 fn id(&self) -> CommandId {
112 $id
113 }
114
115 fn description(&self) -> &'static str {
116 $desc
117 }
118 }
119
120 impl CommandHandler for $name {
121 fn execute(
122 &self,
123 runtime: &mut SessionRuntime<'_>,
124 args: &CommandContext,
125 ) -> CommandResult {
126 execute_semantic_textobj(runtime, args, $kind, $scope, $linewise)
127 }
128 }
129 };
130}
131
132semantic_command!(
134 InnerFunction,
135 ids::INNER_FUNCTION,
136 "Inner function text object (if)",
137 TextObjectKind::Function,
138 TextObjectScope::Inner,
139 true
140);
141semantic_command!(
142 AroundFunction,
143 ids::AROUND_FUNCTION,
144 "Around function text object (af)",
145 TextObjectKind::Function,
146 TextObjectScope::Outer,
147 true
148);
149
150semantic_command!(
152 InnerClass,
153 ids::INNER_CLASS,
154 "Inner class text object (ic)",
155 TextObjectKind::Class,
156 TextObjectScope::Inner,
157 true
158);
159semantic_command!(
160 AroundClass,
161 ids::AROUND_CLASS,
162 "Around class text object (ac)",
163 TextObjectKind::Class,
164 TextObjectScope::Outer,
165 true
166);
167
168semantic_command!(
170 InnerArgument,
171 ids::INNER_ARGUMENT,
172 "Inner argument text object (ia)",
173 TextObjectKind::Argument,
174 TextObjectScope::Inner,
175 false
176);
177semantic_command!(
178 AroundArgument,
179 ids::AROUND_ARGUMENT,
180 "Around argument text object (aa)",
181 TextObjectKind::Argument,
182 TextObjectScope::Outer,
183 false
184);
185
186semantic_command!(
188 InnerConditional,
189 ids::INNER_CONDITIONAL,
190 "Inner conditional text object (io)",
191 TextObjectKind::Conditional,
192 TextObjectScope::Inner,
193 true
194);
195semantic_command!(
196 AroundConditional,
197 ids::AROUND_CONDITIONAL,
198 "Around conditional text object (ao)",
199 TextObjectKind::Conditional,
200 TextObjectScope::Outer,
201 true
202);
203
204semantic_command!(
206 InnerLoop,
207 ids::INNER_LOOP,
208 "Inner loop text object (il)",
209 TextObjectKind::Loop,
210 TextObjectScope::Inner,
211 true
212);
213semantic_command!(
214 AroundLoop,
215 ids::AROUND_LOOP,
216 "Around loop text object (al)",
217 TextObjectKind::Loop,
218 TextObjectScope::Outer,
219 true
220);
221
222semantic_command!(
224 InnerComment,
225 ids::INNER_COMMENT,
226 "Inner comment text object (i/)",
227 TextObjectKind::Comment,
228 TextObjectScope::Inner,
229 false
230);
231semantic_command!(
232 AroundComment,
233 ids::AROUND_COMMENT,
234 "Around comment text object (a/)",
235 TextObjectKind::Comment,
236 TextObjectScope::Outer,
237 false
238);
239
240semantic_command!(
242 InnerBlockTs,
243 ids::INNER_BLOCK_TS,
244 "Inner block text object (treesitter)",
245 TextObjectKind::Block,
246 TextObjectScope::Inner,
247 true
248);
249semantic_command!(
250 AroundBlockTs,
251 ids::AROUND_BLOCK_TS,
252 "Around block text object (treesitter)",
253 TextObjectKind::Block,
254 TextObjectScope::Outer,
255 true
256);
257
258#[must_use]
260pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
261 vec![
262 Box::new(InnerFunction),
263 Box::new(AroundFunction),
264 Box::new(InnerClass),
265 Box::new(AroundClass),
266 Box::new(InnerArgument),
267 Box::new(AroundArgument),
268 Box::new(InnerConditional),
269 Box::new(AroundConditional),
270 Box::new(InnerLoop),
271 Box::new(AroundLoop),
272 Box::new(InnerComment),
273 Box::new(AroundComment),
274 Box::new(InnerBlockTs),
275 Box::new(AroundBlockTs),
276 ]
277}