Tab completion for CREATE TYPE

Started by Thomas Munroover 6 years ago8 messages
#1Thomas Munro
thomas.munro@gmail.com
1 attachment(s)

Hi,

Since I keep forgetting the syntax and options, here is $SUBJECT.

--
Thomas Munro
https://enterprisedb.com

Attachments:

0001-Tab-completion-for-CREATE-TYPE.patchapplication/octet-stream; name=0001-Tab-completion-for-CREATE-TYPE.patchDownload
From 5417f846a88bf66e0f5fad45a83f938777ab28da Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 14 May 2019 17:43:44 +1200
Subject: [PATCH] Tab completion for CREATE TYPE.

---
 src/bin/psql/tab-complete.c | 39 +++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index e4c03de221f..9f431925f10 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2663,6 +2663,45 @@ psql_completion(const char *text, int start, int end)
 	else if (Matches("CREATE", "ROLE|USER|GROUP", MatchAny, "IN"))
 		COMPLETE_WITH("GROUP", "ROLE");
 
+/* CREATE TYPE */
+	else if (Matches("CREATE", "TYPE", MatchAny))
+		COMPLETE_WITH("(", "AS");
+	else if (Matches("CREATE", "TYPE", MatchAny, "AS"))
+		COMPLETE_WITH("ENUM", "RANGE", "(");
+	else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "("))
+	{
+		if (TailMatches("(|*,", MatchAny))
+			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
+		else if (TailMatches("(|*,", MatchAny, "!*)"))
+			COMPLETE_WITH("COLLATE", ",", ")");
+	}
+	else if (Matches("CREATE", "TYPE", MatchAny, "AS", "ENUM|RANGE"))
+		COMPLETE_WITH("(");
+	else if (HeadMatches("CREATE", "TYPE", MatchAny, "("))
+	{
+		if (TailMatches("(|*,"))
+			COMPLETE_WITH("INPUT", "OUTPUT", "RECEIVE", "SEND",
+						  "TYPMOD_IN", "TYPMOD_OUT", "ANALYZE",
+						  "INTERNALLENGTH", "PASSBYVALUE", "ALIGNMENT",
+						  "STORAGE", "LIKE", "CATEGORY", "PREFERRED",
+						  "DEFAULT", "ELEMENT", "DELIMITER",
+						  "COLLATABLE");
+		else if (TailMatches("(*|*,", MatchAny))
+			COMPLETE_WITH("=");
+		else if (TailMatches("=", "!*)"))
+			COMPLETE_WITH(",", ")");
+	}
+	else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "RANGE", "("))
+	{
+		if (TailMatches("(|*,"))
+			COMPLETE_WITH("SUBTYPE", "SUBTYPE_OPCLASS", "COLLATION",
+						  "CANONICAL", "SUBTYPE_DIFF");
+		else if (TailMatches("(|*,", MatchAny))
+			COMPLETE_WITH("=");
+		else if (TailMatches("=", "!*)"))
+			COMPLETE_WITH(",", ")");
+	}
+
 /* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */
 	/* Complete CREATE VIEW <name> with AS */
 	else if (TailMatches("CREATE", "VIEW", MatchAny))
-- 
2.21.0

