From 85a61d6d6390078ec3670467309503e103dfc040 Mon Sep 17 00:00:00 2001 From: Taiki Koshino Date: Thu, 26 Nov 2026 13:24:03 +0900 Subject: [PATCH v1] Improve tab completion for CREATE DOMAIN without AS CREATE DOMAIN allows the AS keyword before the base type to be omitted, but psql tab completion only offered AS after the domain name and only recognized subsequent clauses when AS was present. As a result, users writing the valid abbreviated form could not complete either the base type or domain options such as CHECK and DEFAULT. Offer data types together with AS after the domain name, and recognize the clauses following the base type in both forms. Also correct collation-name completion, whose previous pattern did not account for the base type preceding COLLATE and therefore did not match valid CREATE DOMAIN syntax. Add tests for data type, CHECK clause, and collation-name completion with and without AS. --- src/bin/psql/t/010_tab_completion.pl | 35 ++++++++++++++++++++++++++++ src/bin/psql/tab-complete.in.c | 10 +++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/t/010_tab_completion.pl b/src/bin/psql/t/010_tab_completion.pl index 1d2e5f5b92a..2bb74ba680b 100644 --- a/src/bin/psql/t/010_tab_completion.pl +++ b/src/bin/psql/t/010_tab_completion.pl @@ -44,6 +44,7 @@ $node->safe_psql('postgres', . "CREATE TABLE mytab246 (f1 int, f2 text);\n" . "CREATE TABLE \"mixedName\" (f1 int, f2 text);\n" . "CREATE TYPE enum1 AS ENUM ('foo', 'bar', 'baz', 'BLACK');\n" + . "CREATE COLLATION coll1 FROM \"C\";\n" . "CREATE PUBLICATION some_publication;\n"); # In a VPATH build, we'll be started in the source directory, but we want @@ -339,6 +340,40 @@ check_completion( clear_query(); +# check CREATE DOMAIN completion with and without optional AS +check_completion( + "CREATE DOMAIN d enu\t", + qr/CREATE DOMAIN d enum1 /, + "complete domain data type without AS"); + +check_completion( + "CH\t", + qr/CHECK \(/, + "complete domain option without AS"); + +clear_query(); + +check_completion( + "CREATE DOMAIN d AS enum1 CH\t", + qr/CHECK \(/, + "complete domain option with AS"); + +clear_query(); + +check_completion( + "CREATE DOMAIN d text COLLATE col\t", + qr/coll1 /, + "complete domain collation without AS"); + +clear_query(); + +check_completion( + "CREATE DOMAIN d AS text COLLATE col\t", + qr/coll1 /, + "complete domain collation with AS"); + +clear_query(); + # check words_after_create infrastructure check_completion( "CREATE TABLE mytab\t\t", diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index 96de78cd087..5604fe3a4a3 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -3498,13 +3498,17 @@ match_previous_words(int pattern_id, /* CREATE DOMAIN */ else if (Matches("CREATE", "DOMAIN", MatchAny)) - COMPLETE_WITH("AS"); + COMPLETE_WITH_SCHEMA_QUERY_PLUS(Query_for_list_of_datatypes, "AS"); else if (Matches("CREATE", "DOMAIN", MatchAny, "AS")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes); - else if (Matches("CREATE", "DOMAIN", MatchAny, "AS", MatchAny)) + else if (Matches("CREATE", "DOMAIN", MatchAny, "AS", MatchAny) || + Matches("CREATE", "DOMAIN", MatchAny, MatchAnyExcept("AS"))) COMPLETE_WITH("COLLATE", "DEFAULT", "CONSTRAINT", "NOT NULL", "NULL", "CHECK ("); - else if (Matches("CREATE", "DOMAIN", MatchAny, "COLLATE")) + else if (Matches("CREATE", "DOMAIN", MatchAny, + "AS", MatchAny, "COLLATE") || + Matches("CREATE", "DOMAIN", MatchAny, + MatchAnyExcept("AS"), "COLLATE")) COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_collations); /* CREATE EXTENSION */ -- 2.52.0