diff options
author | Amjith Ramanujam <amjith.r@gmail.com> | 2019-10-16 20:57:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-16 20:57:19 -0700 |
commit | a713b5d08b0fae4e55066f5438522a173f79606b (patch) | |
tree | 8a39781567dfeffeb17facbe32c4c83f8086199e /pgcli | |
parent | 2bf01a77844532cce9686a45b227209f85646dde (diff) | |
parent | e9b18f9e84a5b26eb4a42633f6cee98597d70ad4 (diff) |
Merge pull request #1107 from owst/function_arg_completions_with_alias
Take account of table aliases when completing function args (#1048)
Diffstat (limited to 'pgcli')
-rw-r--r-- | pgcli/packages/sqlcompletion.py | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 85b3bb10..2828b175 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -398,35 +398,11 @@ def suggest_based_on_last_token(token, stmt): if prev_prev_tok and prev_prev_tok.normalized == "INTO": return (Column(table_refs=stmt.get_tables("insert"), context="insert"),) # We're probably in a function argument list - return ( - Column( - table_refs=extract_tables(stmt.full_text), - local_tables=stmt.local_tables, - qualifiable=True, - ), - ) + return _suggest_expression(token_v, stmt) elif token_v == "set": return (Column(table_refs=stmt.get_tables(), local_tables=stmt.local_tables),) elif token_v in ("select", "where", "having", "order by", "distinct"): - # Check for a table alias or schema qualification - parent = (stmt.identifier and stmt.identifier.get_parent_name()) or [] - tables = stmt.get_tables() - if parent: - tables = tuple(t for t in tables if identifies(parent, t)) - return ( - Column(table_refs=tables, local_tables=stmt.local_tables), - Table(schema=parent), - View(schema=parent), - Function(schema=parent), - ) - else: - return ( - Column( - table_refs=tables, local_tables=stmt.local_tables, qualifiable=True - ), - Function(schema=None), - Keyword(token_v.upper()), - ) + return _suggest_expression(token_v, stmt) elif token_v == "as": # Don't suggest anything for aliases return () @@ -554,6 +530,30 @@ def suggest_based_on_last_token(token, stmt): return (Keyword(),) +def _suggest_expression(token_v, stmt): + """ + Return suggestions for an expression, taking account of any partially-typed + identifier's parent, which may be a table alias or schema name. + """ + parent = stmt.identifier.get_parent_name() if stmt.identifier else [] + tables = stmt.get_tables() + + if parent: + tables = tuple(t for t in tables if identifies(parent, t)) + return ( + Column(table_refs=tables, local_tables=stmt.local_tables), + Table(schema=parent), + View(schema=parent), + Function(schema=parent), + ) + + return ( + Column(table_refs=tables, local_tables=stmt.local_tables, qualifiable=True), + Function(schema=None), + Keyword(token_v.upper()), + ) + + def identifies(id, ref): """Returns true if string `id` matches TableReference `ref`""" |