1
0
mirror of https://github.com/dcarrillo/prezto.git synced 2025-10-14 15:49:09 +00:00

python: Optimize completion for loading lazily on demand

Move `pip` to separate completion definition delegating to pip to do
all the completion work.

Since the completion is loaded lazily on-demand, it avoids the
performance overhead during Zsh initialization.

Implementation note:
The helper function `_pip_completion` implementation is based on the
official pip completion function (which can be generated with
`pip completion --zsh`) adhering to the newer compsys style.

See: 'man zshcompsys' for more details.
This commit is contained in:
Indrajit Raychaudhuri
2020-11-30 23:55:27 -06:00
committed by Indrajit Raychaudhuri
parent d686da3c03
commit f0942e6dda
3 changed files with 24 additions and 32 deletions

View File

@@ -5,6 +5,7 @@
# Sorin Ionescu <sorin.ionescu@gmail.com>
# Sebastian Wiesner <lunaryorn@googlemail.com>
# Patrick Bos <egpbos@gmail.com>
# Indrajit Raychaudhuri <irc@indrajit.com>
#
# Load dependencies.
@@ -133,37 +134,6 @@ if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) \
unset pyenv_plugins
fi
# Load PIP completion.
# Detect and use one available from among 'pip', 'pip2', 'pip3' variants
if [[ -n "$PYENV_ROOT" ]]; then
for pip in pip{,2,3}; do
pip_command="$(pyenv which "$pip" 2> /dev/null)"
[[ -n "$pip_command" ]] && break
done
unset pip
else
pip_command="$commands[(i)pip(|[23])]"
fi
if [[ -n "$pip_command" ]]; then
cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/prezto/pip-cache.zsh"
if [[ "$pip_command" -nt "$cache_file" \
|| "${ZDOTDIR:-$HOME}/.zpreztorc" -nt "$cache_file" \
|| ! -s "$cache_file" ]]; then
mkdir -p "$cache_file:h"
# pip is slow; cache its output. And also support 'pip2', 'pip3' variants
"$pip_command" completion --zsh \
| sed -e "s/\(compctl -K [-_[:alnum:]]* pip\).*/\1{,2,3}{,.{0..9}}/" \
>! "$cache_file" \
2> /dev/null
fi
source "$cache_file"
unset cache_file
fi
unset pip_command
# Load conda into the shell session, if requested.
zstyle -T ':prezto:module:python' conda-init
if (( $? && $+commands[conda] )); then