diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index dfbb55102bc..f4af6849f53 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -154,6 +154,18 @@ pub fn complete_item( // Fix files or folders with quotes or hashes pub fn escape_path(path: String, dir: bool) -> String { + // make glob pattern have the highest priority. + let glob_contaminated = path.contains(['[', '*', ']', '?']); + if glob_contaminated { + return if path.contains('\'') { + // decide to use double quote, also need to escape `"` in path + // or else users can't do anything with completed path either. + format!("\"{}\"", path.replace('"', r#"\""#)) + } else { + format!("'{path}'") + }; + } + let filename_contaminated = !dir && path.contains(['\'', '"', ' ', '#', '(', ')']); let dirname_contaminated = dir && path.contains(['\'', '"', ' ', '#']); let maybe_flag = path.starts_with('-'); diff --git a/crates/nu-cli/tests/completions.rs b/crates/nu-cli/tests/completions.rs index 6a0af5b1160..57b0dc509f9 100644 --- a/crates/nu-cli/tests/completions.rs +++ b/crates/nu-cli/tests/completions.rs @@ -584,6 +584,7 @@ fn file_completion_quoted() { let suggestions = completer.complete(target_dir, target_dir.len()); let expected_paths: Vec = vec![ + "\'[a] bc.txt\'".to_string(), "`--help`".to_string(), "`-42`".to_string(), "`-inf`".to_string(), diff --git a/tests/fixtures/quoted_completions/[a] bc.txt b/tests/fixtures/quoted_completions/[a] bc.txt new file mode 100644 index 00000000000..e69de29bb2d