mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Refactor the replace command and rebase
This commit is contained in:
parent
053426a489
commit
bcf528d99e
4 changed files with 19 additions and 34 deletions
|
|
@ -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" },
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue