feat(completion): add jq-backed zsh completion support. fixes #319#320
feat(completion): add jq-backed zsh completion support. fixes #319#320jameswilson wants to merge 3 commits intomglaman:mainfrom
Conversation
Document how to install native Zsh completion via a _drupalorg completion function. Zsh is the default shell on many macOS versions. Fixes mglaman#319 Made-with: Cursor
- Rename existing completion file to clarify Bash-only compatibility. - Create a sister completion file for Zsh. - Update readme.
- load command metadata from drupalorg list --format=json - cache parsed completion data for the current shell session - complete aliases, options, and positional argument placeholders - document the enhanced zsh behavior and placeholder meaning - add regression coverage for the zsh completion helpers
|
Note: this PR is a workaround for an upstream-releated issue when using |
There was a problem hiding this comment.
Pull request overview
Adds a dedicated Zsh completion script for drupalorg that can optionally use jq + drupalorg list --format=json to provide richer completions, alongside updated docs and a small Zsh regression test.
Changes:
- Added
drupalorg-cli-completion.zshwith JSON/jq-backed completion (commands, namespaces, aliases, options, argument placeholders) plus a non-jq fallback. - Renamed/introduced a Bash-only completion script as
drupalorg-cli-completion.bash. - Documented Zsh completion installation in
README.mdand addedtests/zsh-completion-tests.zsh.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
drupalorg-cli-completion.zsh |
Implements jq-backed Zsh completion with in-session caching and a raw fallback mode. |
tests/zsh-completion-tests.zsh |
Regression tests for core Zsh completion helper behavior and session caching. |
drupalorg-cli-completion.bash |
Bash completion script (still minimal) under a clearer Bash-specific filename. |
README.md |
Updates Bash completion path and adds Zsh completion installation/usage guidance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| local option_key=${_DRUPALORG_COMMAND_OPTION_CANONICAL["$command_name:$option_token"]-} | ||
|
|
There was a problem hiding this comment.
Option tokens that include an inline value (e.g. --format=json) won't match _DRUPALORG_COMMAND_OPTION_CANONICAL, so the completer may (a) re-suggest already-used options and (b) fail to detect that an option requires a value. Normalise long options by stripping any =... suffix (and consider handling short options with attached values) before looking up the canonical option key.
| local option_key=${_DRUPALORG_COMMAND_OPTION_CANONICAL["$command_name:$option_token"]-} | |
| local normalized_option_token=$option_token | |
| # Normalise long options with inline values, e.g. "--format=json" -> "--format". | |
| if [[ $normalized_option_token == --*=* ]]; then | |
| normalized_option_token=${normalized_option_token%%=*} | |
| fi | |
| local option_key=${_DRUPALORG_COMMAND_OPTION_CANONICAL["$command_name:$normalized_option_token"]-} |
| ### Installing (Bash) completion | ||
|
|
||
| `drupalorg` comes with completion support for all commands, excluding options. | ||
|
|
||
| To activate it, either source the completion file or add it to the system-wide completion directory, normally `/etc/bash_completion.d/`. | ||
|
|
||
| In your `.bashrc` (or `.profile`) add | ||
|
|
||
| ``` | ||
| source [...]/vendor/mglaman/drupalorg-cli/drupalorg-cli-completion.sh | ||
| source [...]/vendor/mglaman/drupalorg-cli/drupalorg-cli-completion.bash | ||
| ``` |
There was a problem hiding this comment.
The Bash completion instructions don’t mention the bash-completion dependency that provides _init_completion. To prevent the common _init_completion: command not found failure on macOS, document that users may need to install and source bash-completion (or note the minimal completion script requires it).
Summary
This PR adds richer Zsh completion support for
drupalorgby usingdrupalorg list --format=jsonwhenjqis available.What Changed
drupalorg-cli-completion.bashfor clarity that it is Bash-only.drupalorg-cli-completion.zshscript for Zsh completions.drupalorg <TAB>,drupalorg issue:<TAB>is: egdrupalorg i<TAB>drupalorg is -<TAB>,drupalorg is --<TAB><nid>and[branch]: egdrupalorg is <TAB>jqis unavailableTesting
zsh tests/zsh-completion-tests.zshRelated issues