From ebcd0d1edc4434364b7b4027dd4d4f277e62d989 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 29 Jul 2026 13:25:08 +0800 Subject: feat(picker_comp): add module to bridge picker and comp subsystems --- mingling/src/lib.rs | 4 ++++ mingling/src/picker_comp.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 mingling/src/picker_comp.rs (limited to 'mingling') diff --git a/mingling/src/lib.rs b/mingling/src/lib.rs index d7c8e8f..8f2540c 100644 --- a/mingling/src/lib.rs +++ b/mingling/src/lib.rs @@ -24,6 +24,10 @@ pub mod parser; #[cfg(feature = "picker")] pub mod picker; +/// Functionality when combining `picker` and `comp` +#[cfg(all(feature = "picker", feature = "comp"))] +pub mod picker_comp; + mod constants; /// Constants used throughout the Mingling framework. diff --git a/mingling/src/picker_comp.rs b/mingling/src/picker_comp.rs new file mode 100644 index 0000000..26b0b5a --- /dev/null +++ b/mingling/src/picker_comp.rs @@ -0,0 +1,52 @@ +use arg_picker::{Pickable, PickerArg, PickerArgInfo, parselib::ParserStyle}; +use mingling_core::{Suggest, SuggestItem}; + +/// Trait for converting picker arguments into `Suggest` items for autocompletion. +/// +/// This trait is implemented for `PickerArg` references and provides methods to +/// generate completion suggestions with optional descriptions. +pub trait PickerArgSuggest { + #[doc(hidden)] + fn into_suggest_raw(self, description: Option<&String>) -> Suggest; + + /// Converts the picker argument into a `Suggest` with the given description. + /// + /// The description is used when the suggestion item supports additional text, + /// such as `SuggestItem::WithDescription`. + fn into_suggest_with_desc(self, description: impl Into) -> Suggest + where + Self: Sized, + { + let desc = description.into(); + self.into_suggest_raw(Some(&desc)) + } + + /// Converts the picker argument into a `Suggest` without a description. + /// + /// The resulting suggestion will use `SuggestItem::Simple` for each possible flag. + fn into_suggest(self) -> Suggest + where + Self: Sized, + { + self.into_suggest_raw(None) + } +} + +impl<'a, T> PickerArgSuggest for &'a PickerArg<'a, T> +where + T: Pickable<'a>, +{ + fn into_suggest_raw(self, description: Option<&String>) -> Suggest { + let mut suggest = Suggest::new(); + let info = PickerArgInfo::from(self); + let possible_flags = + arg_picker::parselib::build_possible_flags(ParserStyle::global_style(), &info); + for flag in possible_flags { + match description { + Some(desc) => suggest.insert(SuggestItem::WithDescription(flag, desc.clone())), + None => suggest.insert(SuggestItem::Simple(flag)), + }; + } + suggest + } +} -- cgit