Commit d854118c authored by Tom Lane's avatar Tom Lane

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.
parent 69e7c44f
......@@ -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
......
......@@ -12,7 +12,7 @@
* If some other file needs to have access to readline/history, include this
* file and save yourself all this work.
*
* USE_READLINE is the definite pointers regarding existence or not.
* USE_READLINE is what to conditionalize readline-dependent code on.
*/
#ifdef HAVE_LIBREADLINE
#define USE_READLINE 1
......@@ -38,14 +38,14 @@
#include "pqexpbuffer.h"
char *gets_interactive(const char *prompt);
char *gets_fromFile(FILE *source);
extern char *gets_interactive(const char *prompt, PQExpBuffer query_buf);
extern char *gets_fromFile(FILE *source);
void initializeInput(int flags);
extern void initializeInput(int flags);
bool printHistory(const char *fname, unsigned short int pager);
extern bool printHistory(const char *fname, unsigned short int pager);
void pg_append_history(const char *s, PQExpBuffer history_buf);
void pg_send_history(PQExpBuffer history_buf);
extern void pg_append_history(const char *s, PQExpBuffer history_buf);
extern void pg_send_history(PQExpBuffer history_buf);
#endif /* INPUT_H */
......@@ -133,7 +133,7 @@ MainLoop(FILE *source)
/* May need to reset prompt, eg after \r command */
if (query_buf->len == 0)
prompt_status = PROMPT_READY;
line = gets_interactive(get_prompt(prompt_status));
line = gets_interactive(get_prompt(prompt_status), query_buf);
}
else
{
......
This diff is collapsed.
......@@ -8,8 +8,10 @@
#ifndef TAB_COMPLETE_H
#define TAB_COMPLETE_H
#include "postgres_fe.h"
#include "pqexpbuffer.h"
void initialize_readline(void);
extern PQExpBuffer tab_completion_query_buf;
#endif
extern void initialize_readline(void);
#endif /* TAB_COMPLETE_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment