Compare commits

...

24 Commits

Author SHA1 Message Date
Eric Freese
b27277e7ff wip playing with different strategies when for different patterns 2019-06-29 13:49:16 -06:00
Eric Freese
146020d9b2 Merge pull request #454 from zsh-users/develop
v0.6.3
2019-06-24 09:13:46 -06:00
Eric Freese
0636a39b51 0.6.3 2019-06-24 09:10:42 -06:00
Eric Freese
6769c941ba Update changelog for v0.6.3 release 2019-06-24 09:10:42 -06:00
Eric Freese
371d6441c0 Merge pull request #453 from zsh-users/fixes/move-cursor-on-accept
Fix moving cursor to end of buffer when suggestion accepted
2019-06-24 07:16:35 -06:00
Eric Freese
78e4379711 Fix moving cursor to end of buffer when suggestion accepted
$max_cursor_pos in this case was not the correct value to use. It was
calculated based on the old length of the $BUFFER. After the suggestion
is accepted, we need to recalculate the new max cursor length and use it
to set the $CURSOR.

Fixes issue #452. Follow-up to issue #302 (PR #450).
2019-06-23 12:37:08 -06:00
Eric Freese
c80605595c Merge pull request #451 from zsh-users/develop
v0.6.2
2019-06-22 16:57:19 -06:00
Eric Freese
cb52adf429 v0.6.2 2019-06-22 16:54:32 -06:00
Eric Freese
cdf6be4f06 Update readme description to reference new completion strategy 2019-06-22 16:54:03 -06:00
Eric Freese
66a6de3fb5 Update changelog for v0.6.2 release 2019-06-22 16:52:42 -06:00
Eric Freese
2498dbab75 Merge pull request #450 from zsh-users/fixes/vi-delete
Fix deleting last character in vi mode (#302)
2019-06-22 16:42:52 -06:00
Eric Freese
676aebdf44 Fix deleting last character in vi mode (#302)
Typing `d` and then `l` runs `vi-delete` and then `vi-forward-char`.  However,
by default, `vi-forward-char` is configured to accept the suggestion. So in
that case, the suggestion was being accepted and the cursor set to the end of
the buffer before the deletion was run.

The reason the user doesn't see the suggestion accepted is that `vi-delete`
doesn't finish until the movement widget is run, so we're already inside of a
`modify` when `accept` is called. `modify` unsets `POSTDISPLAY` before calling
the original widget so when we get to the accept function, `POSTDISPLAY` is
empty and thus accepting the suggestion is a no-op.

The fix is to make sure we reset the cursor to the correct place before running
the original widget.

We skip the test for versions of zsh below 5.0.8 since there was a bug in
earlier versions where deleting the last char did not work.

See http://www.zsh.org/mla/workers/2014/msg01316.html
2019-06-22 16:34:42 -06:00
Eric Freese
ec43fcfccf Merge pull request #449 from Warkst/i416
Possible fix for #416
2019-06-22 10:56:34 -06:00
Eric Freese
f5236c4351 Merge pull request #448 from ondrej-fabry/patch-1
Update INSTALL.md
2019-06-22 10:50:17 -06:00
Eric Freese
d27983b7ca Merge pull request #447 from zsh-users/features/without-system-module
Degrade gracefully on systems missing zsh/system module
2019-06-22 10:46:26 -06:00
Robin De Mol
d14b17fb09 Possible fix for #416
* Added a note for iTerm2 users under readme/configuration/highlight
style.

* Changed the link to the issue list under troubleshooting so that the
default filters (only open issues) are not applied.
2019-06-20 09:32:47 +02:00
Ondrej Fabry
b24b607fbf Update INSTALL.md
- minor formatting change for better readability
2019-06-19 23:30:36 +02:00
Eric Freese
a437544cc5 Degrade gracefully on systems missing zsh/system module
When using async mode, stale background processes will not be cancelled
when a new one starts. This shouldn't cause any real issues since the
processes should eventually finish and be cleaned up anyway, and
removing the handler with `zle -F` means that stale suggestions should
never be shown.
2019-06-19 13:41:43 -06:00
Eric Freese
112dd3e3c7 Merge pull request #446 from zsh-users/develop
v0.6.1
2019-06-18 11:11:59 -06:00
Eric Freese
8a812bdfd2 v0.6.1 2019-06-18 11:04:24 -06:00
Eric Freese
f178efb847 Update changelog for v0.6.1 release 2019-06-18 11:04:04 -06:00
Eric Freese
48ffc1bf92 Merge pull request #443 from zsh-users/fixes/alias-parse-error
Prefix custom `_complete` implementation with "function" keyword
2019-06-18 07:50:07 -06:00
Eric Freese
adb02c44a2 Prefix custom _complete implementation with "function" keyword
For some reason, when `_complete` is aliased before sourcing the plugin,
zsh was blowing up with a parse error. Adding "function" keyword makes
it parse successfully.

See similar issue: https://github.com/robbyrussell/oh-my-zsh/issues/6723#issuecomment-381220834

Fixes GitHub #442
2019-06-17 22:04:31 -06:00
Eric Freese
3654b83ec0 Update license copyright year in built plugin file
Should have been committed with 19c976f
2019-06-17 21:58:31 -06:00
13 changed files with 185 additions and 67 deletions

View File

@@ -1,5 +1,15 @@
# Changelog
## v0.6.3
- Fixed bug moving cursor to end of buffer after accepting suggestion (#453)
## v0.6.2
- 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)
## v0.6.1
- Fixed bug occurring when `_complete` had been aliased (#443)
## v0.6.0
- Added `completion` suggestion strategy powered by completion system (#111)
- Allow setting `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` to an empty string (#422)

View File

@@ -1,6 +1,12 @@
## Installation
# Installation
### Manual (Git Clone)
* [Manual](#manual-git-clone)
* [Antigen](#antigen)
* [Oh My Zsh](#oh-my-zsh)
* [Arch Linux](#arch-linux)
* [macOS via Homebrew](#macos-via-homebrew)
## Manual (Git Clone)
1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`.
@@ -16,7 +22,7 @@
3. Start a new terminal session.
### Antigen
## Antigen
1. Add the following to your `.zshrc`:
@@ -26,7 +32,7 @@
2. Start a new terminal session.
### Oh My Zsh
## Oh My Zsh
1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`)
@@ -42,7 +48,7 @@
3. Start a new terminal session.
### Arch Linux
## Arch Linux
1. Install [`zsh-autosuggestions`](https://www.archlinux.org/packages/community/any/zsh-autosuggestions/) from the `community` repository.
@@ -60,7 +66,8 @@
3. Start a new terminal session.
### macOS via Homebrew
## macOS via Homebrew
1. Install the `zsh-autosuggestions` package using [Homebrew](https://brew.sh/).
```sh
@@ -74,4 +81,3 @@
```
3. Start a new terminal session.

View File

@@ -2,7 +2,7 @@
_[Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh._
It suggests commands as you type.
It suggests commands as you type based on history and completions.
Requirements: Zsh v4.3.11 or later
@@ -44,6 +44,8 @@ ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff,bg=cyan,bold,underline"
For more info, read the Character Highlighting section of the zsh manual: `man zshzle` or [online](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting).
**Note:** Some iTerm2 users have reported [not being able to see the suggestions](https://github.com/zsh-users/zsh-autosuggestions/issues/416#issuecomment-486516333). If this affects you, the problem is likely caused by incorrect color settings. In order to correct this, go into iTerm2's setting, navigate to profile > colors and make sure that the colors for Basic Colors > Background and ANSI Colors > Bright Black are **different**.
### Suggestion Strategy
@@ -106,8 +108,7 @@ bindkey '^ ' autosuggest-accept
## Troubleshooting
If you have a problem, please search through [the list of issues on GitHub](https://github.com/zsh-users/zsh-autosuggestions/issues) to see if someone else has already reported it.
If you have a problem, please search through [the list of issues on GitHub](https://github.com/zsh-users/zsh-autosuggestions/issues?q=) to see if someone else has already reported it.
### Reporting an Issue

View File

@@ -1 +1 @@
v0.6.0
v0.6.3

View File

@@ -63,5 +63,18 @@ describe 'when using vi mode' do
end
end
end
end
describe '`vi-delete`' do
it 'should be able to remove the last character in the buffer' do
skip 'deleting last char did not work below zsh version 5.0.8' if session.zsh_version < Gem::Version.new('5.0.8')
session.
send_string('echo foo').
send_keys('escape').
send_keys('d').
send_keys('l')
wait_for { session.content }.to eq('echo fo')
end
end
end

View File

@@ -5,12 +5,13 @@ describe 'a zle widget' do
context 'when added to ZSH_AUTOSUGGEST_ACCEPT_WIDGETS' do
let(:options) { ["ZSH_AUTOSUGGEST_ACCEPT_WIDGETS+=(#{widget})"] }
it 'accepts the suggestion when invoked' do
it 'accepts the suggestion and moves the cursor to the end of the buffer when invoked' do
with_history('echo hello') do
session.send_string('e')
wait_for { session.content }.to eq('echo hello')
session.send_keys('C-b')
wait_for { session.content(esc_seqs: true) }.to eq('echo hello')
wait_for { session.cursor }.to eq([10, 0])
end
end
end

View File

@@ -20,6 +20,23 @@ describe 'the `completion` suggestion strategy' do
wait_for { session.content }.to eq("baz \\\nbar")
end
context 'when `_complete` is aliased' do
let(:before_sourcing) do
-> do
session.
run_command('autoload compinit && compinit').
run_command('_foo() { compadd bar; compadd bat }').
run_command('compdef _foo baz').
run_command('alias _complete=_complete')
end
end
it 'suggests the first completion result' do
session.send_string('baz ')
wait_for { session.content }.to eq('baz bar')
end
end
context 'when async mode is enabled' do
let(:options) { ['ZSH_AUTOSUGGEST_USE_ASYNC=true', 'ZSH_AUTOSUGGEST_STRATEGY=completion'] }

View File

@@ -3,9 +3,9 @@
# Async #
#--------------------------------------------------------------------#
zmodload zsh/system
_zsh_autosuggest_async_request() {
zmodload zsh/system 2>/dev/null # For `$sysparams`
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
# If we've got a pending request, cancel it
@@ -14,6 +14,8 @@ _zsh_autosuggest_async_request() {
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&-
zle -F $_ZSH_AUTOSUGGEST_ASYNC_FD
# We won't know the pid unless the user has zsh/system module installed
if [[ -n "$_ZSH_AUTOSUGGEST_CHILD_PID" ]]; then
# Zsh will make a new process group for the child process only if job
# control is enabled (MONITOR option)
if [[ -o MONITOR ]]; then
@@ -27,6 +29,7 @@ _zsh_autosuggest_async_request() {
kill -TERM $_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
fi
fi
fi
# Fork a process to fetch a suggestion and open a pipe to read from it
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}< <(

View File

@@ -17,7 +17,10 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
# Will try each strategy in order until a suggestion is returned
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
ZSH_AUTOSUGGEST_STRATEGY=(history)
ZSH_AUTOSUGGEST_STRATEGY=(
'cd *:completion'
'*:history,completion'
)
}
# Widgets that clear the suggestion

View File

@@ -8,13 +8,35 @@
_zsh_autosuggest_fetch_suggestion() {
typeset -g suggestion
local -a strategies
local strategy
local -a strategy_specs spec_parts strategies
local strategy_spec strategy prefix
# Ensure we are working with an array
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
strategy_specs=(${ZSH_AUTOSUGGEST_STRATEGY})
echo "fetching.." >> debug.log
for strategy_spec in $strategy_specs; do
echo "trying spec: '$strategy_spec'" >> debug.log
spec_parts=(${(s/:/)strategy_spec})
prefix="${spec_parts[1]}"
echo "checking prefix: $prefix" >> debug.log
echo " spec parts: $spec_parts" >> debug.log
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"
@@ -22,6 +44,9 @@ _zsh_autosuggest_fetch_suggestion() {
[[ "$suggestion" != "$1"* ]] && unset suggestion
# Break once we've found a valid suggestion
[[ -n "$suggestion" ]] && break
[[ -n "$suggestion" ]] && return
echo " didn't get suggestion" >> debug.log
done
done
}

View File

@@ -86,7 +86,7 @@ _zsh_autosuggest_capture_completion_async() {
# https://stackoverflow.com/a/7057118/154703
autoload +X _complete
functions[_original_complete]=$functions[_complete]
_complete () {
function _complete() {
unset 'compstate[vared]'
_original_complete "$@"
}

View File

@@ -136,7 +136,11 @@ _zsh_autosuggest_accept() {
unset POSTDISPLAY
# Move the cursor to the end of the buffer
CURSOR=${#BUFFER}
if [[ "$KEYMAP" = "vicmd" ]]; then
CURSOR=$(($#BUFFER - 1))
else
CURSOR=$#BUFFER
fi
fi
_zsh_autosuggest_invoke_original_widget $@

View File

@@ -1,8 +1,8 @@
# Fish-like fast/unobtrusive autosuggestions for zsh.
# https://github.com/zsh-users/zsh-autosuggestions
# v0.6.0
# v0.6.3
# Copyright (c) 2013 Thiago de Arruda
# Copyright (c) 2016-2018 Eric Freese
# Copyright (c) 2016-2019 Eric Freese
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
@@ -43,7 +43,10 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
# Will try each strategy in order until a suggestion is returned
(( ! ${+ZSH_AUTOSUGGEST_STRATEGY} )) && {
typeset -ga ZSH_AUTOSUGGEST_STRATEGY
ZSH_AUTOSUGGEST_STRATEGY=(history)
ZSH_AUTOSUGGEST_STRATEGY=(
'cd *:completion'
'*:history,completion'
)
}
# Widgets that clear the suggestion
@@ -398,7 +401,11 @@ _zsh_autosuggest_accept() {
unset POSTDISPLAY
# Move the cursor to the end of the buffer
CURSOR=${#BUFFER}
if [[ "$KEYMAP" = "vicmd" ]]; then
CURSOR=$(($#BUFFER - 1))
else
CURSOR=$#BUFFER
fi
fi
_zsh_autosuggest_invoke_original_widget $@
@@ -568,7 +575,7 @@ _zsh_autosuggest_capture_completion_async() {
# https://stackoverflow.com/a/7057118/154703
autoload +X _complete
functions[_original_complete]=$functions[_complete]
_complete () {
function _complete() {
unset 'compstate[vared]'
_original_complete "$@"
}
@@ -704,13 +711,35 @@ _zsh_autosuggest_strategy_match_prev_cmd() {
_zsh_autosuggest_fetch_suggestion() {
typeset -g suggestion
local -a strategies
local strategy
local -a strategy_specs spec_parts strategies
local strategy_spec strategy prefix
# Ensure we are working with an array
strategies=(${=ZSH_AUTOSUGGEST_STRATEGY})
strategy_specs=(${ZSH_AUTOSUGGEST_STRATEGY})
echo "fetching.." >> debug.log
for strategy_spec in $strategy_specs; do
echo "trying spec: '$strategy_spec'" >> debug.log
spec_parts=(${(s/:/)strategy_spec})
prefix="${spec_parts[1]}"
echo "checking prefix: $prefix" >> debug.log
echo " spec parts: $spec_parts" >> debug.log
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"
@@ -718,7 +747,10 @@ _zsh_autosuggest_fetch_suggestion() {
[[ "$suggestion" != "$1"* ]] && unset suggestion
# Break once we've found a valid suggestion
[[ -n "$suggestion" ]] && break
[[ -n "$suggestion" ]] && return
echo " didn't get suggestion" >> debug.log
done
done
}
@@ -726,9 +758,9 @@ _zsh_autosuggest_fetch_suggestion() {
# Async #
#--------------------------------------------------------------------#
zmodload zsh/system
_zsh_autosuggest_async_request() {
zmodload zsh/system 2>/dev/null # For `$sysparams`
typeset -g _ZSH_AUTOSUGGEST_ASYNC_FD _ZSH_AUTOSUGGEST_CHILD_PID
# If we've got a pending request, cancel it
@@ -737,6 +769,8 @@ _zsh_autosuggest_async_request() {
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}<&-
zle -F $_ZSH_AUTOSUGGEST_ASYNC_FD
# We won't know the pid unless the user has zsh/system module installed
if [[ -n "$_ZSH_AUTOSUGGEST_CHILD_PID" ]]; then
# Zsh will make a new process group for the child process only if job
# control is enabled (MONITOR option)
if [[ -o MONITOR ]]; then
@@ -750,6 +784,7 @@ _zsh_autosuggest_async_request() {
kill -TERM $_ZSH_AUTOSUGGEST_CHILD_PID 2>/dev/null
fi
fi
fi
# Fork a process to fetch a suggestion and open a pipe to read from it
exec {_ZSH_AUTOSUGGEST_ASYNC_FD}< <(