Skip to content

Commit

Permalink
feat(complete): Give control over display order
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Sep 20, 2024
1 parent 59a61e1 commit 67d9fef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions clap_complete/src/engine/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct CompletionCandidate {
help: Option<StyledStr>,
id: Option<String>,
tag: Option<StyledStr>,
display_order: Option<usize>,
hidden: bool,
}

Expand Down Expand Up @@ -45,6 +46,12 @@ impl CompletionCandidate {
self
}

/// Sort weight within a [`CompletionCandidate::tag`]
pub fn display_order(mut self, order: Option<usize>) -> Self {
self.display_order = order;
self
}

/// Set the visibility of the completion candidate
///
/// Only shown when there is no visible candidate for completing the current argument.
Expand Down Expand Up @@ -88,6 +95,11 @@ impl CompletionCandidate {
self.tag.as_ref()
}

/// Get the grouping tag
pub fn get_display_order(&self) -> Option<usize> {
self.display_order
}

/// Get the visibility of the completion candidate
pub fn is_hide_set(&self) -> bool {
self.hidden
Expand Down
7 changes: 6 additions & 1 deletion clap_complete/src/engine/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,12 @@ fn complete_arg(
tags.push(tag);
}
}
completions.sort_by_key(|c| tags.iter().position(|t| c.get_tag() == t.as_ref()));
completions.sort_by_key(|c| {
(
tags.iter().position(|t| c.get_tag() == t.as_ref()),
c.get_display_order(),
)
});

Ok(completions)
}
Expand Down

0 comments on commit 67d9fef

Please sign in to comment.