From 54e1eee924be6b11db297301d6f95d3d3ddba32b Mon Sep 17 00:00:00 2001
From: Eric Freese <ericdfreese@gmail.com>
Date: Wed, 25 Jan 2017 00:00:13 -0700
Subject: [PATCH] Optimize case where manually typing in a suggestion

---
 src/widgets.zsh         | 11 +++++++++++
 zsh-autosuggestions.zsh | 11 +++++++++++
 2 files changed, 22 insertions(+)

diff --git a/src/widgets.zsh b/src/widgets.zsh
index c6162b3..66eef58 100644
--- a/src/widgets.zsh
+++ b/src/widgets.zsh
@@ -26,6 +26,17 @@ _zsh_autosuggest_modify() {
 	_zsh_autosuggest_invoke_original_widget $@
 	retval=$?
 
+	# Optimize if manually typing in the suggestion
+	if [ $#BUFFER -gt $#orig_buffer ]; then
+		local added=${BUFFER#$orig_buffer}
+
+		# If the string added matches the beginning of the postdisplay
+		if [ "$added" = "${orig_postdisplay:0:$#added}" ]; then
+			POSTDISPLAY="${orig_postdisplay:$#added}"
+			return $retval
+		fi
+	fi
+
 	# Don't fetch a new suggestion if the buffer hasn't changed
 	if [ "$BUFFER" = "$orig_buffer" ]; then
 		POSTDISPLAY="$orig_postdisplay"
diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh
index bcee1ee..266cdff 100644
--- a/zsh-autosuggestions.zsh
+++ b/zsh-autosuggestions.zsh
@@ -291,6 +291,17 @@ _zsh_autosuggest_modify() {
 	_zsh_autosuggest_invoke_original_widget $@
 	retval=$?
 
+	# Optimize if manually typing in the suggestion
+	if [ $#BUFFER -gt $#orig_buffer ]; then
+		local added=${BUFFER#$orig_buffer}
+
+		# If the string added matches the beginning of the postdisplay
+		if [ "$added" = "${orig_postdisplay:0:$#added}" ]; then
+			POSTDISPLAY="${orig_postdisplay:$#added}"
+			return $retval
+		fi
+	fi
+
 	# Don't fetch a new suggestion if the buffer hasn't changed
 	if [ "$BUFFER" = "$orig_buffer" ]; then
 		POSTDISPLAY="$orig_postdisplay"