fix(completion): more quoting for file_completion/directory_completion

This commit is contained in:
blindfs 2025-03-12 11:27:27 +08:00
parent e926919582
commit 789f92b038
2 changed files with 24 additions and 6 deletions

View File

@ -275,7 +275,7 @@ pub fn complete_item(
});
FileSuggestion {
span,
path: escape_path(path, want_directory),
path: escape_path(path),
style,
is_dir,
}
@ -284,7 +284,7 @@ pub fn complete_item(
}
// Fix files or folders with quotes or hashes
pub fn escape_path(path: String, dir: bool) -> String {
pub fn escape_path(path: String) -> String {
// make glob pattern have the highest priority.
if nu_glob::is_glob(path.as_str()) {
let pathbuf = nu_path::expand_tilde(path);
@ -298,13 +298,11 @@ pub fn escape_path(path: String, dir: bool) -> String {
};
}
let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']);
let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']);
let contaminated = path.contains(['\'', '"', ' ', '#', '(', ')', '{', '}', '[', ']']);
let maybe_flag = path.starts_with('-');
let maybe_variable = path.starts_with('$');
let maybe_number = path.parse::<f64>().is_ok();
if filename_contaminated || dirname_contaminated || maybe_flag || maybe_variable || maybe_number
{
if contaminated || maybe_flag || maybe_variable || maybe_number {
format!("`{path}`")
} else {
path

View File

@ -891,6 +891,7 @@ fn partial_completions() {
folder(dir.join("partial-a")),
folder(dir.join("partial-b")),
folder(dir.join("partial-c")),
format!("`{}`", folder(dir.join("partial-d("))),
];
// Match the results
@ -980,6 +981,15 @@ fn partial_completions() {
.join("final_partial")
.join("somefile"),
),
format!(
"`{}`",
file(
dir.join("partial-d(")
.join("..")
.join("final_partial")
.join("somefile"),
)
),
];
// Match the results
@ -1060,6 +1070,16 @@ fn partial_completion_with_dot_expansions() {
.join("final_partial")
.join("somefile"),
),
format!(
"`{}`",
file(
dir.join("partial-d(")
.join("...")
.join("partial_completions")
.join("final_partial")
.join("somefile"),
)
),
];
// Match the results