Skip to content

Commit e0b96e1

Browse files
committed
fix a bug in partial acceptance of suggestions
To reproduce: 1. Run `zsh -f`. 2. Run this: function bye() { BUFFER=bye } zle -N bye bindkey '^B' bye print -s 'hibye unexpected' source ~/zsh-autosuggestions/zsh-autosuggestions.zsh 3. Type `hi` and press Ctrl-B. Expected: POSTBUFFER is empty. Actual: POSTBUFFER is " unexpected".
1 parent ae315de commit e0b96e1

File tree

2 files changed

+6
-28
lines changed

2 files changed

+6
-28
lines changed

src/widgets.zsh

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,9 @@ _zsh_autosuggest_modify() {
6161
return $retval
6262
fi
6363

64-
# Optimize if manually typing in the suggestion
65-
if (( $#BUFFER > $#orig_buffer )); then
66-
local added=${BUFFER#$orig_buffer}
67-
68-
# If the string added matches the beginning of the postdisplay
69-
if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then
70-
POSTDISPLAY="${orig_postdisplay:$#added}"
71-
return $retval
72-
fi
73-
fi
74-
75-
# Don't fetch a new suggestion if the buffer hasn't changed
76-
if [[ "$BUFFER" = "$orig_buffer" ]]; then
77-
POSTDISPLAY="$orig_postdisplay"
64+
# Optimize if manually typing in the suggestion or if buffer hasn't changed
65+
if [[ "$BUFFER" = "$orig_buffer"* && "$orig_postdisplay" = "${BUFFER:$#orig_buffer}"* ]]; then
66+
POSTDISPLAY="${orig_postdisplay:$(($#BUFFER - $#orig_buffer))}"
7867
return $retval
7968
fi
8069

zsh-autosuggestions.zsh

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,9 @@ _zsh_autosuggest_modify() {
323323
return $retval
324324
fi
325325

326-
# Optimize if manually typing in the suggestion
327-
if (( $#BUFFER > $#orig_buffer )); then
328-
local added=${BUFFER#$orig_buffer}
329-
330-
# If the string added matches the beginning of the postdisplay
331-
if [[ "$added" = "${orig_postdisplay:0:$#added}" ]]; then
332-
POSTDISPLAY="${orig_postdisplay:$#added}"
333-
return $retval
334-
fi
335-
fi
336-
337-
# Don't fetch a new suggestion if the buffer hasn't changed
338-
if [[ "$BUFFER" = "$orig_buffer" ]]; then
339-
POSTDISPLAY="$orig_postdisplay"
326+
# Optimize if manually typing in the suggestion or if buffer hasn't changed
327+
if [[ "$BUFFER" = "$orig_buffer"* && "$orig_postdisplay" = "${BUFFER:$#orig_buffer}"* ]]; then
328+
POSTDISPLAY="${orig_postdisplay:$(($#BUFFER - $#orig_buffer))}"
340329
return $retval
341330
fi
342331

0 commit comments

Comments
 (0)