Skip to content

Commit 67d9fef

Browse files
committed
feat(complete): Give control over display order
1 parent 59a61e1 commit 67d9fef

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

clap_complete/src/engine/candidate.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct CompletionCandidate {
1010
help: Option<StyledStr>,
1111
id: Option<String>,
1212
tag: Option<StyledStr>,
13+
display_order: Option<usize>,
1314
hidden: bool,
1415
}
1516

@@ -45,6 +46,12 @@ impl CompletionCandidate {
4546
self
4647
}
4748

49+
/// Sort weight within a [`CompletionCandidate::tag`]
50+
pub fn display_order(mut self, order: Option<usize>) -> Self {
51+
self.display_order = order;
52+
self
53+
}
54+
4855
/// Set the visibility of the completion candidate
4956
///
5057
/// Only shown when there is no visible candidate for completing the current argument.
@@ -88,6 +95,11 @@ impl CompletionCandidate {
8895
self.tag.as_ref()
8996
}
9097

98+
/// Get the grouping tag
99+
pub fn get_display_order(&self) -> Option<usize> {
100+
self.display_order
101+
}
102+
91103
/// Get the visibility of the completion candidate
92104
pub fn is_hide_set(&self) -> bool {
93105
self.hidden

clap_complete/src/engine/complete.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,12 @@ fn complete_arg(
286286
tags.push(tag);
287287
}
288288
}
289-
completions.sort_by_key(|c| tags.iter().position(|t| c.get_tag() == t.as_ref()));
289+
completions.sort_by_key(|c| {
290+
(
291+
tags.iter().position(|t| c.get_tag() == t.as_ref()),
292+
c.get_display_order(),
293+
)
294+
});
290295

291296
Ok(completions)
292297
}

0 commit comments

Comments
 (0)