#2Kyotaro HORIGUCHI
horiguchi.kyotaro@lab.ntt.co.jp
In reply to: Thomas Munro (#1)
Re: Tab completion for CREATE TYPE

Hello.

At Tue, 14 May 2019 17:50:58 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in <CA+hUKGLk=0yLDjfviONJLzcHEzygj=x6VbGH43LnXbBUvQb52g@mail.gmail.com>

Hi,

Since I keep forgetting the syntax and options, here is $SUBJECT.

I played with this a bit and found that "... (attr=[tab]" (no
space between "r" and "=") complets with '='. Isn't it annoying?

Only "UPDATE hoge SET a=[tab]" behaves the same way among
existing completions.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#3Thomas Munro
thomas.munro@gmail.com
In reply to: Kyotaro HORIGUCHI (#2)
2 attachment(s)
Re: Tab completion for CREATE TYPE

On Tue, May 14, 2019 at 6:18 PM Kyotaro HORIGUCHI
<horiguchi.kyotaro@lab.ntt.co.jp> wrote:

I played with this a bit and found that "... (attr=[tab]" (no
space between "r" and "=") complets with '='. Isn't it annoying?

Only "UPDATE hoge SET a=[tab]" behaves the same way among
existing completions.

Hmm. True. Here's one way to fix that.

--
Thomas Munro
https://enterprisedb.com

Attachments:

0001-Tab-completion-for-CREATE-TYPE-v2.patchapplication/octet-stream; name=0001-Tab-completion-for-CREATE-TYPE-v2.patchDownload
From a3cdac64c3defe800f08b65dc20d9e6f29fd5e8a Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 14 May 2019 17:43:44 +1200
Subject: [PATCH 1/2] Tab completion for CREATE TYPE.

---
 src/bin/psql/tab-complete.c | 39 +++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index e4c03de221f..c7e9cbd2a61 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -2663,6 +2663,45 @@ psql_completion(const char *text, int start, int end)
 	else if (Matches("CREATE", "ROLE|USER|GROUP", MatchAny, "IN"))
 		COMPLETE_WITH("GROUP", "ROLE");
 
+/* CREATE TYPE */
+	else if (Matches("CREATE", "TYPE", MatchAny))
+		COMPLETE_WITH("(", "AS");
+	else if (Matches("CREATE", "TYPE", MatchAny, "AS"))
+		COMPLETE_WITH("ENUM", "RANGE", "(");
+	else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "("))
+	{
+		if (TailMatches("(|*,", MatchAny))
+			COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
+		else if (TailMatches("(|*,", MatchAny, "!*)"))
+			COMPLETE_WITH("COLLATE", ",", ")");
+	}
+	else if (Matches("CREATE", "TYPE", MatchAny, "AS", "ENUM|RANGE"))
+		COMPLETE_WITH("(");
+	else if (HeadMatches("CREATE", "TYPE", MatchAny, "("))
+	{
+		if (TailMatches("(|*,"))
+			COMPLETE_WITH("INPUT", "OUTPUT", "RECEIVE", "SEND",
+						  "TYPMOD_IN", "TYPMOD_OUT", "ANALYZE",
+						  "INTERNALLENGTH", "PASSBYVALUE", "ALIGNMENT",
+						  "STORAGE", "LIKE", "CATEGORY", "PREFERRED",
+						  "DEFAULT", "ELEMENT", "DELIMITER",
+						  "COLLATABLE");
+		else if (TailMatches("(*|*,", "!*="))
+			COMPLETE_WITH("=");
+		else if (TailMatches("=", "!*)"))
+			COMPLETE_WITH(",", ")");
+	}
+	else if (HeadMatches("CREATE", "TYPE", MatchAny, "AS", "RANGE", "("))
+	{
+		if (TailMatches("(|*,"))
+			COMPLETE_WITH("SUBTYPE", "SUBTYPE_OPCLASS", "COLLATION",
+						  "CANONICAL", "SUBTYPE_DIFF");
+		else if (TailMatches("(*|*,", "!*="))
+			COMPLETE_WITH("=");
+		else if (TailMatches("=", "!*)"))
+			COMPLETE_WITH(",", ")");
+	}
+
 /* CREATE VIEW --- is allowed inside CREATE SCHEMA, so use TailMatches */
 	/* Complete CREATE VIEW <name> with AS */
 	else if (TailMatches("CREATE", "VIEW", MatchAny))
-- 
2.21.0

0002-Fix-tab-completion-of-UPDATE-v2.patchapplication/octet-stream; name=0002-Fix-tab-completion-of-UPDATE-v2.patchDownload
From 86212aa56b078c7d72d1c475dd67b22d601ebad5 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 14 May 2019 18:54:14 +1200
Subject: [PATCH 2/2] Fix tab completion of UPDATE.

---
 src/bin/psql/tab-complete.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index c7e9cbd2a61..b8f404e452c 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3451,7 +3451,7 @@ psql_completion(const char *text, int start, int end)
 	else if (TailMatches("UPDATE", MatchAny, "SET"))
 		COMPLETE_WITH_ATTR(prev2_wd, "");
 	/* UPDATE <table> SET <attr> = */
-	else if (TailMatches("UPDATE", MatchAny, "SET", MatchAny))
+	else if (TailMatches("UPDATE", MatchAny, "SET", "!*="))
 		COMPLETE_WITH("=");
 
 /* USER MAPPING */
-- 
2.21.0

