Skip to content

Commit

Permalink
Merge pull request #5645 from epage/complete
Browse files Browse the repository at this point in the history
docs(complete): Clarify CompletionCandidate
  • Loading branch information
epage authored Aug 9, 2024
2 parents 6059713 + ad48d3b commit 7916e79
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions clap_complete/src/dynamic/candidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ use std::ffi::OsString;

use clap::builder::StyledStr;

/// A completion candidate definition
/// A shell-agnostic completion candidate
///
/// This makes it easier to add more fields to completion candidate,
/// rather than using `(OsString, Option<StyledStr>)` or `(String, Option<StyledStr>)` to represent a completion candidate
/// [`ShellCompleter`][crate::dynamic::shells::ShellCompleter] will adapt what it can to the
/// current shell.
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CompletionCandidate {
/// Main completion candidate content
content: OsString,

/// Help message with a completion candidate
help: Option<StyledStr>,

/// Whether the completion candidate is hidden
hidden: bool,
}

Expand All @@ -36,11 +31,28 @@ impl CompletionCandidate {
}

/// Set the visibility of the completion candidate
///
/// Only shown when no there is no visible candidate for completing the current argument.
pub fn hide(mut self, hidden: bool) -> Self {
self.hidden = hidden;
self
}

/// Add a prefix to the content of completion candidate
///
/// This is generally used for post-process by [`complete`][crate::dynamic::complete()] for
/// things like pre-pending flags, merging delimiter-separated values, etc.
pub fn add_prefix(mut self, prefix: impl Into<OsString>) -> Self {
let suffix = self.content;
let mut content = prefix.into();
content.push(&suffix);
self.content = content;
self
}
}

/// Reflection API
impl CompletionCandidate {
/// Get the content of the completion candidate
pub fn get_content(&self) -> &OsStr {
&self.content
Expand All @@ -55,13 +67,4 @@ impl CompletionCandidate {
pub fn is_hide_set(&self) -> bool {
self.hidden
}

/// Add a prefix to the content of completion candidate
pub fn add_prefix(mut self, prefix: impl Into<OsString>) -> Self {
let suffix = self.content;
let mut content = prefix.into();
content.push(&suffix);
self.content = content;
self
}
}

0 comments on commit 7916e79

Please sign in to comment.