Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nVHiq-0008Ry-Ci for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Mar 2022 18:53:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nVHip-0005UE-8a for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Mar 2022 18:53:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nVHio-0005U4-VY for pgsql-hackers@lists.postgresql.org; Fri, 18 Mar 2022 18:53:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nVHim-000616-F9 for pgsql-hackers@lists.postgresql.org; Fri, 18 Mar 2022 18:53:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 22IIr4hq1157473; Fri, 18 Mar 2022 14:53:04 -0400 From: Tom Lane To: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= cc: pgsql-hackers@lists.postgresql.org Subject: Re: Tab completion for SET TimeZone In-reply-to: <87a6dnsgyr.fsf@wibble.ilmari.org> References: <87k0curq0w.fsf@wibble.ilmari.org> <87h77yrpsu.fsf@wibble.ilmari.org> <514658.1647438049@sss.pgh.pa.us> <87czikrtc1.fsf@wibble.ilmari.org> <87a6dnsgyr.fsf@wibble.ilmari.org> Comments: In-reply-to =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= message dated "Fri, 18 Mar 2022 10:17:00 -0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1157326.1647629429.0@sss.pgh.pa.us> Date: Fri, 18 Mar 2022 14:53:04 -0400 Message-ID: <1157472.1647629584@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1157326.1647629429.1@sss.pgh.pa.us> =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= writes: > I just realised there's no point in the subselect when I'm not applying > the same function in the WHERE and the SELECT, so here's an updated > version that simplifies that. It also fixes a typo in the commit > message. This doesn't work right for me under libedit -- it will correctly complete "am" to "'America/", but then it fails to complete anything past that. The reason seems to be that once we have a leading single quote, libedit will include that in the text passed to future completion attempts, while readline won't. I ended up needing three query variants, as attached (bikeshedding welcome). I think the reason the COMPLETE_WITH_ENUM_VALUE macro doesn't look similar is that it hasn't made an attempt to work with input that the user didn't quote --- that is, if you type alter type planets rename value ur it just fails to match anything, instead of providing "'uranus'". Should we upgrade that likewise? Not sure it's worth the trouble though; I think COMPLETE_WITH_ENUM_VALUE is there more as a finger exercise than because people use it regularly. I added a regression test case too. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="v5-0001-Add-tab-completion-for-SET-TimeZone-TO.patch"; charset="us-ascii" Content-ID: <1157326.1647629429.2@sss.pgh.pa.us> Content-Description: v5-0001-Add-tab-completion-for-SET-TimeZone-TO.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab= _completion.pl index a54910680e..c6db1b1591 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -338,6 +338,19 @@ check_completion( = clear_line(); = +# check timezone name completion +check_completion( + "SET timezone TO am\t", + qr|'America/|, + "offer partial timezone name"); + +check_completion( + "new_\t", + qr|New_York|, + "complete partial timezone name"); + +clear_line(); + # check completion of a keyword offered in addition to object names; # such a keyword should obey COMP_KEYWORD_CASE foreach ( diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 380cbc0b1f..510ea1d926 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -402,6 +402,21 @@ do { \ matches =3D rl_completion_matches(text, complete_from_schema_query); \ } while (0) = +/* timezone completion is mostly like enum label completion */ +#define COMPLETE_WITH_TIMEZONE_NAME() \ +do { \ + static const char *const list[] =3D { "DEFAULT", NULL }; \ + if (text[0] =3D=3D '\'') \ + completion_charp =3D Query_for_list_of_timezone_names_quoted_in; \ + else if (start =3D=3D 0 || rl_line_buffer[start - 1] !=3D '\'') \ + completion_charp =3D Query_for_list_of_timezone_names_quoted_out; \ + else \ + completion_charp =3D Query_for_list_of_timezone_names_unquoted; \ + completion_charpp =3D list; \ + completion_verbatim =3D true; \ + matches =3D rl_completion_matches(text, complete_from_query); \ +} while (0) + #define COMPLETE_WITH_FUNCTION_ARG(function) \ do { \ set_completion_reference(function); \ @@ -1105,6 +1120,21 @@ static const SchemaQuery Query_for_trigger_of_table= =3D { " FROM pg_catalog.pg_cursors "\ " WHERE name LIKE '%s'" = +#define Query_for_list_of_timezone_names_unquoted \ +" SELECT name "\ +" FROM pg_catalog.pg_timezone_names() "\ +" WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')" + +#define Query_for_list_of_timezone_names_quoted_out \ +"SELECT pg_catalog.quote_literal(name) AS name "\ +" FROM pg_catalog.pg_timezone_names() "\ +" WHERE pg_catalog.lower(name) LIKE pg_catalog.lower('%s')" + +#define Query_for_list_of_timezone_names_quoted_in \ +"SELECT pg_catalog.quote_literal(name) AS name "\ +" FROM pg_catalog.pg_timezone_names() "\ +" WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.= lower('%s')" + /* * These object types were introduced later than our support cutoff of * server version 9.2. We use the VersionedQuery infrastructure so that @@ -4172,6 +4202,8 @@ psql_completion(const char *text, int start, int end= ) " AND nspname NOT LIKE E'pg\\\\_temp%%'", "DEFAULT"); } + else if (TailMatches("TimeZone", "TO|=3D")) + COMPLETE_WITH_TIMEZONE_NAME(); else { /* generic, type based, GUC support */ ------- =_aaaaaaaaaa0--