From bcf528d99e8d1c518b90236a2d8aa57e651effc8 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 8 Jan 2025 18:06:18 +0800 Subject: [PATCH] Refactor the `replace` command and rebase --- yazi-config/preset/keymap-default.toml | 6 +++--- yazi-core/src/input/commands/backward.rs | 12 ++++-------- yazi-core/src/input/commands/forward.rs | 16 ++++------------ yazi-core/src/input/commands/replace.rs | 19 ++++++++----------- 4 files changed, 19 insertions(+), 34 deletions(-) diff --git a/yazi-config/preset/keymap-default.toml b/yazi-config/preset/keymap-default.toml index e7b54bb2..be9916ea 100644 --- a/yazi-config/preset/keymap-default.toml +++ b/yazi-config/preset/keymap-default.toml @@ -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 = "", run = "backward", desc = "Move back to the start of the current or previous word" }, { on = "", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" }, diff --git a/yazi-core/src/input/commands/backward.rs b/yazi-core/src/input/commands/backward.rs index aff30ae4..2c7e7267 100644 --- a/yazi-core/src/input/commands/backward.rs +++ b/yazi-core/src/input/commands/backward.rs @@ -3,12 +3,11 @@ use yazi_shared::{CharKind, event::CmdCow}; use crate::input::Input; struct Opt { - big: bool, + far: bool, } impl From 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)); } diff --git a/yazi-core/src/input/commands/forward.rs b/yazi-core/src/input/commands/forward.rs index 1cb15e2c..1fb18b50 100644 --- a/yazi-core/src/input/commands/forward.rs +++ b/yazi-core/src/input/commands/forward.rs @@ -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 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 { diff --git a/yazi-core/src/input/commands/replace.rs b/yazi-core/src/input/commands/replace.rs index d5e2c6c3..f028ebb2 100644 --- a/yazi-core/src/input/commands/replace.rs +++ b/yazi-core/src/input/commands/replace.rs @@ -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()); } }