From 4bc47d44fa01417d4052634a003293dc4c6f15ce Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Mon, 26 Nov 2018 12:06:10 +0900
Subject: [PATCH 2/2] Add context information in tab-completion debug print

Psql shows completion context instead of just the word to complete.
---
 src/bin/psql/tab-complete.c | 48 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 00aa730050..3823a181ec 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -74,18 +74,37 @@ extern char *filename_completion_function();
 #endif
 
 #undef completion_matches
-#define completion_matches(t, f) completion_debug(__LINE__, (t), (f))
+#define completion_matches(text, func) \
+	completion_debug(__LINE__, (text), (func), \
+					 previous_words, previous_words_count)
 
-static char **completion_debug(int line,
-							   const char *text, rl_compentry_func_t *func)
+#define DEBUG_NCONTEXT 3
+
+static char **
+completion_debug(int line, const char *text, rl_compentry_func_t *func,
+				 char **previous_words, int previous_words_count)
 {
 	char **list = org_completion_matches(text, func);
+	int nwords = DEBUG_NCONTEXT;
 
 	/*
 	 * enclose empty list with brackets since it is an intermediate state
 	 * which is immediately followed by a non-empty list.
 	 */
-	fprintf(stderr, "%s%d: \"%s\" -> (", list ? "" : "[", line, text);
+	fprintf(stderr, "%s%d: ", list ? "" : "[", line);
+
+	fputc('(', stderr);
+	if (previous_words_count > 0)
+	{
+		if (previous_words_count > nwords)
+			fprintf(stderr, "...");
+		else
+			nwords = previous_words_count;
+		for (int i = 0 ; i < nwords ; ++i)
+			fprintf(stderr, "%s ", previous_words[nwords - i - 1]);
+	}
+	fprintf(stderr, "[%s]) -> (", text);
+
 	for (int i = 0; list && list[i]; ++i)
 		fprintf(stderr, "%s\"%s\"", i ? ", " : "", list[i]);			
 	fprintf(stderr, ")%s\n", list ? "": "]");
@@ -1046,7 +1065,8 @@ static void append_variable_names(char ***varnames, int *nvars,
 					  int *maxvars, const char *varname,
 					  const char *prefix, const char *suffix);
 static char **complete_from_variables(const char *text,
-						const char *prefix, const char *suffix, bool need_value);
+					  const char *prefix, const char *suffix, bool need_value,
+					  char ** previous_words, int previous_words_count);
 static char *complete_from_files(const char *text, int state);
 
 static char *pg_strdup_keyword_case(const char *s, const char *ref);
@@ -1419,11 +1439,14 @@ psql_completion(const char *text, int start, int end)
 	else if (text[0] == ':' && text[1] != ':')
 	{
 		if (text[1] == '\'')
-			matches = complete_from_variables(text, ":'", "'", true);
+			matches = complete_from_variables(text, ":'", "'", true,
+ 						previous_words, previous_words_count);
 		else if (text[1] == '"')
-			matches = complete_from_variables(text, ":\"", "\"", true);
+			matches = complete_from_variables(text, ":\"", "\"", true,
+ 						previous_words, previous_words_count);
 		else
-			matches = complete_from_variables(text, ":", "", true);
+			matches = complete_from_variables(text, ":", "", true,
+ 						previous_words, previous_words_count);
 	}
 
 	/* If no previous word, suggest one of the basic sql commands */
@@ -3583,9 +3606,11 @@ psql_completion(const char *text, int start, int end)
 			COMPLETE_WITH_CS("single", "double");
 	}
 	else if (TailMatchesCS("\\unset"))
-		matches = complete_from_variables(text, "", "", true);
+		matches = complete_from_variables(text, "", "", true,
+ 						previous_words, previous_words_count);
 	else if (TailMatchesCS("\\set"))
-		matches = complete_from_variables(text, "", "", false);
+		matches = complete_from_variables(text, "", "", false,
+ 						previous_words, previous_words_count);
 	else if (TailMatchesCS("\\set", MatchAny))
 	{
 		if (TailMatchesCS("AUTOCOMMIT|ON_ERROR_STOP|QUIET|"
@@ -4137,7 +4162,8 @@ append_variable_names(char ***varnames, int *nvars,
  */
 static char **
 complete_from_variables(const char *text, const char *prefix, const char *suffix,
-						bool need_value)
+						bool need_value,
+						char ** previous_words, int previous_words_count)
 {
 	char	  **matches;
 	char	  **varnames;
-- 
2.16.3

