5fbd4c83f6e70a66c555fc7ace7dda2c056b8663
- Author
- Marc Cornellà <hello@mcornella.com>
- Committer
- Marc Cornellà <hello@mcornella.com>
- Date
Message
refactor(cargo): generate completion file in the background
Diff
1diff --git a/plugins/cargo/cargo.plugin.zsh b/plugins/cargo/cargo.plugin.zsh
2index 7082d2d55712d6a4aafbd5c1e4700fab2d9b5928..9d1d237576ff000b8712933183d0b78246c5c380 100644
3--- a/plugins/cargo/cargo.plugin.zsh
4+++ b/plugins/cargo/cargo.plugin.zsh
5@@ -1,23 +1,24 @@
6-if (( $+commands[rustup] && $+commands[cargo] )); then
7- # remove old generated completion file
8- command rm -f "${0:A:h}/_cargo"
9-
10- # generate new completion file
11- ver="$(cargo --version)"
12- ver_file="$ZSH_CACHE_DIR/cargo_version"
13- comp_file="$ZSH_CACHE_DIR/completions/_cargo"
14+if ! (( $+commands[rustup] && $+commands[cargo] )); then
15+ return
16+fi
17
18- mkdir -p "${comp_file:h}"
19- (( ${fpath[(Ie)${comp_file:h}]} )) || fpath=("${comp_file:h}" $fpath)
20+# Remove old generated completion file
21+# TODO: 2021-09-15: remove this line
22+command rm -f "${0:A:h}/_cargo"
23
24- if [[ ! -f "$comp_file" || ! -f "$ver_file" || "$ver" != "$(< "$ver_file")" ]]; then
25- rustup completions zsh cargo >| "$comp_file"
26- echo "$ver" >| "$ver_file"
27- fi
28+# Add completions/ folder in $ZSH_CACHE_DIR
29+comp_file="$ZSH_CACHE_DIR/completions/_cargo"
30+command mkdir -p "${comp_file:h}"
31+(( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
32
33- declare -A _comps
34+# If the completion file doesn't exist yet, we need to autoload it and
35+# bind it to `cargo`. Otherwise, compinit will have already done that
36+if [[ ! -f "$comp_file" ]]; then
37 autoload -Uz _cargo
38+ declare -A _comps
39 _comps[cargo]=_cargo
40-
41- unset ver ver_file comp_file
42 fi
43+
44+# Generate completion file in the background
45+rustup completions zsh cargo >| "$comp_file" &|
46+unset comp_file