Skip to content

Commit

Permalink
Merge pull request #178 from lbolla/issue141-develop
Browse files Browse the repository at this point in the history
Disable autosuggest if buffer is too large
  • Loading branch information
ericfreese committed Jul 18, 2016
2 parents 63816c5 + a9c8efa commit 4723946
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
### 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
This plugin provides three widgets that you can use with `bindkey`:
Expand Down
3 changes: 3 additions & 0 deletions src/config.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
vi-forward-blank-word
vi-forward-blank-word-end
)

# Max size of buffer to trigger autosuggestion. Leave undefined for no upper bound.
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
4 changes: 3 additions & 1 deletion src/widgets.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ _zsh_autosuggest_modify() {
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi
fi

# Add the suggestion to the POSTDISPLAY
Expand Down
30 changes: 30 additions & 0 deletions test/widgets/modify_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ oneTimeSetUp() {
setUp() {
BUFFER=''
POSTDISPLAY=''
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=''
}

tearDown() {
Expand Down Expand Up @@ -42,6 +43,35 @@ testModify() {
"$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() {
stub_and_eval \
_zsh_autosuggest_invoke_original_widget \
Expand Down
7 changes: 6 additions & 1 deletion zsh-autosuggestions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
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 #
#--------------------------------------------------------------------#
Expand Down Expand Up @@ -243,7 +246,9 @@ _zsh_autosuggest_modify() {
# Get a new suggestion if the buffer is not empty after modification
local suggestion
if [ $#BUFFER -gt 0 ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then
suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
fi
fi

# Add the suggestion to the POSTDISPLAY
Expand Down

0 comments on commit 4723946

Please sign in to comment.