Skip to content

Commit

Permalink
Merge pull request #440 from zsh-users/develop
Browse files Browse the repository at this point in the history
v0.6.0
  • Loading branch information
ericfreese committed Jun 16, 2019
2 parents 848aa74 + 19c976f commit b3b34ea
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 40 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.6.0
- Added `completion` suggestion strategy powered by completion system (#111)
- Allow setting `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` to an empty string (#422)
- Don't fetch suggestions after copy-earlier-word (#439)
- Allow users to unignore zle-\* widgets (e.g. zle-line-init) (#432)


## v0.5.2
- Allow disabling automatic widget re-binding for better performance (#418)
- Fix async suggestions when `SH_WORD_SPLIT` is set
Expand Down Expand Up @@ -46,7 +53,7 @@
- Experimental support for asynchronous suggestions (#170)
- Fix problems with multi-line suggestions (#225)
- Optimize case where manually typing in suggestion
- Avoid wrapping any zle-* widgets (#206)
- Avoid wrapping any zle-\* widgets (#206)
- Remove support for deprecated options from v0.0.x
- Handle history entries that begin with dashes
- Gracefully handle being sourced multiple times (#126)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
SRC_DIR := ./src

SRC_FILES := \
$(SRC_DIR)/setup.zsh \
$(SRC_DIR)/config.zsh \
$(SRC_DIR)/util.zsh \
$(SRC_DIR)/bind.zsh \
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_[Fish](http://fishshell.com/)-like fast/unobtrusive autosuggestions for zsh._

It suggests commands as you type, based on command history.
It suggests commands as you type.

Requirements: Zsh v4.3.11 or later

Expand Down Expand Up @@ -34,15 +34,26 @@ You may want to override the default global config variables. Default values of

### Suggestion Highlight Style

Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion is shown with. The default is `fg=8`.
Set `ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE` to configure the style that the suggestion is shown with. The default is `fg=8`, which will set the foreground color to color 8 from the 256-color palette. If your terminal only supports 8 colors, you will need to use a number between 0 and 7.

Background color can also be set, and the suggestion can be styled bold, underlined, or standout. For example, this would show suggestions with bold, underlined, pink text on a cyan background:

```sh
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).


### Suggestion Strategy

`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:
`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 three built-in strategies to choose from:

- `history`: Chooses the most recent match from history.
- `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`.
- `completion`: (experimental) Chooses a suggestion based on what tab-completion would suggest. (requires `zpty` module)

For example, setting `ZSH_AUTOSUGGEST_STRATEGY=(history completion)` will first try to find a suggestion from your history, but, if it can't find a match, will find a suggestion from the completion engine.


### Widget Mapping
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.5.2
v0.6.0
27 changes: 25 additions & 2 deletions spec/async_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,39 @@
end
end

describe '`copy-earlier-word`' do
let(:before_sourcing) do
-> do
session.
run_command('autoload -Uz copy-earlier-word').
run_command('zle -N copy-earlier-word').
run_command('bindkey "^N" copy-earlier-word')
end
end

it 'should cycle through previous words in the buffer' do
session.clear_screen
session.send_string('foo bar baz')
sleep 0.5
session.send_keys('C-n')
wait_for { session.content }.to eq('foo bar bazbaz')
session.send_keys('C-n')
wait_for { session.content }.to eq('foo bar bazbar')
session.send_keys('C-n')
wait_for { session.content }.to eq('foo bar bazfoo')
end
end

describe 'pressing ^C after fetching a suggestion' do
before do
skip 'Workaround does not work below v5.0.8' if session.zsh_version < Gem::Version.new('5.0.8')
end

it 'terminates the prompt and begins a new one' do
session.send_keys('e')
sleep 0.1
sleep 0.5
session.send_keys('C-c')
sleep 0.1
sleep 0.5
session.send_keys('echo')

wait_for { session.content }.to eq("e\necho")
Expand Down
16 changes: 16 additions & 0 deletions spec/integrations/bracketed_paste_magic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,20 @@
end
end
end

context 'with `bracketed-paste` added to the list of widgets that clear the suggestion' do
let(:options) { ['ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste)'] }

it 'does not retain an old suggestion' do
with_history ('echo foo') do
session.send_string('echo ')
wait_for { session.content }.to eq('echo foo')
session.paste_string('bar')
wait_for { session.content }.to eq('echo bar')
session.send_keys('C-a') # Any cursor movement works
sleep 1
expect(session.content).to eq('echo bar')
end
end
end
end
14 changes: 14 additions & 0 deletions spec/integrations/client_zpty_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe 'a running zpty command' do
let(:before_sourcing) { -> { session.run_command('zmodload zsh/zpty && zpty -b kitty cat') } }

context 'when using `completion` strategy' do
let(:options) { ["ZSH_AUTOSUGGEST_STRATEGY=completion"] }

it 'is not affected' do
session.send_keys('a').send_keys('C-h')
session.run_command('zpty -t kitty; echo $?')

wait_for { session.content }.to end_with("\n0")
end
end
end
17 changes: 17 additions & 0 deletions spec/line_init_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
context 'with zle-line-init unignored' do
let(:after_sourcing) do
-> do
session.
run_command('setopt extendedglob').
run_command('ZSH_AUTOSUGGEST_IGNORE_WIDGETS=(${(@)ZSH_AUTOSUGGEST_IGNORE_WIDGETS:#zle-\*} zle-\^line-init)').
run_command('zle-line-init() { BUFFER="echo" }')
end
end

it 'should fetch a suggestion on each line initialization' do
with_history('echo foo') do
session.run_command('zle -N zle-line-init')
wait_for { session.content }.to end_with('echo foo')
end
end
end
3 changes: 1 addition & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

around do |example|
before_sourcing.call
session.run_command(options.join('; '))
session.run_command('source zsh-autosuggestions.zsh')
session.run_command(['source zsh-autosuggestions.zsh', *options].join('; '))
after_sourcing.call
session.clear_screen

Expand Down
38 changes: 38 additions & 0 deletions spec/strategies/completion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
describe 'the `completion` suggestion strategy' do
let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=completion'] }
let(:before_sourcing) do
-> do
session.
run_command('autoload compinit && compinit').
run_command('_foo() { compadd bar; compadd bat }').
run_command('compdef _foo baz')
end
end

it 'suggests the first completion result' do
session.send_string('baz ')
wait_for { session.content }.to eq('baz bar')
end

it 'does not add extra carriage returns when prefix has a line feed' do
skip '`stty` does not work inside zpty below zsh version 5.0.3' if session.zsh_version < Gem::Version.new('5.0.3')
session.send_string('baz \\').send_keys('C-v', 'C-j')
wait_for { session.content }.to eq("baz \\\nbar")
end

context 'when async mode is enabled' do
let(:options) { ['ZSH_AUTOSUGGEST_USE_ASYNC=true', 'ZSH_AUTOSUGGEST_STRATEGY=completion'] }

it 'suggests the first completion result' do
session.send_string('baz ')
wait_for { session.content }.to eq('baz bar')
end

it 'does not add extra carriage returns when prefix has a line feed' do
skip '`stty` does not work inside zpty below zsh version 5.0.3' if session.zsh_version < Gem::Version.new('5.0.3')
session.send_string('baz \\').send_keys('C-v', 'C-j')
wait_for { session.content }.to eq("baz \\\nbar")
end
end
end

5 changes: 4 additions & 1 deletion src/async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ _zsh_autosuggest_async_request() {
_zsh_autosuggest_async_response() {
emulate -L zsh

local suggestion

if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion
zle autosuggest-suggest -- "$(cat <&$1)"
IFS='' read -rd '' -u $1 suggestion
zle autosuggest-suggest -- "$suggestion"

# Close the fd
exec {1}<&-
Expand Down
1 change: 0 additions & 1 deletion src/bind.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ _zsh_autosuggest_bind_widgets() {
ignore_widgets=(
.\*
_\*
zle-\*
autosuggest-\*
$ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX\*
$ZSH_AUTOSUGGEST_IGNORE_WIDGETS
Expand Down
6 changes: 6 additions & 0 deletions src/config.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
up-line-or-history
down-line-or-history
accept-line
copy-earlier-word
)
}

Expand Down Expand Up @@ -83,5 +84,10 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
which-command
yank
yank-pop
zle-\*
)
}

# Pty name for capturing completions for completion suggestion strategy
(( ! ${+ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME} )) &&
typeset -g ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME=zsh_autosuggest_completion_pty
5 changes: 4 additions & 1 deletion src/fetch.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ _zsh_autosuggest_fetch_suggestion() {
# Try to get a suggestion from this strategy
_zsh_autosuggest_strategy_$strategy "$1"

# Break once we've found a suggestion
# Ensure the suggestion matches the prefix
[[ "$suggestion" != "$1"* ]] && unset suggestion

# Break once we've found a valid suggestion
[[ -n "$suggestion" ]] && break
done
}
10 changes: 0 additions & 10 deletions src/setup.zsh

This file was deleted.

1 change: 1 addition & 0 deletions src/start.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ _zsh_autosuggest_start() {
}

# Start the autosuggestion widgets on the next precmd
autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_autosuggest_start

0 comments on commit b3b34ea

Please sign in to comment.