vim_navigation/
command.rs1use crate::{Motion, MotionKind};
34
35#[derive(Clone, Copy, Debug, Eq, PartialEq)]
37pub enum MotionResolution {
38 Ready(Motion),
40 AwaitingTarget,
42 Unavailable,
46}
47
48#[derive(Debug, Default)]
55pub struct MotionState {
56 count: Option<usize>,
57 awaiting_target: Option<Motion>,
58 last_character_search: Option<Motion>,
59}
60
61impl MotionState {
62 #[must_use]
64 pub const fn new() -> Self {
65 Self {
66 count: None,
67 awaiting_target: None,
68 last_character_search: None,
69 }
70 }
71
72 #[must_use]
77 pub const fn has_count(&self) -> bool {
78 self.count.is_some()
79 }
80
81 pub fn push_count_digit(&mut self, digit: char) -> bool {
87 let Some(value) = digit.to_digit(10) else {
88 return false;
89 };
90 if value == 0 && self.count.is_none() {
91 return false;
92 }
93 self.count = Some(
96 self.count
97 .unwrap_or(0)
98 .saturating_mul(10)
99 .saturating_add(value as usize),
100 );
101 true
102 }
103
104 pub fn take_count(&mut self) -> Option<usize> {
109 self.count.take()
110 }
111
112 #[must_use]
114 pub const fn is_awaiting_target(&self) -> bool {
115 self.awaiting_target.is_some()
116 }
117
118 pub fn finish(&mut self, motion: Motion) -> MotionResolution {
129 let count = self.count.take();
130 let mut motion = motion.counted(count.unwrap_or(1), count.is_some());
131 if motion.kind() == MotionKind::MatchingPair && motion.has_explicit_count() {
134 motion =
135 Motion::new(MotionKind::BufferPercentage).counted(motion.count().min(100), true);
136 }
137 if matches!(
139 motion.kind(),
140 MotionKind::BufferTop | MotionKind::BufferBottom
141 ) && motion.has_explicit_count()
142 {
143 motion = Motion::new(MotionKind::BufferTop).counted(motion.count(), true);
144 }
145 if matches!(
146 motion.kind(),
147 MotionKind::FindForward
148 | MotionKind::FindBackward
149 | MotionKind::TillForward
150 | MotionKind::TillBackward
151 ) {
152 if motion.target().is_none() {
153 self.awaiting_target = Some(motion);
154 return MotionResolution::AwaitingTarget;
155 }
156 self.last_character_search = Some(motion);
157 }
158 if matches!(
159 motion.kind(),
160 MotionKind::RepeatCharacterSearch | MotionKind::ReverseCharacterSearch
161 ) {
162 let Some(mut repeated) = self.last_character_search else {
163 return MotionResolution::Unavailable;
164 };
165 if motion.kind() == MotionKind::ReverseCharacterSearch {
166 let Some(target) = repeated.target() else {
167 return MotionResolution::Unavailable;
168 };
169 repeated = Motion::new(reverse_character_search(repeated.kind()))
170 .counted(motion.count(), motion.has_explicit_count())
171 .targeting(target);
172 } else {
173 repeated = repeated.counted(motion.count(), motion.has_explicit_count());
174 }
175 return MotionResolution::Ready(repeated.repeating());
176 }
177 MotionResolution::Ready(motion)
178 }
179
180 pub fn accept_target(&mut self, target: char) -> Option<Motion> {
186 let motion = self.awaiting_target.take()?.targeting(target);
187 self.last_character_search = Some(motion);
188 Some(motion)
189 }
190
191 pub fn reset_pending(&mut self) {
196 self.count = None;
197 self.awaiting_target = None;
198 }
199}
200
201fn reverse_character_search(kind: MotionKind) -> MotionKind {
202 match kind {
203 MotionKind::FindForward => MotionKind::FindBackward,
204 MotionKind::FindBackward => MotionKind::FindForward,
205 MotionKind::TillForward => MotionKind::TillBackward,
206 MotionKind::TillBackward => MotionKind::TillForward,
207 _ => kind,
208 }
209}
210
211#[cfg(test)]
212mod tests {
213 use super::{MotionResolution, MotionState};
214 use crate::{Motion, MotionKind};
215
216 #[test]
217 fn a_pre_targeted_search_becomes_the_repeat_source() {
218 let mut state = MotionState::new();
219 let search = Motion::new(MotionKind::FindForward).targeting('界');
220 assert_eq!(state.finish(search), MotionResolution::Ready(search));
221 assert_eq!(
222 state.finish(Motion::new(MotionKind::RepeatCharacterSearch)),
223 MotionResolution::Ready(search.repeating())
224 );
225 assert_eq!(
226 state.finish(Motion::new(MotionKind::ReverseCharacterSearch)),
227 MotionResolution::Ready(
228 Motion::new(MotionKind::FindBackward)
229 .targeting('界')
230 .repeating()
231 )
232 );
233 }
234
235 #[test]
236 fn counts_saturate_and_zero_remains_a_motion_without_a_prefix() {
237 let mut state = MotionState::new();
238 assert!(!state.push_count_digit('0'));
239 assert!(state.push_count_digit('1'));
240 assert!(state.push_count_digit('2'));
241 assert_eq!(
242 state.finish(Motion::new(MotionKind::WordForward)),
243 MotionResolution::Ready(Motion::new(MotionKind::WordForward).counted(12, true))
244 );
245 for _ in 0..usize::BITS {
246 assert!(state.push_count_digit('9'));
247 }
248 let MotionResolution::Ready(motion) = state.finish(Motion::new(MotionKind::Right)) else {
249 panic!("counted motion should be ready");
250 };
251 assert_eq!(motion.count(), usize::MAX);
252 }
253
254 #[test]
255 fn character_targets_repeat_and_survive_pending_resets() {
256 let mut state = MotionState::new();
257 assert_eq!(
258 state.finish(Motion::new(MotionKind::TillForward)),
259 MotionResolution::AwaitingTarget
260 );
261 assert_eq!(
262 state.accept_target('x'),
263 Some(Motion::new(MotionKind::TillForward).targeting('x'))
264 );
265 assert!(state.push_count_digit('2'));
266 let MotionResolution::Ready(repeated) =
267 state.finish(Motion::new(MotionKind::RepeatCharacterSearch))
268 else {
269 panic!("repeat should use the last character search");
270 };
271 assert_eq!(repeated.kind(), MotionKind::TillForward);
272 assert_eq!(repeated.count(), 2);
273 assert!(repeated.is_repeated());
274
275 state.reset_pending();
276 let MotionResolution::Ready(reversed) =
277 state.finish(Motion::new(MotionKind::ReverseCharacterSearch))
278 else {
279 panic!("reverse repeat should survive pending resets");
280 };
281 assert_eq!(reversed.kind(), MotionKind::TillBackward);
282 assert_eq!(reversed.target(), Some('x'));
283 }
284
285 #[test]
286 fn counted_percent_and_g_use_vim_interpretations() {
287 let mut state = MotionState::new();
288 assert!(state.push_count_digit('5'));
289 assert_eq!(
290 state.finish(Motion::new(MotionKind::MatchingPair)),
291 MotionResolution::Ready(Motion::new(MotionKind::BufferPercentage).counted(5, true))
292 );
293 assert!(state.push_count_digit('4'));
294 assert_eq!(
295 state.finish(Motion::new(MotionKind::BufferBottom)),
296 MotionResolution::Ready(Motion::new(MotionKind::BufferTop).counted(4, true))
297 );
298 }
299}