Skip to content

Commit 47aedc6

Browse files
committed
fix(complete): Ensure paths are sorted
1 parent 431e2bc commit 47aedc6

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

clap_complete/src/engine/custom.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub(crate) fn complete_path(
7777
is_wanted: &dyn Fn(&std::path::Path) -> bool,
7878
) -> Vec<CompletionCandidate> {
7979
let mut completions = Vec::new();
80+
let mut potential = Vec::new();
8081

8182
let value_path = std::path::Path::new(value_os);
8283
let (prefix, current) = split_file_name(value_path);
@@ -88,7 +89,7 @@ pub(crate) fn complete_path(
8889
Some(current_dir) => current_dir,
8990
None => {
9091
// Can't complete without a `current_dir`
91-
return Vec::new();
92+
return completions;
9293
}
9394
};
9495
current_dir.join(prefix)
@@ -109,15 +110,24 @@ pub(crate) fn complete_path(
109110
if entry.metadata().map(|m| m.is_dir()).unwrap_or(false) {
110111
let mut suggestion = prefix.join(raw_file_name);
111112
suggestion.push(""); // Ensure trailing `/`
112-
completions.push(CompletionCandidate::new(suggestion.as_os_str().to_owned()));
113+
let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned());
114+
115+
if is_wanted(&entry.path()) {
116+
completions.push(candidate);
117+
} else {
118+
potential.push(candidate);
119+
}
113120
} else {
114-
let path = entry.path();
115-
if is_wanted(&path) {
121+
if is_wanted(&entry.path()) {
116122
let suggestion = prefix.join(raw_file_name);
117-
completions.push(CompletionCandidate::new(suggestion.as_os_str().to_owned()));
123+
let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned());
124+
completions.push(candidate);
118125
}
119126
}
120127
}
128+
completions.sort();
129+
potential.sort();
130+
completions.extend(potential);
121131

122132
completions
123133
}

0 commit comments

Comments
 (0)