diff options
author | Amjith Ramanujam <amjith@newrelic.com> | 2014-12-17 23:23:53 -0800 |
---|---|---|
committer | Amjith Ramanujam <amjith@newrelic.com> | 2014-12-17 23:23:53 -0800 |
commit | d42711710b80c5ae02766ab6abdcbc0bc1930b41 (patch) | |
tree | 796527239bc2db3abe2f7de430cdf23f709a9b60 | |
parent | 01fc25f919df5023eafe64e98bc6a7a87361debd (diff) |
Separate out the utility methods for sqlcompletion to parseutils.
-rw-r--r-- | pgcli/packages/parseutils.py | 53 | ||||
-rw-r--r-- | pgcli/packages/sqlcompletion.py | 65 |
2 files changed, 61 insertions, 57 deletions
diff --git a/pgcli/packages/parseutils.py b/pgcli/packages/parseutils.py new file mode 100644 index 00000000..e2eb2eee --- /dev/null +++ b/pgcli/packages/parseutils.py @@ -0,0 +1,53 @@ +import re + +# This matches only alphanumerics and underscores. +_LAST_WORD_RE = re.compile(r'(\w+)$') +# This matches everything except a space. +_LAST_WORD_SPL_RE = re.compile(r'([^\s]+)$') + +def last_word(text, include_special_chars=False): + """ + Find the last word in a sentence. + + >>> last_word('abc') + 'abc' + >>> last_word(' abc') + 'abc' + >>> last_word('') + '' + >>> last_word(' ') + '' + >>> last_word('abc ') + '' + >>> last_word('abc def') + 'def' + >>> last_word('abc def ') + '' + >>> last_word('abc def;') + '' + >>> last_word('bac $def') + 'def' + >>> last_word('bac $def', True) + '$def' + >>> last_word('bac \def', True) + '\\\\def' + >>> last_word('bac \def;', True) + '\\\\def;' + """ + + if not text: # Empty string + return '' + + if text[-1].isspace(): + return '' + else: + regex = _LAST_WORD_SPL_RE if include_special_chars else _LAST_WORD_RE + result = regex.findall(text) + if result: + return result[0] + else: + return '' + +def extract_tables(sql): + return [] + diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index 42b2ba83..34b04f1a 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -1,61 +1,12 @@ -import re import sqlparse - -# This matches only alphanumerics and underscores. -_LAST_WORD_RE = re.compile(r'(\w+)$') -# This matches everything except a space. -_LAST_WORD_SPL_RE = re.compile(r'([^\s]+)$') - -def last_word(text, include_special_chars=False): - """ - Find the last word in a sentence. - - >>> last_word('abc') - 'abc' - >>> last_word(' abc') - 'abc' - >>> last_word('') - '' - >>> last_word(' ') - '' - >>> last_word('abc ') - '' - >>> last_word('abc def') - 'def' - >>> last_word('abc def ') - '' - >>> last_word('abc def;') - '' - >>> last_word('bac $def') - 'def' - >>> last_word('bac $def', True) - '$def' - >>> last_word('bac \def', True) - '\\\\def' - >>> last_word('bac \def;', True) - '\\\\def;' - """ - - if not text: # Empty string - return '' - - if text[-1].isspace(): - return '' - else: - regex = _LAST_WORD_SPL_RE if include_special_chars else _LAST_WORD_RE - result = regex.findall(text) - if result: - return result[0] - else: - return '' +from parseutils import last_word, extract_tables def suggest_type(full_text, text_before_cursor): """Takes the full_text that is typed so far and also the text before the cursor to suggest completion type and scope. Returns a tuple with a type of entity ('table', 'column' etc) and a scope. - A scope for a column category will be the table name. Scope is set to None - if unavailable. + A scope for a column category will be a list of tables. """ word_before_cursor = last_word(text_before_cursor, @@ -79,14 +30,14 @@ def suggest_type(full_text, text_before_cursor): last_token = last_token.value if last_token else '' if last_token.lower() in ('set', 'order by', 'group by'): - return ('columns', None) + return ('columns', extract_tables(full_text)) elif last_token.lower() in ('select', 'where', 'having'): - return ('columns-and-functions', None) + return ('columns-and-functions', extract_tables(full_text)) elif last_token.lower() in ('from', 'update', 'into', 'describe'): - return ('tables', None) + return ('tables', []) elif last_token in ('d',): # \d - return ('tables', None) + return ('tables', []) elif last_token.lower() in ('c', 'use'): # \c - return ('databases', None) + return ('databases', []) else: - return ('keywords', None) + return ('keywords', []) |