mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 08:23:24 +01:00
Fix file completions which contains glob pattern (#11766)
# Description Fixes: https://github.com/nushell/nushell/issues/11762 The auto-completion is somehow annoying if a path contains a glob pattern, let's say if user type `ls` and it auto-completes to <code>ls `[a] bc.txt`</code>, and user can't list the file because it's backtick quoted. This pr is going to fix it. # User-Facing Changes ### Before ``` ❯ | ls `[a] bc.txt` `a bc` ``` ### After ``` ❯ | ls "[a] bc.txt" `a bc` ``` # Tests + Formatting Done # After Submitting NaN
This commit is contained in:
parent
4b91ed57dd
commit
20aa59085b
@ -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('-');
|
||||
|
@ -584,6 +584,7 @@ fn file_completion_quoted() {
|
||||
let suggestions = completer.complete(target_dir, target_dir.len());
|
||||
|
||||
let expected_paths: Vec<String> = vec![
|
||||
"\'[a] bc.txt\'".to_string(),
|
||||
"`--help`".to_string(),
|
||||
"`-42`".to_string(),
|
||||
"`-inf`".to_string(),
|
||||
|
0
tests/fixtures/quoted_completions/[a] bc.txt
vendored
Normal file
0
tests/fixtures/quoted_completions/[a] bc.txt
vendored
Normal file
Loading…
Reference in New Issue
Block a user