diff --git a/spec/integrations/vicmd_partial_accept_spec.rb b/spec/integrations/vicmd_partial_accept_spec.rb new file mode 100644 index 0000000..e9c6973 --- /dev/null +++ b/spec/integrations/vicmd_partial_accept_spec.rb @@ -0,0 +1,13 @@ +describe 'a vicmd mode partial-accept widget' do + let(:widget) { 'vi-forward-word-end' } + + context 'in vicmd mode' do + it 'moves the cursor through suggestion as expected' do + session.run_command("bindkey s vi-cmd-mode") + with_history('foobar foo') do + session.send_string('fo').send_keys('s').send_keys('e').send_keys('a').send_string('plop') + wait_for { session.content }.to eq('foobarplop') + end + end + end +end diff --git a/src/widgets.zsh b/src/widgets.zsh index 8a31133..dfbf214 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -164,6 +164,13 @@ _zsh_autosuggest_partial_accept() { _zsh_autosuggest_invoke_original_widget $@ retval=$? + # In vicmd keymap, the cursor is placed on the current character + # instead of after it. We increment it by 1 to keep the following logic + # working even in thi case. + if [[ "$KEYMAP" = "vicmd" ]]; then + (( CURSOR++ )) + fi + # If we've moved past the end of the original buffer if (( $CURSOR > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor @@ -176,6 +183,11 @@ _zsh_autosuggest_partial_accept() { BUFFER="$original_buffer" fi + # Restore CURSOR + if [[ "$KEYMAP" = "vicmd" ]]; then + (( CURSOR-- )) + fi + return $retval } diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index a1037ba..f21f897 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -442,6 +442,13 @@ _zsh_autosuggest_partial_accept() { _zsh_autosuggest_invoke_original_widget $@ retval=$? + # In vicmd keymap, the cursor is placed on the current character + # instead of after it. We increment it by 1 to keep the following logic + # working even in thi case. + if [[ "$KEYMAP" = "vicmd" ]]; then + (( CURSOR++ )) + fi + # If we've moved past the end of the original buffer if (( $CURSOR > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor @@ -454,6 +461,11 @@ _zsh_autosuggest_partial_accept() { BUFFER="$original_buffer" fi + # Restore CURSOR + if [[ "$KEYMAP" = "vicmd" ]]; then + (( CURSOR-- )) + fi + return $retval }