From 7026544a7dc4e9081d94ccb0ad2e5b99db46fc97 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Fri, 29 Apr 2016 14:46:24 -0600 Subject: [PATCH] Fix #141: Disable suggestions for widgets called from widgets --- src/widgets.zsh | 25 ++++++++++++++----------- test/widgets/accept_test.zsh | 8 ++++---- test/widgets/clear_test.zsh | 2 +- test/widgets/modify_test.zsh | 2 +- test/widgets/partial_accept_test.zsh | 4 ++-- zsh-autosuggestions.zsh | 23 +++++++++++++---------- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index ee1129f4..1ae42ccb 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -22,17 +22,20 @@ _zsh_autosuggest_modify() { _zsh_autosuggest_invoke_original_widget $@ retval=$? - # Get a new suggestion if the buffer is not empty after modification - local suggestion - if [ $#BUFFER -gt 0 ]; then - suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" - fi - - # Add the suggestion to the POSTDISPLAY - if [ -n "$suggestion" ]; then - POSTDISPLAY="${suggestion#$BUFFER}" - else - unset POSTDISPLAY + # Only fetch suggestions at the first level of widget recursion + if [ -z "${funcstack[(rn:2:)_zsh_autosuggest_widget_*]}" ]; then + # Get a new suggestion if the buffer is not empty after modification + local suggestion + if [ $#BUFFER -gt 0 ]; then + suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" + fi + + # Add the suggestion to the POSTDISPLAY + if [ -n "$suggestion" ]; then + POSTDISPLAY="${suggestion#$BUFFER}" + else + unset POSTDISPLAY + fi fi return $retval diff --git a/test/widgets/accept_test.zsh b/test/widgets/accept_test.zsh index f126091b..a0ae1670 100644 --- a/test/widgets/accept_test.zsh +++ b/test/widgets/accept_test.zsh @@ -24,7 +24,7 @@ testCursorAtEnd() { stub _zsh_autosuggest_invoke_original_widget - _zsh_autosuggest_accept 'original-widget' + _zsh_autosuggest_widget_accept 'original-widget' assertTrue \ 'original widget not invoked' \ @@ -48,7 +48,7 @@ testCursorNotAtEnd() { stub _zsh_autosuggest_invoke_original_widget - _zsh_autosuggest_accept 'original-widget' + _zsh_autosuggest_widget_accept 'original-widget' assertTrue \ 'original widget not invoked' \ @@ -73,7 +73,7 @@ testViCursorAtEnd() { stub _zsh_autosuggest_invoke_original_widget - _zsh_autosuggest_accept 'original-widget' + _zsh_autosuggest_widget_accept 'original-widget' assertTrue \ 'original widget not invoked' \ @@ -98,7 +98,7 @@ testViCursorNotAtEnd() { stub _zsh_autosuggest_invoke_original_widget - _zsh_autosuggest_accept 'original-widget' + _zsh_autosuggest_widget_accept 'original-widget' assertTrue \ 'original widget not invoked' \ diff --git a/test/widgets/clear_test.zsh b/test/widgets/clear_test.zsh index f0500c55..b901728d 100644 --- a/test/widgets/clear_test.zsh +++ b/test/widgets/clear_test.zsh @@ -19,7 +19,7 @@ testClear() { BUFFER='ec' POSTDISPLAY='ho hello' - _zsh_autosuggest_clear 'original-widget' + _zsh_autosuggest_widget_clear 'original-widget' assertEquals \ 'BUFFER was modified' \ diff --git a/test/widgets/modify_test.zsh b/test/widgets/modify_test.zsh index 4dfd30d5..88c2d4c7 100644 --- a/test/widgets/modify_test.zsh +++ b/test/widgets/modify_test.zsh @@ -25,7 +25,7 @@ testModify() { _zsh_autosuggest_suggestion \ 'echo hello' - _zsh_autosuggest_modify 'original-widget' + _zsh_autosuggest_widget_modify 'original-widget' assertTrue \ 'original widget not invoked' \ diff --git a/test/widgets/partial_accept_test.zsh b/test/widgets/partial_accept_test.zsh index 60c78a62..706819b2 100644 --- a/test/widgets/partial_accept_test.zsh +++ b/test/widgets/partial_accept_test.zsh @@ -25,7 +25,7 @@ testCursorMovesOutOfBuffer() { _zsh_autosuggest_invoke_original_widget \ 'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"' - _zsh_autosuggest_partial_accept 'original-widget' + _zsh_autosuggest_widget_partial_accept 'original-widget' assertTrue \ 'original widget not invoked' \ @@ -51,7 +51,7 @@ testCursorStaysInBuffer() { _zsh_autosuggest_invoke_original_widget \ 'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"' - _zsh_autosuggest_partial_accept 'original-widget' + _zsh_autosuggest_widget_partial_accept 'original-widget' assertTrue \ 'original widget not invoked' \ diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 65d2a176..7bfa230a 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -240,17 +240,20 @@ _zsh_autosuggest_modify() { _zsh_autosuggest_invoke_original_widget $@ retval=$? - # Get a new suggestion if the buffer is not empty after modification - local suggestion - if [ $#BUFFER -gt 0 ]; then - suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" - fi + # Only fetch suggestions at the first level of widget recursion + if [ -z "${funcstack[(rn:2:)_zsh_autosuggest_widget_*]}" ]; then + # Get a new suggestion if the buffer is not empty after modification + local suggestion + if [ $#BUFFER -gt 0 ]; then + suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")" + fi - # Add the suggestion to the POSTDISPLAY - if [ -n "$suggestion" ]; then - POSTDISPLAY="${suggestion#$BUFFER}" - else - unset POSTDISPLAY + # Add the suggestion to the POSTDISPLAY + if [ -n "$suggestion" ]; then + POSTDISPLAY="${suggestion#$BUFFER}" + else + unset POSTDISPLAY + fi fi return $retval