mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2026-11-11 11:01:51 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36dae44064 |
@@ -1,8 +1,5 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## v0.6.3
|
|
||||||
- Fixed bug moving cursor to end of buffer after accepting suggestion (#453)
|
|
||||||
|
|
||||||
## v0.6.2
|
## v0.6.2
|
||||||
- Fixed bug deleting the last character in the buffer in vi mode (#450)
|
- Fixed bug deleting the last character in the buffer in vi mode (#450)
|
||||||
- Degrade gracefully when user doesn't have `zsh/system` module installed (#447)
|
- Degrade gracefully when user doesn't have `zsh/system` module installed (#447)
|
||||||
|
|||||||
@@ -5,13 +5,12 @@ describe 'a zle widget' do
|
|||||||
context 'when added to ZSH_AUTOSUGGEST_ACCEPT_WIDGETS' do
|
context 'when added to ZSH_AUTOSUGGEST_ACCEPT_WIDGETS' do
|
||||||
let(:options) { ["ZSH_AUTOSUGGEST_ACCEPT_WIDGETS+=(#{widget})"] }
|
let(:options) { ["ZSH_AUTOSUGGEST_ACCEPT_WIDGETS+=(#{widget})"] }
|
||||||
|
|
||||||
it 'accepts the suggestion and moves the cursor to the end of the buffer when invoked' do
|
it 'accepts the suggestion when invoked' do
|
||||||
with_history('echo hello') do
|
with_history('echo hello') do
|
||||||
session.send_string('e')
|
session.send_string('e')
|
||||||
wait_for { session.content }.to eq('echo hello')
|
wait_for { session.content }.to eq('echo hello')
|
||||||
session.send_keys('C-b')
|
session.send_keys('C-b')
|
||||||
wait_for { session.content(esc_seqs: true) }.to eq('echo hello')
|
wait_for { session.content(esc_seqs: true) }.to eq('echo hello')
|
||||||
wait_for { session.cursor }.to eq([10, 0])
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+1
-4
@@ -17,10 +17,7 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
|||||||
# Will try each strategy in order until a suggestion is returned
|
# Will try each strategy in order until a suggestion is returned
|
||||||
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
|
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
|
||||||
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
|
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
|
||||||
ZSH_AUTOSUGGEST_STRATEGY=(
|
ZSH_AUTOSUGGEST_STRATEGY=(history)
|
||||||
'cd *:completion'
|
|
||||||
'*:history,completion'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Widgets that clear the suggestion
|
# Widgets that clear the suggestion
|
||||||
|
|||||||
+10
-35
@@ -8,45 +8,20 @@
|
|||||||
|
|
||||||
_zsh_autosuggest_fetch_suggestion() {
|
_zsh_autosuggest_fetch_suggestion() {
|
||||||
typeset -g suggestion
|
typeset -g suggestion
|
||||||
local -a strategy_specs spec_parts strategies
|
local -a strategies
|
||||||
local strategy_spec strategy prefix
|
local strategy
|
||||||
|
|
||||||
# Ensure we are working with an array
|
# Ensure we are working with an array
|
||||||
strategy_specs=(${ZSH_AUTOSUGGEST_STRATEGY})
|
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
|
||||||
|
|
||||||
echo "fetching.." >> debug.log
|
for strategy in $strategies; do
|
||||||
|
# Try to get a suggestion from this strategy
|
||||||
|
_zsh_autosuggest_strategy_$strategy "$1"
|
||||||
|
|
||||||
for strategy_spec in $strategy_specs; do
|
# Ensure the suggestion matches the prefix
|
||||||
echo "trying spec: '$strategy_spec'" >> debug.log
|
[[ "$suggestion" != "$1"* ]] && unset suggestion
|
||||||
spec_parts=(${(s/:/)strategy_spec})
|
|
||||||
prefix="${spec_parts[1]}"
|
|
||||||
|
|
||||||
echo "checking prefix: $prefix" >> debug.log
|
# Break once we've found a valid suggestion
|
||||||
echo " spec parts: $spec_parts" >> debug.log
|
[[ -n "$suggestion" ]] && break
|
||||||
|
|
||||||
if [[ "$1" != ${~prefix} ]]; then
|
|
||||||
echo " '$1' didn't match prefix: '$prefix'" >> debug.log
|
|
||||||
continue;
|
|
||||||
fi
|
|
||||||
|
|
||||||
strategies=(${(s/,/)${spec_parts[2]}})
|
|
||||||
|
|
||||||
echo " trying strategies: $strategies" >> debug.log
|
|
||||||
|
|
||||||
for strategy in $strategies; do
|
|
||||||
|
|
||||||
echo " trying strategy $strategy" >> debug.log
|
|
||||||
|
|
||||||
# Try to get a suggestion from this strategy
|
|
||||||
_zsh_autosuggest_strategy_$strategy "$1"
|
|
||||||
|
|
||||||
# Ensure the suggestion matches the prefix
|
|
||||||
[[ "$suggestion" != "$1"* ]] && unset suggestion
|
|
||||||
|
|
||||||
# Break once we've found a valid suggestion
|
|
||||||
[[ -n "$suggestion" ]] && return
|
|
||||||
|
|
||||||
echo " didn't get suggestion" >> debug.log
|
|
||||||
done
|
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# History Words Suggestion Strategy #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggests the most recent history word that matches the given
|
||||||
|
# prefix.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_strategy_historywords() {
|
||||||
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
||||||
|
emulate -L zsh
|
||||||
|
|
||||||
|
# Enable globbing flags so that we can use (#m)
|
||||||
|
setopt EXTENDED_GLOB
|
||||||
|
|
||||||
|
local last_word="${${=1}[-1]}"
|
||||||
|
|
||||||
|
# Escape backslashes and all of the glob operators so we can use
|
||||||
|
# this string as a pattern to search the $history associative array.
|
||||||
|
# - (#m) globbing flag enables setting references for match data
|
||||||
|
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
|
||||||
|
local prefix="${last_word//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
|
||||||
|
|
||||||
|
# Get the first history word that matches. Concatenate it together with the
|
||||||
|
# prefix to form the full suggestion.
|
||||||
|
# - (r) subscript flag makes the pattern match on values
|
||||||
|
typeset -g suggestion="${1:0:-$#last_word}${historywords[(r)${prefix}?*]}"
|
||||||
|
}
|
||||||
+1
-5
@@ -136,11 +136,7 @@ _zsh_autosuggest_accept() {
|
|||||||
unset POSTDISPLAY
|
unset POSTDISPLAY
|
||||||
|
|
||||||
# Move the cursor to the end of the buffer
|
# Move the cursor to the end of the buffer
|
||||||
if [[ "$KEYMAP" = "vicmd" ]]; then
|
CURSOR=${max_cursor_pos}
|
||||||
CURSOR=$(($#BUFFER - 1))
|
|
||||||
else
|
|
||||||
CURSOR=$#BUFFER
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_zsh_autosuggest_invoke_original_widget $@
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
|
|||||||
+41
-45
@@ -1,6 +1,6 @@
|
|||||||
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
||||||
# https://github.com/zsh-users/zsh-autosuggestions
|
# https://github.com/zsh-users/zsh-autosuggestions
|
||||||
# v0.6.3
|
# v0.6.2
|
||||||
# Copyright (c) 2013 Thiago de Arruda
|
# Copyright (c) 2013 Thiago de Arruda
|
||||||
# Copyright (c) 2016-2019 Eric Freese
|
# Copyright (c) 2016-2019 Eric Freese
|
||||||
#
|
#
|
||||||
@@ -43,10 +43,7 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
|
|||||||
# Will try each strategy in order until a suggestion is returned
|
# Will try each strategy in order until a suggestion is returned
|
||||||
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
|
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
|
||||||
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
|
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
|
||||||
ZSH_AUTOSUGGEST_STRATEGY=(
|
ZSH_AUTOSUGGEST_STRATEGY=(history)
|
||||||
'cd *:completion'
|
|
||||||
'*:history,completion'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Widgets that clear the suggestion
|
# Widgets that clear the suggestion
|
||||||
@@ -401,11 +398,7 @@ _zsh_autosuggest_accept() {
|
|||||||
unset POSTDISPLAY
|
unset POSTDISPLAY
|
||||||
|
|
||||||
# Move the cursor to the end of the buffer
|
# Move the cursor to the end of the buffer
|
||||||
if [[ "$KEYMAP" = "vicmd" ]]; then
|
CURSOR=${max_cursor_pos}
|
||||||
CURSOR=$(($#BUFFER - 1))
|
|
||||||
else
|
|
||||||
CURSOR=$#BUFFER
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
_zsh_autosuggest_invoke_original_widget $@
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
@@ -618,6 +611,34 @@ _zsh_autosuggest_strategy_completion() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# History Words Suggestion Strategy #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Suggests the most recent history word that matches the given
|
||||||
|
# prefix.
|
||||||
|
#
|
||||||
|
|
||||||
|
_zsh_autosuggest_strategy_historywords() {
|
||||||
|
# Reset options to defaults and enable LOCAL_OPTIONS
|
||||||
|
emulate -L zsh
|
||||||
|
|
||||||
|
# Enable globbing flags so that we can use (#m)
|
||||||
|
setopt EXTENDED_GLOB
|
||||||
|
|
||||||
|
local last_word="${${=1}[-1]}"
|
||||||
|
|
||||||
|
# Escape backslashes and all of the glob operators so we can use
|
||||||
|
# this string as a pattern to search the $history associative array.
|
||||||
|
# - (#m) globbing flag enables setting references for match data
|
||||||
|
# TODO: Use (b) flag when we can drop support for zsh older than v5.0.8
|
||||||
|
local prefix="${last_word//(#m)[\\*?[\]<>()|^~#]/\\$MATCH}"
|
||||||
|
|
||||||
|
# Get the first history word that matches. Concatenate it together with the
|
||||||
|
# prefix to form the full suggestion.
|
||||||
|
# - (r) subscript flag makes the pattern match on values
|
||||||
|
typeset -g suggestion="${1:0:-$#last_word}${historywords[(r)${prefix}?*]}"
|
||||||
|
}
|
||||||
|
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
# History Suggestion Strategy #
|
# History Suggestion Strategy #
|
||||||
#--------------------------------------------------------------------#
|
#--------------------------------------------------------------------#
|
||||||
@@ -711,46 +732,21 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
|
|||||||
|
|
||||||
_zsh_autosuggest_fetch_suggestion() {
|
_zsh_autosuggest_fetch_suggestion() {
|
||||||
typeset -g suggestion
|
typeset -g suggestion
|
||||||
local -a strategy_specs spec_parts strategies
|
local -a strategies
|
||||||
local strategy_spec strategy prefix
|
local strategy
|
||||||
|
|
||||||
# Ensure we are working with an array
|
# Ensure we are working with an array
|
||||||
strategy_specs=(${ZSH_AUTOSUGGEST_STRATEGY})
|
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
|
||||||
|
|
||||||
echo "fetching.." >> debug.log
|
for strategy in $strategies; do
|
||||||
|
# Try to get a suggestion from this strategy
|
||||||
|
_zsh_autosuggest_strategy_$strategy "$1"
|
||||||
|
|
||||||
for strategy_spec in $strategy_specs; do
|
# Ensure the suggestion matches the prefix
|
||||||
echo "trying spec: '$strategy_spec'" >> debug.log
|
[[ "$suggestion" != "$1"* ]] && unset suggestion
|
||||||
spec_parts=(${(s/:/)strategy_spec})
|
|
||||||
prefix="${spec_parts[1]}"
|
|
||||||
|
|
||||||
echo "checking prefix: $prefix" >> debug.log
|
# Break once we've found a valid suggestion
|
||||||
echo " spec parts: $spec_parts" >> debug.log
|
[[ -n "$suggestion" ]] && break
|
||||||
|
|
||||||
if [[ "$1" != ${~prefix} ]]; then
|
|
||||||
echo " '$1' didn't match prefix: '$prefix'" >> debug.log
|
|
||||||
continue;
|
|
||||||
fi
|
|
||||||
|
|
||||||
strategies=(${(s/,/)${spec_parts[2]}})
|
|
||||||
|
|
||||||
echo " trying strategies: $strategies" >> debug.log
|
|
||||||
|
|
||||||
for strategy in $strategies; do
|
|
||||||
|
|
||||||
echo " trying strategy $strategy" >> debug.log
|
|
||||||
|
|
||||||
# Try to get a suggestion from this strategy
|
|
||||||
_zsh_autosuggest_strategy_$strategy "$1"
|
|
||||||
|
|
||||||
# Ensure the suggestion matches the prefix
|
|
||||||
[[ "$suggestion" != "$1"* ]] && unset suggestion
|
|
||||||
|
|
||||||
# Break once we've found a valid suggestion
|
|
||||||
[[ -n "$suggestion" ]] && return
|
|
||||||
|
|
||||||
echo " didn't get suggestion" >> debug.log
|
|
||||||
done
|
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user