From 05df4e175ec370dc5e28d566a66a1cdfea5fc710 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 12 Nov 2023 06:11:23 +0800 Subject: [PATCH] clean up --- yazi-core/src/input/commands/backward.rs | 22 ++---------------- yazi-core/src/input/commands/forward.rs | 28 ++++------------------- yazi-core/src/input/input.rs | 29 +++++++----------------- 3 files changed, 14 insertions(+), 65 deletions(-) diff --git a/yazi-core/src/input/commands/backward.rs b/yazi-core/src/input/commands/backward.rs index 604597fc..d0aebfcb 100644 --- a/yazi-core/src/input/commands/backward.rs +++ b/yazi-core/src/input/commands/backward.rs @@ -1,5 +1,4 @@ use yazi_config::keymap::Exec; -use yazi_shared::CharKind; use crate::input::Input; @@ -14,27 +13,10 @@ impl From<()> for Opt { impl Input { pub fn backward(&mut self, _: impl Into) -> bool { - return self.move_word(false, false); - let snap = self.snap(); - if snap.cursor == 0 { - return self.move_(0); - } - let idx = snap.idx(snap.cursor).unwrap_or(snap.len()); - 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); - if prev != CharKind::Space && prev != c { - return self.move_(-(i as isize)); - } - prev = c; - } - if prev != CharKind::Space { - return self.move_(-(snap.len() as isize)); - } - false + let step = Self::find_word_boundary(snap.value[..idx].chars().rev(), false); + self.move_(-(step as isize)) } } diff --git a/yazi-core/src/input/commands/forward.rs b/yazi-core/src/input/commands/forward.rs index b9cbcece..b852b10e 100644 --- a/yazi-core/src/input/commands/forward.rs +++ b/yazi-core/src/input/commands/forward.rs @@ -1,7 +1,6 @@ use yazi_config::keymap::Exec; -use yazi_shared::CharKind; -use crate::input::{op::InputOp, Input}; +use crate::input::Input; pub struct Opt { end_of_word: bool, @@ -17,30 +16,11 @@ impl From for Opt { impl Input { pub fn forward(&mut self, opt: impl Into) -> bool { let opt = opt.into() as Opt; - return self.move_word(false, opt.end_of_word); let snap = self.snap(); - if snap.value.is_empty() { - return self.move_(0); - } + let idx = snap.idx(snap.cursor).unwrap_or(snap.len()); - let mut it = snap.value.chars().skip(snap.cursor).enumerate(); - let mut prev = CharKind::new(it.next().unwrap().1); - for (i, c) in it { - let c = CharKind::new(c); - let b = if opt.end_of_word { - prev != CharKind::Space && prev != c && i != 1 - } else { - c != CharKind::Space && c != prev - }; - 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; - } - - self.move_(snap.len() as isize) + let step = Self::find_word_boundary(snap.value[idx..].chars(), opt.end_of_word); + self.move_(step as isize) } } diff --git a/yazi-core/src/input/input.rs b/yazi-core/src/input/input.rs index b796bbc4..fca2dd98 100644 --- a/yazi-core/src/input/input.rs +++ b/yazi-core/src/input/input.rs @@ -57,7 +57,7 @@ impl Input { /// /// Otherwise, returns how many characters to move to reach right *AFTER* the /// word boundary, or the end of the iterator. - fn find_word_boundary( + pub(super) fn find_word_boundary( input: impl Iterator + Clone, stop_before_boundary: bool, ) -> usize { @@ -81,18 +81,6 @@ impl Input { spaces_count + character_count } - pub(super) fn move_word(&mut self, forwards: bool, end: bool) -> bool { - let snap = self.snap(); - let idx = snap.idx(snap.cursor).unwrap_or(snap.len()); - - let movement = if forwards { - Self::find_word_boundary(snap.value[idx..].chars(), end) as isize - } else { - -(Self::find_word_boundary(snap.value[..idx].chars().rev(), false) as isize) - }; - self.move_(movement) - } - fn delete_range(&mut self, range: impl RangeBounds) { let snap = self.snap_mut(); snap.cursor = match range.start_bound() { @@ -114,11 +102,10 @@ impl Input { return self.type_str(c.encode_utf8(&mut bits)); } - use KeyCode as K; - use KeyCode::Char as C; + use KeyCode::{Backspace, Char as C, Delete}; match key { - Key { code: K::Backspace, shift: false, ctrl: false, alt: false } => self.backspace(), + Key { code: Backspace, shift: false, ctrl: false, alt: false } => self.backspace(), // Handle Emacs-style keybindings. Key { code: C('a'), shift: false, ctrl: true, alt: false } => self.move_(isize::MIN), Key { code: C('e'), shift: false, ctrl: true, alt: false } => self.move_(isize::MAX), @@ -126,8 +113,8 @@ impl Input { Key { code: C('f'), shift: false, ctrl: true, alt: false } => self.move_(1), Key { code: C('h'), shift: false, ctrl: true, alt: false } => self.backspace(), Key { code: C('d'), shift: false, ctrl: true, alt: false } => self.forward_delete(), - Key { code: C('b'), shift: false, ctrl: false, alt: true } => self.move_word(false, false), - Key { code: C('f'), shift: false, ctrl: false, alt: true } => self.move_word(true, false), + Key { code: C('b'), shift: false, ctrl: false, alt: true } => self.backspace(), + Key { code: C('f'), shift: false, ctrl: false, alt: true } => self.forward(false), Key { code: C('u'), shift: false, ctrl: true, alt: false } => { let snap = self.snap_mut(); let end = snap.idx(snap.cursor).unwrap_or(snap.len()); @@ -135,14 +122,14 @@ impl Input { true } Key { code: C('k'), shift: false, ctrl: true, alt: false } - | Key { code: K::Delete, shift: false, ctrl: false, alt: false } => { + | Key { code: Delete, shift: false, ctrl: false, alt: false } => { let snap = self.snap_mut(); let start = snap.idx(snap.cursor).unwrap_or(snap.len()); self.delete_range(start..); true } Key { code: C('w'), shift: false, ctrl: true, alt: false } - | Key { code: K::Backspace, shift: false, ctrl: false, alt: true } => { + | Key { code: Backspace, shift: false, ctrl: false, alt: true } => { let snap = self.snap_mut(); let end = snap.idx(snap.cursor).unwrap_or(snap.len()); let start = end - Self::find_word_boundary(snap.value[..end].chars().rev(), false); @@ -195,8 +182,8 @@ impl Input { } else { snap.value.remove(snap.idx(snap.cursor - 1).unwrap()); } - self.move_(-1); + self.move_(-1); self.flush_value(); true }