diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-02 11:33:44 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-02 11:33:44 +0900 |
| commit | f1dee6362f9b25e78f7e6417e957c0ca34356ecb (patch) | |
| tree | c25c07d81153b4fb12ae840db9c3eb71f5d7fab4 /crates/shirabe/src/command/global_command.rs | |
| parent | 876f1bf158cf03944ba56fee351fa97b34fda9e0 (diff) | |
| download | php-shirabe-f1dee6362f9b25e78f7e6417e957c0ca34356ecb.tar.gz php-shirabe-f1dee6362f9b25e78f7e6417e957c0ca34356ecb.tar.zst php-shirabe-f1dee6362f9b25e78f7e6417e957c0ca34356ecb.zip | |
feat(command): wire suggested values into every command definition
Ports the per-command completion metadata that PHP passes as the
suggestedValues constructor argument, resolving all TODO(cli-completion)
markers:
- CompletionTrait providers on 18 argument/option sites (installed/root/
available package names, package types, prefer-install)
- static value lists (--format on show/outdated/search/fund/licenses/
check-platform-reqs, archive's FORMATS, audit --ignore-severity,
update --bump-after-update, repository's action list)
- command-specific closures: ConfigCommand::suggest_setting_keys,
ShowCommand::suggest_package_based_on_mode, RepositoryCommand's
suggest_repo_names/suggest_type_for_add, exec/run-script inline
closures (downcast from the this argument, as the closures are bound
to their concrete command in PHP)
- GlobalCommand::complete, delegating completion to the wrapped
subcommand through CompletionInput::from_string
- a complete() override on every Composer command forwarding to
base_command_complete (BaseCommand inheritance restoration)
Also fixes CompleteCommand to call merge_application_definition(true) as
PHP's default-argument call does; with false the application-level
"command" argument was missing from the bound definition, shifting every
argument-position detection by one.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/command/global_command.rs')
| -rw-r--r-- | crates/shirabe/src/command/global_command.rs | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/crates/shirabe/src/command/global_command.rs b/crates/shirabe/src/command/global_command.rs index 0414fd76..2795b778 100644 --- a/crates/shirabe/src/command/global_command.rs +++ b/crates/shirabe/src/command/global_command.rs @@ -11,6 +11,9 @@ use crate::util::Platform; use shirabe_external_packages::composer::pcre::Preg; use shirabe_external_packages::symfony::console::command::command::Command; use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput; +use shirabe_external_packages::symfony::console::completion::completion_suggestions::{ + CompletionSuggestions, StringOrSuggestion, +}; use shirabe_external_packages::symfony::console::input::ArgvInput; use shirabe_external_packages::symfony::console::input::ArrayInput; use shirabe_external_packages::symfony::console::input::InputInterface; @@ -41,8 +44,6 @@ impl GlobalCommand { command } - // TODO(cli-completion): pub fn complete(&self, input: &CompletionInput, suggestions: &mut CompletionSuggestions) - // TODO remove for Symfony 6+ as it is then in the interface. // Mirrors PHP's `method_exists($input, '__toString')` guard followed by // `$input->__toString()`. `InputInterface` does not declare `__toString`, so the @@ -151,6 +152,72 @@ impl Command for GlobalCommand { true } + fn complete( + &self, + input: &CompletionInput, + suggestions: &mut CompletionSuggestions, + ) -> anyhow::Result<()> { + let application = self + .get_application() + .expect("a proxy command is always attached to its application"); + if input.must_suggest_argument_values_for("command-name") { + // The application borrow must be dropped before suggest_values (harmless) and + // before any command re-entry below. + let values: Vec<StringOrSuggestion> = { + let mut app_ref = application.borrow_mut(); + let app = app_ref + .as_any_mut() + .downcast_mut::<Application>() + .expect("shirabe always installs its own Application"); + app.all(None)? + .values() + // PHP: $command->isHidden() ? null : $command->getName(), then + // array_filter drops the nulls. + .filter(|command| !command.borrow().is_hidden()) + .filter_map(|command| command.borrow().get_name()) + .map(StringOrSuggestion::String) + .collect() + }; + suggestions.suggest_values(values); + + return Ok(()); + } + + let command_name = input.get_argument("command-name")?.to_string(); + let has = { + let mut app_ref = application.borrow_mut(); + let app = app_ref + .as_any_mut() + .downcast_mut::<Application>() + .expect("shirabe always installs its own Application"); + app.has(&command_name) + }; + if has { + let prepared = self.prepare_subcommand_input( + std::rc::Rc::new(std::cell::RefCell::new(input.clone())), + true, + )?; + let mut input = CompletionInput::from_string(&prepared.to_string(), 2)?; + let command = { + let mut app_ref = application.borrow_mut(); + let app = app_ref + .as_any_mut() + .downcast_mut::<Application>() + .expect("shirabe always installs its own Application"); + app.find(&command_name)? + }; + command.borrow().merge_application_definition(true); + + { + let command_ref = command.borrow(); + let definition = command_ref.get_definition(); + input.bind(&definition)?; + } + command.borrow().complete(&input, suggestions)?; + } + Ok(()) + } + fn run( &self, input: std::rc::Rc<std::cell::RefCell<dyn InputInterface>>, |
