From 352ed4198034a5a7016ee6b105bf95fb415a5110 Mon Sep 17 00:00:00 2001 From: Jan Brun Rasmussen Date: Tue, 8 Sep 2020 19:02:25 +0200 Subject: Add schema suggestion for functions (#1206) * Add schema suggestion for functions - Update sqlcompletion.py - Update metadata files * Move autocomletion for function under condition - Make sure suggestion are only added under drop, alter etc. --- AUTHORS | 1 + changelog.rst | 1 + pgcli/packages/sqlcompletion.py | 16 ++++++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 4be4126a..21263c89 100644 --- a/AUTHORS +++ b/AUTHORS @@ -112,6 +112,7 @@ Contributors: * Anthony DeBarros (anthonydb) * Seungyong Kwak (GUIEEN) * Tom Caruso (tomplex) + * Jan Brun Rasmussen (janbrunrasmussen) Creator: -------- diff --git a/changelog.rst b/changelog.rst index 2c5e7ca7..b303970f 100644 --- a/changelog.rst +++ b/changelog.rst @@ -10,6 +10,7 @@ Features: * Support setting color for null, string, number, keyword value * Support Prompt Toolkit 2 * Update functions, datatypes literals for auto-suggestion field +* Add suggestion for schema in function auto-complete Bug fixes: ---------- diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 93736e34..6ef88595 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -92,7 +92,7 @@ class SqlStatement(object): return self.parsed.token_first().value.lower() == "insert" def get_tables(self, scope="full"): - """ Gets the tables available in the statement. + """Gets the tables available in the statement. param `scope:` possible values: 'full', 'insert', 'before' If 'insert', only the first table is returned. If 'before', only tables before the cursor are returned. @@ -431,11 +431,23 @@ def suggest_based_on_last_token(token, stmt): elif token_v == "function": schema = stmt.get_identifier_schema() + # stmt.get_previous_token will fail for e.g. `SELECT 1 FROM functions WHERE function:` try: prev = stmt.get_previous_token(token).value.lower() if prev in ("drop", "alter", "create", "create or replace"): - return (Function(schema=schema, usage="signature"),) + + # Suggest functions from either the currently-selected schema or the + # public schema if no schema has been specified + suggest = [] + + if not schema: + # Suggest schemas + suggest.insert(0, Schema()) + + suggest.append(Function(schema=schema, usage="signature")) + return tuple(suggest) + except ValueError: pass return tuple() -- cgit v1.2.3