diff --git a/clap_complete/src/engine/custom.rs b/clap_complete/src/engine/custom.rs index 51da696fefe..00dde78d67c 100644 --- a/clap_complete/src/engine/custom.rs +++ b/clap_complete/src/engine/custom.rs @@ -7,146 +7,146 @@ use clap_lex::OsStrExt as _; use super::CompletionCandidate; -/// Extend [`Arg`][clap::Arg] with a [`ValueCandidates`] +/// Extend [`Arg`][clap::Arg] with a completer /// /// # Example /// /// ```rust /// use clap::Parser; -/// use clap_complete::engine::{ArgValueCandidates, CompletionCandidate}; +/// use clap_complete::engine::{ArgValueCompleter, CompletionCandidate}; +/// +/// fn custom_completer(current: &std::ffi::OsStr) -> Vec { +/// let mut completions = vec![]; +/// let Some(current) = current.to_str() else { +/// return completions; +/// }; +/// +/// if "foo".starts_with(current) { +/// completions.push(CompletionCandidate::new("foo")); +/// } +/// if "bar".starts_with(current) { +/// completions.push(CompletionCandidate::new("bar")); +/// } +/// if "baz".starts_with(current) { +/// completions.push(CompletionCandidate::new("baz")); +/// } +/// completions +/// } /// /// #[derive(Debug, Parser)] /// struct Cli { -/// #[arg(long, add = ArgValueCandidates::new(|| { vec![ -/// CompletionCandidate::new("foo"), -/// CompletionCandidate::new("bar"), -/// CompletionCandidate::new("baz")] }))] +/// #[arg(long, add = ArgValueCompleter::new(custom_completer))] /// custom: Option, /// } /// ``` #[derive(Clone)] -pub struct ArgValueCandidates(Arc); +pub struct ArgValueCompleter(Arc); -impl ArgValueCandidates { - /// Create a new `ArgValueCandidates` with a custom completer +impl ArgValueCompleter { + /// Create a new `ArgValueCompleter` with a custom completer pub fn new(completer: C) -> Self where - C: ValueCandidates + 'static, + C: ValueCompleter + 'static, { Self(Arc::new(completer)) } - /// All potential candidates for an argument. + /// Candidates that match `current` /// /// See [`CompletionCandidate`] for more information. - pub fn candidates(&self) -> Vec { - self.0.candidates() + pub fn complete(&self, current: &OsStr) -> Vec { + self.0.complete(current) } } -impl std::fmt::Debug for ArgValueCandidates { +impl std::fmt::Debug for ArgValueCompleter { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(type_name::()) } } -impl ArgExt for ArgValueCandidates {} +impl ArgExt for ArgValueCompleter {} -/// User-provided completion candidates for an [`Arg`][clap::Arg], see [`ArgValueCandidates`] +/// User-provided completion candidates for an [`Arg`][clap::Arg], see [`ArgValueCompleter`] /// /// This is useful when predefined value hints are not enough. -pub trait ValueCandidates: Send + Sync { +pub trait ValueCompleter: Send + Sync { /// All potential candidates for an argument. /// /// See [`CompletionCandidate`] for more information. - fn candidates(&self) -> Vec; + fn complete(&self, current: &OsStr) -> Vec; } -impl ValueCandidates for F +impl ValueCompleter for F where - F: Fn() -> Vec + Send + Sync, + F: Fn(&OsStr) -> Vec + Send + Sync, { - fn candidates(&self) -> Vec { - self() + fn complete(&self, current: &OsStr) -> Vec { + self(current) } } -/// Extend [`Arg`][clap::Arg] with a completer +/// Extend [`Arg`][clap::Arg] with a [`ValueCandidates`] /// /// # Example /// /// ```rust /// use clap::Parser; -/// use clap_complete::engine::{ArgValueCompleter, CompletionCandidate}; -/// -/// fn custom_completer(current: &std::ffi::OsStr) -> Vec { -/// let mut completions = vec![]; -/// let Some(current) = current.to_str() else { -/// return completions; -/// }; -/// -/// if "foo".starts_with(current) { -/// completions.push(CompletionCandidate::new("foo")); -/// } -/// if "bar".starts_with(current) { -/// completions.push(CompletionCandidate::new("bar")); -/// } -/// if "baz".starts_with(current) { -/// completions.push(CompletionCandidate::new("baz")); -/// } -/// completions -/// } +/// use clap_complete::engine::{ArgValueCandidates, CompletionCandidate}; /// /// #[derive(Debug, Parser)] /// struct Cli { -/// #[arg(long, add = ArgValueCompleter::new(custom_completer))] +/// #[arg(long, add = ArgValueCandidates::new(|| { vec![ +/// CompletionCandidate::new("foo"), +/// CompletionCandidate::new("bar"), +/// CompletionCandidate::new("baz")] }))] /// custom: Option, /// } /// ``` #[derive(Clone)] -pub struct ArgValueCompleter(Arc); +pub struct ArgValueCandidates(Arc); -impl ArgValueCompleter { - /// Create a new `ArgValueCompleter` with a custom completer +impl ArgValueCandidates { + /// Create a new `ArgValueCandidates` with a custom completer pub fn new(completer: C) -> Self where - C: ValueCompleter + 'static, + C: ValueCandidates + 'static, { Self(Arc::new(completer)) } - /// Candidates that match `current` + /// All potential candidates for an argument. /// /// See [`CompletionCandidate`] for more information. - pub fn complete(&self, current: &OsStr) -> Vec { - self.0.complete(current) + pub fn candidates(&self) -> Vec { + self.0.candidates() } } -impl std::fmt::Debug for ArgValueCompleter { +impl std::fmt::Debug for ArgValueCandidates { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(type_name::()) } } -impl ArgExt for ArgValueCompleter {} +impl ArgExt for ArgValueCandidates {} -/// User-provided completion candidates for an [`Arg`][clap::Arg], see [`ArgValueCompleter`] +/// User-provided completion candidates for an [`Arg`][clap::Arg], see [`ArgValueCandidates`] /// /// This is useful when predefined value hints are not enough. -pub trait ValueCompleter: Send + Sync { +pub trait ValueCandidates: Send + Sync { /// All potential candidates for an argument. /// /// See [`CompletionCandidate`] for more information. - fn complete(&self, current: &OsStr) -> Vec; + fn candidates(&self) -> Vec; } -impl ValueCompleter for F +impl ValueCandidates for F where - F: Fn(&OsStr) -> Vec + Send + Sync, + F: Fn() -> Vec + Send + Sync, { - fn complete(&self, current: &OsStr) -> Vec { - self(current) + fn candidates(&self) -> Vec { + self() } }