450acc0113b114352ce3b94870fe63fc461844bd

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix: automatically create completion for `cargo` and `rustup` plugins (#10087)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/cargo/.gitignore b/plugins/cargo/.gitignore
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..42d7ecdd691c3c2ed2e1c3af1395072c13e655f1
  4--- /dev/null
  5+++ b/plugins/cargo/.gitignore
  6@@ -0,0 +1 @@
  7+_cargo
  8diff --git a/plugins/cargo/_cargo b/plugins/cargo/_cargo
  9deleted file mode 100644
 10index ebff993107b9f8362eef7d11c34d64cb093609d9..0000000000000000000000000000000000000000
 11--- a/plugins/cargo/_cargo
 12+++ /dev/null
 13@@ -1,407 +0,0 @@
 14-#compdef cargo
 15-
 16-autoload -U regexp-replace
 17-
 18-_cargo() {
 19-    local curcontext="$curcontext" ret=1
 20-    local -a command_scope_spec common parallel features msgfmt triple target registry
 21-    local -a state line state_descr # These are set by _arguments
 22-    typeset -A opt_args
 23-
 24-    common=(
 25-        '(-q --quiet)*'{-v,--verbose}'[use verbose output]'
 26-        '(-q --quiet -v --verbose)'{-q,--quiet}'[no output printed to stdout]'
 27-        '-Z+[pass unstable (nightly-only) flags to cargo]: :_cargo_unstable_flags'
 28-        '--frozen[require that Cargo.lock and cache are up to date]'
 29-        '--locked[require that Cargo.lock is up to date]'
 30-        '--color=[specify colorization option]:coloring:(auto always never)'
 31-        '(- 1 *)'{-h,--help}'[show help message]'
 32-    )
 33-
 34-    # leading items in parentheses are an exclusion list for the arguments following that arg
 35-    # See: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
 36-    #   - => exclude all other options
 37-    #   1 => exclude positional arg 1
 38-    #   * => exclude all other args
 39-    #   +blah => exclude +blah
 40-    _arguments -s -S -C $common \
 41-        '(- 1 *)--list[list installed commands]' \
 42-        '(- 1 *)--explain=[provide a detailed explanation of an error message]:error code' \
 43-        '(- 1 *)'{-V,--version}'[show version information]' \
 44-        '(+beta +nightly)+stable[use the stable toolchain]' \
 45-        '(+stable +nightly)+beta[use the beta toolchain]' \
 46-        '(+stable +beta)+nightly[use the nightly toolchain]' \
 47-        '1: :_cargo_cmds' \
 48-        '*:: :->args'
 49-
 50-    # These flags are mutually exclusive specifiers for the scope of a command; as
 51-    # they are used in multiple places without change, they are expanded into the
 52-    # appropriate command's `_arguments` where appropriate.
 53-    command_scope_spec=(
 54-        '(--bin --example --test --lib)--bench=[specify benchmark name]: :_cargo_benchmark_names'
 55-        '(--bench --bin --test --lib)--example=[specify example name]:example name'
 56-        '(--bench --example --test --lib)--bin=[specify binary name]:binary name'
 57-        '(--bench --bin --example --test)--lib=[specify library name]:library name'
 58-        '(--bench --bin --example --lib)--test=[specify test name]:test name'
 59-    )
 60-
 61-    parallel=(
 62-        '(-j --jobs)'{-j+,--jobs=}'[specify number of parallel jobs]:jobs [# of CPUs]'
 63-    )
 64-
 65-    features=(
 66-        '(--all-features)--features=[specify features to activate]:feature'
 67-        '(--features)--all-features[activate all available features]'
 68-        "--no-default-features[don't build the default features]"
 69-    )
 70-
 71-    msgfmt='--message-format=[specify error format]:error format [human]:(human json short)'
 72-    triple='--target=[specify target triple]:target triple'
 73-    target='--target-dir=[specify directory for all generated artifacts]:directory:_directories'
 74-    manifest='--manifest-path=[specify path to manifest]:path:_directories'
 75-    registry='--registry=[specify registry to use]:registry'
 76-
 77-    case $state in
 78-        args)
 79-            curcontext="${curcontext%:*}-${words[1]}:"
 80-            case ${words[1]} in
 81-                bench)
 82-                    _arguments -s -A "^--" $common $parallel $features $msgfmt $triple $target $manifest \
 83-                        "${command_scope_spec[@]}" \
 84-                        '--all-targets[benchmark all targets]' \
 85-                        "--no-run[compile but don't run]" \
 86-                        '(-p --package)'{-p+,--package=}'[specify package to run benchmarks for]:package:_cargo_package_names' \
 87-                        '--exclude=[exclude packages from the benchmark]:spec' \
 88-                        '--no-fail-fast[run all benchmarks regardless of failure]' \
 89-                        '1: :_guard "^-*" "bench name"' \
 90-                        '*:args:_default'
 91-                        ;;
 92-
 93-                build|b)
 94-                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
 95-                        '--all-targets[equivalent to specifying --lib --bins --tests --benches --examples]' \
 96-                        "${command_scope_spec[@]}" \
 97-                        '(-p --package)'{-p+,--package=}'[specify package to build]:package:_cargo_package_names' \
 98-                        '--release[build in release mode]' \
 99-                        '--build-plan[output the build plan in JSON]' \
