Browse Source

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.
master
Indrajit Raychaudhuri 4 years ago committed by Indrajit Raychaudhuri
parent
commit
6af0ab58c7
  1. 5
      modules/python/README.md
  2. 19
      modules/python/functions/_pip
  3. 32
      modules/python/init.zsh

5
modules/python/README.md

@ -2,6 +2,9 @@
Enables local Python and local Python package installation. Enables local Python and local Python package installation.
This module must be loaded _before_ the _`completion`_ module so that the
provided completion definitions are loaded.
## Settings ## Settings
This module supports virtual environments from conda and This module supports virtual environments from conda and
@ -143,7 +146,7 @@ Then add `$python_info[virtualenv]` to `$PROMPT` or `$RPROMPT` and call
Similarly, you can use `:prezto:module:python:info:version:format` with `%v` for Similarly, you can use `:prezto:module:python:info:version:format` with `%v` for
the version and add `$python_info[version]` to your prompt for the current the version and add `$python_info[version]` to your prompt for the current
python version/ python version.
## Authors ## Authors

19
modules/python/functions/_pip

@ -0,0 +1,19 @@
#compdef -P pip[0-9.]#
#autoload
#
# Pip completion, delegating to pip to do all the completion work.
#
# Authors:
# Indrajit Raychaudhuri <irc@indrajit.com>
#
if (( $+commands[$words[1]] )); then
function _pip_completion {
compadd -- $( COMP_WORDS="$words[*]" COMP_CWORD=$(( CURRENT - 1 )) \
PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
}
_pip_completion "$@"
fi

32
modules/python/init.zsh

@ -5,6 +5,7 @@
# Sorin Ionescu <sorin.ionescu@gmail.com> # Sorin Ionescu <sorin.ionescu@gmail.com>
# Sebastian Wiesner <lunaryorn@googlemail.com> # Sebastian Wiesner <lunaryorn@googlemail.com>
# Patrick Bos <egpbos@gmail.com> # Patrick Bos <egpbos@gmail.com>
# Indrajit Raychaudhuri <irc@indrajit.com>
# #
# Load dependencies. # Load dependencies.
@ -133,37 +134,6 @@ if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) \
unset pyenv_plugins unset pyenv_plugins
fi 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. # Load conda into the shell session, if requested.
zstyle -T ':prezto:module:python' conda-init zstyle -T ':prezto:module:python' conda-init
if (( $? && $+commands[conda] )); then if (( $? && $+commands[conda] )); then

Loading…
Cancel
Save