From ce09186e2ebc271e2147e0a504170434e00f0199 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee Date: Mon, 9 Oct 2023 20:07:45 +0530 Subject: [PATCH] Preserve relative paths for local files (#10658) - fixes #10649 # Description # User-Facing Changes Tab completions for paths starting with `./` shall have the prefix preserved. # Tests + Formatting # After Submitting --- .../src/completions/completion_common.rs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index bcb46c47c7..393f7ec336 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -39,6 +39,8 @@ enum OriginalCwd { None, Home(PathBuf), Some(PathBuf), + // referencing a single local file + Local(PathBuf), } impl OriginalCwd { @@ -53,6 +55,10 @@ impl OriginalCwd { Ok(suffix) => format!("~{}{}", SEP, suffix.to_string_lossy()), _ => p.to_string_lossy().into_owned(), }, + Self::Local(base) => Path::new(".") + .join(pathdiff::diff_paths(p, base).unwrap_or(p.to_path_buf())) + .to_string_lossy() + .into_owned(), }; if p.is_dir() { @@ -68,9 +74,7 @@ fn surround_remove(partial: &str) -> String { let ret = partial.strip_prefix(c).unwrap_or(partial); return match ret.split(c).collect::>()[..] { [inside] => inside.to_string(), - [inside, outside] if inside.ends_with(is_separator) => { - format!("{inside}{outside}") - } + [inside, outside] if inside.ends_with(is_separator) => format!("{inside}{outside}"), _ => ret.to_string(), }; } @@ -108,6 +112,14 @@ pub fn complete_item( original_cwd = OriginalCwd::Home(home_dir().unwrap_or(cwd_pathbuf.clone())); home_dir().unwrap_or(cwd_pathbuf) } + Some(Component::CurDir) => { + components.next(); + original_cwd = match components.peek().cloned() { + Some(Component::Normal(_)) | None => OriginalCwd::Local(cwd_pathbuf.clone()), + _ => OriginalCwd::Some(cwd_pathbuf.clone()), + }; + cwd_pathbuf + } _ => { original_cwd = OriginalCwd::Some(cwd_pathbuf.clone()); cwd_pathbuf @@ -126,9 +138,7 @@ pub fn complete_item( cwd.pop(); } } - Component::Normal(c) => { - partial.push(c.to_string_lossy().into_owned()); - } + Component::Normal(c) => partial.push(c.to_string_lossy().into_owned()), } }