#4Edgy Hacker
edgy.hacker@gmail.com
In reply to: Thomas Munro (#3)
Re: Tab completion for CREATE TYPE

On Tue, May 14, 2019 at 06:58:14PM +1200, Thomas Munro wrote:

On Tue, May 14, 2019 at 6:18 PM Kyotaro HORIGUCHI
<horiguchi.kyotaro@lab.ntt.co.jp> wrote:

I played with this a bit and found that "... (attr=[tab]" (no
space between "r" and "=") complets with '='. Isn't it annoying?

Only "UPDATE hoge SET a=[tab]" behaves the same way among
existing completions.

Hmm. True. Here's one way to fix that.

Hmm... just got here.

What happens around here?

Show quoted text

--
Thomas Munro
https://enterprisedb.com

#5Thomas Munro
thomas.munro@gmail.com
In reply to: Edgy Hacker (#4)
Re: Tab completion for CREATE TYPE

On Tue, May 14, 2019 at 8:32 PM Edgy Hacker <edgy.hacker@gmail.com> wrote:

Hmm... just got here.

Welcome.

What happens around here?

Please see https://wiki.postgresql.org/wiki/So,_you_want_to_be_a_developer%3F .

--
Thomas Munro
https://enterprisedb.com

#6Edgy Hacker
edgy.hacker@gmail.com
In reply to: Thomas Munro (#5)
Re: Tab completion for CREATE TYPE

On Tue, May 14, 2019 at 09:01:27PM +1200, Thomas Munro wrote:

On Tue, May 14, 2019 at 8:32 PM Edgy Hacker <edgy.hacker@gmail.com> wrote:

Hmm... just got here.

Welcome.

Thanks.

What happens around here?

Please see https://wiki.postgresql.org/wiki/So,_you_want_to_be_a_developer%3F .

Not exactly a prospective developer but if it ever comes it...

Show quoted text

--
Thomas Munro
https://enterprisedb.com

#7Kyotaro HORIGUCHI
horiguchi.kyotaro@lab.ntt.co.jp
In reply to: Thomas Munro (#3)
Re: Tab completion for CREATE TYPE

At Tue, 14 May 2019 18:58:14 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in <CA+hUKG+ojKTKw=aG6QU=VmPMc8Sq7nM4Ah7fk1e+g1YngCVNmg@mail.gmail.com>

On Tue, May 14, 2019 at 6:18 PM Kyotaro HORIGUCHI
<horiguchi.kyotaro@lab.ntt.co.jp> wrote:

I played with this a bit and found that "... (attr=[tab]" (no
space between "r" and "=") complets with '='. Isn't it annoying?

Only "UPDATE hoge SET a=[tab]" behaves the same way among
existing completions.

Hmm. True. Here's one way to fix that.

Thanks. That's what was in my mind.

Some definition item names are induced from some current states
(e.g. "CREATE TYPE name AS RANGE (" => "SUBTYPE = ") but I think
it's too much.

COLLATE is not suggested with possible collations but I think
suggesting it is not so useful.

PASSEDBYVALUE is suggested with '=', which is different from
documented syntax but I don't think that's not such a problem for
those who spell this command out.

# By the way, collatable and preferred are boolean which behaves
# the same way with passedbyvalue. Is there any intention in the
# difference in the documentation?

The completion lists contain all possible words correctly (I
think "analyse" is an implicit synonym.).

As the result, I find it perfect.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

#8Thomas Munro
thomas.munro@gmail.com
In reply to: Kyotaro HORIGUCHI (#7)
Re: Tab completion for CREATE TYPE

On Tue, May 14, 2019 at 11:13 PM Kyotaro HORIGUCHI
<horiguchi.kyotaro@lab.ntt.co.jp> wrote:

At Tue, 14 May 2019 18:58:14 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in <CA+hUKG+ojKTKw=aG6QU=VmPMc8Sq7nM4Ah7fk1e+g1YngCVNmg@mail.gmail.com>

On Tue, May 14, 2019 at 6:18 PM Kyotaro HORIGUCHI
<horiguchi.kyotaro@lab.ntt.co.jp> wrote:

I played with this a bit and found that "... (attr=[tab]" (no
space between "r" and "=") complets with '='. Isn't it annoying?

Only "UPDATE hoge SET a=[tab]" behaves the same way among
existing completions.

Hmm. True. Here's one way to fix that.

Thanks. That's what was in my mind.

I pushed a fix for that separately. I remembered that we had decided
to use MatchAnyExcept("...") instead of "!...", so I did it that way.

Some definition item names are induced from some current states
(e.g. "CREATE TYPE name AS RANGE (" => "SUBTYPE = ") but I think
it's too much.

COLLATE is not suggested with possible collations but I think
suggesting it is not so useful.

Yes, there is room to make it smarter.

PASSEDBYVALUE is suggested with '=', which is different from
documented syntax but I don't think that's not such a problem for
those who spell this command out.

# By the way, collatable and preferred are boolean which behaves
# the same way with passedbyvalue. Is there any intention in the
# difference in the documentation?

Good question.

The completion lists contain all possible words correctly (I
think "analyse" is an implicit synonym.).

I am not a fan of doing anything at all to support alternative
spellings for keywords etc, even though I personally use British
spelling in most contexts outside PostgreSQL source code. We don't
support MATERIALISED, CATALOGUE, BACKWARDS/FORWARDS (with an S), etc,
so I don't know why we have this one single word ANALYSE from a
different spelling system than the one used by SQL.

As the result, I find it perfect.

Pushed. Thanks for the review!

--
Thomas Munro
https://enterprisedb.com