From 15c5db898fc2ecfe5b89baa1e88a856d7fb8d283 Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Mon, 14 Mar 2016 21:41:14 -0600 Subject: [PATCH] Fix #137 --- src/widgets.zsh | 10 +++++++- test/widgets/accept_test.zsh | 50 ++++++++++++++++++++++++++++++++++++ zsh-autosuggestions.zsh | 10 +++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 3c15788..be7d2e6 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -32,8 +32,16 @@ _zsh_autosuggest_modify() { # Accept the entire suggestion _zsh_autosuggest_accept() { + local -i max_cursor_pos=$#BUFFER + + # When vicmd keymap is active, the cursor can't move all the way + # to the end of the buffer + if [ "$KEYMAP" = "vicmd" ]; then + max_cursor_pos=$((max_cursor_pos - 1)) + fi + # Only accept if the cursor is at the end of the buffer - if [ $CURSOR -eq $#BUFFER ]; then + if [ $CURSOR -eq $max_cursor_pos ]; then # Add the suggestion to the buffer BUFFER="$BUFFER$POSTDISPLAY" diff --git a/test/widgets/accept_test.zsh b/test/widgets/accept_test.zsh index 571fb77..48bcf3c 100644 --- a/test/widgets/accept_test.zsh +++ b/test/widgets/accept_test.zsh @@ -54,6 +54,56 @@ testCursorNotAtEnd() { "$POSTDISPLAY" } +testViCursorAtEnd() { + BUFFER='echo' + POSTDISPLAY=' hello' + CURSOR=3 + KEYMAP='vicmd' + + stub _zsh_autosuggest_invoke_original_widget + + _zsh_autosuggest_accept 'original-widget' + + assertTrue \ + 'original widget not invoked' \ + 'stub_called _zsh_autosuggest_invoke_original_widget' + + assertEquals \ + 'BUFFER was not modified' \ + 'echo hello' \ + "$BUFFER" + + assertEquals \ + 'POSTDISPLAY was not cleared' \ + '' \ + "$POSTDISPLAY" +} + +testViCursorNotAtEnd() { + BUFFER='echo' + POSTDISPLAY=' hello' + CURSOR=2 + KEYMAP='vicmd' + + stub _zsh_autosuggest_invoke_original_widget + + _zsh_autosuggest_accept 'original-widget' + + assertTrue \ + 'original widget not invoked' \ + 'stub_called _zsh_autosuggest_invoke_original_widget' + + assertEquals \ + 'BUFFER was modified' \ + 'echo' \ + "$BUFFER" + + assertEquals \ + 'POSTDISPLAY was modified' \ + ' hello' \ + "$POSTDISPLAY" +} + testWidget() { stub _zsh_autosuggest_highlight_reset stub _zsh_autosuggest_accept diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index b79327d..b9c9eca 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -245,8 +245,16 @@ _zsh_autosuggest_modify() { # Accept the entire suggestion _zsh_autosuggest_accept() { + local -i max_cursor_pos=$#BUFFER + + # When vicmd keymap is active, the cursor can't move all the way + # to the end of the buffer + if [ "$KEYMAP" = "vicmd" ]; then + max_cursor_pos=$((max_cursor_pos - 1)) + fi + # Only accept if the cursor is at the end of the buffer - if [ $CURSOR -eq $#BUFFER ]; then + if [ $CURSOR -eq $max_cursor_pos ]; then # Add the suggestion to the buffer BUFFER="$BUFFER$POSTDISPLAY"