Skip to content

Commit b598780

Browse files
committed
Adding support for ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE to disable suggestions on small buffers
1 parent a411ef3 commit b598780

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ Widgets that modify the buffer and are not found in any of these arrays will fet
7474
**Note:** A widget shouldn't belong to more than one of the above arrays.
7575

7676

77-
### Disabling suggestion for large buffers
77+
### Disabling suggestion for small or large buffers
7878

79-
Set `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` to an integer value to disable autosuggestion for large buffers. The default is unset, which means that autosuggestion will be tried for any buffer size. Recommended value is 20.
80-
This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for strings that are too long.
79+
Set `ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE` or `ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE` to an integer value to disable autosuggestion for small or larger buffers. The default for both is unset, which means that autosuggestion will be tried for any buffer size. Recommended range is [5, 20].
80+
This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for strings that are too long, or for irrelevant suggestions on smaller buffers.
8181

8282
### Asynchronous Mode
8383

spec/options/buffer_max_size_spec.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
wait_for { session.content }.to eq(long_command)
2626
end
2727

28-
it 'is not provided when the buffer is longer than the specified length'
28+
it 'is not provided when the buffer is longer than the specified length' do
29+
session.send_string(long_command[0...(buffer_max_size + 1)])
30+
wait_for { session.content }.to eq(long_command[0...(buffer_max_size + 1)])
31+
end
2932
end
3033
end

spec/options/buffer_min_size_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe 'a suggestion' do
2+
let(:term_opts) { { width: 200 } }
3+
let(:command) { "echo foobar" }
4+
5+
around do |example|
6+
with_history(command) { example.run }
7+
end
8+
9+
it 'is provided for any buffer length' do
10+
session.send_string(command[0...-1])
11+
wait_for { session.content }.to eq(command)
12+
end
13+
14+
context 'when ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE is specified' do
15+
let(:buffer_min_size) { 5 }
16+
let(:options) { ["ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE=#{buffer_min_size}"] }
17+
18+
it 'is provided when the buffer is longer than the specified length' do
19+
session.send_string(command[0...(buffer_min_size + 1)])
20+
wait_for { session.content }.to eq(command)
21+
end
22+
23+
it 'is provided when the buffer is equal to the specified length' do
24+
session.send_string(command[0...(buffer_min_size)])
25+
wait_for { session.content }.to eq(command)
26+
end
27+
28+
it 'is not provided when the buffer is shorter than the specified length' do
29+
session.send_string(command[0...(buffer_min_size - 1)])
30+
wait_for { session.content }.to eq(command[0...(buffer_min_size - 1)])
31+
end
32+
end
33+
end

src/widgets.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ _zsh_autosuggest_modify() {
7373
fi
7474

7575
# Get a new suggestion if the buffer is not empty after modification
76-
if (( $#BUFFER > 0 )); then
77-
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
76+
if ([[ -z "$ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE" ]] || (( $#BUFFER >= $ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE ))) && \
77+
([[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE ))); then
7878
_zsh_autosuggest_fetch
7979
fi
8080
fi

zsh-autosuggestions.zsh

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# v0.7.0
44
# Copyright (c) 2013 Thiago de Arruda
55
# Copyright (c) 2016-2021 Eric Freese
6-
#
6+
#
77
# Permission is hereby granted, free of charge, to any person
88
# obtaining a copy of this software and associated documentation
99
# files (the "Software"), to deal in the Software without
@@ -12,10 +12,10 @@
1212
# copies of the Software, and to permit persons to whom the
1313
# Software is furnished to do so, subject to the following
1414
# conditions:
15-
#
15+
#
1616
# The above copyright notice and this permission notice shall be
1717
# included in all copies or substantial portions of the Software.
18-
#
18+
#
1919
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2020
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
2121
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -335,10 +335,9 @@ _zsh_autosuggest_modify() {
335335
fi
336336

337337
# Get a new suggestion if the buffer is not empty after modification
338-
if (( $#BUFFER > 0 )); then
339-
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
338+
if ([[ -z "$ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE" ]] || (( $#BUFFER >= $ZSH_AUTOSUGGEST_BUFFER_MIN_SIZE ))) && \
339+
([[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE ))); then
340340
_zsh_autosuggest_fetch
341-
fi
342341
fi
343342

344343
return $retval

0 commit comments

Comments
 (0)