Skip to content

Commit da78192

Browse files
committed
one approach to fix completions with function call and lparen
1 parent 1f60224 commit da78192

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

‎pgcli/packages/sqlcompletion.py‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ def suggest_type(full_text, text_before_cursor):
77
"""Takes the full_text that is typed so far and also the text before the
88
cursor to suggest completion type and scope.
99
10-
Returns a tuple with a type of entity ('table', 'column' etc) and a scope.
10+
Returns a tuple with a type of entity ('table', 'column' etc), a scope
11+
and a flag to tell the caller to ignore the word at cursor and return all
12+
matches in current scope.
1113
A scope for a column category will be a list of tables.
1214
"""
1315

@@ -31,19 +33,25 @@ def suggest_type(full_text, text_before_cursor):
3133
last_token = last_token.value if last_token else ''
3234

3335
def is_function_word(word):
36+
"""
37+
Checks iw we are at function call, such as MAX(, MIN(, AVG(, etc.
38+
In this case, we want to return all columns.
39+
:param word: word at cursor, for example MAX(
40+
:return: true or false
41+
"""
3442
return word and len(word) > 1 and word[-1] == '('
3543

3644
if is_function_word(word_before_cursor):
37-
return ('columns', extract_tables(full_text))
45+
return ('columns', extract_tables(full_text), True)
3846
elif last_token.lower() in ('set', 'by', 'distinct'):
39-
return ('columns', extract_tables(full_text))
47+
return ('columns', extract_tables(full_text), False)
4048
elif last_token.lower() in ('select', 'where', 'having'):
41-
return ('columns-and-functions', extract_tables(full_text))
49+
return ('columns-and-functions', extract_tables(full_text), False)
4250
elif last_token.lower() in ('from', 'update', 'into', 'describe'):
43-
return ('tables', [])
51+
return ('tables', [], False)
4452
elif last_token in ('d',): # \d
45-
return ('tables', [])
53+
return ('tables', [], False)
4654
elif last_token.lower() in ('c', 'use'): # \c
47-
return ('databases', [])
55+
return ('databases', [], False)
4856
else:
49-
return ('keywords', [])
57+
return ('keywords', [], False)

‎pgcli/pgcompleter.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,26 @@ def get_completions(self, document, complete_event):
8383
if not self.smart_completion:
8484
return self.find_matches(word_before_cursor, self.all_completions)
8585

86-
category, scope = suggest_type(document.text,
86+
category, scope, match_all = suggest_type(document.text,
8787
document.text_before_cursor)
8888

89+
word_to_match = '' if match_all else word_before_cursor
90+
8991
if category == 'columns':
9092
scoped_cols = []
9193
for table in scope:
9294
scoped_cols.extend(self.columns[table])
93-
return self.find_matches(word_before_cursor, scoped_cols)
95+
return self.find_matches(word_to_match, scoped_cols)
9496
elif category == 'columns-and-functions':
9597
scoped_cols = []
9698
for table in scope:
9799
scoped_cols.extend(self.columns[table])
98-
return self.find_matches(word_before_cursor, scoped_cols +
100+
return self.find_matches(word_to_match, scoped_cols +
99101
self.functions)
100102
elif category == 'tables':
101-
return self.find_matches(word_before_cursor, self.tables)
103+
return self.find_matches(word_to_match, self.tables)
102104
elif category == 'databases':
103-
return self.find_matches(word_before_cursor, self.databases)
105+
return self.find_matches(word_to_match, self.databases)
104106
elif category == 'keywords':
105-
return self.find_matches(word_before_cursor, self.keywords +
107+
return self.find_matches(word_to_match, self.keywords +
106108
self.special_commands)

0 commit comments

Comments
 (0)