mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Support fallback strategies by setting array in config
This commit is contained in:
parent
62f5f14f2f
commit
a78ea16c50
8 changed files with 93 additions and 18 deletions
1
Makefile
1
Makefile
|
@ -9,6 +9,7 @@ SRC_FILES := \
|
|||
$(SRC_DIR)/highlight.zsh \
|
||||
$(SRC_DIR)/widgets.zsh \
|
||||
$(SRC_DIR)/strategies/*.zsh \
|
||||
$(SRC_DIR)/fetch.zsh \
|
||||
$(SRC_DIR)/async.zsh \
|
||||
$(SRC_DIR)/start.zsh
|
||||
|
||||
|
|
|
@ -39,10 +39,10 @@ Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion
|
|||
|
||||
### Suggestion Strategy
|
||||
|
||||
Set `ZSH_AUTOSUGGEST_STRATEGY` to choose the strategy for generating suggestions. There are currently two to choose from:
|
||||
`ZSH_AUTOSUGGEST_STRATEGY` is an array that specifies how suggestions should be generated. The strategies in the array are tried successively until a suggestion is found. There are currently two built-in strategies to choose from:
|
||||
|
||||
- `history`: Chooses the most recent match from history.
|
||||
- `match_prev_cmd`: Chooses the most recent match whose preceding history item matches the most recently executed command ([more info](src/strategies/match_prev_cmd.zsh)). Note that this strategy won't work as expected with ZSH options that don't preserve the history order such as `HIST_IGNORE_ALL_DUPS` or `HIST_EXPIRE_DUPS_FIRST`.
|
||||
- `match_prev_cmd`: Like `history`, but chooses the most recent match whose preceding history item matches the most recently executed command ([more info](src/strategies/match_prev_cmd.zsh)). Note that this strategy won't work as expected with ZSH options that don't preserve the history order such as `HIST_IGNORE_ALL_DUPS` or `HIST_EXPIRE_DUPS_FIRST`.
|
||||
|
||||
|
||||
### Widget Mapping
|
||||
|
|
|
@ -1,20 +1,44 @@
|
|||
describe 'a suggestion for a given prefix' do
|
||||
let(:options) { ['_zsh_autosuggest_strategy_history() { 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
|
||||
session.send_string('e')
|
||||
wait_for { session.content }.to eq('echo foo')
|
||||
let(:options) { [ history_strategy ] }
|
||||
|
||||
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' do
|
||||
context 'when ZSH_AUTOSUGGEST_STRATEGY is set to an array' do
|
||||
let(:options) { [
|
||||
'_zsh_autosuggest_strategy_custom() { suggestion="echo foo" }',
|
||||
'ZSH_AUTOSUGGEST_STRATEGY=custom'
|
||||
foobar_strategy,
|
||||
foobaz_strategy,
|
||||
'ZSH_AUTOSUGGEST_STRATEGY=(foobar foobaz)'
|
||||
] }
|
||||
|
||||
it 'is determined by calling the specified strategy function' do
|
||||
session.send_string('e')
|
||||
wait_for { session.content }.to eq('echo foo')
|
||||
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
|
||||
|
||||
context 'when ZSH_AUTOSUGGEST_STRATEGY is set to a string' 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
|
||||
|
|
|
@ -35,7 +35,7 @@ _zsh_autosuggest_async_server() {
|
|||
# Run suggestion search in the background
|
||||
(
|
||||
local suggestion
|
||||
_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$query"
|
||||
_zsh_autosuggest_fetch_suggestion "$query"
|
||||
echo -n -E "$suggestion"$'\0'
|
||||
) &
|
||||
|
||||
|
|
|
@ -11,7 +11,9 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
|||
# Prefix to use when saving original versions of bound widgets
|
||||
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||
|
||||
ZSH_AUTOSUGGEST_STRATEGY=history
|
||||
# Strategies to use to fetch a suggestion
|
||||
# Will try each strategy in order until a suggestion is returned
|
||||
ZSH_AUTOSUGGEST_STRATEGY=(history)
|
||||
|
||||
# Widgets that clear the suggestion
|
||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||
|
|
23
src/fetch.zsh
Normal file
23
src/fetch.zsh
Normal file
|
@ -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
|
||||
}
|
|
@ -99,7 +99,7 @@ _zsh_autosuggest_fetch() {
|
|||
_zsh_autosuggest_async_request "$BUFFER"
|
||||
else
|
||||
local suggestion
|
||||
_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$BUFFER"
|
||||
_zsh_autosuggest_fetch_suggestion "$BUFFER"
|
||||
_zsh_autosuggest_suggest "$suggestion"
|
||||
fi
|
||||
}
|
||||
|
|
|
@ -47,7 +47,9 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8'
|
|||
# Prefix to use when saving original versions of bound widgets
|
||||
ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
||||
|
||||
ZSH_AUTOSUGGEST_STRATEGY=history
|
||||
# Strategies to use to fetch a suggestion
|
||||
# Will try each strategy in order until a suggestion is returned
|
||||
ZSH_AUTOSUGGEST_STRATEGY=(history)
|
||||
|
||||
# Widgets that clear the suggestion
|
||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=(
|
||||
|
@ -382,7 +384,7 @@ _zsh_autosuggest_fetch() {
|
|||
_zsh_autosuggest_async_request "$BUFFER"
|
||||
else
|
||||
local suggestion
|
||||
_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$BUFFER"
|
||||
_zsh_autosuggest_fetch_suggestion "$BUFFER"
|
||||
_zsh_autosuggest_suggest "$suggestion"
|
||||
fi
|
||||
}
|
||||
|
@ -583,6 +585,29 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
|
|||
typeset -g suggestion="$history[$histkey]"
|
||||
}
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
# 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
|
||||
}
|
||||
|
||||
#--------------------------------------------------------------------#
|
||||
# Async #
|
||||
#--------------------------------------------------------------------#
|
||||
|
@ -619,7 +644,7 @@ _zsh_autosuggest_async_server() {
|
|||
# Run suggestion search in the background
|
||||
(
|
||||
local suggestion
|
||||
_zsh_autosuggest_strategy_$ZSH_AUTOSUGGEST_STRATEGY "$query"
|
||||
_zsh_autosuggest_fetch_suggestion "$query"
|
||||
echo -n -E "$suggestion"$'\0'
|
||||
) &
|
||||
|
||||
|
|
Loading…
Reference in a new issue