Skip to content

Commit

Permalink
bug #46341 Fix aliases handling in command name completion (Seldaek)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.4 branch.

Discussion
----------

Fix aliases handling in command name completion

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

While working on composer/composer#10320 I noticed that command aliases like `composer why` did not autocomplete and then even if typed manually the args/options did not autocomplete if using the alias. This PR fixes it.

Commits
-------

8ffc015 Fix aliases handling in command name completion
  • Loading branch information
chalasr committed May 13, 2022
2 parents 5584221 + 8ffc015 commit 2d44f22
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -363,9 +363,18 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType()
&& 'command' === $input->getCompletionName()
) {
$suggestions->suggestValues(array_filter(array_map(function (Command $command) {
return $command->isHidden() ? null : $command->getName();
}, $this->all())));
$commandNames = [];
foreach ($this->all() as $name => $command) {
// skip hidden commands and aliased commands as they already get added below
if ($command->isHidden() || $command->getName() !== $name) {
continue;
}
$commandNames[] = $command->getName();
foreach ($command->getAliases() as $name) {
$commandNames[] = $name;
}
}
$suggestions->suggestValues(array_filter($commandNames));

return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Console/Command/CompleteCommand.php
Expand Up @@ -105,11 +105,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
} elseif (
$completionInput->mustSuggestArgumentValuesFor('command')
&& $command->getName() !== $completionInput->getCompletionValue()
&& !\in_array($completionInput->getCompletionValue(), $command->getAliases(), true)
) {
$this->log(' No command found, completing using the Application class.');

// expand shortcut names ("cache:cl<TAB>") into their full name ("cache:clear")
$suggestions->suggestValue($command->getName());
$suggestions->suggestValues(array_filter(array_merge([$command->getName()], $command->getAliases())));
} else {
$command->mergeApplicationDefinition();
$completionInput->bind($command->getDefinition());
Expand Down
Expand Up @@ -102,9 +102,10 @@ public function testCompleteCommandName(array $input, array $suggestions)

public function provideCompleteCommandNameInputs()
{
yield 'empty' => [['bin/console'], ['help', 'list', 'completion', 'hello']];
yield 'partial' => [['bin/console', 'he'], ['help', 'list', 'completion', 'hello']];
yield 'complete-shortcut-name' => [['bin/console', 'hell'], ['hello']];
yield 'empty' => [['bin/console'], ['help', 'list', 'completion', 'hello', 'ahoy']];
yield 'partial' => [['bin/console', 'he'], ['help', 'list', 'completion', 'hello', 'ahoy']];
yield 'complete-shortcut-name' => [['bin/console', 'hell'], ['hello', 'ahoy']];
yield 'complete-aliases' => [['bin/console', 'ah'], ['hello', 'ahoy']];
}

/**
Expand All @@ -120,6 +121,8 @@ public function provideCompleteCommandInputDefinitionInputs()
{
yield 'definition' => [['bin/console', 'hello', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-interaction']];
yield 'custom' => [['bin/console', 'hello'], ['Fabien', 'Robin', 'Wouter']];
yield 'definition-aliased' => [['bin/console', 'ahoy', '-'], ['--help', '--quiet', '--verbose', '--version', '--ansi', '--no-interaction']];
yield 'custom-aliased' => [['bin/console', 'ahoy'], ['Fabien', 'Robin', 'Wouter']];
}

private function execute(array $input)
Expand All @@ -134,6 +137,7 @@ class CompleteCommandTest_HelloCommand extends Command
public function configure(): void
{
$this->setName('hello')
->setAliases(['ahoy'])
->addArgument('name', InputArgument::REQUIRED)
;
}
Expand Down

0 comments on commit 2d44f22

Please sign in to comment.