diff --git a/spec/strategies/completion_spec.rb b/spec/strategies/completion_spec.rb new file mode 100644 index 0000000..62cf0e5 --- /dev/null +++ b/spec/strategies/completion_spec.rb @@ -0,0 +1,30 @@ +describe 'the `completion` suggestion strategy' do + let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=completion'] } + let(:before_sourcing) do + -> do + session. + run_command('autoload compinit && compinit'). + run_command('_foo() { compadd bar }'). + run_command('compdef _foo baz') + end + end + + it 'suggests the first completion result' do + session.send_string('baz ') + wait_for { session.content }.to eq('baz bar') + end + + context 'when async mode is enabled' do + before do + skip 'Async mode not supported below v5.0.8' if session.zsh_version < Gem::Version.new('5.0.8') + end + + let(:options) { ['ZSH_AUTOSUGGEST_USE_ASYNC=true', 'ZSH_AUTOSUGGEST_STRATEGY=completion'] } + + it 'suggests the first completion result' do + session.send_string('baz ') + wait_for { session.content }.to eq('baz bar') + end + end +end + diff --git a/spec/strategies/match_prev_cmd_spec.rb b/spec/strategies/match_prev_cmd_spec.rb index f1596ba..5a143b8 100644 --- a/spec/strategies/match_prev_cmd_spec.rb +++ b/spec/strategies/match_prev_cmd_spec.rb @@ -1,6 +1,6 @@ require 'strategies/special_characters_helper' -describe 'the match_prev_cmd strategy' do +describe 'the `match_prev_cmd` strategy' do let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=match_prev_cmd'] } it 'suggests the last matching history entry after the previous command' do diff --git a/src/strategies/completion.zsh b/src/strategies/completion.zsh index c8b176b..0808575 100644 --- a/src/strategies/completion.zsh +++ b/src/strategies/completion.zsh @@ -9,7 +9,10 @@ _zsh_autosuggest_capture_setup() { zmodload zsh/zutil # For `zparseopts` - autoload compinit && compinit + # Ensure completions have been initialized + if ! whence compdef >/dev/null; then + autoload -Uz compinit && compinit + fi # There is a bug in zpty module (fixed in zsh/master) by which a # zpty that exits will kill all zpty processes that were forked diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh index 756cfbc..da10235 100644 --- a/zsh-autosuggestions.zsh +++ b/zsh-autosuggestions.zsh @@ -506,7 +506,10 @@ zle -N autosuggest-toggle _zsh_autosuggest_widget_toggle _zsh_autosuggest_capture_setup() { zmodload zsh/zutil # For `zparseopts` - autoload compinit && compinit + # Ensure completions have been initialized + if ! whence compdef >/dev/null; then + autoload -Uz compinit && compinit + fi # There is a bug in zpty module (fixed in zsh/master) by which a # zpty that exits will kill all zpty processes that were forked