Skip to content

Commit 7fa3db3

Browse files
committed
psql: handle tab completion of timezone names after "SET TIMEZONE TO".
Dagfinn Ilmari Mannsåker and Tom Lane Discussion: https://postgr.es/m/87k0curq0w.fsf@wibble.ilmari.org
1 parent ba9a7e3 commit 7fa3db3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ sub clear_line
338338

339339
clear_line();
340340

341+
# check timezone name completion
342+
check_completion(
343+
"SET timezone TO am\t",
344+
qr|'America/|,
345+
"offer partial timezone name");
346+
347+
check_completion(
348+
"new_\t",
349+
qr|New_York|,
350+
"complete partial timezone name");
351+
352+
clear_line();
353+
341354
# check completion of a keyword offered in addition to object names;
342355
# such a keyword should obey COMP_KEYWORD_CASE
343356
foreach (

src/bin/psql/tab-complete.c

+35
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,24 @@ do { \
402402
matches = rl_completion_matches(text, complete_from_schema_query); \
403403
} while (0)
404404

405+
/*
406+
* Timezone completion is mostly like enum label completion, but we work
407+
* a little harder since this is a more common use-case.
408+
*/
409+
#define COMPLETE_WITH_TIMEZONE_NAME() \
410+
do { \
411+
static const char *const list[] = { "DEFAULT", NULL }; \
412+
if (text[0] == '\'') \
413+
completion_charp = Query_for_list_of_timezone_names_quoted_in; \
414+
else if (start == 0 || rl_line_buffer[start - 1] != '\'') \
415+
completion_charp = Query_for_list_of_timezone_names_quoted_out; \
416+
else \
417+
completion_charp = Query_for_list_of_timezone_names_unquoted; \
418+
completion_charpp = list; \
419+
completion_verbatim = true; \
420+
matches = rl_completion_matches(text, complete_from_query); \
421+
} while (0)
422+
405423
#define COMPLETE_WITH_FUNCTION_ARG(function) \
406424
do { \
407425
set_completion_reference(function); \
@@ -1105,6 +1123,21 @@ static const SchemaQuery Query_for_trigger_of_table = {
11051123
" FROM pg_catalog.pg_cursors "\
11061124
" WHERE name LIKE '%s'"
11071125

1126+
#define Query_for_list_of_timezone_names_unquoted \
1127+
" SELECT name "\
1128+
" FROM pg_catalog.pg_timezone_names() "\
1129+
" WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1130+
1131+
#define Query_for_list_of_timezone_names_quoted_out \
1132+
"SELECT pg_catalog.quote_literal(name) AS name "\
1133+
" FROM pg_catalog.pg_timezone_names() "\
1134+
" WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')"
1135+
1136+
#define Query_for_list_of_timezone_names_quoted_in \
1137+
"SELECT pg_catalog.quote_literal(name) AS name "\
1138+
" FROM pg_catalog.pg_timezone_names() "\
1139+
" WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
1140+
11081141
/*
11091142
* These object types were introduced later than our support cutoff of
11101143
* server version 9.2. We use the VersionedQuery infrastructure so that
@@ -4176,6 +4209,8 @@ psql_completion(const char *text, int start, int end)
41764209
" AND nspname NOT LIKE E'pg\\\\_temp%%'",
41774210
"DEFAULT");
41784211
}
4212+
else if (TailMatches("TimeZone", "TO|="))
4213+
COMPLETE_WITH_TIMEZONE_NAME();
41794214
else
41804215
{
41814216
/* generic, type based, GUC support */

0 commit comments

Comments
 (0)