From bf95b4c093b7894668a899441cdeabb14403e7f7 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Fri, 4 May 2018 15:57:41 +0200 Subject: [PATCH 1/9] First try to fix partial accept in vicmd mode --- zsh-autosuggestions.zsh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index a1037ba..fd4745b 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -438,10 +438,26 @@ _zsh_autosuggest_partial_accept() { # Temporarily accept the suggestion. BUFFER="$BUFFER$POSTDISPLAY" + print keymap: $KEYMAP >> /tmp/zsh_autosuggest_log + print original widget name: $@ >> /tmp/zsh_autosuggest_log + print original buffer: $original_buffer >> /tmp/zsh_autosuggest_log + print original postdisplay: $POSTDISPLAY >> /tmp/zsh_autosuggest_log + print original cursor: $CURSOR >> /tmp/zsh_autosuggest_log + # Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@ retval=$? + # In vicmd keymap, the cursor is placed on the last character, not after + # For this reason, we need to increment it by 1 for our tests on it to + # keep working, then roll it back to its initial value + if [ $KEYMAP = 'vicmd' ]; then + (( CURSOR++ )) + fi + + print retval: $retval >> /tmp/zsh_autosuggest_log + print final cursor: $CURSOR >> /tmp/zsh_autosuggest_log + # 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 +470,11 @@ _zsh_autosuggest_partial_accept() { BUFFER="$original_buffer" fi + if [ $KEYMAP = 'vicmd' ]; then + (( CURSOR-- )) + fi + + print '' >> /tmp/zsh_autosuggest_log return $retval } From 603132c6b87b0bf18c8ba942b98669be7088165d Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Fri, 4 May 2018 16:07:07 +0200 Subject: [PATCH 2/9] Put the modifications in the source, and removed debugging instructions --- src/widgets.zsh | 12 ++++++++++++ zsh-autosuggestions.zsh | 17 ++++------------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 8a31133..190ddce 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 fd4745b..931598f 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -438,26 +438,17 @@ _zsh_autosuggest_partial_accept() { # Temporarily accept the suggestion. BUFFER="$BUFFER$POSTDISPLAY" - print keymap: $KEYMAP >> /tmp/zsh_autosuggest_log - print original widget name: $@ >> /tmp/zsh_autosuggest_log - print original buffer: $original_buffer >> /tmp/zsh_autosuggest_log - print original postdisplay: $POSTDISPLAY >> /tmp/zsh_autosuggest_log - print original cursor: $CURSOR >> /tmp/zsh_autosuggest_log - # Original widget moves the cursor _zsh_autosuggest_invoke_original_widget $@ retval=$? - # In vicmd keymap, the cursor is placed on the last character, not after - # For this reason, we need to increment it by 1 for our tests on it to - # keep working, then roll it back to its initial value + # 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 - print retval: $retval >> /tmp/zsh_autosuggest_log - print final cursor: $CURSOR >> /tmp/zsh_autosuggest_log - # If we've moved past the end of the original buffer if (( $CURSOR > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor @@ -470,11 +461,11 @@ _zsh_autosuggest_partial_accept() { BUFFER="$original_buffer" fi + # Restore CURSOR if [ $KEYMAP = 'vicmd' ]; then (( CURSOR-- )) fi - print '' >> /tmp/zsh_autosuggest_log return $retval } From b9345ebcebd706e5e9cf71728bc0d4af5b0bb055 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Fri, 4 May 2018 18:04:19 +0200 Subject: [PATCH 3/9] Using a separate variable instead of rolling back CURSOR to its initial value --- src/widgets.zsh | 12 ++++-------- zsh-autosuggestions.zsh | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 190ddce..43cd974 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -166,13 +166,14 @@ _zsh_autosuggest_partial_accept() { # 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. + # working even in this case. + CURSOR_POS=$CURSOR if [ $KEYMAP = 'vicmd' ]; then - (( CURSOR++ )) + (( CURSOR_POS++ )) fi # If we've moved past the end of the original buffer - if (( $CURSOR > $#original_buffer )); then + if (( $CURSOR_POS > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor POSTDISPLAY="$RBUFFER" @@ -183,11 +184,6 @@ _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 931598f..96996dc 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -444,13 +444,14 @@ _zsh_autosuggest_partial_accept() { # 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. + # working even in this case. + CURSOR_POS=$CURSOR if [ $KEYMAP = 'vicmd' ]; then - (( CURSOR++ )) + (( CURSOR_POS++ )) fi # If we've moved past the end of the original buffer - if (( $CURSOR > $#original_buffer )); then + if (( $CURSOR_POS > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor POSTDISPLAY="$RBUFFER" @@ -461,11 +462,6 @@ _zsh_autosuggest_partial_accept() { BUFFER="$original_buffer" fi - # Restore CURSOR - if [ $KEYMAP = 'vicmd' ]; then - (( CURSOR-- )) - fi - return $retval } From b6396ea2eb92e3839a4817737febafba4524cf60 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Mon, 7 May 2018 10:12:11 +0200 Subject: [PATCH 4/9] Fix quotes for code style consistency --- src/widgets.zsh | 2 +- zsh-autosuggestions.zsh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 43cd974..56b0cca 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -168,7 +168,7 @@ _zsh_autosuggest_partial_accept() { # instead of after it. We increment it by 1 to keep the following logic # working even in this case. CURSOR_POS=$CURSOR - if [ $KEYMAP = 'vicmd' ]; then + if [[ "$KEYMAP" = "vicmd" ]]; then (( CURSOR_POS++ )) fi diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 96996dc..4aa85df 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -446,7 +446,7 @@ _zsh_autosuggest_partial_accept() { # instead of after it. We increment it by 1 to keep the following logic # working even in this case. CURSOR_POS=$CURSOR - if [ $KEYMAP = 'vicmd' ]; then + if [[ "$KEYMAP" = "vicmd" ]]; then (( CURSOR_POS++ )) fi From 1535f1f3e562d8331f5b2b99771e62c54f275569 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Mon, 7 May 2018 13:05:36 +0200 Subject: [PATCH 5/9] Started to add a test --- .../integrations/vicmd_partial_accept_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 spec/integrations/vicmd_partial_accept_spec.rb diff --git a/spec/integrations/vicmd_partial_accept_spec.rb b/spec/integrations/vicmd_partial_accept_spec.rb new file mode 100644 index 0000000..f645890 --- /dev/null +++ b/spec/integrations/vicmd_partial_accept_spec.rb @@ -0,0 +1,19 @@ +describe 'a vicmd mode partial-accept widget' do + let(:widget) { 'vi-forward-word-end' } + + context 'in vicmd mode' do +# -> do +# let(:before_sourcing) do +# session. +# end +# end + + 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') + wait_for { session.content }.to eq('foobar') + end + end + end +end From 9619e2e7794ba6acda84ab3f4debca279c475ec8 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Mon, 7 May 2018 13:19:42 +0200 Subject: [PATCH 6/9] Now the test should be working --- spec/integrations/vicmd_partial_accept_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/integrations/vicmd_partial_accept_spec.rb b/spec/integrations/vicmd_partial_accept_spec.rb index f645890..8f5c7d4 100644 --- a/spec/integrations/vicmd_partial_accept_spec.rb +++ b/spec/integrations/vicmd_partial_accept_spec.rb @@ -11,8 +11,8 @@ describe 'a vicmd mode partial-accept widget' 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') - wait_for { session.content }.to eq('foobar') + 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 From c4c425a8a060974c139b39a26d385304f2d6808a Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Mon, 7 May 2018 13:33:24 +0200 Subject: [PATCH 7/9] Went back to modifying CURSOR directly --- src/widgets.zsh | 14 +++++++++----- zsh-autosuggestions.zsh | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 56b0cca..190ddce 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -166,14 +166,13 @@ _zsh_autosuggest_partial_accept() { # 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 this case. - CURSOR_POS=$CURSOR - if [[ "$KEYMAP" = "vicmd" ]]; then - (( CURSOR_POS++ )) + # working even in thi case. + if [ $KEYMAP = 'vicmd' ]; then + (( CURSOR++ )) fi # If we've moved past the end of the original buffer - if (( $CURSOR_POS > $#original_buffer )); then + if (( $CURSOR > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor POSTDISPLAY="$RBUFFER" @@ -184,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 4aa85df..931598f 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -444,14 +444,13 @@ _zsh_autosuggest_partial_accept() { # 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 this case. - CURSOR_POS=$CURSOR - if [[ "$KEYMAP" = "vicmd" ]]; then - (( CURSOR_POS++ )) + # working even in thi case. + if [ $KEYMAP = 'vicmd' ]; then + (( CURSOR++ )) fi # If we've moved past the end of the original buffer - if (( $CURSOR_POS > $#original_buffer )); then + if (( $CURSOR > $#original_buffer )); then # Set POSTDISPLAY to text right of the cursor POSTDISPLAY="$RBUFFER" @@ -462,6 +461,11 @@ _zsh_autosuggest_partial_accept() { BUFFER="$original_buffer" fi + # Restore CURSOR + if [ $KEYMAP = 'vicmd' ]; then + (( CURSOR-- )) + fi + return $retval } From 4c88da24fa06a3219d1dbf023121a90336a687e1 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Mon, 7 May 2018 13:35:40 +0200 Subject: [PATCH 8/9] Cleanup --- spec/integrations/vicmd_partial_accept_spec.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spec/integrations/vicmd_partial_accept_spec.rb b/spec/integrations/vicmd_partial_accept_spec.rb index 8f5c7d4..e9c6973 100644 --- a/spec/integrations/vicmd_partial_accept_spec.rb +++ b/spec/integrations/vicmd_partial_accept_spec.rb @@ -2,12 +2,6 @@ describe 'a vicmd mode partial-accept widget' do let(:widget) { 'vi-forward-word-end' } context 'in vicmd mode' do -# -> do -# let(:before_sourcing) do -# session. -# end -# end - it 'moves the cursor through suggestion as expected' do session.run_command("bindkey s vi-cmd-mode") with_history('foobar foo') do From 5fd85405b30683a0c308c5f4edc030ac3267dfa6 Mon Sep 17 00:00:00 2001 From: Arnaud Venturi Date: Mon, 7 May 2018 13:37:09 +0200 Subject: [PATCH 9/9] Fixed the style of the test again --- src/widgets.zsh | 4 ++-- zsh-autosuggestions.zsh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/widgets.zsh b/src/widgets.zsh index 190ddce..dfbf214 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -167,7 +167,7 @@ _zsh_autosuggest_partial_accept() { # 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 + if [[ "$KEYMAP" = "vicmd" ]]; then (( CURSOR++ )) fi @@ -184,7 +184,7 @@ _zsh_autosuggest_partial_accept() { fi # Restore CURSOR - if [ $KEYMAP = 'vicmd' ]; then + if [[ "$KEYMAP" = "vicmd" ]]; then (( CURSOR-- )) fi diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 931598f..f21f897 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -445,7 +445,7 @@ _zsh_autosuggest_partial_accept() { # 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 + if [[ "$KEYMAP" = "vicmd" ]]; then (( CURSOR++ )) fi @@ -462,7 +462,7 @@ _zsh_autosuggest_partial_accept() { fi # Restore CURSOR - if [ $KEYMAP = 'vicmd' ]; then + if [[ "$KEYMAP" = "vicmd" ]]; then (( CURSOR-- )) fi