From 7b81eb79b8d09f37fba8ffe13adcc0e5378273f9 Mon Sep 17 00:00:00 2001 From: Lorenzo Bolla Date: Fri, 15 Jul 2016 09:39:33 +0100 Subject: [PATCH] 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