Refactor the replace command and rebase

This commit is contained in:
sxyazi 2025-01-08 18:06:18 +08:00
parent 053426a489
commit bcf528d99e
No known key found for this signature in database
4 changed files with 19 additions and 34 deletions

View file

@ -250,11 +250,11 @@ keymap = [
# Word-wise movement
{ on = "b", run = "backward", desc = "Move back to the start of the current or previous word" },
{ on = "B", run = "backward --big", desc = "Move back to the start of the current or previous WORD" },
{ on = "B", run = "backward --far", desc = "Move back to the start of the current or previous WORD" },
{ on = "w", run = "forward", desc = "Move forward to the start of the next word" },
{ on = "W", run = "forward --big", desc = "Move forward to the start of the next WORD" },
{ on = "W", run = "forward --far", desc = "Move forward to the start of the next WORD" },
{ on = "e", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" },
{ on = "E", run = "forward --big --end-of-word", desc = "Move forward to the end of the current or next WORD" },
{ on = "E", run = "forward --far --end-of-word", desc = "Move forward to the end of the current or next WORD" },
{ on = "<A-b>", run = "backward", desc = "Move back to the start of the current or previous word" },
{ on = "<A-f>", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" },

View file

@ -3,12 +3,11 @@ use yazi_shared::{CharKind, event::CmdCow};
use crate::input::Input;
struct Opt {
big: bool,
far: bool,
}
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { big: c.bool("big") }
}
fn from(c: CmdCow) -> Self { Self { far: c.bool("far") } }
}
impl Input {
@ -24,11 +23,8 @@ impl Input {
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.big {
(c == CharKind::Space) != (prev == CharKind::Space)
} else {
c != prev
};
let new_char_kind =
if opt.far { (c == CharKind::Space) != (prev == CharKind::Space) } else { c != prev };
if prev != CharKind::Space && new_char_kind {
return self.move_(-(i as isize));
}

View file

@ -3,17 +3,12 @@ use yazi_shared::{CharKind, event::CmdCow};
use crate::input::{Input, op::InputOp};
struct Opt {
far: bool,
end_of_word: bool,
big: bool,
}
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self {
Self {
end_of_word: c.bool("end-of-word"),
big: c.bool("big"),
}
}
fn from(c: CmdCow) -> Self { Self { far: c.bool("far"), end_of_word: c.bool("end-of-word") } }
}
impl Input {
@ -28,11 +23,8 @@ impl Input {
for (i, c) in it {
let c = CharKind::new(c);
let new_char_kind = if opt.big {
(c == CharKind::Space) != (prev == CharKind::Space)
} else {
c != prev
};
let new_char_kind =
if opt.far { (c == CharKind::Space) != (prev == CharKind::Space) } else { c != prev };
let b = if opt.end_of_word {
prev != CharKind::Space && new_char_kind && i != 1
} else {

View file

@ -16,20 +16,17 @@ impl Input {
pub fn replace_str(&mut self, s: &str) {
let snap = self.snap_mut();
if snap.value.is_empty() {
snap.mode = InputMode::Normal;
render!();
return;
}
snap.mode = InputMode::Normal;
let start = snap.idx(snap.cursor).unwrap();
let end = snap.idx(snap.cursor + 1).unwrap();
let mut it = snap.value[start..].char_indices();
match (it.next(), it.next()) {
(None, _) => {}
(Some(_), None) => snap.value.replace_range(start..snap.len(), s),
(Some(_), Some((len, _))) => snap.value.replace_range(start..start + len, s),
}
snap.mode = InputMode::Normal;
snap.value.replace_range(start..end, s);
self.snaps.tag(self.limit()).then(|| self.flush_value());
render!();
self.snaps.tag(self.limit()).then(|| self.flush_value());
}
}