Merge pull request #330 from zsh-users/features/completion-suggestions
Features/completion suggestions
This commit is contained in:
@@ -11,7 +11,9 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
||||
# Prefix to use when saving original versions of bound widgets
|
||||
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||
|
||||
ZSH_AUTOSUGGEST_STRATEGY=default
|
||||
# Strategies to use to fetch a suggestion
|
||||
# Will try each strategy in order until a suggestion is returned
|
||||
ZSH_AUTOSUGGEST_STRATEGY=(history)
|
||||
|
||||
# Widgets that clear the suggestion
|
||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||
@@ -68,4 +70,7 @@ ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(
|
||||
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
|
||||
|
||||
# Pty name for calculating autosuggestions asynchronously
|
||||
ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_pty
|
||||
ZSH_AUTOSUGGEST_ASYNC_PTY_NAME=zsh_autosuggest_async_pty
|
||||
|
||||
# Pty name for capturing completions for completion suggestion strategy
|
||||
ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME=zsh_autosuggest_completion_pty
|
||||
|
||||
24
src/fetch.zsh
Normal file
24
src/fetch.zsh
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
# Fetch Suggestion #
|
||||
#--------------------------------------------------------------------#
|
||||
# Loops through all specified strategies and returns a suggestion
|
||||
# from the first strategy to provide one.
|
||||
#
|
||||
|
||||
_zsh_autosuggest_fetch_suggestion() {
|
||||
typeset -g suggestion
|
||||
local -a strategies
|
||||
local strategy
|
||||
|
||||
# Ensure we are working with an array
|
||||
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
|
||||
|
||||
for strategy in $strategies; do
|
||||
# Try to get a suggestion from this strategy
|
||||
_zsh_autosuggest_strategy_$strategy "$1"
|
||||
|
||||
# Break once we've found a suggestion
|
||||
[[ -n "$suggestion" ]] && break
|
||||
done
|
||||
}
|
||||
124
src/strategies/completion.zsh
Normal file
124
src/strategies/completion.zsh
Normal file
@@ -0,0 +1,124 @@
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
# Completion Suggestion Strategy #
|
||||
#--------------------------------------------------------------------#
|
||||
# Fetches suggestions from zsh's completion engine
|
||||
# Based on https://github.com/Valodim/zsh-capture-completion
|
||||
#
|
||||
|
||||
_zsh_autosuggest_capture_setup() {
|
||||
zmodload zsh/zutil # For `zparseopts`
|
||||
|
||||
# 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!
|
||||
zstyle ':completion:*' list-grouped false
|
||||
|
||||
# No list separator, this saves some stripping later on
|
||||
zstyle ':completion:*' list-separator ''
|
||||
|
||||
# Override compadd (this is our hook)
|
||||
compadd () {
|
||||
setopt localoptions norcexpandparam
|
||||
|
||||
# Just delegate and leave if any of -O, -A or -D are given
|
||||
if [[ ${@[1,(i)(-|--)]} == *-(O|A|D)\ * ]]; then
|
||||
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
|
||||
[[ -n $__hits ]] || return
|
||||
|
||||
# Extract prefixes and suffixes from compadd call. we can't do zsh's cool
|
||||
# -r remove-func magic, but it's better than nothing.
|
||||
typeset -A apre hpre hsuf asuf
|
||||
zparseopts -E P:=apre p:=hpre S:=asuf s:=hsuf
|
||||
|
||||
# Print the first match
|
||||
echo -nE - $'\0'$IPREFIX$apre$hpre$__hits[1]$dsuf$hsuf$asuf$'\0'
|
||||
}
|
||||
}
|
||||
|
||||
_zsh_autosuggest_capture_widget() {
|
||||
_zsh_autosuggest_capture_setup
|
||||
|
||||
zle complete-word
|
||||
}
|
||||
|
||||
zle -N autosuggest-capture-completion _zsh_autosuggest_capture_widget
|
||||
|
||||
_zsh_autosuggest_capture_buffer() {
|
||||
local BUFFERCONTENT="$1"
|
||||
|
||||
_zsh_autosuggest_capture_setup
|
||||
|
||||
zmodload zsh/parameter # For `$functions`
|
||||
|
||||
# Make vared completion work as if for a normal command line
|
||||
# https://stackoverflow.com/a/7057118/154703
|
||||
autoload +X _complete
|
||||
functions[_original_complete]=$functions[_complete]
|
||||
_complete () {
|
||||
unset 'compstate[vared]'
|
||||
_original_complete "$@"
|
||||
}
|
||||
|
||||
# Open zle with buffer set so we can capture completions for it
|
||||
vared BUFFERCONTENT
|
||||
}
|
||||
|
||||
_zsh_autosuggest_capture_completion() {
|
||||
typeset -g completion
|
||||
local line REPLY
|
||||
|
||||
# Zle will be inactive if we are in async mode
|
||||
if zle; then
|
||||
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME zle autosuggest-capture-completion
|
||||
else
|
||||
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_buffer "\$1"
|
||||
zpty -w $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME $'\t'
|
||||
fi
|
||||
|
||||
# The completion result is surrounded by null bytes, so read the
|
||||
# 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
|
||||
completion="${${${(M)line:#*$'\0'*$'\0'*}#*$'\0'}%%$'\0'*}"
|
||||
|
||||
# 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"
|
||||
|
||||
# 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"
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
# Default Suggestion Strategy #
|
||||
# History Suggestion Strategy #
|
||||
#--------------------------------------------------------------------#
|
||||
# Suggests the most recent history item that matches the given
|
||||
# prefix.
|
||||
#
|
||||
|
||||
_zsh_autosuggest_strategy_default() {
|
||||
_zsh_autosuggest_strategy_history() {
|
||||
# Reset options to defaults and enable LOCAL_OPTIONS
|
||||
emulate -L zsh
|
||||
|
||||
@@ -97,7 +97,7 @@ _zsh_autosuggest_fetch() {
|
||||
_zsh_autosuggest_async_request "$BUFFER"
|
||||
else
|
||||
local suggestion
|
||||
_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$BUFFER"
|
||||
_zsh_autosuggest_fetch_suggestion "$BUFFER"
|
||||
_zsh_autosuggest_suggest "$suggestion"
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user