Merge pull request #418 from zsh-users/features/manual-rebind

Allow disabling of automatic widget re-binding
This commit is contained in:
Eric Freese 2019-04-10 13:53:37 -06:00 committed by GitHub
commit 937d6fc241
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 27 deletions

View file

@ -63,12 +63,16 @@ Widgets that modify the buffer and are not found in any of these arrays will fet
### Disabling suggestion for large buffers ### Disabling suggestion for large buffers
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. 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.
This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for too long strings. This can be useful when pasting large amount of text in the terminal, to avoid triggering autosuggestion for strings that are too long.
### Enable Asynchronous Mode ### Enable Asynchronous Mode
As of `v0.4.0`, suggestions can be fetched asynchronously. To enable this behavior, set the `ZSH_AUTOSUGGEST_USE_ASYNC` variable (it can be set to anything). As of `v0.4.0`, suggestions can be fetched asynchronously. To enable this behavior, set the `ZSH_AUTOSUGGEST_USE_ASYNC` variable (it can be set to anything).
### Disabling automatic widget re-binding
Set `ZSH_AUTOSUGGEST_MANUAL_REBIND` (it can be set to anything) to disable automatic widget re-binding on each precmd. This can be a big boost to performance, but you'll need to handle re-binding yourself if any of the widget lists change or if you or another plugin wrap any of the autosuggest widgets. To re-bind widgets, run `_zsh_autosuggest_bind_widgets`.
### Key Bindings ### Key Bindings

View file

@ -94,4 +94,27 @@ describe 'a modification to the widget lists' do
wait_for { session.content(esc_seqs: true) }.to eq('echo hello') wait_for { session.content(esc_seqs: true) }.to eq('echo hello')
end end
end end
context 'when manual rebind is enabled' do
let(:options) { ["ZSH_AUTOSUGGEST_MANUAL_REBIND=true"] }
it 'does not take effect until bind command is re-run' do
with_history('echo hello') do
session.send_string('e')
wait_for { session.content }.to eq('echo hello')
session.send_keys('C-b')
sleep 1
expect(session.content(esc_seqs: true)).not_to eq('echo hello')
session.send_keys('C-c')
session.run_command('_zsh_autosuggest_bind_widgets').clear_screen
wait_for { session.content }.to eq('')
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')
end
end
end
end end

View file

@ -85,7 +85,3 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
yank-pop yank-pop
) )
} }
# Max size of buffer to trigger autosuggestion. Leave null for no upper bound.
(( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) &&
typeset -g ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=

View file

@ -5,15 +5,17 @@
# Start the autosuggestion widgets # Start the autosuggestion widgets
_zsh_autosuggest_start() { _zsh_autosuggest_start() {
# By default we re-bind widgets on every precmd to ensure we wrap other
# wrappers. Specifically, highlighting breaks if our widgets are wrapped by
# zsh-syntax-highlighting widgets. This also allows modifications to the
# widget list variables to take effect on the next precmd. However this has
# a decent performance hit, so users can set ZSH_AUTOSUGGEST_MANUAL_REBIND
# to disable the automatic re-binding.
if (( ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )); then
add-zsh-hook -d precmd _zsh_autosuggest_start add-zsh-hook -d precmd _zsh_autosuggest_start
fi
_zsh_autosuggest_bind_widgets _zsh_autosuggest_bind_widgets
# Re-bind widgets on every precmd to ensure we wrap other wrappers.
# Specifically, highlighting breaks if our widgets are wrapped by
# zsh-syntax-highlighting widgets. This also allows modifications
# to the widget list variables to take effect on the next precmd.
add-zsh-hook precmd _zsh_autosuggest_bind_widgets
} }
# Start the autosuggestion widgets on the next precmd # Start the autosuggestion widgets on the next precmd

View file

@ -85,7 +85,7 @@ _zsh_autosuggest_modify() {
# Get a new suggestion if the buffer is not empty after modification # Get a new suggestion if the buffer is not empty after modification
if (( $#BUFFER > 0 )); then if (( $#BUFFER > 0 )); then
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then if (( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
_zsh_autosuggest_fetch _zsh_autosuggest_fetch
fi fi
fi fi
@ -95,7 +95,7 @@ _zsh_autosuggest_modify() {
# Fetch a new suggestion based on what's currently in the buffer # Fetch a new suggestion based on what's currently in the buffer
_zsh_autosuggest_fetch() { _zsh_autosuggest_fetch() {
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then if (( ${+ZSH_AUTOSUGGEST_USE_ASYNC} )); then
_zsh_autosuggest_async_request "$BUFFER" _zsh_autosuggest_async_request "$BUFFER"
else else
local suggestion local suggestion

View file

@ -122,10 +122,6 @@ typeset -g ZSH_AUTOSUGGEST_ORIGINAL_WIDGET_PREFIX=autosuggest-orig-
) )
} }
# Max size of buffer to trigger autosuggestion. Leave null for no upper bound.
(( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) &&
typeset -g ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
# Utility Functions # # Utility Functions #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
@ -356,7 +352,7 @@ _zsh_autosuggest_modify() {
# Get a new suggestion if the buffer is not empty after modification # Get a new suggestion if the buffer is not empty after modification
if (( $#BUFFER > 0 )); then if (( $#BUFFER > 0 )); then
if [[ -z "$ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE" ]] || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then if (( ! ${+ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE} )) || (( $#BUFFER <= $ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE )); then
_zsh_autosuggest_fetch _zsh_autosuggest_fetch
fi fi
fi fi
@ -366,7 +362,7 @@ _zsh_autosuggest_modify() {
# Fetch a new suggestion based on what's currently in the buffer # Fetch a new suggestion based on what's currently in the buffer
_zsh_autosuggest_fetch() { _zsh_autosuggest_fetch() {
if [[ -n "${ZSH_AUTOSUGGEST_USE_ASYNC+x}" ]]; then if (( ${+ZSH_AUTOSUGGEST_USE_ASYNC} )); then
_zsh_autosuggest_async_request "$BUFFER" _zsh_autosuggest_async_request "$BUFFER"
else else
local suggestion local suggestion
@ -673,15 +669,17 @@ _zsh_autosuggest_async_response() {
# Start the autosuggestion widgets # Start the autosuggestion widgets
_zsh_autosuggest_start() { _zsh_autosuggest_start() {
# By default we re-bind widgets on every precmd to ensure we wrap other
# wrappers. Specifically, highlighting breaks if our widgets are wrapped by
# zsh-syntax-highlighting widgets. This also allows modifications to the
# widget list variables to take effect on the next precmd. However this has
# a decent performance hit, so users can set ZSH_AUTOSUGGEST_MANUAL_REBIND
# to disable the automatic re-binding.
if (( ${+ZSH_AUTOSUGGEST_MANUAL_REBIND} )); then
add-zsh-hook -d precmd _zsh_autosuggest_start add-zsh-hook -d precmd _zsh_autosuggest_start
fi
_zsh_autosuggest_bind_widgets _zsh_autosuggest_bind_widgets
# Re-bind widgets on every precmd to ensure we wrap other wrappers.
# Specifically, highlighting breaks if our widgets are wrapped by
# zsh-syntax-highlighting widgets. This also allows modifications
# to the widget list variables to take effect on the next precmd.
add-zsh-hook precmd _zsh_autosuggest_bind_widgets
} }
# Start the autosuggestion widgets on the next precmd # Start the autosuggestion widgets on the next precmd