fork/cargo

git clone https://git.julia.blue/fork/cargo

Eric HussAdd --offline for comp (#15623)21601a246

0.91.0
24.0 KiB480 linesraw
1#compdef cargo
2
3autoload -U regexp-replace
4
5_cargo() {
6    local curcontext="$curcontext" ret=1
7    local -a command_scope_spec common parallel features msgfmt triple target registry
8    local -a state line state_descr # These are set by _arguments
9    typeset -A opt_args
10
11    common=(
12        '(-q --quiet)*'{-v,--verbose}'[use verbose output]'
13        '(-q --quiet -v --verbose)'{-q,--quiet}'[no output printed to stdout]'
14        '-Z+[pass unstable (nightly-only) flags to cargo]: :_cargo_unstable_flags'
15        '--offline[run without accessing the network]'
16        '--frozen[require that Cargo.lock and cache are up-to-date]'
17        '--locked[require that Cargo.lock is up-to-date]'
18        '--color=[specify colorization option]:coloring:(auto always never)'
19        '(- 1 *)'{-h,--help}'[show help message]'
20    )
21
22    # leading items in parentheses are an exclusion list for the arguments following that arg
23    # See: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
24    #   - => exclude all other options
25    #   1 => exclude positional arg 1
26    #   * => exclude all other args
27    #   +blah => exclude +blah
28    _arguments -s -S -C $common \
29        '(- 1 *)--list[list installed commands]' \
30        '(- 1 *)--explain=[provide a detailed explanation of an error message]:error code' \
31        '(- 1 *)'{-V,--version}'[show version information]' \
32        '(+beta +nightly)+stable[use the stable toolchain]' \
33        '(+stable +nightly)+beta[use the beta toolchain]' \
34        '(+stable +beta)+nightly[use the nightly toolchain]' \
35        '1: :_cargo_cmds' \
36        '*:: :->args'
37
38    # These flags are mutually exclusive specifiers for the scope of a command; as
39    # they are used in multiple places without change, they are expanded into the
40    # appropriate command's `_arguments` where appropriate.
41    command_scope_spec=(
42        '(--bin --example --test --lib)--bench=[specify benchmark name]: :_cargo_benchmark_names'
43        '(--bench --bin --test --lib)--example=[specify example name]:example name:_cargo_example_names'
44        '(--bench --example --test --lib)--bin=[specify binary name]:binary name'
45        '(--bench --bin --example --test)--lib=[specify library name]:library name'
46        '(--bench --bin --example --lib)--test=[specify test name]:test name'
47    )
48
49    jobs=(
50        '(-j --jobs)'{-j+,--jobs=}'[specify number of parallel jobs]:jobs [# of CPUs]'
51    )
52
53    parallel=(
54        "${jobs[@]}"
55        '--keep-going[do not abort build on first build error]'
56    )
57
58    features=(
59        '(--all-features)'{-F+,--features=}'[specify features to activate]:feature'
60        '(--features -F)--all-features[activate all available features]'
61        "--no-default-features[don't build the default features]"
62    )
63
64    msgfmt='--message-format=[specify error format]:error format [human]:(human json short)'
65    triple='--target=[specify target triple]:target triple:_cargo_target_triple'
66    target='--target-dir=[specify directory for all generated artifacts]:directory:_directories'
67    manifest='--manifest-path=[specify path to manifest]:path:_directories'
68    registry='--registry=[specify registry to use]:registry'
69
70    case $state in
71        args)
72            curcontext="${curcontext%:*}-${words[1]}:"
73            case ${words[1]} in
74                add)
75                    _arguments -s -A "^--" $common $manifest $registry \
76                        {-F+,--features=}'[specify features to activate]:feature' \
77                        "--default-features[enable the default features]" \
78                        "--no-default-features[don't enable the default features]" \
79                        "--optional[mark the dependency as optional]" \
80                        "--no-optional[mark the dependency as required]" \
81                        "--dev[add as a dev dependency]" \
82                        "--build[add as a build dependency]" \
83                        "--target=[add as a dependency to the given target platform]" \
84                        "--rename=[rename the dependency]" \
85                        "--dry-run[don't actually write the manifest]" \
86                        '--branch=[branch to use when adding from git]:branch' \
87                        '--git=[specify URL from which to add the crate]:url:_urls' \
88                        '--path=[local filesystem path to crate to add]: :_directories' \
89                        '--rev=[specific commit to use when adding from git]:commit' \
90                        '--tag=[tag to use when adding from git]:tag' \
91                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
92                        '1: :_guard "^-*" "crate name"' \
93                        '*:args:_default'
94                        ;;
95                bench)
96                    _arguments -s -A "^--" $common $jobs $features $msgfmt $triple $target $manifest \
97                        "${command_scope_spec[@]}" \
98                        '--all-targets[benchmark all targets]' \
99                        "--no-run[compile but don't run]" \
100                        '(-p --package)'{-p+,--package=}'[specify package to run benchmarks for]:package:_cargo_package_names' \
101                        '--exclude=[exclude packages from the benchmark]:spec' \
102                        '--no-fail-fast[run all benchmarks regardless of failure]' \
103                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
104                        '1: :_guard "^-*" "bench name"' \
105                        '*:args:_default'
106                        ;;
107
108                build | b)
109                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
110                        '--all-targets[equivalent to specifying --lib --bins --tests --benches --examples]' \
111                        "${command_scope_spec[@]}" \
112                        '(-p --package)'{-p+,--package=}'[specify package to build]:package:_cargo_package_names' \
113                        '--release[build in release mode]' \
114                        '--build-plan[output the build plan in JSON]' \
115                        '--ignore-rust-version[Ignore rust-version specification in packages]'
116                        ;;
117
118                check | c)
119                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
120                        '--all-targets[equivalent to specifying --lib --bins --tests --benches --examples]' \
121                        "${command_scope_spec[@]}" \
122                        '(-p --package)'{-p+,--package=}'[specify package to check]:package:_cargo_package_names' \
123                        '--release[check in release mode]' \
124                        '--ignore-rust-version[Ignore rust-version specification in packages]'
125                        ;;
126
127                clean)
128                    _arguments -s -S $common $triple $target $manifest \
129                        '(-p --package)'{-p+,--package=}'[specify package to clean]:package:_cargo_package_names' \
130                        '--release[clean release artifacts]' \
131                        '--doc[clean just the documentation directory]'
132                        ;;
133
134                doc | d)
135                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
136                        '--no-deps[do not build docs for dependencies]' \
137                        '--document-private-items[include non-public items in the documentation]' \
138                        '--open[open docs in browser after the build]' \
139                        '(-p --package)'{-p+,--package=}'[specify package to document]:package:_cargo_package_names' \
140                        '--release[build artifacts in release mode, with optimizations]' \
141                        '--ignore-rust-version[Ignore rust-version specification in packages]'
142                        ;;
143
144                fetch)
145                    _arguments -s -S $common $triple $manifest
146                        ;;
147
148                fix)
149                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
150                        "${command_scope_spec[@]}" \
151                        '--broken-code[fix code even if it already has compiler errors]' \
152                        '--edition[fix in preparation for the next edition]' \
153                        '--edition-idioms[fix warnings to migrate to the idioms of an edition]' \
154                        '--allow-no-vcs[fix code even if a VCS was not detected]' \
155                        '--allow-dirty[fix code even if the working directory is dirty]' \
156                        '--allow-staged[fix code even if the working directory has staged changes]' \
157                        '--ignore-rust-version[Ignore rust-version specification in packages]'
158                ;;
159
160                generate-lockfile)
161                    _arguments -s -S $common $manifest
162                        ;;
163
164                help)
165                    _cargo_cmds
166                        ;;
167                info)
168                    _arguments -s -A "^--" $common $registry \
169                        '--index=[specify registry index]:index' \
170                        '*: :_guard "^-*" "crate"'
171                        ;;
172
173                init)
174                    _arguments -s -S $common $registry \
175                        '--lib[use library template]' \
176                        '--edition=[specify edition to set for the crate generated]:edition:(2015 2018 2021)' \
177                        '--vcs=[initialize a new repo with a given VCS]:vcs:(git hg pijul fossil none)' \
178                        '--name=[set the resulting package name]:name' \
179                        '1:path:_directories'
180                        ;;
181
182                install)
183                    _arguments -s -S $common $parallel $features $triple $registry \
184                        '(-f --force)'{-f,--force}'[force overwriting of existing crates or binaries]' \
185                        '--bin=[only install the specified binary]:binary' \
186                        '--branch=[branch to use when installing from git]:branch' \
187                        '--debug[Build in debug mode (with the "dev" profile) instead of release mode]' \
188                        '--example=[install the specified example instead of binaries]:example:_cargo_example_names' \
189                        '--git=[specify URL from which to install the crate]:url:_urls' \
190                        '--path=[local filesystem path to crate to install]: :_directories' \
191                        '--rev=[specific commit to use when installing from git]:commit' \
192                        '--root=[directory to install packages into]: :_directories' \
193                        '--tag=[tag to use when installing from git]:tag' \
194                        '--version=[version to install from crates.io]:version' \
195                        '--list[list all installed packages and their versions]' \
196                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
197                        '*: :_guard "^-*" "crate"'
198                        ;;
199
200                locate-project)
201                    _arguments -s -S $common $manifest \
202                        '--message-format=[specify output representation]:output representation [json]:(json plain)' \
203                        '--workspace[locate Cargo.toml of the workspace root]'
204                        ;;
205
206                login)
207                    _arguments -s -S $common $registry \
208                        '*: :_guard "^-*" "token"'
209                        ;;
210
211                metadata)
212                    _arguments -s -S $common $features $manifest \
213                        "--no-deps[output information only about the root package and don't fetch dependencies]" \
214                        '--format-version=[specify format version]:version [1]:(1)'
215                        ;;
216
217                new)
218                    _arguments -s -S $common $registry \
219                        '--lib[use library template]' \
220                        '--vcs:initialize a new repo with a given VCS:(git hg none)' \
221                        '--name=[set the resulting package name]'
222                        ;;
223
224                owner)
225                    _arguments -s -S $common $registry \
226                        '(-a --add)'{-a,--add}'[specify name of a user or team to invite as an owner]:name' \
227                        '--index=[specify registry index]:index' \
228                        '(-l --list)'{-l,--list}'[list owners of a crate]' \
229                        '(-r --remove)'{-r,--remove}'[specify name of a user or team to remove as an owner]:name' \
230                        '--token=[specify API token to use when authenticating]:token' \
231                        '*: :_guard "^-*" "crate"'
232                        ;;
233
234                package)
235                    _arguments -s -S $common $parallel $features $triple $target $manifest $registry \
236                        '--index=[specify registry index]:index' \
237                        '(-l --list)'{-l,--list}'[print files included in a package without making one]' \
238                        '--no-metadata[ignore warnings about a lack of human-usable metadata]' \
239                        '--allow-dirty[allow dirty working directories to be packaged]' \
240                        "--no-verify[don't build to verify contents]"
241                        ;;
242
243                pkgid)
244                    _arguments -s -S $common $manifest \
245                        '(-p --package)'{-p+,--package=}'[specify package to get ID specifier for]:package:_cargo_package_names' \
246                        '*: :_guard "^-*" "spec"'
247                        ;;
248
249                publish)
250                    _arguments -s -S $common $parallel $features $triple $target $manifest $registry \
251                        '--index=[specify registry index]:index' \
252                        '--allow-dirty[allow dirty working directories to be packaged]' \
253                        "--no-verify[don't verify the contents by building them]" \
254                        '--token=[specify token to use when uploading]:token' \
255                        '--dry-run[perform all checks without uploading]'
256                        ;;
257
258                remove | rm)
259                    _arguments -s -A "^--" $common $manifest \
260                        "--dev[remove as a dev dependency]" \
261                        "--build[remove as a build dependency]" \
262                        "--target=[remove as a dependency from the given target platform]" \
263                        "--dry-run[don't actually write the manifest]" \
264                        '(-p --package)'{-p+,--package=}'[package to remove from]:package:_cargo_package_names' \
265                        '1: :_guard "^-*" "crate name"' \
266                        '*:args:_default'
267                        ;;
268
269                run | r)
270                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
271                        '--example=[name of the bin target]:name:_cargo_example_names' \
272                        '--bin=[name of the bin target]:name' \
273                        '(-p --package)'{-p+,--package=}'[specify package with the target to run]:package:_cargo_package_names' \
274                        '--release[build in release mode]' \
275                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
276                        '*: :_default'
277                        ;;
278
279                rustc)
280                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
281                        '(-p --package)'{-p+,--package=}'[specify package to build]:package:_cargo_package_names' \
282                        '--profile=[specify profile to build the selected target for]:profile' \
283                        '--release[build artifacts in release mode, with optimizations]' \
284                        "${command_scope_spec[@]}" \
285                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
286                        '*: : _dispatch rustc rustc -default-'
287                        ;;
288
289                rustdoc)
290                    _arguments -s -S $common $parallel $features $msgfmt $triple $target $manifest \
291                        '--document-private-items[include non-public items in the documentation]' \
292                        '--open[open the docs in a browser after the operation]' \
293                        '(-p --package)'{-p+,--package=}'[specify package to document]:package:_cargo_package_names' \
294                        '--release[build artifacts in release mode, with optimizations]' \
295                        "${command_scope_spec[@]}" \
296                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
297                        '*: : _dispatch rustdoc rustdoc -default-'
298                        ;;
299
300                search)
301                    _arguments -s -S $common $registry \
302                        '--index=[specify registry index]:index' \
303                        '--limit=[limit the number of results]:results [10]' \
304                        '*: :_guard "^-*" "query"'
305                        ;;
306
307                test | t)
308                    _arguments -s -S $common $jobs $features $msgfmt $triple $target $manifest \
309                        '--test=[test name]: :_cargo_test_names' \
310                        '--no-fail-fast[run all tests regardless of failure]' \
311                        '--no-run[compile but do not run]' \
312                        '(-p --package)'{-p+,--package=}'[package to run tests for]:package:_cargo_package_names' \
313                        '--all[test all packages in the workspace]' \
314                        '--release[build artifacts in release mode, with optimizations]' \
315                        '1: :_cargo_test_names' \
316                        '(--doc --bin --example --test --bench)--lib[only test library]' \
317                        '(--lib --bin --example --test --bench)--doc[only test documentation]' \
318                        '(--lib --doc --example --test --bench)--bin=[binary name]' \
319                        '(--lib --doc --bin --test --bench)--example=[example name]:_cargo_example_names' \
320                        '(--lib --doc --bin --example --bench)--test=[test name]' \
321                        '(--lib --doc --bin --example --test)--bench=[benchmark name]' \
322                        '--ignore-rust-version[Ignore rust-version specification in packages]' \
323                        '*: :_default'
324                        ;;
325
326                tree)
327                    _arguments -s -S $common $features $triple $manifest \
328                        '(-p --package)'{-p+,--package=}'[package to use as the root]:package:_cargo_package_names' \
329                        '(-i --invert)'{-i+,--invert=}'[invert the tree for the given package]:package:_cargo_package_names' \
330                        '--prefix=[line prefix]:prefix:(depth indent none)' \
331                        '--no-dedupe[repeat shared dependencies]' \
332                        '(-d --duplicates)'{-d,--duplicates}'[packages with multiple versions]' \
333                        '--charset=[utf8 or ascii]:charset:(utf8 ascii)' \
334                        '(-f --format)'{-f,--format=}'[format string]:format' \
335                        '(-e --edges)'{-e,--edges=}'[edge kinds]:kind:(features normal build dev all no-dev no-build no-normal)' \
336                        ;;
337
338                uninstall)
339                    _arguments -s -S $common \
340                        '(-p --package)'{-p+,--package=}'[specify package to uninstall]:package:_cargo_package_names' \
341                        '--bin=[only uninstall the specified binary]:name' \
342                        '--root=[directory to uninstall packages from]: :_files -/' \
343                        '*:crate:_cargo_installed_crates -F line'
344                        ;;
345
346                update)
347                    _arguments -s -S $common $manifest \
348                        '--aggressive=[force dependency update]' \
349                        '--recursive=[force dependency update]' \
350                        "--dry-run[don't actually write the lockfile]" \
351                        '(-p --package)'{-p+,--package=}'[specify package to update]:package:_cargo_package_names' \
352                        '--precise=[update single dependency to precise release]:release' \
353                        '*:package:_cargo_package_names'
354                        ;;
355
356                version)
357                    _arguments -s -S $common
358                        ;;
359
360                yank)
361                    _arguments -s -S $common $registry \
362                        '--version=[specify yank version]:version' \
363                        '--undo[undo a yank, putting a version back into the index]' \
364                        '--index=[specify registry index to yank from]:registry index' \
365                        '--token=[specify API token to use when authenticating]:token' \
366                        '*: :_guard "^-*" "crate"'
367                        ;;
368                *)
369                    # allow plugins to define their own functions
370                    if ! _call_function ret _cargo-${words[1]}; then
371                        # fallback on default completion for unknown commands
372                        _default && ret=0
373                    fi
374                    (( ! ret ))
375                ;;
376            esac
377            ;;
378    esac
379}
380
381_cargo_unstable_flags() {
382    local flags
383    flags=( help ${${${(M)${(f)"$(_call_program flags cargo -Z help)"}:#*--*}/ #-- #/:}##*-Z } )
384    _describe -t flags 'unstable flag' flags
385}
386
387_cargo_installed_crates() {
388    local expl
389    _description crates expl 'crate'
390    compadd "$@" "$expl[@]" - ${${${(f)"$(cargo install --list)"}:# *}%% *}
391}
392
393_cargo_cmds() {
394    local -a commands
395    # This uses Parameter Expansion Flags, which are a built-in Zsh feature.
396    # See more: http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
397    # and       http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion
398    #
399    # # How this work?
400    #
401    # First it splits the result of `cargo --list` at newline, then it removes the first line.
402    # Then it removes indentation (4 whitespaces) before each items. (Note the x## pattern [1]).
403    # Then it replaces those spaces between item and description with a `:`
404    #
405    # [1]: https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org#patterns
406    commands=( ${${${(M)"${(f)$(_call_program commands cargo --list)}":#    *}/ ##/}/ ##/:} )
407    _describe -t commands 'command' commands
408}
409
410_cargo_target_triple() {
411    local -a result
412
413    if (( $+commands[rustup] )); then
414        result=( ${(f)"$(rustup target list --installed)"} )
415    else
416        result=( ${(f)"$(rustc --print target-list)"} )
417    fi
418
419    _describe 'target triple' result
420}
421
422#FIXME: Disabled until fixed
423#gets package names from the manifest file
424_cargo_package_names() {
425    _message -e packages package
426}
427
428# Extracts the values of "name" from the array given in $1 and shows them as
429# command line options for completion
430_cargo_names_from_array() {
431    local manifest=$(cargo locate-project --message-format plain)
432    if [[ -z $manifest ]]; then
433        return 0
434    fi
435
436    local last_line
437    local -a names;
438    local in_block=false
439    local block_name=$1
440    names=()
441    while read -r line; do
442        if [[ $last_line == "[[$block_name]]" ]]; then
443            in_block=true
444        else
445            if [[ $last_line =~ '\s*\[\[.*' ]]; then
446                in_block=false
447            fi
448        fi
449
450        if [[ $in_block == true ]]; then
451            if [[ $line =~ '\s*name\s*=' ]]; then
452                regexp-replace line '^\s*name\s*=\s*|"' ''
453                names+=( "$line" )
454            fi
455        fi
456
457        last_line=$line
458    done < "$manifest"
459    _describe "$block_name" names
460
461}
462
463#Gets the test names from the manifest file
464_cargo_test_names() {
465    _cargo_names_from_array "test"
466}
467
468#Gets the bench names from the manifest file
469_cargo_benchmark_names() {
470    _cargo_names_from_array "bench"
471}
472
473_cargo_example_names() {
474    if [[ -d examples ]]; then
475        local -a files=(${(@f)$(echo examples/*.rs(:t:r))})
476        _values 'example' "${files[@]}"
477    fi
478}
479
480_cargo