From 47aedc6906c0c35a89833331041a083630d777fb Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Aug 2024 14:11:48 -0500 Subject: [PATCH] fix(complete): Ensure paths are sorted --- clap_complete/src/engine/custom.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/clap_complete/src/engine/custom.rs b/clap_complete/src/engine/custom.rs index 1ce089fcb74..240972cfb54 100644 --- a/clap_complete/src/engine/custom.rs +++ b/clap_complete/src/engine/custom.rs @@ -77,6 +77,7 @@ pub(crate) fn complete_path( is_wanted: &dyn Fn(&std::path::Path) -> bool, ) -> Vec { let mut completions = Vec::new(); + let mut potential = Vec::new(); let value_path = std::path::Path::new(value_os); let (prefix, current) = split_file_name(value_path); @@ -88,7 +89,7 @@ pub(crate) fn complete_path( Some(current_dir) => current_dir, None => { // Can't complete without a `current_dir` - return Vec::new(); + return completions; } }; current_dir.join(prefix) @@ -109,15 +110,24 @@ pub(crate) fn complete_path( if entry.metadata().map(|m| m.is_dir()).unwrap_or(false) { let mut suggestion = prefix.join(raw_file_name); suggestion.push(""); // Ensure trailing `/` - completions.push(CompletionCandidate::new(suggestion.as_os_str().to_owned())); + let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned()); + + if is_wanted(&entry.path()) { + completions.push(candidate); + } else { + potential.push(candidate); + } } else { - let path = entry.path(); - if is_wanted(&path) { + if is_wanted(&entry.path()) { let suggestion = prefix.join(raw_file_name); - completions.push(CompletionCandidate::new(suggestion.as_os_str().to_owned())); + let candidate = CompletionCandidate::new(suggestion.as_os_str().to_owned()); + completions.push(candidate); } } } + completions.sort(); + potential.sort(); + completions.extend(potential); completions }