Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve zsh completion performance #7766

Merged
merged 1 commit into from Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 23 additions & 25 deletions completions/zsh/_brew
Expand Up @@ -45,20 +45,19 @@ __brew_completion_caching_policy() {
tmp=( $1(mw-2N) )
(( $#tmp )) || return 0

# otherwise, invalidate if latest tap index file is missing or newer than
# cache file
# otherwise, invalidate if latest tap index file is missing or newer than cache file
tmp=( ${HOMEBREW_REPOSITORY:-/usr/local/Homebrew}/Library/Taps/*/*/.git/index(om[1]N) )
[[ -z $tmp || $tmp -nt $1 ]]
}

__brew_formulae() {
local -a formulae
local -a list
local comp_cachename=brew_formulae
if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
formulae=($(brew search))
_store_cache $comp_cachename formulae
if ! _retrieve_cache $comp_cachename; then
list=( $(brew search) )
_store_cache $comp_cachename list
fi
_describe -t formulae 'all formulae' formulae
_describe -t formulae 'all formulae' list
}

__brew_installed_formulae() {
Expand Down Expand Up @@ -145,18 +144,17 @@ __brew_common_commands() {
}

__brew_all_commands() {
local -a commands
local -a list
local comp_cachename=brew_all_commands
if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
HOMEBREW_CACHE=$(brew --cache)
HOMEBREW_REPOSITORY=$(brew --repo)
[[ -f "$HOMEBREW_CACHE/all_commands_list.txt" ]] &&
commands=($(cat "$HOMEBREW_CACHE/all_commands_list.txt")) ||
commands=($(cat "$HOMEBREW_REPOSITORY/completions/internal_commands_list.txt"))
commands=(${commands:#*instal}) # Exclude instal, uninstal, etc.
_store_cache $comp_cachename commands
if ! _retrieve_cache $comp_cachename; then
local cache_dir=$(brew --cache)
[[ -f $cache_dir/all_commands_list.txt ]] &&
list=( $(<$cache_dir/all_commands_list.txt) ) ||
list=( $(<$(brew --repo)/completions/internal_commands_list.txt) )
list=( ${list:#*instal} ) # Exclude instal, uninstal, etc.
_store_cache $comp_cachename list
fi
_describe -t all-commands 'all commands' commands
_describe -t all-commands 'all commands' list
}

__brew_commands() {
Expand Down Expand Up @@ -857,10 +855,10 @@ _brew() {
case "$state" in
command)
# set default cache policy
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp
[[ -n $tmp ]] ||
zstyle ":completion:${curcontext%:*}:*" cache-policy \
__brew_completion_caching_policy
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp ||
zstyle ":completion:${curcontext%:*}:*" cache-policy __brew_completion_caching_policy
zstyle -s ":completion:${curcontext%:*}:*" use-cache tmp ||
zstyle ":completion:${curcontext%:*}:*" use-cache true

__brew_commands && return 0
;;
Expand All @@ -878,10 +876,10 @@ _brew() {

# set default cache policy (we repeat this dance because the context
# service differs from above)
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp
[[ -n $tmp ]] ||
zstyle ":completion:${curcontext%:*}:*" cache-policy \
__brew_completion_caching_policy
zstyle -s ":completion:${curcontext%:*}:*" cache-policy tmp ||
zstyle ":completion:${curcontext%:*}:*" cache-policy __brew_completion_caching_policy
zstyle -s ":completion:${curcontext%:*}:*" use-cache tmp ||
zstyle ":completion:${curcontext%:*}:*" use-cache true

# call completion for named command e.g. _brew_list
local completion_func="_brew_${command//-/_}"
Expand Down
2 changes: 1 addition & 1 deletion completions/zsh/_brew_cask
Expand Up @@ -21,7 +21,7 @@ __brew_all_casks() {
local expl
local comp_cachename=brew_casks

if _cache_invalid $comp_cachename || ! _retrieve_cache $comp_cachename; then
if ! _retrieve_cache $comp_cachename; then
list=( $(brew search --casks) )
_store_cache $comp_cachename list
fi
Expand Down