diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 9d9f77cd..7dc7a552 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -185,18 +185,24 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier else: return [{'type': 'column', 'tables': extract_tables(full_text)}, {'type': 'function', 'schema': []}] - elif token_v in ('copy', 'from', 'update', 'into', 'describe') or ( - token_v.endswith('join') and token.ttype in Keyword): + elif (token_v.endswith('join') and token.ttype in Keyword) or (token_v in + ('copy', 'from', 'update', 'into', 'describe', 'truncate')): schema = (identifier and identifier.get_parent_name()) or [] - if schema: - # If already schema-qualified, suggest only tables/views - return [{'type': 'table', 'schema': schema}, - {'type': 'view', 'schema': schema}] - else: - # Suggest schemas OR public tables/views - return [{'type': 'schema'}, - {'type': 'table', 'schema': []}, - {'type': 'view', 'schema': []}] + + # Suggest tables from either the currently-selected schema or the + # public schema if no schema has been specified + suggest = [{'type': 'table', 'schema': schema}] + + if not schema: + # Suggest schemas + suggest.insert(0, {'type': 'schema'}) + + # Only tables can be TRUNCATED, otherwise suggest views + if token_v != 'truncate': + suggest.append({'type': 'view', 'schema': schema}) + + return suggest + elif token_v in ('table', 'view', 'function'): # E.g. 'DROP FUNCTION ', 'ALTER TABLE ' rel_type = token_v diff --git a/pgcli/pgcompleter.py b/pgcli/pgcompleter.py index 72459c3b..d20832a0 100644 --- a/pgcli/pgcompleter.py +++ b/pgcli/pgcompleter.py @@ -32,9 +32,9 @@ class PGCompleter(Completer): 'RAW', 'RENAME', 'RESOURCE', 'REVOKE', 'RIGHT', 'ROW', 'ROWID', 'ROWNUM', 'ROWS', 'SELECT', 'SESSION', 'SET', 'SHARE', 'SIZE', 'SMALLINT', 'START', 'SUCCESSFUL', 'SYNONYM', 'SYSDATE', 'TABLE', - 'TEMPLATE', 'THEN', 'TO', 'TRIGGER', 'UID', 'UNION', 'UNIQUE', - 'UPDATE', 'USE', 'USER', 'USING', 'VALIDATE', 'VALUES', 'VARCHAR', - 'VARCHAR2', 'VIEW', 'WHEN', 'WHENEVER', 'WHERE', 'WITH', ] + 'TEMPLATE', 'THEN', 'TO', 'TRIGGER', 'TRUNCATE', 'UID', 'UNION', + 'UNIQUE', 'UPDATE', 'USE', 'USER', 'USING', 'VALIDATE', 'VALUES', + 'VARCHAR', 'VARCHAR2', 'VIEW', 'WHEN', 'WHENEVER', 'WHERE', 'WITH'] functions = ['AVG', 'COUNT', 'FIRST', 'FORMAT', 'LAST', 'LCASE', 'LEN', 'MAX', 'MIN', 'MID', 'NOW', 'ROUND', 'SUM', 'TOP', 'UCASE']