use crate::motion::FULL_PERCENT;
use crate::{CountSource, Motion, MotionKind};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MotionResolution {
Ready(Motion),
AwaitingTarget,
Unavailable,
}
#[derive(Debug, Default)]
pub struct MotionState {
count: Option<usize>,
awaiting_target: Option<Motion>,
last_character_search: Option<Motion>,
}
impl MotionState {
#[must_use]
pub const fn new() -> Self {
Self {
count: None,
awaiting_target: None,
last_character_search: None,
}
}
#[must_use]
pub const fn has_count(&self) -> bool {
self.count.is_some()
}
pub fn push_count_digit(&mut self, digit: char) -> bool {
let Some(value) = digit.to_digit(10) else {
return false;
};
if value == 0 && self.count.is_none() {
return false;
}
self.count = Some(
self.count
.unwrap_or(0)
.saturating_mul(10)
.saturating_add(value as usize),
);
true
}
pub fn take_count(&mut self) -> Option<usize> {
self.count.take()
}
#[must_use]
pub const fn is_awaiting_target(&self) -> bool {
self.awaiting_target.is_some()
}
pub fn finish(&mut self, motion: Motion) -> MotionResolution {
let count = self.count.take();
let source = if count.is_some() {
CountSource::Explicit
} else {
CountSource::Implicit
};
let mut motion = motion.counted(count.unwrap_or(1), source);
if motion.kind() == MotionKind::MatchingPair && motion.has_explicit_count() {
motion = Motion::new(MotionKind::BufferPercentage)
.counted(motion.count().min(FULL_PERCENT), CountSource::Explicit);
}
if matches!(
motion.kind(),
MotionKind::BufferTop | MotionKind::BufferBottom
) && motion.has_explicit_count()
{
motion =
Motion::new(MotionKind::BufferTop).counted(motion.count(), CountSource::Explicit);
}
if matches!(
motion.kind(),
MotionKind::FindForward
| MotionKind::FindBackward
| MotionKind::TillForward
| MotionKind::TillBackward
) {
if motion.target().is_none() {
self.awaiting_target = Some(motion);
return MotionResolution::AwaitingTarget;
}
self.last_character_search = Some(motion);
}
if matches!(
motion.kind(),
MotionKind::RepeatCharacterSearch | MotionKind::ReverseCharacterSearch
) {
let Some(mut repeated) = self.last_character_search else {
return MotionResolution::Unavailable;
};
if motion.kind() == MotionKind::ReverseCharacterSearch {
let Some(target) = repeated.target() else {
return MotionResolution::Unavailable;
};
let source = if motion.has_explicit_count() {
CountSource::Explicit
} else {
CountSource::Implicit
};
repeated = Motion::new(reverse_character_search(repeated.kind()))
.counted(motion.count(), source)
.targeting(target);
} else {
let source = if motion.has_explicit_count() {
CountSource::Explicit
} else {
CountSource::Implicit
};
repeated = repeated.counted(motion.count(), source);
}
return MotionResolution::Ready(repeated.repeating());
}
MotionResolution::Ready(motion)
}
pub fn accept_target(&mut self, target: char) -> Option<Motion> {
let motion = self.awaiting_target.take()?.targeting(target);
self.last_character_search = Some(motion);
Some(motion)
}
pub fn reset_pending(&mut self) {
self.count = None;
self.awaiting_target = None;
}
}
fn reverse_character_search(kind: MotionKind) -> MotionKind {
match kind {
MotionKind::FindForward => MotionKind::FindBackward,
MotionKind::FindBackward => MotionKind::FindForward,
MotionKind::TillForward => MotionKind::TillBackward,
MotionKind::TillBackward => MotionKind::TillForward,
_ => kind,
}
}
#[cfg(test)]
mod tests {
use super::{MotionResolution, MotionState};
use crate::{CountSource, Motion, MotionKind};
#[test]
fn a_pre_targeted_search_becomes_the_repeat_source() {
let mut state = MotionState::new();
let search = Motion::new(MotionKind::FindForward).targeting('界');
assert_eq!(state.finish(search), MotionResolution::Ready(search));
assert_eq!(
state.finish(Motion::new(MotionKind::RepeatCharacterSearch)),
MotionResolution::Ready(search.repeating())
);
assert_eq!(
state.finish(Motion::new(MotionKind::ReverseCharacterSearch)),
MotionResolution::Ready(
Motion::new(MotionKind::FindBackward)
.targeting('界')
.repeating()
)
);
}
#[test]
fn counts_saturate_and_zero_remains_a_motion_without_a_prefix() {
let mut state = MotionState::new();
assert!(!state.push_count_digit('0'));
assert!(state.push_count_digit('1'));
assert!(state.push_count_digit('2'));
assert_eq!(
state.finish(Motion::new(MotionKind::WordForward)),
MotionResolution::Ready(
Motion::new(MotionKind::WordForward).counted(12, CountSource::Explicit)
)
);
for _ in 0..usize::BITS {
assert!(state.push_count_digit('9'));
}
let MotionResolution::Ready(motion) = state.finish(Motion::new(MotionKind::Right)) else {
panic!("counted motion should be ready");
};
assert_eq!(motion.count(), usize::MAX);
}
#[test]
fn character_targets_repeat_and_survive_pending_resets() {
let mut state = MotionState::new();
assert_eq!(
state.finish(Motion::new(MotionKind::TillForward)),
MotionResolution::AwaitingTarget
);
assert_eq!(
state.accept_target('x'),
Some(Motion::new(MotionKind::TillForward).targeting('x'))
);
assert!(state.push_count_digit('2'));
let MotionResolution::Ready(repeated) =
state.finish(Motion::new(MotionKind::RepeatCharacterSearch))
else {
panic!("repeat should use the last character search");
};
assert_eq!(repeated.kind(), MotionKind::TillForward);
assert_eq!(repeated.count(), 2);
assert!(repeated.is_repeated());
state.reset_pending();
let MotionResolution::Ready(reversed) =
state.finish(Motion::new(MotionKind::ReverseCharacterSearch))
else {
panic!("reverse repeat should survive pending resets");
};
assert_eq!(reversed.kind(), MotionKind::TillBackward);
assert_eq!(reversed.target(), Some('x'));
}
#[test]
fn counted_percent_and_g_use_vim_interpretations() {
let mut state = MotionState::new();
assert!(state.push_count_digit('5'));
assert_eq!(
state.finish(Motion::new(MotionKind::MatchingPair)),
MotionResolution::Ready(
Motion::new(MotionKind::BufferPercentage).counted(5, CountSource::Explicit)
)
);
assert!(state.push_count_digit('4'));
assert_eq!(
state.finish(Motion::new(MotionKind::BufferBottom)),
MotionResolution::Ready(
Motion::new(MotionKind::BufferTop).counted(4, CountSource::Explicit)
)
);
}
}