diff --git a/README.md b/README.md index f1a0d20..2ce6bd5 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/src/config.zsh b/src/config.zsh index 73d98fe..4353736 100644 --- a/src/config.zsh +++ b/src/config.zsh @@ -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= diff --git a/src/widgets.zsh b/src/widgets.zsh index ee1129f..b2f8a0e 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -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 diff --git a/test/widgets/modify_test.zsh b/test/widgets/modify_test.zsh index 4dfd30d..7ed6346 100644 --- a/test/widgets/modify_test.zsh +++ b/test/widgets/modify_test.zsh @@ -9,6 +9,7 @@ oneTimeSetUp() { setUp() { BUFFER='' POSTDISPLAY='' + ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE='' } tearDown() { @@ -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 \ diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 9f25514..5ebd993 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -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 # #--------------------------------------------------------------------# @@ -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