Approved · 0 retained findingsclap-rs/clap#6455
feat(complete): Expose possible value completers
Input
PR author’s description
Inspired by #6452
PR diff
Current GitHub diff ↗+use super::ValueCandidates;Browse 3 changed files
clap_complete/src/engine/complete.rs+40 −32
@@ -7,7 +7,9 @@ use super::ArgValueCandidates; use super::ArgValueCompleter; use super::CompletionCandidate; use super::SubcommandCandidates;+use super::ValueCandidates; use super::custom::complete_path;+use super::custom::possible_value_candidates; /// Complete the given command, shell-agnostic pub fn complete(@@ -359,17 +361,16 @@ fn complete_arg_value( if let Some(completer) = arg.get::<ArgValueCompleter>() { values.extend(completer.complete_at(arg_index, value_os)); } else if let Some(completer) = arg.get::<ArgValueCandidates>() {- values.extend(complete_custom_arg_value(value_os, completer));+ values.extend(complete_value_candidates(+ value_os,+ completer.value_candidates(),+ )); } else if let Some(possible_values) = possible_values(arg) { if let Ok(value) = value {- values.extend(possible_values.into_iter().filter_map(|p| {- let name = p.get_name();- name.starts_with(value).then(|| {- CompletionCandidate::new(OsString::from(name))- .help(p.get_help().cloned())- .hide(p.is_hide_set())- })- }));+ values.extend(complete_candidates_str(+ value,+ possible_value_candidates(possible_values),+ )); } } else { match arg.get_value_hint() {@@ -438,17 +439,36 @@ fn rsplit_delimiter<'s, 'o>( Some((Some(prefix), Ok(value))) } -fn complete_custom_arg_value(+fn complete_value_candidates( value: &OsStr,- completer: &ArgValueCandidates,+ completer: &dyn ValueCandidates, ) -> Vec<CompletionCandidate> {- debug!("complete_custom_arg_value: completer={completer:?}, value={value:?}");+ debug!("complete_value_candidates: value={value:?}"); let mut values = completer.candidates(); values.retain(|comp| comp.get_value().starts_with(&value.to_string_lossy())); values } +fn complete_candidates_str(+ value: &str,+ mut values: Vec<CompletionCandidate>,+) -> Vec<CompletionCandidate> {+ debug!("complete_candidates_str: value={value:?}");++ values.retain(|comp| comp.get_value().starts_with(value));+ values+}++fn complete_value_candidates_str(+ value: &str,+ completer: &dyn ValueCandidates,+) -> Vec<CompletionCandidate> {+ debug!("complete_value_candidates_str: value={value:?}");++ complete_candidates_str(value, completer.candidates())+}+ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<CompletionCandidate> { debug!( "complete_subcommand: cmd={:?}, value={:?}",@@ -463,7 +483,10 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<CompletionCandid if cmd.is_allow_external_subcommands_set() { let external_completer = cmd.get::<SubcommandCandidates>(); if let Some(completer) = external_completer {- scs.extend(complete_external_subcommand(value, completer));+ scs.extend(complete_value_candidates_str(+ value,+ completer.value_candidates(),+ )); } } @@ -472,21 +495,6 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<CompletionCandid scs } -fn complete_external_subcommand(- value: &str,- completer: &SubcommandCandidates,-) -> Vec<CompletionCandidate> {- debug!("complete_custom_arg_value: completer={completer:?}, value={value:?}");-- let mut values = Vec::new();- let custom_arg_values = completer.candidates();- values.extend(custom_arg_values);-- values.retain(|comp| comp.get_value().starts_with(value));-- values-}- /// Gets all the long options, their visible aliases and flags of a [`clap::Command`] with formatted `--` prefix. /// Includes `help` and `version` depending on the [`clap::Command`] settings. fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> {@@ -556,13 +564,13 @@ fn populate_arg_candidate(candidate: CompletionCandidate, arg: &clap::Arg) -> Co } /// Get the possible values for completion-fn possible_values(a: &clap::Arg) -> Option<Vec<clap::builder::PossibleValue>> {+fn possible_values(+ a: &clap::Arg,+) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> { if !a.get_num_args().expect("built").takes_values() { None } else {- a.get_value_parser()- .possible_values()- .map(|pvs| pvs.collect())+ a.get_value_parser().possible_values() } }
clap_complete/src/engine/custom.rs+45 −0
@@ -1,5 +1,6 @@ use std::any::type_name; use std::ffi::OsStr;+use std::ffi::OsString; use std::sync::Arc; use clap::builder::ArgExt;@@ -155,6 +156,10 @@ impl ArgValueCandidates { pub fn candidates(&self) -> Vec<CompletionCandidate> { self.0.candidates() }++ pub(crate) fn value_candidates(&self) -> &dyn ValueCandidates {+ &*self.0+ } } impl std::fmt::Debug for ArgValueCandidates {@@ -199,6 +204,10 @@ impl SubcommandCandidates { pub fn candidates(&self) -> Vec<CompletionCandidate> { self.0.candidates() }++ pub(crate) fn value_candidates(&self) -> &dyn ValueCandidates {+ &*self.0+ } } impl std::fmt::Debug for SubcommandCandidates {@@ -416,3 +425,39 @@ fn path_has_name(path: &std::path::Path) -> bool { let trailing = *trailing as char; !std::path::is_separator(trailing) && path.file_name().is_some() }++/// Create [`CompletionCandidate`]s for [`clap::builder::PossibleValue`]+///+/// This is implicitly applied for arguments with either a [`clap::builder::PossibleValuesParser`] or [`clap::builder::EnumValueParser`].+///+/// Using this explicitly is intended for arguments that also accept other value formats that need+/// to be completed and the completer is eager ([`ValueCandidates`]).+/// For lazy completion, see [`complete_possible_values`].+pub fn possible_value_candidates(+ values: impl IntoIterator<Item = clap::builder::PossibleValue>,+) -> Vec<CompletionCandidate> {+ values+ .into_iter()+ .map(|p| {+ CompletionCandidate::new(OsString::from(p.get_name()))+ .help(p.get_help().cloned())+ .hide(p.is_hide_set())+ })+ .collect()+}++/// Create [`CompletionCandidate`]s for [`clap::builder::PossibleValue`]+///+/// This is implicitly applied for arguments with either a [`clap::builder::PossibleValuesParser`] or [`clap::builder::EnumValueParser`].+///+/// Using this explicitly is intended for arguments that also accept other value formats that need+/// to be completed and the completer is lazy ([`ValueCompleter`]).+/// For eager completion, see [`possible_value_candidates`].+pub fn complete_possible_values(+ value: &OsStr,+ values: impl IntoIterator<Item = clap::builder::PossibleValue>,+) -> Vec<CompletionCandidate> {+ let mut values = possible_value_candidates(values);+ values.retain(|comp| comp.get_value().starts_with(&value.to_string_lossy()));+ values+}
clap_complete/src/engine/mod.rs+2 −0
@@ -14,3 +14,5 @@ pub use custom::PathCompleter; pub use custom::SubcommandCandidates; pub use custom::ValueCandidates; pub use custom::ValueCompleter;+pub use custom::complete_possible_values;+pub use custom::possible_value_candidates;
Outcome
Why this verdict
approve — No checked defect was established by the available proof adapters.
Lens advisories (not part of the checked findings):
- historical_risk: The changed code refactors 'complete_custom_arg_value' to 'complete_value_candidates' and 'complete_candidates_str', and changes this function's signature from taking 'ArgValueCandidates' to '&dyn ValueCandidates', meaning the public API is being modified at an unstable registry index.
Lens coverage: 6 delivered
The recorded observations did not become verified findings.
Evidence and perspectives
Observation · Historical riskThe changed code refactors 'complete_custom_arg_value' to 'complete_value_candidates' and 'complete_candidates_str', and changes this function's signature from taking 'ArgValueCandidates' to '&dyn ValueCandidates', meaning the public API is being modified at an unstable registry index.
Recorded location clap_complete/src/engine/complete.rs:441
Observation · Dependency blast radiusThe change introduces a new public dependency boundary by calling `complete_candidates_str` and `complete_value_candidates` which accept `&dyn ValueCandidates`, exposing the `ValueCandidates` trait to callers that previously only used `ArgValueCandidates`.
Recorded location clap_complete/src/engine/complete.rs:14