From 712244a029c0f30fe1195a1400aee85541aa4b6b Mon Sep 17 00:00:00 2001
In-Reply-To: <1227.1543008751@sss.pgh.pa.us>
References: <1227.1543008751@sss.pgh.pa.us>
From: David Fetter <david@fetter.org>
Date: Fri, 23 Nov 2018 14:55:17 -0800
Subject: [PATCH] v0002 Surface tab completions for debugging

To access:
    make -s COPT=-DTABCOMPLETION_DEBUG
---
 src/bin/psql/tab-complete.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 9dbd555166..8b81c90d19 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -60,6 +60,32 @@ extern char *filename_completion_function();
 #define completion_matches rl_completion_matches
 #endif
 
+/*
+ * By enabling the following definition the source line number is emitted to
+ * stderr for every completion attempt. You can isolate them from console
+ * interaction by redirecting stderr into a file.
+ */
+#ifdef TABCOMPLETION_DEBUG
+#ifdef HAVE_RL_COMPLETION_MATCHES
+#define org_completion_matches rl_completion_matches
+#else
+#define org_completion_matches completion_matches
+#endif
+
+static char **completion_debug(int line, const char *text, char **list)
+{
+	fprintf(stderr, "[%d: (%s", line, text);
+	for (int i = 0; list && list[i]; ++i)
+		fprintf(stderr, ", %s", list[i]);
+	fprintf(stderr, ")]\n");
+	return list;
+}
+
+#undef completion_matches
+#define completion_matches(text, func) \
+	completion_debug(__LINE__, (text), org_completion_matches((text),(func)))
+#endif
+
 /* word break characters */
 #define WORD_BREAKS		"\t\n@$><=;|&{() "
 
-- 
2.19.1

