From f1dee6362f9b25e78f7e6417e957c0ca34356ecb Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Aug 2026 11:33:44 +0900 Subject: 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 --- crates/shirabe/src/command/global_command.rs | 71 +++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) (limited to 'crates/shirabe/src/command/global_command.rs') 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 = { + let mut app_ref = application.borrow_mut(); + let app = app_ref + .as_any_mut() + .downcast_mut::() + .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::() + .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::() + .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>, -- cgit v1.3.1