[Fix] Add autocompletion support to zsh
Fixes #1707.
parent
37e0977e60
commit
68fe26a5e4
|
@ -92,6 +92,7 @@ __nvm ()
|
||||||
# ZSH, load and run bashcompinit before calling the complete function.
|
# ZSH, load and run bashcompinit before calling the complete function.
|
||||||
if [[ -n ${ZSH_VERSION-} ]]; then
|
if [[ -n ${ZSH_VERSION-} ]]; then
|
||||||
autoload -U +X bashcompinit && bashcompinit
|
autoload -U +X bashcompinit && bashcompinit
|
||||||
|
autoload -U +X compinit && compinit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
complete -o default -F __nvm nvm
|
complete -o default -F __nvm nvm
|
||||||
|
|
|
@ -330,6 +330,8 @@ nvm_do_install() {
|
||||||
PROFILE_INSTALL_DIR="$(nvm_install_dir | command sed "s:^$HOME:\$HOME:")"
|
PROFILE_INSTALL_DIR="$(nvm_install_dir | command sed "s:^$HOME:\$HOME:")"
|
||||||
|
|
||||||
SOURCE_STR="\\nexport NVM_DIR=\"${PROFILE_INSTALL_DIR}\"\\n[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm\\n"
|
SOURCE_STR="\\nexport NVM_DIR=\"${PROFILE_INSTALL_DIR}\"\\n[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm\\n"
|
||||||
|
ZSH_COMPLETION_STR='# Load compinit/compdef\n autoload -U compinit\n compinit'
|
||||||
|
|
||||||
# shellcheck disable=SC2016
|
# shellcheck disable=SC2016
|
||||||
COMPLETION_STR='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion\n'
|
COMPLETION_STR='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion\n'
|
||||||
BASH_OR_ZSH=false
|
BASH_OR_ZSH=false
|
||||||
|
@ -358,6 +360,7 @@ nvm_do_install() {
|
||||||
# shellcheck disable=SC2016
|
# shellcheck disable=SC2016
|
||||||
if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then
|
if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then
|
||||||
echo "=> Appending bash_completion source string to $NVM_PROFILE"
|
echo "=> Appending bash_completion source string to $NVM_PROFILE"
|
||||||
|
command printf "$ZSH_COMPLETION_STR" >> "$NVM_PROFILE"
|
||||||
command printf "$COMPLETION_STR" >> "$NVM_PROFILE"
|
command printf "$COMPLETION_STR" >> "$NVM_PROFILE"
|
||||||
else
|
else
|
||||||
echo "=> bash_completion source string already in ${NVM_PROFILE}"
|
echo "=> bash_completion source string already in ${NVM_PROFILE}"
|
||||||
|
|
|
@ -14,6 +14,8 @@ cleanup () {
|
||||||
unset HOME
|
unset HOME
|
||||||
unset NVM_ENV
|
unset NVM_ENV
|
||||||
unset NVM_DETECT_PROFILE
|
unset NVM_DETECT_PROFILE
|
||||||
|
unset BASH_VERSION
|
||||||
|
unset ZSH_VERSION
|
||||||
unset -f setup cleanup die
|
unset -f setup cleanup die
|
||||||
rm -f ".bashrc" ".bash_profile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1
|
rm -f ".bashrc" ".bash_profile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1
|
||||||
}
|
}
|
||||||
|
@ -27,7 +29,7 @@ setup
|
||||||
#
|
#
|
||||||
|
|
||||||
# .bashrc should be detected for bash
|
# .bashrc should be detected for bash
|
||||||
NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; unset PROFILE; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
|
||||||
die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
|
die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
|
||||||
fi
|
fi
|
||||||
|
@ -39,7 +41,7 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# .zshrc should be detected for zsh
|
# .zshrc should be detected for zsh
|
||||||
NVM_DETECT_PROFILE="$(SHELL="/usr/bin/zsh"; unset PROFILE; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
|
||||||
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
|
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
|
||||||
fi
|
fi
|
||||||
|
@ -56,7 +58,7 @@ fi
|
||||||
#
|
#
|
||||||
|
|
||||||
# $PROFILE is a valid file
|
# $PROFILE is a valid file
|
||||||
NVM_DETECT_PROFILE="$(PROFILE="test_profile"; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(PROFILE="test_profile"; unset ZSH_VERSION; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
|
||||||
die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file"
|
die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file"
|
||||||
fi
|
fi
|
||||||
|
@ -75,35 +77,35 @@ fi
|
||||||
#
|
#
|
||||||
|
|
||||||
# It should favor .profile if file exists
|
# It should favor .profile if file exists
|
||||||
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
|
||||||
die "nvm_detect_profile should have selected .profile"
|
die "nvm_detect_profile should have selected .profile"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Otherwise, it should favor .bashrc if file exists
|
# Otherwise, it should favor .bashrc if file exists
|
||||||
rm ".profile"
|
rm ".profile"
|
||||||
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
|
||||||
die "nvm_detect_profile should have selected .bashrc"
|
die "nvm_detect_profile should have selected .bashrc"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Otherwise, it should favor .bash_profile if file exists
|
# Otherwise, it should favor .bash_profile if file exists
|
||||||
rm ".bashrc"
|
rm ".bashrc"
|
||||||
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
|
||||||
die "nvm_detect_profile should have selected .bash_profile"
|
die "nvm_detect_profile should have selected .bash_profile"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Otherwise, it should favor .zshrc if file exists
|
# Otherwise, it should favor .zshrc if file exists
|
||||||
rm ".bash_profile"
|
rm ".bash_profile"
|
||||||
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
|
||||||
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
|
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
|
||||||
die "nvm_detect_profile should have selected .zshrc"
|
die "nvm_detect_profile should have selected .zshrc"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# It should be empty if none is found
|
# It should be empty if none is found
|
||||||
rm ".zshrc"
|
rm ".zshrc"
|
||||||
NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
|
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
|
||||||
if [ ! -z "$NVM_DETECT_PROFILE" ]; then
|
if [ ! -z "$NVM_DETECT_PROFILE" ]; then
|
||||||
die "nvm_detect_profile should have returned an empty value"
|
die "nvm_detect_profile should have returned an empty value"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in New Issue