From a058226576a9b83c9eccd18ead8464ed62ae7791 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 8 Jan 2025 18:24:12 +0800 Subject: [PATCH] Extract the logic of comparing two `CharKind` instances into a method of `CharKind` --- yazi-core/src/input/commands/backward.rs | 8 +++----- yazi-core/src/input/commands/forward.rs | 10 ++++------ yazi-shared/src/chars.rs | 4 ++++ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/yazi-core/src/input/commands/backward.rs b/yazi-core/src/input/commands/backward.rs index 2c7e72672..35f3d65b3 100644 --- a/yazi-core/src/input/commands/backward.rs +++ b/yazi-core/src/input/commands/backward.rs @@ -22,13 +22,11 @@ impl Input { let mut it = snap.value[..idx].chars().rev().enumerate(); let mut prev = CharKind::new(it.next().unwrap().1); for (i, c) in it { - let c = CharKind::new(c); - let new_char_kind = - if opt.far { (c == CharKind::Space) != (prev == CharKind::Space) } else { c != prev }; - if prev != CharKind::Space && new_char_kind { + let k = CharKind::new(c); + if prev != CharKind::Space && prev.vary(k, opt.far) { return self.move_(-(i as isize)); } - prev = c; + prev = k; } if prev != CharKind::Space { diff --git a/yazi-core/src/input/commands/forward.rs b/yazi-core/src/input/commands/forward.rs index 1fb18b509..97b08e43b 100644 --- a/yazi-core/src/input/commands/forward.rs +++ b/yazi-core/src/input/commands/forward.rs @@ -22,20 +22,18 @@ impl Input { }; for (i, c) in it { - let c = CharKind::new(c); - let new_char_kind = - if opt.far { (c == CharKind::Space) != (prev == CharKind::Space) } else { c != prev }; + let k = CharKind::new(c); let b = if opt.end_of_word { - prev != CharKind::Space && new_char_kind && i != 1 + prev != CharKind::Space && prev.vary(k, opt.far) && i != 1 } else { - c != CharKind::Space && new_char_kind + k != CharKind::Space && k.vary(prev, opt.far) }; if b && !matches!(snap.op, InputOp::None | InputOp::Select(_)) { return self.move_(i as isize); } else if b { return self.move_(if opt.end_of_word { i - 1 } else { i } as isize); } - prev = c; + prev = k; } self.move_(snap.len() as isize) diff --git a/yazi-shared/src/chars.rs b/yazi-shared/src/chars.rs index 491389c21..e234b03cc 100644 --- a/yazi-shared/src/chars.rs +++ b/yazi-shared/src/chars.rs @@ -20,6 +20,10 @@ impl CharKind { Self::Other } } + + pub fn vary(self, other: Self, far: bool) -> bool { + if far { (self == Self::Space) != (other == Self::Space) } else { self != other } + } } pub fn strip_trailing_newline(mut s: String) -> String {