100-                        ;;
101-
102-                check|c)
103-                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
104-                        '--all-targets[equivalent to specifying --lib --bins --tests --benches --examples]' \
105-                        "${command_scope_spec[@]}" \
106-                        '(-p --package)'{-p+,--package=}'[specify package to check]:package:_cargo_package_names' \
107-                        '--release[check in release mode]' \
108-                        ;;
109-
110-                clean)
111-                    _arguments -s -S $common $triple $target $manifest \
112-                        '(-p --package)'{-p+,--package=}'[specify package to clean]:package:_cargo_package_names' \
113diff --git a/plugins/cargo/cargo.plugin.zsh b/plugins/cargo/cargo.plugin.zsh
114new file mode 100644
115index 0000000000000000000000000000000000000000..92eae535993494cbba5196c4089bd94e070e0880
116--- /dev/null
117+++ b/plugins/cargo/cargo.plugin.zsh
118@@ -0,0 +1,11 @@
119+# COMPLETION FUNCTION
120+if (( $+commands[rustup] && $+commands[cargo] )); then
121+  if [[ ! -f $ZSH_CACHE_DIR/cargo_version ]] \
122+    || [[ "$(cargo --version)" != "$(< "$ZSH_CACHE_DIR/cargo_version")" ]] \
123+    || [[ ! -f $ZSH/plugins/cargo/_cargo ]]; then
124+    rustup completions zsh cargo > $ZSH/plugins/cargo/_cargo
125+    cargo --version > $ZSH_CACHE_DIR/cargo_version
126+  fi
127+  autoload -Uz _cargo
128+  _comps[cargo]=_cargo
129+fi
130diff --git a/plugins/rustup/.gitignore b/plugins/rustup/.gitignore
131new file mode 100644
132index 0000000000000000000000000000000000000000..ad38ae3bf5afdff6c794aefb26c6cb30894dff1b
133--- /dev/null
134+++ b/plugins/rustup/.gitignore
135@@ -0,0 +1 @@
136+_rustup
137diff --git a/plugins/rustup/_rustup b/plugins/rustup/_rustup
138deleted file mode 100644
139index dab33533a559ce6813bea10018a39389591ccfd0..0000000000000000000000000000000000000000
140--- a/plugins/rustup/_rustup
141+++ /dev/null
142@@ -1,1143 +0,0 @@
143-#compdef rustup
144-
145-autoload -U is-at-least
146-
147-_rustup() {
148-    typeset -A opt_args
149-    typeset -a _arguments_options
150-    local ret=1
151-
152-    if is-at-least 5.2; then
153-        _arguments_options=(-s -S -C)
154-    else
155-        _arguments_options=(-s -C)
156-    fi
157-
158-    local context curcontext="$curcontext" state line
159-    _arguments "${_arguments_options[@]}" \
160-'-v[Enable verbose output]' \
161-'--verbose[Enable verbose output]' \
162-'(-v --verbose)-q[Disable progress output]' \
163-'(-v --verbose)--quiet[Disable progress output]' \
164-'-h[Prints help information]' \
165-'--help[Prints help information]' \
166-'-V[Prints version information]' \
167-'--version[Prints version information]' \
168-'::+toolchain -- release channel (e.g. +stable) or custom toolchain to set override:_files' \
169-":: :_rustup_commands" \
170-"*::: :->rustup" \
171-&& ret=0
172-    case $state in
173-    (rustup)
174-        words=($line[2] "${words[@]}")
175-        (( CURRENT += 1 ))
176-        curcontext="${curcontext%:*:*}:rustup-command-$line[2]:"
177-        case $line[2] in
178-            (dump-testament)
179-_arguments "${_arguments_options[@]}" \
180-'-h[Prints help information]' \
181-'--help[Prints help information]' \
182-'-V[Prints version information]' \
183-'--version[Prints version information]' \
184-&& ret=0
185-;;
186-(show)
187-_arguments "${_arguments_options[@]}" \
188-'-h[Prints help information]' \
189-'--help[Prints help information]' \
190-'-V[Prints version information]' \
191-'--version[Prints version information]' \
192-":: :_rustup__show_commands" \
193-"*::: :->show" \
194-&& ret=0
195-case $state in
196-    (show)
197-        words=($line[1] "${words[@]}")
198-        (( CURRENT += 1 ))
199-        curcontext="${curcontext%:*:*}:rustup-show-command-$line[1]:"
200-        case $line[1] in
201-            (active-toolchain)
202-_arguments "${_arguments_options[@]}" \
203-'-h[Prints help information]' \
204-'--help[Prints help information]' \
205-'-V[Prints version information]' \
206-'--version[Prints version information]' \
207-&& ret=0
208-;;
209-(home)
210-_arguments "${_arguments_options[@]}" \
211-'-h[Prints help information]' \
212-'--help[Prints help information]' \
213-'-V[Prints version information]' \
214-'--version[Prints version information]' \
215-&& ret=0
216-;;
217-(profile)
218-_arguments "${_arguments_options[@]}" \
219-'-h[Prints help information]' \
220-'--help[Prints help information]' \
221-'-V[Prints version information]' \
222-'--version[Prints version information]' \
223-&& ret=0
224-;;
225-(keys)
226-_arguments "${_arguments_options[@]}" \
227-'-h[Prints help information]' \
228-'--help[Prints help information]' \
229-'-V[Prints version information]' \
230-'--version[Prints version information]' \
231-&& ret=0
232-;;
233-(help)
234-_arguments "${_arguments_options[@]}" \
235-'-h[Prints help information]' \
236-'--help[Prints help information]' \
237-'-V[Prints version information]' \
238-'--version[Prints version information]' \
239-&& ret=0
240-;;
241-        esac
242diff --git a/plugins/rustup/rustup.plugin.zsh b/plugins/rustup/rustup.plugin.zsh
243new file mode 100644
244index 0000000000000000000000000000000000000000..c7a9b3060ff9a9a5eb064d8dd3d224840267fabd
245--- /dev/null
246+++ b/plugins/rustup/rustup.plugin.zsh
247@@ -0,0 +1,12 @@
248+# COMPLETION FUNCTION
249+if (( $+commands[rustup] )); then
250+  if [[ ! -f $ZSH_CACHE_DIR/rustup_version ]] \
251+    || [[ "$(rustup --version 2> /dev/null)" \
252+      != "$(< "$ZSH_CACHE_DIR/rustup_version")" ]] \
253+    || [[ ! -f $ZSH/plugins/rustup/_rustup ]]; then
254+    rustup completions zsh > $ZSH/plugins/rustup/_rustup
255+    rustup --version 2> /dev/null > $ZSH_CACHE_DIR/rustup_version
256+  fi
257+  autoload -Uz _rustup
258+  _comps[rustup]=_rustup
259+fi