Preserve relative paths for local files (#10658)

- fixes #10649

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

# User-Facing Changes
Tab completions for paths starting with `./` shall have the prefix
preserved.
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Himadri Bhattacharjee 2023-10-09 20:07:45 +05:30 committed by GitHub
parent 0c67d742f0
commit ce09186e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,6 +39,8 @@ enum OriginalCwd {
None, None,
Home(PathBuf), Home(PathBuf),
Some(PathBuf), Some(PathBuf),
// referencing a single local file
Local(PathBuf),
} }
impl OriginalCwd { impl OriginalCwd {
@ -53,6 +55,10 @@ impl OriginalCwd {
Ok(suffix) => format!("~{}{}", SEP, suffix.to_string_lossy()), Ok(suffix) => format!("~{}{}", SEP, suffix.to_string_lossy()),
_ => p.to_string_lossy().into_owned(), _ => 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() { if p.is_dir() {
@ -68,9 +74,7 @@ fn surround_remove(partial: &str) -> String {
let ret = partial.strip_prefix(c).unwrap_or(partial); let ret = partial.strip_prefix(c).unwrap_or(partial);
return match ret.split(c).collect::<Vec<_>>()[..] { return match ret.split(c).collect::<Vec<_>>()[..] {
[inside] => inside.to_string(), [inside] => inside.to_string(),
[inside, outside] if inside.ends_with(is_separator) => { [inside, outside] if inside.ends_with(is_separator) => format!("{inside}{outside}"),
format!("{inside}{outside}")
}
_ => ret.to_string(), _ => ret.to_string(),
}; };
} }
@ -108,6 +112,14 @@ pub fn complete_item(
original_cwd = OriginalCwd::Home(home_dir().unwrap_or(cwd_pathbuf.clone())); original_cwd = OriginalCwd::Home(home_dir().unwrap_or(cwd_pathbuf.clone()));
home_dir().unwrap_or(cwd_pathbuf) 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()); original_cwd = OriginalCwd::Some(cwd_pathbuf.clone());
cwd_pathbuf cwd_pathbuf
@ -126,9 +138,7 @@ pub fn complete_item(
cwd.pop(); cwd.pop();
} }
} }
Component::Normal(c) => { Component::Normal(c) => partial.push(c.to_string_lossy().into_owned()),
partial.push(c.to_string_lossy().into_owned());
}
} }
} }