Skip to content

Commit 54d7a9a

Browse files
committed
cleanup: Switch to guard clause in accept widget handler
1 parent 5217ed5 commit 54d7a9a

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

src/widgets.zsh

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,23 @@ _zsh_autosuggest_accept() {
127127
max_cursor_pos=$((max_cursor_pos - 1))
128128
fi
129129

130+
if (( $CURSOR != $max_cursor_pos )); then
131+
_zsh_autosuggest_invoke_original_widget $@
132+
return
133+
fi
134+
130135
# Only accept if the cursor is at the end of the buffer
131-
if (( $CURSOR == $max_cursor_pos )); then
132-
# Add the suggestion to the buffer
133-
BUFFER="$BUFFER$POSTDISPLAY"
136+
# Add the suggestion to the buffer
137+
BUFFER="$BUFFER$POSTDISPLAY"
134138

135-
# Remove the suggestion
136-
unset POSTDISPLAY
139+
# Remove the suggestion
140+
unset POSTDISPLAY
137141

138-
# Move the cursor to the end of the buffer
139-
if [[ "$KEYMAP" = "vicmd" ]]; then
140-
CURSOR=$(($#BUFFER - 1))
141-
else
142-
CURSOR=$#BUFFER
143-
fi
142+
# Move the cursor to the end of the buffer
143+
if [[ "$KEYMAP" = "vicmd" ]]; then
144+
CURSOR=$(($#BUFFER - 1))
145+
else
146+
CURSOR=$#BUFFER
144147
fi
145148

146149
_zsh_autosuggest_invoke_original_widget $@

zsh-autosuggestions.zsh

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,20 +389,23 @@ _zsh_autosuggest_accept() {
389389
max_cursor_pos=$((max_cursor_pos - 1))
390390
fi
391391

392+
if (( $CURSOR != $max_cursor_pos )); then
393+
_zsh_autosuggest_invoke_original_widget $@
394+
return
395+
fi
396+
392397
# Only accept if the cursor is at the end of the buffer
393-
if (( $CURSOR == $max_cursor_pos )); then
394-
# Add the suggestion to the buffer
395-
BUFFER="$BUFFER$POSTDISPLAY"
398+
# Add the suggestion to the buffer
399+
BUFFER="$BUFFER$POSTDISPLAY"
396400

397-
# Remove the suggestion
398-
unset POSTDISPLAY
401+
# Remove the suggestion
402+
unset POSTDISPLAY
399403

400-
# Move the cursor to the end of the buffer
401-
if [[ "$KEYMAP" = "vicmd" ]]; then
402-
CURSOR=$(($#BUFFER - 1))
403-
else
404-
CURSOR=$#BUFFER
405-
fi
404+
# Move the cursor to the end of the buffer
405+
if [[ "$KEYMAP" = "vicmd" ]]; then
406+
CURSOR=$(($#BUFFER - 1))
407+
else
408+
CURSOR=$#BUFFER
406409
fi
407410

408411
_zsh_autosuggest_invoke_original_widget $@

0 commit comments

Comments
 (0)