Skip to content

Commit cf445d0

Browse files
authored
Merge pull request #487 from zsh-users/features/completion-ignore
Allow skipping completion suggestions when buffer matches a pattern
2 parents 0d2c9de + 7afb736 commit cf445d0

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
@@ -592,6 +592,12 @@ _zsh_autosuggest_capture_completion_async() {
592592
}
593593

594594
_zsh_autosuggest_strategy_completion() {
595+
# Reset options to defaults and enable LOCAL_OPTIONS
596+
emulate -L zsh
597+
598+
# Enable extended glob for completion ignore pattern
599+
setopt EXTENDED_GLOB
600+
595601
typeset -g suggestion
596602
local line REPLY
597603

@@ -601,6 +607,9 @@ _zsh_autosuggest_strategy_completion() {
601607
# Exit if we don't have zpty
602608
zmodload zsh/zpty 2>/dev/null || return
603609

610+
# Exit if our search string matches the ignore pattern
611+
[[ -n "$ZSH_AUTOSUGGEST_COMPLETION_IGNORE" ]] && [[ "$1" == $~ZSH_AUTOSUGGEST_COMPLETION_IGNORE ]] && return
612+
604613
# Zle will be inactive if we are in async mode
605614
if zle; then
606615
zpty $ZSH_AUTOSUGGEST_COMPLETIONS_PTY_NAME _zsh_autosuggest_capture_completion_sync

0 commit comments

Comments
 (0)