Skip to content

Commit fe75517

Browse files
committed
Fix psql's tab-completion of enum label values.
Since enum labels have to be single-quoted, this part of the tab completion machinery got side-swiped by commit cd69ec6. A side-effect of that commit is that (at least with some versions of Readline) the text string passed for completion will omit the leading quote mark of the enum label literal. Libedit still acts the same as before, though, so adapt COMPLETE_WITH_ENUM_VALUE so that it can cope with either convention. Also, when we fail to find any valid completion, set rl_completion_suppress_quote = 1. Otherwise readline will go ahead and append a closing quote, which is unwanted. Per report from Peter Eisentraut. Back-patch to v13 where cd69ec6 came in. Discussion: https://postgr.es/m/8ca82d89-ec3d-8b28-8291-500efaf23b25@enterprisedb.com
1 parent ed48e35 commit fe75517

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/bin/psql/t/010_tab_completion.pl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
$node->safe_psql('postgres',
4343
"CREATE TABLE tab1 (f1 int, f2 text);\n"
4444
. "CREATE TABLE mytab123 (f1 int, f2 text);\n"
45-
. "CREATE TABLE mytab246 (f1 int, f2 text);\n");
45+
. "CREATE TABLE mytab246 (f1 int, f2 text);\n"
46+
. "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz');\n");
4647

4748
# Developers would not appreciate this test adding a bunch of junk to
4849
# their ~/.psql_history, so be sure to redirect history into a temp file.
@@ -223,6 +224,16 @@ sub clear_line
223224

224225
clear_line();
225226

227+
# check enum label completion
228+
# some versions of readline/libedit require two tabs here, some only need one
229+
# also, some versions will offer quotes, some will not
230+
check_completion(
231+
"ALTER TYPE enum1 RENAME VALUE 'ba\t\t",
232+
qr|'?bar'? +'?baz'?|,
233+
"offer multiple enum choices");
234+
235+
clear_line();
236+
226237
# send psql an explicit \q to shut it down, else pty won't close properly
227238
$timer->start(5);
228239
$in .= "\\q\n";

src/bin/psql/tab-complete.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,25 +278,40 @@ do { \
278278
matches = rl_completion_matches(text, complete_from_query); \
279279
} while (0)
280280

281+
/*
282+
* libedit will typically include the literal's leading single quote in
283+
* "text", while readline will not. Adapt our offered strings to fit.
284+
* But include a quote if there's not one just before "text", to get the
285+
* user off to the right start.
286+
*/
281287
#define COMPLETE_WITH_ENUM_VALUE(type) \
282288
do { \
283289
char *_completion_schema; \
284290
char *_completion_type; \
291+
bool use_quotes; \
285292
\
286293
_completion_schema = strtokx(type, " \t\n\r", ".", "\"", 0, \
287294
false, false, pset.encoding); \
288295
(void) strtokx(NULL, " \t\n\r", ".", "\"", 0, \
289296
false, false, pset.encoding); \
290297
_completion_type = strtokx(NULL, " \t\n\r", ".", "\"", 0, \
291-
false, false, pset.encoding); \
292-
if (_completion_type == NULL)\
298+
false, false, pset.encoding); \
299+
use_quotes = (text[0] == '\'' || \
300+
start == 0 || rl_line_buffer[start - 1] != '\''); \
301+
if (_completion_type == NULL) \
293302
{ \
294-
completion_charp = Query_for_list_of_enum_values; \
303+
if (use_quotes) \
304+
completion_charp = Query_for_list_of_enum_values_quoted; \
305+
else \
306+
completion_charp = Query_for_list_of_enum_values_unquoted; \
295307
completion_info_charp = type; \
296308
} \
297309
else \
298310
{ \
299-
completion_charp = Query_for_list_of_enum_values_with_schema; \
311+
if (use_quotes) \
312+
completion_charp = Query_for_list_of_enum_values_with_schema_quoted; \
313+
else \
314+
completion_charp = Query_for_list_of_enum_values_with_schema_unquoted; \
300315
completion_info_charp = _completion_type; \
301316
completion_info_charp2 = _completion_schema; \
302317
} \
@@ -678,7 +693,7 @@ static const SchemaQuery Query_for_list_of_collations = {
678693
" AND (pg_catalog.quote_ident(nspname)='%s' "\
679694
" OR '\"' || nspname || '\"' ='%s') "
680695

681-
#define Query_for_list_of_enum_values \
696+
#define Query_for_list_of_enum_values_quoted \
682697
"SELECT pg_catalog.quote_literal(enumlabel) "\
683698
" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\
684699
" WHERE t.oid = e.enumtypid "\
@@ -687,7 +702,16 @@ static const SchemaQuery Query_for_list_of_collations = {
687702
" OR '\"' || typname || '\"'='%s') "\
688703
" AND pg_catalog.pg_type_is_visible(t.oid)"
689704

690-
#define Query_for_list_of_enum_values_with_schema \
705+
#define Query_for_list_of_enum_values_unquoted \
706+
"SELECT enumlabel "\
707+
" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t "\
708+
" WHERE t.oid = e.enumtypid "\
709+
" AND substring(enumlabel,1,%d)='%s' "\
710+
" AND (pg_catalog.quote_ident(typname)='%s' "\
711+
" OR '\"' || typname || '\"'='%s') "\
712+
" AND pg_catalog.pg_type_is_visible(t.oid)"
713+
714+
#define Query_for_list_of_enum_values_with_schema_quoted \
691715
"SELECT pg_catalog.quote_literal(enumlabel) "\
692716
" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\
693717
" WHERE t.oid = e.enumtypid "\
@@ -698,6 +722,17 @@ static const SchemaQuery Query_for_list_of_collations = {
698722
" AND (pg_catalog.quote_ident(nspname)='%s' "\
699723
" OR '\"' || nspname || '\"' ='%s') "
700724

725+
#define Query_for_list_of_enum_values_with_schema_unquoted \
726+
"SELECT enumlabel "\
727+
" FROM pg_catalog.pg_enum e, pg_catalog.pg_type t, pg_catalog.pg_namespace n "\
728+
" WHERE t.oid = e.enumtypid "\
729+
" AND n.oid = t.typnamespace "\
730+
" AND substring(enumlabel,1,%d)='%s' "\
731+
" AND (pg_catalog.quote_ident(typname)='%s' "\
732+
" OR '\"' || typname || '\"'='%s') "\
733+
" AND (pg_catalog.quote_ident(nspname)='%s' "\
734+
" OR '\"' || nspname || '\"' ='%s') "
735+
701736
#define Query_for_list_of_template_databases \
702737
"SELECT pg_catalog.quote_ident(d.datname) "\
703738
" FROM pg_catalog.pg_database d "\
@@ -4417,8 +4452,12 @@ psql_completion(const char *text, int start, int end)
44174452
if (matches == NULL)
44184453
{
44194454
COMPLETE_WITH_CONST(true, "");
4455+
/* Also, prevent Readline from appending stuff to the non-match */
44204456
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
44214457
rl_completion_append_character = '\0';
4458+
#endif
4459+
#ifdef HAVE_RL_COMPLETION_SUPPRESS_QUOTE
4460+
rl_completion_suppress_quote = 1;
44224461
#endif
44234462
}
44244463

0 commit comments

Comments
 (0)