Skip to content

Commit

Permalink
Extract the logic of comparing two CharKind instances into a method…
Browse files Browse the repository at this point in the history
… of `CharKind`
  • Loading branch information
sxyazi committed Jan 8, 2025
1 parent bcf528d commit a058226
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 3 additions & 5 deletions yazi-core/src/input/commands/backward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions yazi-core/src/input/commands/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions yazi-shared/src/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a058226

Please sign in to comment.