Support fallback strategies by setting array in config
parent
949c374195
commit
bcbdad83e9
@ -1,20 +1,45 @@
|
|||||||
describe 'a suggestion for a given prefix' do
|
describe 'a suggestion for a given prefix' do
|
||||||
let(:options) { ['_zsh_autosuggest_strategy_default() { suggestion="echo foo" }'] }
|
let(:history_strategy) { '_zsh_autosuggest_strategy_history() { suggestion="history" }' }
|
||||||
|
let(:foobar_strategy) { '_zsh_autosuggest_strategy_foobar() { [[ "foobar baz" = $1* ]] && suggestion="foobar baz" }' }
|
||||||
|
let(:foobaz_strategy) { '_zsh_autosuggest_strategy_foobaz() { [[ "foobaz bar" = $1* ]] && suggestion="foobaz bar" }' }
|
||||||
|
|
||||||
it 'is determined by calling the default strategy function' do
|
let(:options) { [ history_strategy ] }
|
||||||
session.send_string('e')
|
|
||||||
wait_for { session.content }.to eq('echo foo')
|
it 'by default is determined by calling the `history` strategy function' do
|
||||||
|
session.send_string('h')
|
||||||
|
wait_for { session.content }.to eq('history')
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when ZSH_AUTOSUGGEST_STRATEGY is set to an array' do
|
||||||
|
let(:options) { [
|
||||||
|
foobar_strategy,
|
||||||
|
foobaz_strategy,
|
||||||
|
'ZSH_AUTOSUGGEST_STRATEGY=(foobar foobaz)'
|
||||||
|
] }
|
||||||
|
|
||||||
|
it 'is determined by the first strategy function to return a suggestion' do
|
||||||
|
session.send_string('foo')
|
||||||
|
wait_for { session.content }.to eq('foobar baz')
|
||||||
|
|
||||||
|
session.send_string('baz')
|
||||||
|
wait_for { session.content }.to eq('foobaz bar')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when ZSH_AUTOSUGGEST_STRATEGY is set' do
|
context 'when ZSH_AUTOSUGGEST_STRATEGY is set to a string' do
|
||||||
let(:options) { [
|
let(:options) { [
|
||||||
'_zsh_autosuggest_strategy_custom() { suggestion="echo foo" }',
|
foobar_strategy,
|
||||||
'ZSH_AUTOSUGGEST_STRATEGY=custom'
|
foobaz_strategy,
|
||||||
|
'ZSH_AUTOSUGGEST_STRATEGY="foobar foobaz"'
|
||||||
] }
|
] }
|
||||||
|
|
||||||
it 'is determined by calling the specified strategy function' do
|
it 'is determined by the first strategy function to return a suggestion' do
|
||||||
session.send_string('e')
|
session.send_string('foo')
|
||||||
wait_for { session.content }.to eq('echo foo')
|
wait_for { session.content }.to eq('foobar baz')
|
||||||
|
|
||||||
|
session.send_string('baz')
|
||||||
|
wait_for { session.content }.to eq('foobaz bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Fetch Suggestion #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Loops through all specified strategies and returns a suggestion
|
||||||
|
# from the first strategy to provide one.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_fetch_suggestion() {
|
||||||
|
typeset -g suggestion
|
||||||
|
local -a strategies
|
||||||
|
|
||||||
|
# Ensure we are working with an array
|
||||||
|
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
|
||||||
|
|
||||||
|
for strategy in $strategies; do
|
||||||
|
# Try to get a suggestion from this strategy
|
||||||
|
_zsh_autosuggest_strategy_$strategy "$1"
|
||||||
|
|
||||||
|
# Break once we've found a suggestion
|
||||||
|
[[ -n "$suggestion" ]] && break
|
||||||
|
done
|
||||||
|
}
|
@ -1,17 +0,0 @@
|
|||||||
|
|
||||||
#--------------------------------------------------------------------#
|
|
||||||
# Default Suggestion Strategy #
|
|
||||||
#--------------------------------------------------------------------#
|
|
||||||
# Will provide suggestions from your history. If no matches are found
|
|
||||||
# in history, will provide a suggestion from the completion engine.
|
|
||||||
#
|
|
||||||
|
|
||||||
_zsh_autosuggest_strategy_default() {
|
|
||||||
typeset -g suggestion
|
|
||||||
|
|
||||||
_zsh_autosuggest_strategy_history "$1"
|
|
||||||
|
|
||||||
if [[ -z "$suggestion" ]]; then
|
|
||||||
_zsh_autosuggest_strategy_completion "$1"
|
|
||||||
fi
|
|
||||||
}
|
|
Loading…
Reference in New Issue