From 99e5484f9313ad3054d894d8de0f3c39a4f42f5a Mon Sep 17 00:00:00 2001 From: easonysliu Date: Mon, 16 Mar 2026 16:06:01 +0800 Subject: [PATCH] fix: strip leftover suffix when completing with cursor not at end When the input cursor is not at the end of the text and a completion is submitted (e.g. via `cmp:close --submit`), the `Input::complete()` method would preserve the text after the cursor (`after`), appending it to the completed path. For example, typing `/home/user/Doc` and moving the cursor back to after `D`, then completing `Documents/`, would produce `/home/user/Documents/oc` instead of `/home/user/Documents/`. Fix this by stripping the remainder of the current path component from the `after` slice before building the new value. Any text beyond the next path separator is preserved so that multi-component paths are not truncated. Resolves #2943 Co-Authored-By: Claude (claude-opus-4-6) --- yazi-widgets/src/input/commands/complete.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/yazi-widgets/src/input/commands/complete.rs b/yazi-widgets/src/input/commands/complete.rs index d2986542..6b2ebf45 100644 --- a/yazi-widgets/src/input/commands/complete.rs +++ b/yazi-widgets/src/input/commands/complete.rs @@ -15,6 +15,17 @@ const SEPARATOR: char = std::path::MAIN_SEPARATOR; impl Input { pub fn complete(&mut self, opt: CompleteOpt) -> Result { let (before, after) = self.partition(); + + // Strip the remainder of the current path component from `after`, so that + // completing when the cursor is in the middle of a word replaces the entire + // word instead of appending the completion before the leftover suffix. + // e.g. input "/home/user/D|oc" (cursor at |) completing "Documents/" should + // yield "/home/user/Documents/", not "/home/user/Documents/oc". + let after = match after.find(SEPARATOR) { + Some(i) => &after[i..], + None => "", + }; + let new = if let Some((prefix, _)) = before.rsplit_once(SEPARATOR) { format!("{prefix}/{}{after}", opt.completable()).replace(SEPARATOR, MAIN_SEPARATOR_STR) } else {