pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Cc: tanghy.fnst@fujitsu.com <tanghy.fnst@fujitsu.com>
Cc: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Cc: Japin Li <japinli@hotmail.com>
Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Cc: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: smithpb2250@gmail.com <smithpb2250@gmail.com>
Cc: david.zhang@highgo.ca <david.zhang@highgo.ca>
Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Subject: Re: Support tab completion for upper character inputs in psql
Date: Mon, 31 Jan 2022 16:39:01 -0500
Message-ID: <838381.1643665141@sss.pgh.pa.us> (raw)
In-Reply-To: <534641.1643594097@sss.pgh.pa.us>
References: <OS0PR01MB611303B647967B6949B759F1FB4E9@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<OS0PR01MB6113CA04E06D5BF221BC4FE2FB429@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<OS0PR01MB61136721363BD08576725F22FB089@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<80b093ae-88bd-8d8a-68b9-b535a3ab11be@enterprisedb.com>
	<OS0PR01MB61137C7B2B09C0431415B9DBFBD69@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<54bd7839-f463-ef03-f2d2-ccb8184b9b27@enterprisedb.com>
	<1068530.1641480996@sss.pgh.pa.us>
	<TYCPR01MB6128BD8EA1529F4270114C18FB4D9@TYCPR01MB6128.jpnprd01.prod.outlook.com>
	<MEYP282MB1669EAF2183E67EAF86F7F61B64D9@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM>
	<TYCPR01MB6128D4933189A74AAD326F98FB4D9@TYCPR01MB6128.jpnprd01.prod.outlook.com>
	<3547066.1642272686@sss.pgh.pa.us>
	<OS0PR01MB6113C130461FB8A9DBA0DAEBFB5A9@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<OS0PR01MB6113DB08D8CEA4E214DA37BBFB5F9@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<2304294.1643152287@sss.pgh.pa.us>
	<2645164.1643315022@sss.pgh.pa.u! ! s>
	<OS0PR01MB611360D58EA41FC6557D92C0FB229@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<2771362.1643385796@sss.pgh.pa.us>
	<2805502.1643405157@sss.pgh.pa.us>
	<2818574.1643408192@sss.pgh.pa.us>
	<OS0PR01MB61130E73CFDB2C0E8FD1A892FB249@OS0PR01MB6113.jpnprd01.prod.outlook.com>
	<324281.1643567679@sss.pgh.pa.us>
	<8735l41ynm.fsf@wibble.ilmari.org>
	<534641.1643594097@sss.pgh.pa.us>

I wrote:
> =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= <ilmari@ilmari.org> writes:
>> First, as noted in the test, it doesn't preserve the case of the input
>> for keywords appended to the query result.  This is easily fixed by
>> using `pg_strdup_keyword_case()`, per the first attached patch.

> I thought about that, and intentionally didn't do it, because it
> would also affect the menus produced by tab completion.
> ...
> We could do something hacky like matching case only when there's
> no longer any matching object names, but that might be too magic.

I experimented with that, and it actually doesn't seem as weird
as I feared.  See if you like this ...

			regards, tom lane

Attachments:

respect-COMP_KEYWORD_CASE-v2.patch (text/x-diff, 3.9K) download diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index c4f6552ac9..7a265e0676 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -40,7 +40,7 @@ $node->start; # set up a few database objects $node->safe_psql('postgres', - "CREATE TABLE tab1 (f1 int primary key, f2 text);\n" + "CREATE TABLE tab1 (c1 int primary key, c2 text);\n" . "CREATE TABLE mytab123 (f1 int, f2 text);\n" . "CREATE TABLE mytab246 (f1 int, f2 text);\n" . "CREATE TABLE \"mixedName\" (f1 int, f2 text);\n" @@ -317,14 +317,30 @@ check_completion( clear_line(); -# check completion of a keyword offered in addition to object names -# (that code path currently doesn't preserve case of what's typed) -check_completion( - "comment on constraint foo on dom\t", - qr|DOMAIN|, - "offer keyword in addition to query result"); - -clear_query(); +# check completion of a keyword offered in addition to object names; +# such a keyword should obey COMP_KEYWORD_CASE once only keyword +# completions are possible +foreach ( + [ 'lower', 'CO', 'column' ], + [ 'upper', 'co', 'COLUMN' ], + [ 'preserve-lower', 'co', 'column' ], + [ 'preserve-upper', 'CO', 'COLUMN' ],) +{ + my ($case, $in, $out) = @$_; + + check_completion( + "\\set COMP_KEYWORD_CASE $case\n", + qr/postgres=#/, + "set completion case to '$case'"); + check_completion("alter table tab1 rename c\t\t", + qr|COLUMN|, + "offer keyword COLUMN for input c<TAB>, COMP_KEYWORD_CASE = $case"); + clear_query(); + check_completion("alter table tab1 rename $in\t\t\t", + qr|$out|, + "offer keyword $out for input $in<TAB>, COMP_KEYWORD_CASE = $case"); + clear_query(); +} # send psql an explicit \q to shut it down, else pty won't close properly $timer->start(5); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index b2ec50b4f2..bdc9760fba 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -4742,7 +4742,8 @@ _complete_from_query(const char *simple_query, { static int list_index, num_schema_only, - num_other; + num_other, + num_keywords; static PGresult *result = NULL; static bool non_empty_object; static bool schemaquoted; @@ -4766,6 +4767,7 @@ _complete_from_query(const char *simple_query, list_index = 0; num_schema_only = 0; num_other = 0; + num_keywords = 0; PQclear(result); result = NULL; @@ -4986,7 +4988,10 @@ _complete_from_query(const char *simple_query, /* In verbatim mode, we return all the items as-is */ if (verbatim) + { + num_other++; return pg_strdup(item); + } /* * In normal mode, a name requiring quoting will be returned only @@ -5031,8 +5036,12 @@ _complete_from_query(const char *simple_query, list_index++; if (pg_strncasecmp(text, item, strlen(text)) == 0) { - num_other++; - return pg_strdup(item); + num_keywords++; + /* Match keyword case if we are returning only keywords */ + if (num_schema_only == 0 && num_other == 0) + return pg_strdup_keyword_case(item, text); + else + return pg_strdup(item); } } } @@ -5049,8 +5058,12 @@ _complete_from_query(const char *simple_query, list_index++; if (pg_strncasecmp(text, item, strlen(text)) == 0) { - num_other++; - return pg_strdup(item); + num_keywords++; + /* Match keyword case if we are returning only keywords */ + if (num_schema_only == 0 && num_other == 0) + return pg_strdup_keyword_case(item, text); + else + return pg_strdup(item); } } } @@ -5062,7 +5075,7 @@ _complete_from_query(const char *simple_query, * completion subject text, which is not what we want. */ #ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER - if (num_schema_only > 0 && num_other == 0) + if (num_schema_only > 0 && num_other == 0 && num_keywords == 0) rl_completion_append_character = '\0'; #endif =

view thread (54+ messages)  latest in thread

Message-ID: <838381.1643665141@sss.pgh.pa.us>
Permalink:  ../838381.1643665141@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/838381.1643665141@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ilmari@ilmari.org, tanghy.fnst@fujitsu.com, peter.eisentraut@enterprisedb.com, japinli@hotmail.com, horikyota.ntt@gmail.com, laurenz.albe@cybertec.at, smithpb2250@gmail.com, david.zhang@highgo.ca, pgsql-hackers@lists.postgresql.org
  Subject: Re: Support tab completion for upper character inputs in psql
  In-Reply-To: <838381.1643665141@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox