New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add completion to commands options and arguments #10320
Conversation
8fb91d1
to
b93530d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting started on this, looking forward to having it available!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing work! 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hands on experience.
src/Composer/Command/BaseCommand.php
Outdated
|
||
$packages = $repos->search('^'.$input->getCompletionValue(), RepositoryInterface::SEARCH_NAME); | ||
|
||
foreach (array_slice($packages, 0, 150) as $package) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that we're limiting to 150 here has non-obvious consequences when used, see #10325. This part needs some special attention to polish the UX here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did improve this in 55dc808 - for vendor names though when going through packagist.org results there are still so many that it's kind of a mess and you'll in most cases have to type enough of what you want to get it, but then it completes packages names within the vendor which is already quite useful IMO.
825c21f
to
e4a71ac
Compare
deb29af
to
dec43d6
Compare
The composer.lock diff comment has been updated to reflect new changes in this PR. |
…vendor is fully known, refs composer#10320
Please rebase on latest main branch :) |
dec43d6
to
6da2d34
Compare
I rebased, fixed some phpstan warnings. |
$suggestions->suggestValues(array_filter(array_map(function (Command $command) { | ||
return $command->isHidden() ? null : $command->getName(); | ||
}, $application->all()))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completion of subcommand is hard. I have this for beginning; but the global state of the application must be modified.
if ($application->has($commandName = $input->getArgument('command-name'))) {
$input = CompletionInput::fromString(preg_replace(['{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '{|$}'], ['', ''], $input->__toString(), 1), 3);
$command = $this->getApplication()->find($commandName);
$input->bind($command->getDefinition());
$command->complete($input, $suggestions);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean by "the global state of the application must be modified"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the things that are done in the run
method.
composer/src/Composer/Command/GlobalCommand.php
Lines 90 to 116 in 5e4a8a8
// The COMPOSER env var should not apply to the global execution scope | |
if (Platform::getEnv('COMPOSER')) { | |
Platform::clearEnv('COMPOSER'); | |
} | |
// change to global dir | |
$config = Factory::createConfig(); | |
$home = $config->get('home'); | |
if (!is_dir($home)) { | |
$fs = new Filesystem(); | |
$fs->ensureDirectoryExists($home); | |
if (!is_dir($home)) { | |
throw new \RuntimeException('Could not create home directory'); | |
} | |
} | |
try { | |
chdir($home); | |
} catch (\Exception $e) { | |
throw new \RuntimeException('Could not switch to home directory "'.$home.'"', 0, $e); | |
} | |
$this->getIO()->writeError('<info>Changed current directory to '.$home.'</info>'); | |
// create new input without "global" command prefix | |
$input = new StringInput(Preg::replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1)); | |
$this->getApplication()->resetComposer(); |
That said ... that's not so complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh I see. Yes that should be easy to extract in a method to avoid duplication?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See dd2e2d7 - I tried doing it but it doesn't seem to work, I think I missed something, perhaps you can have a look?
…g all available packages, add support for -p/-a in show command
dd2e2d7
to
6aa7e15
Compare
eda1d89
to
55dc808
Compare
97c7ffa
to
eac83aa
Compare
OK PR is good to merge for me now except for the glitches in the global command. @GromNaN do you think you have time to look at that soon? Otherwise I'd probably merge as is and it can be done in a follow-up PR. |
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
Completion on |
Next step will be the setup the completion script with composer.phar. composer/src/Composer/Compiler.php Line 123 in 575fbfb
|
Thanks @GromNaN! Let's look at the global command in a further PR then whenever you have a chance :) |
Fix #10292
How to use ?
Install bash and bash_completion on your system.
Init completion script
bin/composer <tab,tab>
Code requiressymfony/console >= 5.4
to work andPHP >= 7.1
to not fail (void
return type). The dependency updates are only for testing.Note: for testing I use the debug command described here symfony/symfony#43594 (comment)