mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2024-11-18 09:51:06 +01:00
Fix vi-mode partial-accept
Issue #188. PR #324. Thanks to @toadjaune and @IngoHeimbach.
This commit is contained in:
parent
42f5a06f7f
commit
393f7b8bb9
4 changed files with 70 additions and 8 deletions
|
@ -17,5 +17,51 @@ describe 'when using vi mode' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '`vi-forward-word-end`' do
|
||||||
|
it 'should accept through the end of the current word' do
|
||||||
|
with_history('foobar foo') do
|
||||||
|
session.
|
||||||
|
send_string('foo').
|
||||||
|
send_keys('escape').
|
||||||
|
send_keys('e'). # vi-forward-word-end
|
||||||
|
send_keys('a'). # vi-add-next
|
||||||
|
send_string('baz')
|
||||||
|
|
||||||
|
wait_for { session.content }.to eq('foobarbaz')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '`vi-forward-word`' do
|
||||||
|
it 'should accept through the first character of the next word' do
|
||||||
|
with_history('foobar foo') do
|
||||||
|
session.
|
||||||
|
send_string('foo').
|
||||||
|
send_keys('escape').
|
||||||
|
send_keys('w'). # vi-forward-word
|
||||||
|
send_keys('a'). # vi-add-next
|
||||||
|
send_string('az')
|
||||||
|
|
||||||
|
wait_for { session.content }.to eq('foobar faz')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '`vi-find-next-char`' do
|
||||||
|
it 'should accept through the next occurrence of the character' do
|
||||||
|
with_history('foobar foo') do
|
||||||
|
session.
|
||||||
|
send_string('foo').
|
||||||
|
send_keys('escape').
|
||||||
|
send_keys('f'). # vi-find-next-char
|
||||||
|
send_keys('o').
|
||||||
|
send_keys('a'). # vi-add-next
|
||||||
|
send_string('b')
|
||||||
|
|
||||||
|
wait_for { session.content }.to eq('foobar fob')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,8 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
||||||
vi-forward-word-end
|
vi-forward-word-end
|
||||||
vi-forward-blank-word
|
vi-forward-blank-word
|
||||||
vi-forward-blank-word-end
|
vi-forward-blank-word-end
|
||||||
|
vi-find-next-char
|
||||||
|
vi-find-next-char-skip
|
||||||
)
|
)
|
||||||
|
|
||||||
# Widgets that should be ignored (globbing supported but must be escaped)
|
# Widgets that should be ignored (globbing supported but must be escaped)
|
||||||
|
|
|
@ -153,7 +153,7 @@ _zsh_autosuggest_execute() {
|
||||||
|
|
||||||
# Partially accept the suggestion
|
# Partially accept the suggestion
|
||||||
_zsh_autosuggest_partial_accept() {
|
_zsh_autosuggest_partial_accept() {
|
||||||
local -i retval
|
local -i retval cursor_loc
|
||||||
|
|
||||||
# Save the contents of the buffer so we can restore later if needed
|
# Save the contents of the buffer so we can restore later if needed
|
||||||
local original_buffer="$BUFFER"
|
local original_buffer="$BUFFER"
|
||||||
|
@ -165,13 +165,19 @@ _zsh_autosuggest_partial_accept() {
|
||||||
_zsh_autosuggest_invoke_original_widget $@
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
retval=$?
|
retval=$?
|
||||||
|
|
||||||
|
# Normalize cursor location across vi/emacs modes
|
||||||
|
cursor_loc=$CURSOR
|
||||||
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
||||||
|
cursor_loc=$((cursor_loc + 1))
|
||||||
|
fi
|
||||||
|
|
||||||
# If we've moved past the end of the original buffer
|
# If we've moved past the end of the original buffer
|
||||||
if (( $CURSOR > $#original_buffer )); then
|
if (( $cursor_loc > $#original_buffer )); then
|
||||||
# Set POSTDISPLAY to text right of the cursor
|
# Set POSTDISPLAY to text right of the cursor
|
||||||
POSTDISPLAY="$RBUFFER"
|
POSTDISPLAY="${BUFFER[$(($cursor_loc + 1)),$#BUFFER]}"
|
||||||
|
|
||||||
# Clip the buffer at the cursor
|
# Clip the buffer at the cursor
|
||||||
BUFFER="$LBUFFER"
|
BUFFER="${BUFFER[1,$cursor_loc]}"
|
||||||
else
|
else
|
||||||
# Restore the original buffer
|
# Restore the original buffer
|
||||||
BUFFER="$original_buffer"
|
BUFFER="$original_buffer"
|
||||||
|
|
|
@ -85,6 +85,8 @@ ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS=(
|
||||||
vi-forward-word-end
|
vi-forward-word-end
|
||||||
vi-forward-blank-word
|
vi-forward-blank-word
|
||||||
vi-forward-blank-word-end
|
vi-forward-blank-word-end
|
||||||
|
vi-find-next-char
|
||||||
|
vi-find-next-char-skip
|
||||||
)
|
)
|
||||||
|
|
||||||
# Widgets that should be ignored (globbing supported but must be escaped)
|
# Widgets that should be ignored (globbing supported but must be escaped)
|
||||||
|
@ -431,7 +433,7 @@ _zsh_autosuggest_execute() {
|
||||||
|
|
||||||
# Partially accept the suggestion
|
# Partially accept the suggestion
|
||||||
_zsh_autosuggest_partial_accept() {
|
_zsh_autosuggest_partial_accept() {
|
||||||
local -i retval
|
local -i retval cursor_loc
|
||||||
|
|
||||||
# Save the contents of the buffer so we can restore later if needed
|
# Save the contents of the buffer so we can restore later if needed
|
||||||
local original_buffer="$BUFFER"
|
local original_buffer="$BUFFER"
|
||||||
|
@ -443,13 +445,19 @@ _zsh_autosuggest_partial_accept() {
|
||||||
_zsh_autosuggest_invoke_original_widget $@
|
_zsh_autosuggest_invoke_original_widget $@
|
||||||
retval=$?
|
retval=$?
|
||||||
|
|
||||||
|
# Normalize cursor location across vi/emacs modes
|
||||||
|
cursor_loc=$CURSOR
|
||||||
|
if [[ "$KEYMAP" = "vicmd" ]]; then
|
||||||
|
cursor_loc=$((cursor_loc + 1))
|
||||||
|
fi
|
||||||
|
|
||||||
# If we've moved past the end of the original buffer
|
# If we've moved past the end of the original buffer
|
||||||
if (( $CURSOR > $#original_buffer )); then
|
if (( $cursor_loc > $#original_buffer )); then
|
||||||
# Set POSTDISPLAY to text right of the cursor
|
# Set POSTDISPLAY to text right of the cursor
|
||||||
POSTDISPLAY="$RBUFFER"
|
POSTDISPLAY="${BUFFER[$(($cursor_loc + 1)),$#BUFFER]}"
|
||||||
|
|
||||||
# Clip the buffer at the cursor
|
# Clip the buffer at the cursor
|
||||||
BUFFER="$LBUFFER"
|
BUFFER="${BUFFER[1,$cursor_loc]}"
|
||||||
else
|
else
|
||||||
# Restore the original buffer
|
# Restore the original buffer
|
||||||
BUFFER="$original_buffer"
|
BUFFER="$original_buffer"
|
||||||
|
|
Loading…
Reference in a new issue