This commit is contained in:
sxyazi 2023-11-12 06:11:23 +08:00
parent ad88b70a4b
commit 05df4e175e
No known key found for this signature in database
3 changed files with 14 additions and 65 deletions

View file

@ -1,5 +1,4 @@
use yazi_config::keymap::Exec; use yazi_config::keymap::Exec;
use yazi_shared::CharKind;
use crate::input::Input; use crate::input::Input;
@ -14,27 +13,10 @@ impl From<()> for Opt {
impl Input { impl Input {
pub fn backward(&mut self, _: impl Into<Opt>) -> bool { pub fn backward(&mut self, _: impl Into<Opt>) -> bool {
return self.move_word(false, false);
let snap = self.snap(); let snap = self.snap();
if snap.cursor == 0 {
return self.move_(0);
}
let idx = snap.idx(snap.cursor).unwrap_or(snap.len()); 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 { let step = Self::find_word_boundary(snap.value[..idx].chars().rev(), false);
return self.move_(-(snap.len() as isize)); self.move_(-(step as isize))
}
false
} }
} }

View file

@ -1,7 +1,6 @@
use yazi_config::keymap::Exec; use yazi_config::keymap::Exec;
use yazi_shared::CharKind;
use crate::input::{op::InputOp, Input}; use crate::input::Input;
pub struct Opt { pub struct Opt {
end_of_word: bool, end_of_word: bool,
@ -17,30 +16,11 @@ impl From<bool> for Opt {
impl Input { impl Input {
pub fn forward(&mut self, opt: impl Into<Opt>) -> bool { pub fn forward(&mut self, opt: impl Into<Opt>) -> bool {
let opt = opt.into() as Opt; let opt = opt.into() as Opt;
return self.move_word(false, opt.end_of_word);
let snap = self.snap(); let snap = self.snap();
if snap.value.is_empty() { let idx = snap.idx(snap.cursor).unwrap_or(snap.len());
return self.move_(0);
}
let mut it = snap.value.chars().skip(snap.cursor).enumerate(); let step = Self::find_word_boundary(snap.value[idx..].chars(), opt.end_of_word);
let mut prev = CharKind::new(it.next().unwrap().1); self.move_(step as isize)
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)
} }
} }

View file

@ -57,7 +57,7 @@ impl Input {
/// ///
/// Otherwise, returns how many characters to move to reach right *AFTER* the /// Otherwise, returns how many characters to move to reach right *AFTER* the
/// word boundary, or the end of the iterator. /// word boundary, or the end of the iterator.
fn find_word_boundary( pub(super) fn find_word_boundary(
input: impl Iterator<Item = char> + Clone, input: impl Iterator<Item = char> + Clone,
stop_before_boundary: bool, stop_before_boundary: bool,
) -> usize { ) -> usize {
@ -81,18 +81,6 @@ impl Input {
spaces_count + character_count 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<usize>) { fn delete_range(&mut self, range: impl RangeBounds<usize>) {
let snap = self.snap_mut(); let snap = self.snap_mut();
snap.cursor = match range.start_bound() { snap.cursor = match range.start_bound() {
@ -114,11 +102,10 @@ impl Input {
return self.type_str(c.encode_utf8(&mut bits)); return self.type_str(c.encode_utf8(&mut bits));
} }
use KeyCode as K; use KeyCode::{Backspace, Char as C, Delete};
use KeyCode::Char as C;
match key { 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. // Handle Emacs-style keybindings.
Key { code: C('a'), shift: false, ctrl: true, alt: false } => self.move_(isize::MIN), 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), 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('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('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('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('b'), shift: false, ctrl: false, alt: true } => self.backspace(),
Key { code: C('f'), shift: false, ctrl: false, alt: true } => self.move_word(true, false), Key { code: C('f'), shift: false, ctrl: false, alt: true } => self.forward(false),
Key { code: C('u'), shift: false, ctrl: true, alt: false } => { Key { code: C('u'), shift: false, ctrl: true, alt: false } => {
let snap = self.snap_mut(); let snap = self.snap_mut();
let end = snap.idx(snap.cursor).unwrap_or(snap.len()); let end = snap.idx(snap.cursor).unwrap_or(snap.len());
@ -135,14 +122,14 @@ impl Input {
true true
} }
Key { code: C('k'), shift: false, ctrl: true, alt: false } 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 snap = self.snap_mut();
let start = snap.idx(snap.cursor).unwrap_or(snap.len()); let start = snap.idx(snap.cursor).unwrap_or(snap.len());
self.delete_range(start..); self.delete_range(start..);
true true
} }
Key { code: C('w'), shift: false, ctrl: true, alt: false } 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 snap = self.snap_mut();
let end = snap.idx(snap.cursor).unwrap_or(snap.len()); let end = snap.idx(snap.cursor).unwrap_or(snap.len());
let start = end - Self::find_word_boundary(snap.value[..end].chars().rev(), false); let start = end - Self::find_word_boundary(snap.value[..end].chars().rev(), false);
@ -195,8 +182,8 @@ impl Input {
} else { } else {
snap.value.remove(snap.idx(snap.cursor - 1).unwrap()); snap.value.remove(snap.idx(snap.cursor - 1).unwrap());
} }
self.move_(-1);
self.move_(-1);
self.flush_value(); self.flush_value();
true true
} }