diff options
Diffstat (limited to 'crates/shirabe/src/console')
| -rw-r--r-- | crates/shirabe/src/console/input.rs | 85 | ||||
| -rw-r--r-- | crates/shirabe/src/console/input/input_argument.rs | 44 | ||||
| -rw-r--r-- | crates/shirabe/src/console/input/input_option.rs | 52 |
3 files changed, 173 insertions, 8 deletions
diff --git a/crates/shirabe/src/console/input.rs b/crates/shirabe/src/console/input.rs index 9141f0cb..9661c7ad 100644 --- a/crates/shirabe/src/console/input.rs +++ b/crates/shirabe/src/console/input.rs @@ -4,9 +4,86 @@ pub mod input_option; pub use input_argument::*; pub use input_option::*; +use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput; +use shirabe_external_packages::symfony::console::completion::completion_suggestions::{ + CompletionSuggestions, StringOrSuggestion, +}; + +/// PHP: `\Closure(CompletionInput,CompletionSuggestions):list<string|Suggestion>`. +/// +/// PHP closures are bound to the command instance (`$this`); commands cannot capture a handle +/// to themselves while `configure()` runs inside `new()`, so the bound command is passed in as +/// `this` at call time instead. +pub type SuggestedValuesClosure = Box< + dyn Fn( + &dyn crate::command::BaseCommand, + &CompletionInput, + &mut CompletionSuggestions, + ) -> anyhow::Result<Vec<String>>, +>; + +/// PHP: the `list<string>|\Closure(...)` union taken by the suggestedValues parameter of the +/// `Composer\Console\Input\InputArgument` / `InputOption` backport. +/// +/// The closure returns `Vec<String>` rather than `list<string|Suggestion>`: every Composer +/// closure returns plain strings, and `complete` lifts them into suggestions. PHP's runtime +/// "Closure must return an array" LogicException is statically guaranteed by the type. +pub enum SuggestedValues { + List(Vec<String>), + Closure(SuggestedValuesClosure), +} + +impl std::fmt::Debug for SuggestedValues { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SuggestedValues::List(values) => f.debug_tuple("List").field(values).finish(), + SuggestedValues::Closure(_) => f.write_str("Closure(..)"), + } + } +} + +impl SuggestedValues { + /// Whether PHP's `[] !== $suggestedValues` is false, i.e. no suggestions were declared. + pub(crate) fn is_empty(&self) -> bool { + matches!(self, SuggestedValues::List(values) if values.is_empty()) + } + + /// The shared body of the `InputArgument::complete` / `InputOption::complete` backport. + pub(crate) fn complete( + &self, + this: &dyn crate::command::BaseCommand, + input: &CompletionInput, + suggestions: &mut CompletionSuggestions, + ) -> anyhow::Result<()> { + let values = match self { + SuggestedValues::List(values) => values.clone(), + SuggestedValues::Closure(closure) => closure(this, input, suggestions)?, + }; + if !values.is_empty() { + suggestions + .suggest_values(values.into_iter().map(StringOrSuggestion::String).collect()); + } + Ok(()) + } + + /// PHP: `$this->suggestX()($input)` — invoking a suggestion closure directly. Calling this + /// on a List is a programming error (PHP would fatal on `$array()`). + pub fn call( + &self, + this: &dyn crate::command::BaseCommand, + input: &CompletionInput, + suggestions: &mut CompletionSuggestions, + ) -> anyhow::Result<Vec<String>> { + match self { + SuggestedValues::Closure(closure) => closure(this, input, suggestions), + SuggestedValues::List(_) => panic!("SuggestedValues::call on a non-closure"), + } + } +} + pub enum InputDefinitionItem { - Argument(input_argument::InputArgument), - Option(input_option::InputOption), + Argument(std::rc::Rc<input_argument::InputArgument>), + Option(std::rc::Rc<input_option::InputOption>), } impl InputDefinitionItem { @@ -26,12 +103,12 @@ impl InputDefinitionItem { impl From<input_argument::InputArgument> for InputDefinitionItem { fn from(value: input_argument::InputArgument) -> Self { - Self::Argument(value) + Self::Argument(std::rc::Rc::new(value)) } } impl From<input_option::InputOption> for InputDefinitionItem { fn from(value: input_option::InputOption) -> Self { - Self::Option(value) + Self::Option(std::rc::Rc::new(value)) } } diff --git a/crates/shirabe/src/console/input/input_argument.rs b/crates/shirabe/src/console/input/input_argument.rs index 5fd9cecc..ab32ffba 100644 --- a/crates/shirabe/src/console/input/input_argument.rs +++ b/crates/shirabe/src/console/input/input_argument.rs @@ -1,11 +1,15 @@ //! ref: composer/src/Composer/Console/Input/InputArgument.php +use crate::console::input::SuggestedValues; +use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput; +use shirabe_external_packages::symfony::console::completion::completion_suggestions::CompletionSuggestions; use shirabe_external_packages::symfony::console::input::InputArgument as BaseInputArgument; use shirabe_php_shim::PhpMixed; #[derive(Debug)] pub struct InputArgument { inner: BaseInputArgument, + suggested_values: SuggestedValues, } impl InputArgument { @@ -18,7 +22,23 @@ impl InputArgument { mode: Option<i64>, description: &str, default: Option<PhpMixed>, - // TODO(cli-completion): suggested_values closure / list dropped along with completion support + ) -> anyhow::Result<Self> { + Self::new5( + name, + mode, + description, + default, + SuggestedValues::List(Vec::new()), + ) + } + + /// PHP's constructor with the fifth parameter, `$suggestedValues`. + pub fn new5( + name: &str, + mode: Option<i64>, + description: &str, + default: Option<PhpMixed>, + suggested_values: SuggestedValues, ) -> anyhow::Result<Self> { let inner = BaseInputArgument::new( name.to_string(), @@ -26,7 +46,27 @@ impl InputArgument { description.to_string(), default.unwrap_or(PhpMixed::Null), )?; - Ok(Self { inner }) + Ok(Self { + inner, + suggested_values, + }) + } + + /// Adds suggestions to `suggestions` for the current completion input. + /// + /// PHP closures are bound to the command; `this` is the command dispatching the + /// completion (see [`SuggestedValues`]). + pub fn complete( + &self, + this: &dyn crate::command::BaseCommand, + input: &CompletionInput, + suggestions: &mut CompletionSuggestions, + ) -> anyhow::Result<()> { + self.suggested_values.complete(this, input, suggestions) + } + + pub(crate) fn get_name(&self) -> String { + self.inner.get_name().to_string() } /// Unwraps to the underlying Symfony `InputArgument` (used when forwarding a Composer-typed diff --git a/crates/shirabe/src/console/input/input_option.rs b/crates/shirabe/src/console/input/input_option.rs index 938f5a39..7fa3d699 100644 --- a/crates/shirabe/src/console/input/input_option.rs +++ b/crates/shirabe/src/console/input/input_option.rs @@ -1,11 +1,15 @@ //! ref: composer/src/Composer/Console/Input/InputOption.php +use crate::console::input::SuggestedValues; +use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput; +use shirabe_external_packages::symfony::console::completion::completion_suggestions::CompletionSuggestions; use shirabe_external_packages::symfony::console::input::InputOption as BaseInputOption; use shirabe_php_shim::PhpMixed; #[derive(Debug)] pub struct InputOption { inner: BaseInputOption, + suggested_values: SuggestedValues, } impl InputOption { @@ -21,13 +25,57 @@ impl InputOption { mode: Option<i64>, description: &str, default: Option<PhpMixed>, - // TODO(cli-completion): suggested_values closure / list dropped along with completion support + ) -> anyhow::Result<Self> { + Self::new6( + name, + shortcut, + mode, + description, + default, + SuggestedValues::List(Vec::new()), + ) + } + + /// PHP's constructor with the sixth parameter, `$suggestedValues`. + pub fn new6( + name: &str, + shortcut: Option<PhpMixed>, + mode: Option<i64>, + description: &str, + default: Option<PhpMixed>, + suggested_values: SuggestedValues, ) -> anyhow::Result<Self> { let shortcut = shortcut.unwrap_or(PhpMixed::Null); let default_mixed = default.unwrap_or(PhpMixed::Null); let inner = BaseInputOption::new(name, shortcut, mode, description.to_string(), default_mixed)?; - Ok(Self { inner }) + // PHP throws LogicException here; suggested values on a valueless option cannot happen + // at runtime unless a configure() is wrong, so this is a programming error. + assert!( + suggested_values.is_empty() || inner.accept_value(), + "Cannot set suggested values if the option does not accept a value." + ); + Ok(Self { + inner, + suggested_values, + }) + } + + /// Adds suggestions to `suggestions` for the current completion input. + /// + /// PHP closures are bound to the command; `this` is the command dispatching the + /// completion (see [`SuggestedValues`]). + pub fn complete( + &self, + this: &dyn crate::command::BaseCommand, + input: &CompletionInput, + suggestions: &mut CompletionSuggestions, + ) -> anyhow::Result<()> { + self.suggested_values.complete(this, input, suggestions) + } + + pub(crate) fn get_name(&self) -> String { + self.inner.get_name().to_string() } /// Unwraps to the underlying Symfony `InputOption` (used when forwarding a Composer-typed |
