Skip to content

Commit

Permalink
Add possibility to replace the entire buffer from suggestion strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvdk committed Jan 3, 2025
1 parent 0e810e5 commit f0a08e1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/async.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ _zsh_autosuggest_async_request() {
# Fetch and print the suggestion
local suggestion
local new_buffer
_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
Expand All @@ -61,11 +63,13 @@ _zsh_autosuggest_async_response() {
emulate -L zsh

local suggestion
local new_buffer

if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion
IFS='' read -rd '' -u $1 suggestion
zle autosuggest-suggest -- "$suggestion"
read -r -u $1 suggestion
read -r -u $1 new_buffer
zle autosuggest-suggest -- "$suggestion" "$new_buffer"

# Close the fd
builtin exec {1}<&-
Expand Down
5 changes: 4 additions & 1 deletion src/fetch.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ _zsh_autosuggest_fetch_suggestion() {
_zsh_autosuggest_strategy_$strategy "$1"

# 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
[[ -n "$suggestion" ]] && break
Expand Down
14 changes: 11 additions & 3 deletions src/widgets.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#

_ZSH_AUTOSUGGEST_NEW_BUFFER=

# Disable suggestions
_zsh_autosuggest_disable() {
typeset -g _ZSH_AUTOSUGGEST_DISABLED
Expand Down Expand Up @@ -88,8 +90,9 @@ _zsh_autosuggest_fetch() {
_zsh_autosuggest_async_request "$BUFFER"
else
local suggestion
local new_buffer
_zsh_autosuggest_fetch_suggestion "$BUFFER"
_zsh_autosuggest_suggest "$suggestion"
_zsh_autosuggest_suggest "$suggestion" "$new_buffer"
fi
}

Expand All @@ -98,11 +101,14 @@ _zsh_autosuggest_suggest() {
emulate -L zsh

local suggestion="$1"
local new_buffer="$2"

if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
POSTDISPLAY="${suggestion#$BUFFER}"
_ZSH_AUTOSUGGEST_NEW_BUFFER=$new_buffer
else
POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
fi
}

Expand All @@ -125,10 +131,11 @@ _zsh_autosuggest_accept() {

# Only accept if the cursor is at the end of the buffer
# Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY"
BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"

# Remove the suggestion
POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=

# Run the original widget before manually moving the cursor so that the
# cursor movement doesn't make the widget do something unexpected
Expand All @@ -148,10 +155,11 @@ _zsh_autosuggest_accept() {
# Accept the entire suggestion and execute it
_zsh_autosuggest_execute() {
# Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY"
BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"

# Remove the suggestion
POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=

# Call the original `accept-line` to handle syntax highlighting or
# other potential custom behavior
Expand Down
29 changes: 22 additions & 7 deletions zsh-autosuggestions.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ _zsh_autosuggest_highlight_apply() {
# Autosuggest Widget Implementations #
#--------------------------------------------------------------------#

_ZSH_AUTOSUGGEST_NEW_BUFFER=

# Disable suggestions
_zsh_autosuggest_disable() {
typeset -g _ZSH_AUTOSUGGEST_DISABLED
Expand Down Expand Up @@ -352,8 +354,9 @@ _zsh_autosuggest_fetch() {
_zsh_autosuggest_async_request "$BUFFER"
else
local suggestion
local new_buffer
_zsh_autosuggest_fetch_suggestion "$BUFFER"
_zsh_autosuggest_suggest "$suggestion"
_zsh_autosuggest_suggest "$suggestion" "$new_buffer"
fi
}

Expand All @@ -362,11 +365,14 @@ _zsh_autosuggest_suggest() {
emulate -L zsh

local suggestion="$1"
local new_buffer="$2"

if [[ -n "$suggestion" ]] && (( $#BUFFER )); then
POSTDISPLAY="${suggestion#$BUFFER}"
_ZSH_AUTOSUGGEST_NEW_BUFFER=$new_buffer
else
POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=
fi
}

Expand All @@ -389,10 +395,11 @@ _zsh_autosuggest_accept() {

# Only accept if the cursor is at the end of the buffer
# Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY"
BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"

# Remove the suggestion
POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=

# Run the original widget before manually moving the cursor so that the
# cursor movement doesn't make the widget do something unexpected
Expand All @@ -412,10 +419,11 @@ _zsh_autosuggest_accept() {
# Accept the entire suggestion and execute it
_zsh_autosuggest_execute() {
# Add the suggestion to the buffer
BUFFER="$BUFFER$POSTDISPLAY"
BUFFER="${_ZSH_AUTOSUGGEST_NEW_BUFFER:-"$BUFFER$POSTDISPLAY"}"

# Remove the suggestion
POSTDISPLAY=
_ZSH_AUTOSUGGEST_NEW_BUFFER=

# Call the original `accept-line` to handle syntax highlighting or
# other potential custom behavior
Expand Down Expand Up @@ -749,7 +757,10 @@ _zsh_autosuggest_fetch_suggestion() {
_zsh_autosuggest_strategy_$strategy "$1"

# 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
[[ -n "$suggestion" ]] && break
Expand Down Expand Up @@ -795,8 +806,10 @@ _zsh_autosuggest_async_request() {
# Fetch and print the suggestion
local suggestion
local new_buffer
_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
Expand All @@ -818,11 +831,13 @@ _zsh_autosuggest_async_response() {
emulate -L zsh

local suggestion
local new_buffer

if [[ -z "$2" || "$2" == "hup" ]]; then
# Read everything from the fd and give it as a suggestion
IFS='' read -rd '' -u $1 suggestion
zle autosuggest-suggest -- "$suggestion"
read -r -u $1 suggestion
read -r -u $1 new_buffer
zle autosuggest-suggest -- "$suggestion" "$new_buffer"

# Close the fd
builtin exec {1}<&-
Expand Down

0 comments on commit f0a08e1

Please sign in to comment.