a33ae92f4d78d4e4089d4d04a1d9f18bb11f39e1

Author
QuarticCat <QuarticCat@protonmail.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(rustup): generate completion file in the background (#10392)

Closes #10392

Diff

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