Skip to content

Commit 7afb736

Browse files
committed
Allow skipping completion suggestions when buffer matches a pattern
Set ZSH_AUTOSUGGEST_COMPLETION_IGNORE to a glob pattern to have the completion suggestion strategy never make suggestions when the buffer matches the pattern. This can be helpful when some completion routines you have are particularly expensive and you want to prevent them from running automatically on every keystroke. See GitHub issue #463.
1 parent bdbe43e commit 7afb736

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ Set `ZSH_AUTOSUGGEST_HISTORY_IGNORE` to a glob pattern to prevent offering sugge
9393

9494
**Note:** This only affects the `history` and `match_prev_cmd` suggestion strategies.
9595

96+
### Skipping completion suggestions for certain cases
97+
98+
Set `ZSH_AUTOSUGGEST_COMPLETION_IGNORE` to a glob pattern to prevent offering completion suggestions when the buffer matches that pattern. For example, set it to `"git *"` to disable completion suggestions for git subcommands.
99+
100+
**Note:** This only affects the `completion` suggestion strategy.
101+
96102

97103
### Key Bindings
98104

spec/strategies/completion_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
session.
66
run_command('autoload compinit && compinit').
77
run_command('_foo() { compadd bar; compadd bat }').
8-
run_command('compdef _foo baz')
8+
run_command('_num() { compadd two; compadd three }').
9+
run_command('compdef _foo baz').
10+
run_command('compdef _num one')
911
end
1012
end
1113

@@ -37,6 +39,21 @@
3739
end
3840
end
3941

42+
context 'when ZSH_AUTOSUGGEST_COMPLETION_IGNORE is set to a pattern' do
43+
let(:options) { ['ZSH_AUTOSUGGEST_STRATEGY=completion', 'ZSH_AUTOSUGGEST_COMPLETION_IGNORE="one *"'] }
44+
45+
it 'makes suggestions when the buffer does not match the pattern' do
46+
session.send_string('baz ')
47+
wait_for { session.content }.to eq('baz bar')
48+
end
49+
50+
it 'does not make suggestions when the buffer matches the pattern' do
51+
session.send_string('one t')
52+
sleep 1
53+
expect(session.content).to eq('one t')
54+
end
55+
end
56+
4057
context 'when async mode is enabled' do
4158
let(:options) { ['ZSH_AUTOSUGGEST_USE_ASYNC=true', 'ZSH_AUTOSUGGEST_STRATEGY=completion'] }
4259

src/strategies/completion.zsh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ _zsh_autosuggest_capture_completion_async() {
9696
}
9797

9898
_zsh_autosuggest_strategy_completion() {
99+
# Reset options to defaults and enable LOCAL_OPTIONS
100+
emulate -L zsh
101+
102+
# Enable extended glob for completion ignore pattern
103+
setopt EXTENDED_GLOB
104+
99105
typeset -g suggestion
100106
local line REPLY
101107

@@ -105,6 +111,9 @@ _zsh_autosuggest_strategy_completion() {
105111
# Exit if we don't have zpty
106112
zmodload zsh/zpty 2>/dev/null || return
107113

114+
# Exit if our search string matches the ignore pattern
115+
[[ -n "$ZSH_AUTOSUGGEST_COMPLETION_IGNORE" ]] && [[ "$1" == $~ZSH_AUTOSUGGEST_COMPLETION_IGNORE ]] && return
116+
108117
# Zle will be inactive if we are in async mode
109118
if zle; then
110119
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync

zsh-autosuggestions.zsh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,12 @@ _zsh_autosuggest_capture_completion_async() {
582582
}
583583

584584
_zsh_autosuggest_strategy_completion() {
585+
# Reset options to defaults and enable LOCAL_OPTIONS
586+
emulate -L zsh
587+
588+
# Enable extended glob for completion ignore pattern
589+
setopt EXTENDED_GLOB
590+
585591
typeset -g suggestion
586592
local line REPLY
587593

@@ -591,6 +597,9 @@ _zsh_autosuggest_strategy_completion() {
591597
# Exit if we don't have zpty
592598
zmodload zsh/zpty 2>/dev/null || return
593599

600+
# Exit if our search string matches the ignore pattern
601+
[[ -n "$ZSH_AUTOSUGGEST_COMPLETION_IGNORE" ]] && [[ "$1" == $~ZSH_AUTOSUGGEST_COMPLETION_IGNORE ]] && return
602+
594603
# Zle will be inactive if we are in async mode
595604
if zle; then
596605
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync

0 commit comments

Comments
 (0)