From e1959d0f617f8a070623eeec1b5d9d1ec35ba40a Mon Sep 17 00:00:00 2001 From: Eric Freese Date: Tue, 28 Feb 2017 11:18:21 -0700 Subject: [PATCH] Put in a general fix for #219 - Handling input from `zle -U` Depends on patch to ZSH from workers/40702: http://www.zsh.org/mla/workers/2017/msg00414.html --- spec/integrations/zle_input_stack_spec.rb | 24 +++++++++++++++++++++++ src/widgets.zsh | 8 ++++++++ zsh-autosuggestions.zsh | 8 ++++++++ 3 files changed, 40 insertions(+) create mode 100644 spec/integrations/zle_input_stack_spec.rb diff --git a/spec/integrations/zle_input_stack_spec.rb b/spec/integrations/zle_input_stack_spec.rb new file mode 100644 index 0000000..8a2c990 --- /dev/null +++ b/spec/integrations/zle_input_stack_spec.rb @@ -0,0 +1,24 @@ +describe 'using `zle -U`' do + let(:before_sourcing) do + -> do + session. + run_command('_zsh_autosuggest_strategy_test() { sleep 1; _zsh_autosuggest_strategy_default "$1" }'). + run_command('foo() { zle -U - "echo hello" }; zle -N foo; bindkey ^B foo') + end + end + + let(:options) { ['unset ZSH_AUTOSUGGEST_USE_ASYNC', 'ZSH_AUTOSUGGEST_STRATEGY=test'] } + + # TODO: This is only possible with the $KEYS_QUEUED_COUNT widget parameter, coming soon... + xit 'does not fetch a suggestion for every inserted character' do + session.send_keys('C-b') + wait_for { session.content }.to eq('echo hello') + end + + it 'shows a suggestion when the widget completes' do + with_history('echo hello world') do + session.send_keys('C-b') + wait_for { session.content(esc_seqs: true) }.to match(/\Aecho hello\e\[[0-9]+m world/) + end + end +end diff --git a/src/widgets.zsh b/src/widgets.zsh index a0a59d5..7f15a27 100644 --- a/src/widgets.zsh +++ b/src/widgets.zsh @@ -15,6 +15,9 @@ _zsh_autosuggest_clear() { _zsh_autosuggest_modify() { local -i retval + # Only added to zsh very recently + local -i KEYS_QUEUED_COUNT + # Save the contents of the buffer/postdisplay local orig_buffer="$BUFFER" local orig_postdisplay="$POSTDISPLAY" @@ -26,6 +29,11 @@ _zsh_autosuggest_modify() { _zsh_autosuggest_invoke_original_widget $@ retval=$? + # Don't fetch a new suggestion if there's more input to be read immediately + if [[ $PENDING > 0 ]] || [[ $KEYS_QUEUED_COUNT > 0 ]]; then + return $retval + fi + # Optimize if manually typing in the suggestion if [ $#BUFFER -gt $#orig_buffer ]; then local added=${BUFFER#$orig_buffer} diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 05e1bb4..2b41154 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -293,6 +293,9 @@ _zsh_autosuggest_clear() { _zsh_autosuggest_modify() { local -i retval + # Only added to zsh very recently + local -i KEYS_QUEUED_COUNT + # Save the contents of the buffer/postdisplay local orig_buffer="$BUFFER" local orig_postdisplay="$POSTDISPLAY" @@ -304,6 +307,11 @@ _zsh_autosuggest_modify() { _zsh_autosuggest_invoke_original_widget $@ retval=$? + # Don't fetch a new suggestion if there's more input to be read immediately + if [[ $PENDING > 0 ]] || [[ $KEYS_QUEUED_COUNT > 0 ]]; then + return $retval + fi + # Optimize if manually typing in the suggestion if [ $#BUFFER -gt $#orig_buffer ]; then local added=${BUFFER#$orig_buffer}