From 7b81eb79b8d09f37fba8ffe13adcc0e5378273f9 Mon Sep 17 00:00:00 2001 From: Lorenzo Bolla Date: Fri, 15 Jul 2016 09:39:33 +0100 Subject: [PATCH 1/4] Disable autosuggest if buffer is too large Make buffer max size configurable, defaulted to infinity --- src/widgets.zsh | 4 +++- test/widgets/modify_test.zsh | 30 ++++++++++++++++++++++++++++++ zsh-autosuggestions.zsh | 4 +++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index ee1129f..89b9f2c 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_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_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..8ba5b4b 100644 --- a/test/widgets/modify_test.zsh +++ b/test/widgets/modify_test.zsh @@ -9,6 +9,7 @@ oneTimeSetUp() { setUp() { BUFFER='' POSTDISPLAY='' + ZSH_BUFFER_MAX_SIZE='' } tearDown() { @@ -42,6 +43,35 @@ testModify() { "$POSTDISPLAY" } +testModifyBufferTooLarge() { + + ZSH_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..ff201b9 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -243,7 +243,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_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_BUFFER_MAX_SIZE" ]; then + suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" + fi fi # Add the suggestion to the POSTDISPLAY From 2450c95d8a377192938bb49088b1cb74a3f41a5b Mon Sep 17 00:00:00 2001 From: Lorenzo Bolla Date: Mon, 18 Jul 2016 10:55:19 +0100 Subject: [PATCH 2/4] Rename and document new config var --- src/config.zsh | 3 +++ src/widgets.zsh | 2 +- test/widgets/modify_test.zsh | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) 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 89b9f2c..b2f8a0e 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -25,7 +25,7 @@ _zsh_autosuggest_modify() { # Get a new suggestion if the buffer is not empty after modification local suggestion if [ $#BUFFER -gt 0 ]; then - if [ -z "$ZSH_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_BUFFER_MAX_SIZE" ]; then + if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" fi fi diff --git a/test/widgets/modify_test.zsh b/test/widgets/modify_test.zsh index 8ba5b4b..7ed6346 100644 --- a/test/widgets/modify_test.zsh +++ b/test/widgets/modify_test.zsh @@ -9,7 +9,7 @@ oneTimeSetUp() { setUp() { BUFFER='' POSTDISPLAY='' - ZSH_BUFFER_MAX_SIZE='' + ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE='' } tearDown() { @@ -45,7 +45,7 @@ testModify() { testModifyBufferTooLarge() { - ZSH_BUFFER_MAX_SIZE='20' + ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE='20' stub_and_eval \ _zsh_autosuggest_invoke_original_widget \ From cdf56a330514cdff64fd4e09a737e9c4b93869d5 Mon Sep 17 00:00:00 2001 From: Lorenzo Bolla Date: Mon, 18 Jul 2016 10:56:21 +0100 Subject: [PATCH 3/4] Include result of `make` --- zsh-autosuggestions.zsh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index ff201b9..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,7 @@ _zsh_autosuggest_modify() { # Get a new suggestion if the buffer is not empty after modification local suggestion if [ $#BUFFER -gt 0 ]; then - if [ -z "$ZSH_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_BUFFER_MAX_SIZE" ]; then + if [ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" -o $#BUFFER -lt "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]; then suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" fi fi From a9c8efa04881395836f283f62091791280d44a26 Mon Sep 17 00:00:00 2001 From: Lorenzo Bolla Date: Mon, 18 Jul 2016 16:06:50 +0100 Subject: [PATCH 4/4] Update README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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`: