Merge pull request #178 from lbolla/issue141-develop

Disable autosuggest if buffer is too large
This commit is contained in:
Eric Freese 2016-07-18 10:28:45 -06:00 committed by GitHub
commit 472394681e
5 changed files with 48 additions and 2 deletions

View file

@ -86,6 +86,12 @@ Widgets not in any of these lists will update the suggestion when invoked.
**Note:** A widget shouldn't belong to more than one of the above arrays. **Note:** A widget shouldn't belong to more than one of the above arrays.
### Disabling suggestion for large buffers
Set `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` to an integer value to disable autosuggestion for large buffers. The default is unset, which means that autosuggestion will be tried for any buffer size. Recommended value is 20.
This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for too long strings.
### Key Bindings ### Key Bindings
This plugin provides three widgets that you can use with `bindkey`: This plugin provides three widgets that you can use with `bindkey`:

View file

@ -47,3 +47,6 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word vi-forward-blank-word
vi-forward-blank-word-end vi-forward-blank-word-end
) )
# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound.
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=

View file

@ -25,8 +25,10 @@ _zsh_autosuggest_modify() {
# Get a new suggestion if the buffer is not empty after modification # Get a new suggestion if the buffer is not empty after modification
local suggestion local suggestion
if [ $#BUFFER -gt 0 ]; then if [ $#BUFFER -gt 0 ]; then
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi fi
fi
# Add the suggestion to the POSTDISPLAY # Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then if [ -n "$suggestion" ]; then

View file

@ -9,6 +9,7 @@ oneTimeSetUp() {
setUp() { setUp() {
BUFFER='' BUFFER=''
POSTDISPLAY='' POSTDISPLAY=''
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=''
} }
tearDown() { tearDown() {
@ -42,6 +43,35 @@ testModify() {
"$POSTDISPLAY" "$POSTDISPLAY"
} }
testModifyBufferTooLarge() {
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE='20'
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
'BUFFER+="012345678901234567890"'
stub_and_echo \
_zsh_autosuggest_suggestion \
'012345678901234567890123456789'
_zsh_autosuggest_modify 'original-widget'
assertTrue \
'original widget not invoked' \
'stub_called _zsh_autosuggest_invoke_original_widget'
assertEquals \
'BUFFER was not modified' \
'012345678901234567890' \
"$BUFFER"
assertEquals \
'POSTDISPLAY does not contain suggestion' \
'' \
"$POSTDISPLAY"
}
testRetval() { testRetval() {
stub_and_eval \ stub_and_eval \
_zsh_autosuggest_invoke_original_widget \ _zsh_autosuggest_invoke_original_widget \

View file

@ -74,6 +74,9 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word-end vi-forward-blank-word-end
) )
# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound.
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Handle Deprecated Variables/Widgets # # Handle Deprecated Variables/Widgets #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
@ -243,8 +246,10 @@ _zsh_autosuggest_modify() {
# Get a new suggestion if the buffer is not empty after modification # Get a new suggestion if the buffer is not empty after modification
local suggestion local suggestion
if [ $#BUFFER -gt 0 ]; then if [ $#BUFFER -gt 0 ]; then
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi fi
fi
# Add the suggestion to the POSTDISPLAY # Add the suggestion to the POSTDISPLAY
if [ -n "$suggestion" ]; then if [ -n "$suggestion" ]; then