Skip to content

Commit

Permalink
Fix #141: Disable suggestions for widgets called from widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfreese committed Apr 29, 2016
1 parent e87bc74 commit 0d1f6e6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
34 changes: 23 additions & 11 deletions src/widgets.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#

# Helper to detect when we're at the first level of recursion to
# handle widgets that call other widgets (e.g. bracketed-paste-magic)
_zsh_autosuggest_is_at_first_level() {
first="${funcstack[(i)_zsh_autosuggest_widget_*]}"
last="${funcstack[(I)_zsh_autosuggest_widget_*]}"

[ "$first" = "$last" ]
}

# Clear the suggestion
_zsh_autosuggest_clear() {
# Remove the suggestion
Expand All @@ -22,17 +31,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 recursion
if _zsh_autosuggest_is_at_first_level; 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
Expand Down
8 changes: 4 additions & 4 deletions test/widgets/accept_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand All @@ -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' \
Expand All @@ -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' \
Expand All @@ -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' \
Expand Down
2 changes: 1 addition & 1 deletion test/widgets/clear_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down
2 changes: 1 addition & 1 deletion test/widgets/modify_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down
4 changes: 2 additions & 2 deletions test/widgets/partial_accept_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand All @@ -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' \
Expand Down
32 changes: 22 additions & 10 deletions zsh-autosuggestions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ _zsh_autosuggest_highlight_apply() {
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#

# Helper to detect when we're at the first level of recursion to
# handle widgets that call other widgets (e.g. bracketed-paste-magic)
_zsh_autosuggest_is_at_first_level() {
first="${funcstack[(i)_zsh_autosuggest_widget_*]}"
last="${funcstack[(I)_zsh_autosuggest_widget_*]}"

[ "$first" = "$last" ]
}

# Clear the suggestion
_zsh_autosuggest_clear() {
# Remove the suggestion
Expand All @@ -240,17 +249,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 recursion
if _zsh_autosuggest_is_at_first_level; 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
Expand Down

0 comments on commit 0d1f6e6

Please sign in to comment.