From d854118c8df8c413d069f7e88bb01b9e18e4c8ed Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 20 Dec 2015 13:28:11 -0500 Subject: Teach psql's tab completion to consider the entire input string. Up to now, the tab completion logic has only examined the last few words of the current input line; "last few" being originally as few as four words, but lately up to nine words. Furthermore, it only looked at what libreadline considers the current line of input, which made it rather myopic if you split your command across lines. This was tolerable, sort of, so long as the match patterns were only designed to consider the last few words of input; but with the recent addition of HeadMatches() and Matches() matching rules, we really have to do better if we want those to behave sanely. Hence, change the code to break the entire line down into words, and to include any previous lines in the command buffer along with the active readline input buffer. This will be a little bit slower than the previous coding, but some measurements say that even a query of several thousand characters can be parsed in a hundred or so microseconds on modern machines; so it's really not going to be significant for interactive tab completion. To reduce the cost some, I arranged to avoid the per-word malloc calls that used to occur: all the words are now kept in one malloc'd buffer. --- src/bin/psql/input.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/bin/psql/input.c') diff --git a/src/bin/psql/input.c b/src/bin/psql/input.c index 2bc065adcff..ccd9a3ef611 100644 --- a/src/bin/psql/input.c +++ b/src/bin/psql/input.c @@ -53,12 +53,17 @@ static void finishInput(void); * gets_interactive() * * Gets a line of interactive input, using readline if desired. + * + * prompt: the prompt string to be used + * query_buf: buffer containing lines already read in the current command + * (query_buf is not modified here, but may be consulted for tab completion) + * * The result is a malloc'd string. * * Caller *must* have set up sigint_interrupt_jmp before calling. */ char * -gets_interactive(const char *prompt) +gets_interactive(const char *prompt, PQExpBuffer query_buf) { #ifdef USE_READLINE if (useReadline) @@ -76,6 +81,9 @@ gets_interactive(const char *prompt) rl_reset_screen_size(); #endif + /* Make current query_buf available to tab completion callback */ + tab_completion_query_buf = query_buf; + /* Enable SIGINT to longjmp to sigint_interrupt_jmp */ sigint_interrupt_enabled = true; @@ -85,6 +93,9 @@ gets_interactive(const char *prompt) /* Disable SIGINT again */ sigint_interrupt_enabled = false; + /* Pure neatnik-ism */ + tab_completion_query_buf = NULL; + return result; } #endif -- cgit v1.2.3