composer-bash-completion-plugin
composer-bash-completion-plugin copied to clipboard
[Feature] Complete packages after require statement
Hey Stephen,
first of all ... let me thank you for that awesome plugin. The idea of fetching valid suggestions from the available *Commands and InputOptions is brilliant and logical.
I've seen other completion functions capable of providing completions for the require command by using a (cached) result of composer show.
composer show -a | grep -v '^No composer'
... can be used to find all available packages without the line ...
No composer.json found in the current directory, showing available packages from packagist
... if composer is executed inside a directory that has no composer.json.
I didn't have the time to dive into the code of the CompletionCommand yet.
Would you rather add something like this in the bash/zsh-snippets or do this in PHP with a ComposerCompletionCommand that extends CompletionCommand added to this repository?
Just for completeness:
composer archive <package>
composer show <package>
... should use the same list.
Hey @nifr, I'm interested in adding this, though it'll be a bit of challenge making it run fast enough for completion to not be painful and show reasonably up to date package names. The completion behaviour can be implemented in PHP using a callback function as a completion handler.
The first call to composer show --available --name-only for me is pretty slow at ~14 seconds, and subsequent requests are still slow at ~4 seconds each, so that approach would need caching to some degree. Dumping the output of that command into a file and scanning it for matches seems like a good approach - matching against the full list only takes 20ms with grep, or ~170ms in PHP.
Another possible way to source package details would be to read the cached composer repository JSON files in ~/.composer/cache/repo/. This is faster than composer show at around 0.5 to 1s, but it doesn't list packages you've never downloaded and it's still not that snappy considering it has to parse tens of MB of JSON:
$ find ~/.composer/cache/repo/ -iname '*json' -exec du -ch {} +
You're welcome to have a go at implementing this. I'll have a think about a good solution in any case. Thanks for your other suggestions too :+1:
https://github.com/iArren/composer-bash-completion does the caching into "packages.list" file. maybe do the same? extra ttl could be set too, perhaps 10-60 minutes to redo the fetch (could be smaller if cache is fetched in background)