Better filepath completions (#485)

This commit is contained in:
JT 2021-12-13 17:46:30 +11:00 committed by GitHub
parent 1336acd34a
commit 906c0e6bca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -207,6 +207,18 @@ fn file_path_completion(
) -> Vec<(nu_protocol::Span, String)> {
use std::path::{is_separator, Path};
let partial = if let Some(s) = partial.strip_prefix('"') {
s
} else {
partial
};
let partial = if let Some(s) = partial.strip_suffix('"') {
s
} else {
partial
};
let (base_dir_name, partial) = {
// If partial is only a word we want to search in the current dir
let (base, rest) = partial.rsplit_once(is_separator).unwrap_or((".", partial));
@ -237,6 +249,10 @@ fn file_path_completion(
file_name.push(SEP);
}
if path.contains(' ') {
path = format!("\"{}\"", path);
}
Some((span, path))
} else {
None

View File

@ -57,10 +57,14 @@ impl Command for External {
// Check if this is a single call to a directory, if so auto-cd
let path = nu_path::expand_path(&name.item);
let orig = name.item.clone();
name.item = path.to_string_lossy().to_string();
let path = Path::new(&name.item);
if (name.item.starts_with('.') || name.item.starts_with('/') || name.item.starts_with('\\'))
if (orig.starts_with('.')
|| orig.starts_with('~')
|| orig.starts_with('/')
|| orig.starts_with('\\'))
&& path.is_dir()
&& args.is_empty()
{