From 44755143dbd2f565f56ae7beaf615dbf1bc3e5e7 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 4 Jan 2025 11:05:27 +0800 Subject: [PATCH] fix: bracketed paste (Ctrl-v from the terminal) also needs to exit replace mode --- yazi-core/src/input/commands/backspace.rs | 2 +- yazi-core/src/input/commands/complete.rs | 2 +- yazi-core/src/input/commands/replace.rs | 8 ++++++-- yazi-core/src/input/commands/show.rs | 2 +- yazi-core/src/input/commands/type_.rs | 23 ++++++----------------- yazi-core/src/input/input.rs | 3 +-- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/yazi-core/src/input/commands/backspace.rs b/yazi-core/src/input/commands/backspace.rs index cc7d9162..c25b557a 100644 --- a/yazi-core/src/input/commands/backspace.rs +++ b/yazi-core/src/input/commands/backspace.rs @@ -17,7 +17,7 @@ impl From for Opt { impl Input { #[yazi_codegen::command] pub fn backspace(&mut self, opt: Opt) { - let snap = self.snaps.current_mut(); + let snap = self.snap_mut(); if !opt.under && snap.cursor < 1 { return; } else if opt.under && snap.cursor >= snap.count() { diff --git a/yazi-core/src/input/commands/complete.rs b/yazi-core/src/input/commands/complete.rs index 872867af..065557ab 100644 --- a/yazi-core/src/input/commands/complete.rs +++ b/yazi-core/src/input/commands/complete.rs @@ -39,7 +39,7 @@ impl Input { format!("{}{after}", opt.word).replace(SEPARATOR, MAIN_SEPARATOR_STR) }; - let snap = self.snaps.current_mut(); + let snap = self.snap_mut(); if new == snap.value { return; } diff --git a/yazi-core/src/input/commands/replace.rs b/yazi-core/src/input/commands/replace.rs index 7daf22d9..20b77c08 100644 --- a/yazi-core/src/input/commands/replace.rs +++ b/yazi-core/src/input/commands/replace.rs @@ -14,11 +14,15 @@ impl Input { } } + // FIXME: need to create a new snap for each replace operation, otherwise we + // can't undo/redo it with `u` and `Ctrl-r` pub fn replace_str(&mut self, s: &str) { - let snap = self.snaps.current_mut(); + let snap = self.snap_mut(); let start = snap.idx(snap.cursor).unwrap(); - let end = snap.idx(snap.cursor + 1).unwrap(); + let end = snap.idx(snap.cursor + 1).unwrap(); // FIXME: panic if the input is empty while in replace mode + + snap.mode = InputMode::Normal; snap.value.replace_range(start..end, s); self.flush_value(); diff --git a/yazi-core/src/input/commands/show.rs b/yazi-core/src/input/commands/show.rs index b3128fe7..ac4e3879 100644 --- a/yazi-core/src/input/commands/show.rs +++ b/yazi-core/src/input/commands/show.rs @@ -40,7 +40,7 @@ impl Input { // Set cursor after reset if let Some(cursor) = opt.cfg.cursor { - self.snaps.current_mut().cursor = cursor; + self.snap_mut().cursor = cursor; self.move_(0); } diff --git a/yazi-core/src/input/commands/type_.rs b/yazi-core/src/input/commands/type_.rs index 6ff23142..09d59622 100644 --- a/yazi-core/src/input/commands/type_.rs +++ b/yazi-core/src/input/commands/type_.rs @@ -12,32 +12,21 @@ impl From for Opt { impl Input { pub fn type_(&mut self, key: &Key) -> bool { - if self.mode() == InputMode::Replace { - let Some(c) = key.plain() else { - return false; - }; + let Some(c) = key.plain() else { return false }; - let snap = self.snaps.current_mut(); - snap.mode = InputMode::Normal; - self.replace_str(c.encode_utf8(&mut [0; 4])); - - return true; - } - - if self.mode() != InputMode::Insert { - return false; - } - - if let Some(c) = key.plain() { + if self.mode() == InputMode::Insert { self.type_str(c.encode_utf8(&mut [0; 4])); return true; + } else if self.mode() == InputMode::Replace { + self.replace_str(c.encode_utf8(&mut [0; 4])); + return true; } false } pub fn type_str(&mut self, s: &str) { - let snap = self.snaps.current_mut(); + let snap = self.snap_mut(); if snap.cursor < 1 { snap.value.insert_str(0, s); } else { diff --git a/yazi-core/src/input/input.rs b/yazi-core/src/input/input.rs index 532e8d1f..27dff313 100644 --- a/yazi-core/src/input/input.rs +++ b/yazi-core/src/input/input.rs @@ -3,7 +3,6 @@ use std::ops::Range; use tokio::sync::mpsc::UnboundedSender; use unicode_width::UnicodeWidthStr; use yazi_config::{INPUT, popup::Position}; -use yazi_macro::render; use yazi_plugin::CLIPBOARD; use yazi_shared::errors::InputError; @@ -35,7 +34,7 @@ impl Input { pub(super) fn handle_op(&mut self, cursor: usize, include: bool) -> bool { let old = self.snap().clone(); - let snap = self.snaps.current_mut(); + let snap = self.snap_mut(); match snap.op { InputOp::None | InputOp::Select(_) => {