Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd854118

Browse files
committed
Teach psql's tab completion to consider the entire input string.
Up to now, the tab completion logic has only examined the last few wordsof the current input line; "last few" being originally as few as fourwords, but lately up to nine words. Furthermore, it only looked at whatlibreadline considers the current line of input, which made it rathermyopic if you split your command across lines. This was tolerable,sort of, so long as the match patterns were only designed to consider thelast few words of input; but with the recent addition of HeadMatches()and Matches() matching rules, we really have to do better if we wantthose to behave sanely.Hence, change the code to break the entire line down into words, and toinclude any previous lines in the command buffer along with the activereadline input buffer.This will be a little bit slower than the previous coding, but somemeasurements say that even a query of several thousand characters can beparsed in a hundred or so microseconds on modern machines; so it's reallynot going to be significant for interactive tab completion. To reducethe cost some, I arranged to avoid the per-word malloc calls that usedto occur: all the words are now kept in one malloc'd buffer.
1 parent69e7c44 commitd854118

File tree

5 files changed

+150
-95
lines changed

5 files changed

+150
-95
lines changed

‎src/bin/psql/input.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,17 @@ static void finishInput(void);
5353
* gets_interactive()
5454
*
5555
* Gets a line of interactive input, using readline if desired.
56+
*
57+
* prompt: the prompt string to be used
58+
* query_buf: buffer containing lines already read in the current command
59+
* (query_buf is not modified here, but may be consulted for tab completion)
60+
*
5661
* The result is a malloc'd string.
5762
*
5863
* Caller *must* have set up sigint_interrupt_jmp before calling.
5964
*/
6065
char*
61-
gets_interactive(constchar*prompt)
66+
gets_interactive(constchar*prompt,PQExpBufferquery_buf)
6267
{
6368
#ifdefUSE_READLINE
6469
if (useReadline)
@@ -76,6 +81,9 @@ gets_interactive(const char *prompt)
7681
rl_reset_screen_size();
7782
#endif
7883

84+
/* Make current query_buf available to tab completion callback */
85+
tab_completion_query_buf=query_buf;
86+
7987
/* Enable SIGINT to longjmp to sigint_interrupt_jmp */
8088
sigint_interrupt_enabled= true;
8189

@@ -85,6 +93,9 @@ gets_interactive(const char *prompt)
8593
/* Disable SIGINT again */
8694
sigint_interrupt_enabled= false;
8795

96+
/* Pure neatnik-ism */
97+
tab_completion_query_buf=NULL;
98+
8899
returnresult;
89100
}
90101
#endif

‎src/bin/psql/input.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* If some other file needs to have access to readline/history, include this
1313
* file and save yourself all this work.
1414
*
15-
* USE_READLINE isthe definite pointers regarding existence or not.
15+
* USE_READLINE iswhat to conditionalize readline-dependent code on.
1616
*/
1717
#ifdefHAVE_LIBREADLINE
1818
#defineUSE_READLINE 1
@@ -38,14 +38,14 @@
3838
#include"pqexpbuffer.h"
3939

4040

41-
char*gets_interactive(constchar*prompt);
42-
char*gets_fromFile(FILE*source);
41+
externchar*gets_interactive(constchar*prompt,PQExpBufferquery_buf);
42+
externchar*gets_fromFile(FILE*source);
4343

44-
voidinitializeInput(intflags);
44+
externvoidinitializeInput(intflags);
4545

46-
boolprintHistory(constchar*fname,unsigned shortintpager);
46+
externboolprintHistory(constchar*fname,unsigned shortintpager);
4747

48-
voidpg_append_history(constchar*s,PQExpBufferhistory_buf);
49-
voidpg_send_history(PQExpBufferhistory_buf);
48+
externvoidpg_append_history(constchar*s,PQExpBufferhistory_buf);
49+
externvoidpg_send_history(PQExpBufferhistory_buf);
5050

5151
#endif/* INPUT_H */

‎src/bin/psql/mainloop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ MainLoop(FILE *source)
133133
/* May need to reset prompt, eg after \r command */
134134
if (query_buf->len==0)
135135
prompt_status=PROMPT_READY;
136-
line=gets_interactive(get_prompt(prompt_status));
136+
line=gets_interactive(get_prompt(prompt_status),query_buf);
137137
}
138138
else
139139
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp