Add possibility to replace the entire buffer from suggestion strategies

This commit is contained in:
Daan van der Kallen 2025-01-03 17:03:41 -05:00
parent 0e810e5afa
commit f0a08e1647
4 changed files with 44 additions and 14 deletions

View file

@ -38,8 +38,10 @@ _zsh_autosuggest_async_request() {
# Fetch and print the suggestion # Fetch and print the suggestion
local suggestion local suggestion
local new_buffer
_zsh_autosuggest_fetch_suggestion "$1" _zsh_autosuggest_fetch_suggestion "$1"
echo -nE "$suggestion" echo -E $suggestion
echo -E $new_buffer
) )
# There's a weird bug here where ^C stops working unless we force a fork # There's a weird bug here where ^C stops working unless we force a fork
@ -61,11 +63,13 @@ _zsh_autosuggest_async_response() {
emulate -L zsh emulate -L zsh
local suggestion local suggestion
local new_buffer
if [[ -z "$2" || "$2" == "hup" ]]; then if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion # Read everything from the fd and give it as a suggestion
IFS='' read -rd '' -u $1 suggestion read -r -u $1 suggestion
zle autosuggest-suggest -- "$suggestion" read -r -u $1 new_buffer
zle autosuggest-suggest -- "$suggestion" "$new_buffer"
# Close the fd # Close the fd
builtin exec {1}<&- builtin exec {1}<&-

View file

@ -19,7 +19,10 @@ _zsh_autosuggest_fetch_suggestion() {
_zsh_autosuggest_strategy_$strategy "$1" _zsh_autosuggest_strategy_$strategy "$1"
# Ensure the suggestion matches the prefix # Ensure the suggestion matches the prefix
[[ "$suggestion" != "$1"* ]] && unset suggestion if [[ "$suggestion" != "$1"* ]]; then
unset suggestion
unset new_buffer
fi
# Break once we've found a valid suggestion # Break once we've found a valid suggestion
[[ -n "$suggestion" ]] && break [[ -n "$suggestion" ]] && break

View file

@ -3,6 +3,8 @@
# Autosuggest Widget Implementations # # Autosuggest Widget Implementations #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
_ZSH_AUTOSUGGEST_NEW_BUFFER=
# Disable suggestions # Disable suggestions
_zsh_autosuggest_disable() { _zsh_autosuggest_disable() {
typeset -g _ZSH_AUTOSUGGEST_DISABLED typeset -g _ZSH_AUTOSUGGEST_DISABLED
@ -88,8 +90,9 @@ _zsh_autosuggest_fetch() {
_zsh_autosuggest_async_request "$BUFFER" _zsh_autosuggest_async_request "$BUFFER"
else else
local suggestion local suggestion
local new_buffer
_zsh_autosuggest_fetch_suggestion "$BUFFER" _zsh_autosuggest_fetch_suggestion "$BUFFER"
_zsh_autosuggest_suggest "$suggestion" _zsh_autosuggest_suggest "$suggestion" "$new_buffer"
fi fi
} }
@ -98,11 +101,14 @@ _zsh_autosuggest_suggest() {
emulate -L zsh emulate -L zsh
local suggestion="$1" local suggestion="$1"
local new_buffer="$2"
if [[ -n "$suggestion" ]] && (( $#BUFFER )); then if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
POSTDISPLAY="${suggestion#$BUFFER}" POSTDISPLAY="${suggestion#$BUFFER}"
_ZSH_AUTOSUGGEST_NEW_BUFFER=$new_buffer
else else
POSTDISPLAY= POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
fi fi
} }
@ -125,10 +131,11 @@ _zsh_autosuggest_accept() {
# Only accept if the cursor is at the end of the buffer # Only accept if the cursor is at the end of the buffer
# Add the suggestion to the buffer # Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY" BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"
# Remove the suggestion # Remove the suggestion
POSTDISPLAY= POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
# Run the original widget before manually moving the cursor so that the # Run the original widget before manually moving the cursor so that the
# cursor movement doesn't make the widget do something unexpected # cursor movement doesn't make the widget do something unexpected
@ -148,10 +155,11 @@ _zsh_autosuggest_accept() {
# Accept the entire suggestion and execute it # Accept the entire suggestion and execute it
_zsh_autosuggest_execute() { _zsh_autosuggest_execute() {
# Add the suggestion to the buffer # Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY" BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"
# Remove the suggestion # Remove the suggestion
POSTDISPLAY= POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
# Call the original `accept-line` to handle syntax highlighting or # Call the original `accept-line` to handle syntax highlighting or
# other potential custom behavior # other potential custom behavior

View file

@ -267,6 +267,8 @@ _zsh_autosuggest_highlight_apply() {
# Autosuggest Widget Implementations # # Autosuggest Widget Implementations #
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
_ZSH_AUTOSUGGEST_NEW_BUFFER=
# Disable suggestions # Disable suggestions
_zsh_autosuggest_disable() { _zsh_autosuggest_disable() {
typeset -g _ZSH_AUTOSUGGEST_DISABLED typeset -g _ZSH_AUTOSUGGEST_DISABLED
@ -352,8 +354,9 @@ _zsh_autosuggest_fetch() {
_zsh_autosuggest_async_request "$BUFFER" _zsh_autosuggest_async_request "$BUFFER"
else else
local suggestion local suggestion
local new_buffer
_zsh_autosuggest_fetch_suggestion "$BUFFER" _zsh_autosuggest_fetch_suggestion "$BUFFER"
_zsh_autosuggest_suggest "$suggestion" _zsh_autosuggest_suggest "$suggestion" "$new_buffer"
fi fi
} }
@ -362,11 +365,14 @@ _zsh_autosuggest_suggest() {
emulate -L zsh emulate -L zsh
local suggestion="$1" local suggestion="$1"
local new_buffer="$2"
if [[ -n "$suggestion" ]] && (( $#BUFFER )); then if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
POSTDISPLAY="${suggestion#$BUFFER}" POSTDISPLAY="${suggestion#$BUFFER}"
_ZSH_AUTOSUGGEST_NEW_BUFFER=$new_buffer
else else
POSTDISPLAY= POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
fi fi
} }
@ -389,10 +395,11 @@ _zsh_autosuggest_accept() {
# Only accept if the cursor is at the end of the buffer # Only accept if the cursor is at the end of the buffer
# Add the suggestion to the buffer # Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY" BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"
# Remove the suggestion # Remove the suggestion
POSTDISPLAY= POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
# Run the original widget before manually moving the cursor so that the # Run the original widget before manually moving the cursor so that the
# cursor movement doesn't make the widget do something unexpected # cursor movement doesn't make the widget do something unexpected
@ -412,10 +419,11 @@ _zsh_autosuggest_accept() {
# Accept the entire suggestion and execute it # Accept the entire suggestion and execute it
_zsh_autosuggest_execute() { _zsh_autosuggest_execute() {
# Add the suggestion to the buffer # Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY" BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"
# Remove the suggestion # Remove the suggestion
POSTDISPLAY= POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
# Call the original `accept-line` to handle syntax highlighting or # Call the original `accept-line` to handle syntax highlighting or
# other potential custom behavior # other potential custom behavior
@ -749,7 +757,10 @@ _zsh_autosuggest_fetch_suggestion() {
_zsh_autosuggest_strategy_$strategy "$1" _zsh_autosuggest_strategy_$strategy "$1"
# Ensure the suggestion matches the prefix # Ensure the suggestion matches the prefix
[[ "$suggestion" != "$1"* ]] && unset suggestion if [[ "$suggestion" != "$1"* ]]; then
unset suggestion
unset new_buffer
fi
# Break once we've found a valid suggestion # Break once we've found a valid suggestion
[[ -n "$suggestion" ]] && break [[ -n "$suggestion" ]] && break
@ -795,8 +806,10 @@ _zsh_autosuggest_async_request() {
# Fetch and print the suggestion # Fetch and print the suggestion
local suggestion local suggestion
local new_buffer
_zsh_autosuggest_fetch_suggestion "$1" _zsh_autosuggest_fetch_suggestion "$1"
echo -nE "$suggestion" echo -E $suggestion
echo -E $new_buffer
) )
# There's a weird bug here where ^C stops working unless we force a fork # There's a weird bug here where ^C stops working unless we force a fork
@ -818,11 +831,13 @@ _zsh_autosuggest_async_response() {
emulate -L zsh emulate -L zsh
local suggestion local suggestion
local new_buffer
if [[ -z "$2" || "$2" == "hup" ]]; then if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion # Read everything from the fd and give it as a suggestion
IFS='' read -rd '' -u $1 suggestion read -r -u $1 suggestion
zle autosuggest-suggest -- "$suggestion" read -r -u $1 new_buffer
zle autosuggest-suggest -- "$suggestion" "$new_buffer"
# Close the fd # Close the fd
builtin exec {1}<&- builtin exec {1}<&-