Merge pull request #350 from zsh-users/features/improved-completion-suggestions

Improve completion suggestions
pull/359/head
Eric Freese 7 years ago committed by GitHub
commit 106bf02d49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,76 +2,61 @@
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Completion Suggestion Strategy # # Completion Suggestion Strategy #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Fetches suggestions from zsh's completion engine # Fetches a suggestion from the completion engine
# Based on https://github.com/Valodim/zsh-capture-completion
# #
_zsh_autosuggest_capture_setup() { _zsh_autosuggest_capture_postcompletion() {
zmodload zsh/zutil # For `zparseopts` # Always insert the first completion into the buffer
compstate[insert]=1
# Ensure completions have been initialized
if ! whence compdef >/dev/null; then
autoload -Uz compinit && compinit
fi
# There is a bug in zpty module (fixed in zsh/master) by which a
# zpty that exits will kill all zpty processes that were forked
# before it. Here we set up a zsh exit hook to SIGKILL the zpty
# process immediately, before it has a chance to kill any other
# zpty processes.
zshexit() {
kill -KILL $$
sleep 1 # Block for long enough for the signal to come through
}
# Never group stuff! # Don't list completions
zstyle ':completion:*' list-grouped false unset compstate[list]
}
# No list separator, this saves some stripping later on _zsh_autosuggest_capture_completion_widget() {
zstyle ':completion:*' list-separator '' local -a +h comppostfuncs
comppostfuncs=(_zsh_autosuggest_capture_postcompletion)
# Override compadd (this is our hook) # Only capture completions at the end of the buffer
compadd () { CURSOR=$#BUFFER
setopt localoptions norcexpandparam
# Just delegate and leave if any of -O, -A or -D are given # Run the original widget wrapping `.complete-word` so we don't
if [[ ${@[1,(i)(-|--)]} == *-(O|A|D)\ * ]]; then # recursively try to fetch suggestions, since our pty is forked
builtin compadd "$@" # after autosuggestions is initialized.
return $? zle -- ${(k)widgets[(r)completion:.complete-word:_main_complete]}
fi
# Capture completions by injecting -A parameter into the compadd call. # The completion has been added, print the buffer as the suggestion
# This takes care of matching for us. echo -nE - $'\0'$BUFFER$'\0'
typeset -a __hits }
builtin compadd -A __hits "$@"
# Exit if no completion results zle -N autosuggest-capture-completion _zsh_autosuggest_capture_completion_widget
[[ -n $__hits ]] || return
# Extract prefixes and suffixes from compadd call. we can't do zsh's cool _zsh_autosuggest_capture_setup() {
# -r remove-func magic, but it's better than nothing. # There is a bug in zpty module in older zsh versions by which a
typeset -A apre hpre hsuf asuf # zpty that exits will kill all zpty processes that were forked
zparseopts -E P:=apre p:=hpre S:=asuf s:=hsuf # before it. Here we set up a zsh exit hook to SIGKILL the zpty
# process immediately, before it has a chance to kill any other
# zpty processes.
if ! is-at-least 5.4; then
zshexit() {
kill -KILL $$
sleep 1 # Block for long enough for the signal to come through
}
fi
# Print the first match bindkey '^I' autosuggest-capture-completion
echo -nE - $'\0'$IPREFIX$apre$hpre$__hits[1]$dsuf$hsuf$asuf$'\0'
}
} }
_zsh_autosuggest_capture_widget() { _zsh_autosuggest_capture_completion_sync() {
_zsh_autosuggest_capture_setup _zsh_autosuggest_capture_setup
zle complete-word zle autosuggest-capture-completion
} }
zle -N autosuggest-capture-completion _zsh_autosuggest_capture_widget _zsh_autosuggest_capture_completion_async() {
_zsh_autosuggest_capture_buffer() {
local BUFFERCONTENT="$1"
_zsh_autosuggest_capture_setup _zsh_autosuggest_capture_setup
zmodload zsh/parameter # For `$functions` zmodload zsh/parameter 2>/dev/null || return # For `$functions`
# Make vared completion work as if for a normal command line # Make vared completion work as if for a normal command line
# https://stackoverflow.com/a/7057118/154703 # https://stackoverflow.com/a/7057118/154703
@ -83,46 +68,37 @@ _zsh_autosuggest_capture_buffer() {
} }
# Open zle with buffer set so we can capture completions for it # Open zle with buffer set so we can capture completions for it
vared BUFFERCONTENT vared 1
} }
_zsh_autosuggest_capture_completion() { _zsh_autosuggest_strategy_completion() {
zmodload zsh/zpty 2>/dev/null || return typeset -g suggestion
typeset -g completion
local line REPLY local line REPLY
# Exit if we don't have completions
whence compdef >/dev/null || return
# Exit if we don't have zpty
zmodload zsh/zpty 2>/dev/null || return
# Zle will be inactive if we are in async mode # Zle will be inactive if we are in async mode
if zle; then if zle; then
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME zle autosuggest-capture-completion zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync
else else
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_buffer "\$1" zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_async "\$1"
zpty -w $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t' zpty -w $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t'
fi fi
# The completion result is surrounded by null bytes, so read the {
# content between the first two null bytes. # The completion result is surrounded by null bytes, so read the
zpty -r $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME line '*'$'\0''*'$'\0' # content between the first two null bytes.
zpty -r $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME line '*'$'\0''*'$'\0'
# On older versions of zsh, we sometimes get extra bytes after the
# second null byte, so trim those off the end # On older versions of zsh, we sometimes get extra bytes after the
completion="${${${(M)line:#*$'\0'*$'\0'*}#*$'\0'}%%$'\0'*}" # second null byte, so trim those off the end
suggestion="${${${(M)line:#*$'\0'*$'\0'*}#*$'\0'}%%$'\0'*}"
# Destroy the pty } always {
zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME # Destroy the pty
} zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME
}
_zsh_autosuggest_strategy_completion() {
typeset -g suggestion
local completion
# Fetch the first completion result
_zsh_autosuggest_capture_completion "$1"
[[ -z "$completion" ]] && return
# Add the completion string to the buffer to build the full suggestion
local -i i=1
while [[ "$completion" != "${1[$i,-1]}"* ]]; do ((i++)); done
suggestion="${1[1,$i-1]}$completion"
} }

