49bae43f6202e7e1171d8911d8b243714dfe888a

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

Message

refactor(plugins): generate completion functions in the background

Diff

  1diff --git a/plugins/cargo/cargo.plugin.zsh b/plugins/cargo/cargo.plugin.zsh
  2index 6651f64329b40347b77cc7ea175a4e46697afbdc..e4b338207429106b0110ac4d548c6659a588e361 100644
  3--- a/plugins/cargo/cargo.plugin.zsh
  4+++ b/plugins/cargo/cargo.plugin.zsh
  5@@ -7,7 +7,7 @@ print ${(%):-'Please update your .zshrc to use the `%Brust%b` plugin instead.%f'
  6 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  7 0="${${(M)0:#/*}:-$PWD/$0}"
  8 # Remove old generated completion file
  9-command rm -f "${0:A:h}/_cargo"
 10+command rm -f "${0:A:h}/_cargo" "$ZSH_CACHE_DIR/cargo_version"
 11 
 12 (( ${fpath[(Ie)$ZSH/plugins/rust]} )) || {
 13   fpath=("$ZSH/plugins/rust" $fpath)
 14diff --git a/plugins/deno/deno.plugin.zsh b/plugins/deno/deno.plugin.zsh
 15index 15fe15a7bbe0878964ff3ad8add4a19a891c7dea..77c2125d25411d76dfe15b106a38247526a2140f 100644
 16--- a/plugins/deno/deno.plugin.zsh
 17+++ b/plugins/deno/deno.plugin.zsh
 18@@ -12,30 +12,29 @@ alias dts='deno test'
 19 alias dup='deno upgrade'
 20 
 21 # COMPLETION FUNCTION
 22-if (( $+commands[deno] )); then
 23-  # Handle $0 according to the standard:
 24-  # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
 25-  0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
 26-  0="${${(M)0:#/*}:-$PWD/$0}"
 27-
 28-  # remove old generated completion file
 29-  command rm -f "${0:A:h}/_deno"
 30-
 31-  ver="$(deno --version)"
 32-  ver_file="$ZSH_CACHE_DIR/deno_version"
 33-  comp_file="$ZSH_CACHE_DIR/completions/_deno"
 34+if (( ! $+commands[deno] )); then
 35+  return
 36+fi
 37 
 38-  mkdir -p "${comp_file:h}"
 39-  (( ${fpath[(Ie)${comp_file:h}]} )) || fpath=("${comp_file:h}" $fpath)
 40+# TODO: 2021-12-28: remove this block
 41+# Handle $0 according to the standard:
 42+# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
 43+0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
 44+0="${${(M)0:#/*}:-$PWD/$0}"
 45+# Remove old generated files
 46+command rm -f "${0:A:h}/_deno" "$ZSH_CACHE_DIR/deno_version"
 47 
 48-  if [[ ! -f "$comp_file" || ! -f "$ver_file" || "$ver" != "$(< "$ver_file")" ]]; then
 49-    deno completions zsh >| "$comp_file"
 50-    echo "$ver" >| "$ver_file"
 51-  fi
 52+# TODO: 2021-12-28: remove this bit of code as it exists in oh-my-zsh.sh
 53+# Add completions folder in $ZSH_CACHE_DIR
 54+command mkdir -p "$ZSH_CACHE_DIR/completions"
 55+(( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
 56 
 57+# If the completion file doesn't exist yet, we need to autoload it and
 58+# bind it to `deno`. Otherwise, compinit will have already done that.
 59+if [[ ! -f "$ZSH_CACHE_DIR/completions/_deno" ]]; then
 60   declare -A _comps
 61   autoload -Uz _deno
 62   _comps[deno]=_deno
 63-
 64-  unset ver ver_file comp_file
 65 fi
 66+
 67+deno completions zsh >| "$ZSH_CACHE_DIR/completions/_deno" &|
 68diff --git a/plugins/fnm/fnm.plugin.zsh b/plugins/fnm/fnm.plugin.zsh
 69index 73e2b958a619b103d660172605385e28d651c738..e225887926c78abdb35f3636420e4eb40598a0fb 100644
 70--- a/plugins/fnm/fnm.plugin.zsh
 71+++ b/plugins/fnm/fnm.plugin.zsh
 72@@ -1,28 +1,26 @@
 73-if (( $+commands[fnm] )); then
 74-  # Handle $0 according to the standard:
 75-  # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
 76-  0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
 77-  0="${${(M)0:#/*}:-$PWD/$0}"
 78-
 79-  # remove old generated completion file
 80-  command rm -f "${0:A:h}/_fnm"
 81-
 82-  ver="$(fnm --version)"
 83-  ver_file="$ZSH_CACHE_DIR/fnm_version"
 84-  comp_file="$ZSH_CACHE_DIR/completions/_fnm"
 85+if (( ! $+commands[fnm] )); then
 86+  return
 87+fi
 88 
 89-  mkdir -p "${comp_file:h}"
 90-  (( ${fpath[(Ie)${comp_file:h}]} )) || fpath=("${comp_file:h}" $fpath)
 91+# TODO: 2021-12-28: remove this block
 92+# Handle $0 according to the standard:
 93+# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
 94+0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
 95+0="${${(M)0:#/*}:-$PWD/$0}"
 96+# remove old generated files
 97+command rm -f "${0:A:h}/_fnm" "$ZSH_CACHE_DIR/fnm_version"
 98 
 99-  if [[ ! -f "$comp_file" || ! -f "$ver_file" || "$ver" != "$(< "$ver_file")" ]]; then
100-    fnm completions --shell=zsh >| "$comp_file"
101-    echo "$ver" >| "$ver_file"
102-  fi
103+# TODO: 2021-12-28: remove this bit of code as it exists in oh-my-zsh.sh
104+# Add completions folder in $ZSH_CACHE_DIR
105+command mkdir -p "$ZSH_CACHE_DIR/completions"
106+(( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
107 
108+# If the completion file doesn't exist yet, we need to autoload it and
109+# bind it to `fnm`. Otherwise, compinit will have already done that.
110+if [[ ! -f "$ZSH_CACHE_DIR/completions/_fnm" ]]; then
111   declare -A _comps
112   autoload -Uz _fnm
113   _comps[fnm]=_fnm
114-
115-  unset ver ver_file comp_file
116 fi
117 
118+fnm completions --shell=zsh >| "$ZSH_CACHE_DIR/completions/_fnm" &|
119diff --git a/plugins/gh/gh.plugin.zsh b/plugins/gh/gh.plugin.zsh
120index 3092bfa4abedd00641f778b5b11bb83cad64f9ce..17995e1cf12c8ba5c0473d2812838ccc9391d968 100644
121--- a/plugins/gh/gh.plugin.zsh
122+++ b/plugins/gh/gh.plugin.zsh
123@@ -1,29 +1,27 @@
124 # Autocompletion for the GitHub CLI (gh).
125-if (( $+commands[gh] )); then
126-  # Handle $0 according to the standard:
127-  # # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
128-  0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
129-  0="${${(M)0:#/*}:-$PWD/$0}"
130-
131-  # remove old generated completion file
132-  command rm -f "${0:A:h}/_gh"
133-
134-  ver="$(gh --version)"
135-  ver_file="$ZSH_CACHE_DIR/gh_version"
136-  comp_file="$ZSH_CACHE_DIR/completions/_gh"
137+if (( ! $+commands[gh] )); then
138+  return
139+fi
140 
141-  mkdir -p "${comp_file:h}"
142-  (( ${fpath[(Ie)${comp_file:h}]} )) || fpath=("${comp_file:h}" $fpath)
143+# TODO: 2021-12-28: remove this block
144+# Handle $0 according to the standard:
145+# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
146+0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
147+0="${${(M)0:#/*}:-$PWD/$0}"
148+# Remove old generated files
149+command rm -f "${0:A:h}/_gh" "$ZSH_CACHE_DIR/gh_version"
150 
151-  if [[ ! -f "$comp_file" || ! -f "$ver_file" || "$ver" != "$(< "$ver_file")" ]]; then
152-    gh completion --shell zsh >| "$comp_file"
153-    echo "$ver" >| "$ver_file"
154-  fi
155+# TODO: 2021-12-28: remove this bit of code as it exists in oh-my-zsh.sh
156+# Add completions folder in $ZSH_CACHE_DIR
157+command mkdir -p "$ZSH_CACHE_DIR/completions"
158+(( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
159 
160+# If the completion file doesn't exist yet, we need to autoload it and
161+# bind it to `gh`. Otherwise, compinit will have already done that.
162+if [[ ! -f "$ZSH_CACHE_DIR/completions/_gh" ]]; then
163   declare -A _comps
164   autoload -Uz _gh
165   _comps[gh]=_gh
166-
167-  unset ver ver_file comp_file
168 fi
169 
170+gh completion --shell zsh >| "$ZSH_CACHE_DIR/completions/_gh" &|
171diff --git a/plugins/rustup/rustup.plugin.zsh b/plugins/rustup/rustup.plugin.zsh
172index d8225901bfee105ce3eb399d3795bbe6824abb2a..3d59c1c54604c4ddfd0b3c4dacf98855c8d7634e 100644
173--- a/plugins/rustup/rustup.plugin.zsh
174+++ b/plugins/rustup/rustup.plugin.zsh
175@@ -7,7 +7,7 @@ print ${(%):-'Please update your .zshrc to use the `%Brust%b` plugin instead.%f'
176 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
177 0="${${(M)0:#/*}:-$PWD/$0}"
178 # Remove old generated completion file
179-command rm -f "${0:A:h}/_rustup"
180+command rm -f "${0:A:h}/_rustup" "$ZSH_CACHE_DIR/rustup_version"
181 
182 (( ${fpath[(Ie)$ZSH/plugins/rust]} )) || {
183   fpath=("$ZSH/plugins/rust" $fpath)