@ -472,76 +472,61 @@ zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Completion Suggestion Strategy # # Completion Suggestion Strategy #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Fetches suggestions from zsh's completion engine # Fetches a suggestion from the completion engine
# Based on https://github.com/Valodim/zsh-capture-completion
# #
_zsh_autosuggest_capture_setup() { _zsh_autosuggest_capture_postcompletion() {
zmodload zsh/zutil # For `zparseopts` # Always insert the first completion into the buffer
compstate[insert]=1
# Ensure completions have been initialized
if ! whence compdef >/dev/null; then
autoload -Uz compinit && compinit
fi
# There is a bug in zpty module (fixed in zsh/master) by which a # Don't list completions
# zpty that exits will kill all zpty processes that were forked unset compstate[list]
# before it. Here we set up a zsh exit hook to SIGKILL the zpty }
# process immediately, before it has a chance to kill any other
# zpty processes.
zshexit() {
kill -KILL $$
sleep 1 # Block for long enough for the signal to come through
}
# Never group stuff! _zsh_autosuggest_capture_completion_widget() {
zstyle ':completion:*' list-grouped false local -a +h comppostfuncs
comppostfuncs=(_zsh_autosuggest_capture_postcompletion)
# No list separator, this saves some stripping later on # Only capture completions at the end of the buffer
zstyle ':completion:*' list-separator '' CURSOR=$#BUFFER
# Override compadd (this is our hook) # Run the original widget wrapping `.complete-word` so we don't
compadd () { # recursively try to fetch suggestions, since our pty is forked
setopt localoptions norcexpandparam # after autosuggestions is initialized.
zle -- ${(k)widgets[(r)completion:.complete-word:_main_complete]}
# Just delegate and leave if any of -O, -A or -D are given # The completion has been added, print the buffer as the suggestion
if [[ ${@[1,(i)(-|--)]} == *-(O|A|D)\ * ]]; then echo -nE - $'\0'$BUFFER$'\0'
builtin compadd "$@" }
return $?
fi
# Capture completions by injecting -A parameter into the compadd call.
# This takes care of matching for us.
typeset -a __hits
builtin compadd -A __hits "$@"
# Exit if no completion results zle -N autosuggest-capture-completion _zsh_autosuggest_capture_completion_widget
[[ -n $__hits ]] || return
# Extract prefixes and suffixes from compadd call. we can't do zsh's cool _zsh_autosuggest_capture_setup() {
# -r remove-func magic, but it's better than nothing. # There is a bug in zpty module in older zsh versions by which a
typeset -A apre hpre hsuf asuf # zpty that exits will kill all zpty processes that were forked
zparseopts -E P:=apre p:=hpre S:=asuf s:=hsuf # before it. Here we set up a zsh exit hook to SIGKILL the zpty
# process immediately, before it has a chance to kill any other
# zpty processes.
if ! is-at-least 5.4; then
zshexit() {
kill -KILL $$
sleep 1 # Block for long enough for the signal to come through
}
fi
# Print the first match bindkey '^I' autosuggest-capture-completion
echo -nE - $'\0'$IPREFIX$apre$hpre$__hits[1]$dsuf$hsuf$asuf$'\0'
}
} }
_zsh_autosuggest_capture_widget() { _zsh_autosuggest_capture_completion_sync() {
_zsh_autosuggest_capture_setup _zsh_autosuggest_capture_setup
zle complete-word zle autosuggest-capture-completion
} }
zle -N autosuggest-capture-completion _zsh_autosuggest_capture_widget _zsh_autosuggest_capture_completion_async() {
_zsh_autosuggest_capture_buffer() {
local BUFFERCONTENT="$1"
_zsh_autosuggest_capture_setup _zsh_autosuggest_capture_setup
zmodload zsh/parameter # For `$functions` zmodload zsh/parameter 2>/dev/null || return # For `$functions`
# Make vared completion work as if for a normal command line # Make vared completion work as if for a normal command line
# https://stackoverflow.com/a/7057118/154703 # https://stackoverflow.com/a/7057118/154703
@ -553,48 +538,39 @@ _zsh_autosuggest_capture_buffer() {
} }
# Open zle with buffer set so we can capture completions for it # Open zle with buffer set so we can capture completions for it
vared BUFFERCONTENT vared 1
} }
_zsh_autosuggest_capture_completion() { _zsh_autosuggest_strategy_completion() {
zmodload zsh/zpty 2>/dev/null || return typeset -g suggestion
typeset -g completion
local line REPLY local line REPLY
# Exit if we don't have completions
whence compdef >/dev/null || return
# Exit if we don't have zpty
zmodload zsh/zpty 2>/dev/null || return
# Zle will be inactive if we are in async mode # Zle will be inactive if we are in async mode
if zle; then if zle; then
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME zle autosuggest-capture-completion zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync
else else
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_buffer "\$1" zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_async "\$1"
zpty -w $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t' zpty -w $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t'
fi fi
# The completion result is surrounded by null bytes, so read the {
# content between the first two null bytes. # The completion result is surrounded by null bytes, so read the
zpty -r $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME line '*'$'\0''*'$'\0' # content between the first two null bytes.
zpty -r $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME line '*'$'\0''*'$'\0'
# On older versions of zsh, we sometimes get extra bytes after the
# second null byte, so trim those off the end # On older versions of zsh, we sometimes get extra bytes after the
completion="${${${(M)line:#*$'\0'*$'\0'*}#*$'\0'}%%$'\0'*}" # second null byte, so trim those off the end
suggestion="${${${(M)line:#*$'\0'*$'\0'*}#*$'\0'}%%$'\0'*}"
# Destroy the pty } always {
zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME # Destroy the pty
} zpty -d $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME
}
_zsh_autosuggest_strategy_completion() {
typeset -g suggestion
local completion
# Fetch the first completion result
_zsh_autosuggest_capture_completion "$1"
[[ -z "$completion" ]] && return
# Add the completion string to the buffer to build the full suggestion
local -i i=1
while [[ "$completion" != "${1[$i,-1]}"* ]]; do ((i++)); done
suggestion="${1[1,$i-1]}$completion"
} }
#--------------------------------------------------------------------# #--------------------------------------------------------------------#

Loading…
Cancel
Save