From 7492594c37a022c036675755553d6b5988287777 Mon Sep 17 00:00:00 2001 From: Amjith Ramanujam Date: Thu, 25 Dec 2014 01:55:00 -0800 Subject: [PATCH] Improve the Insert completion. --- TODO | 2 ++ pgcli/packages/parseutils.py | 4 ++-- pgcli/packages/sqlcompletion.py | 16 ++++++---------- tests/test_naive_completion.py | 5 +++-- tests/test_sqlcompletion.py | 16 ++++++++++++++-- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/TODO b/TODO index bdf2b1d2..8fcc5d1b 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,8 @@ # vi: ft=vimwiki * [ ] Skip the password prompt by default. It should only be presented if -W option is provided. * [ ] Detect a '.' and parse the word before it and get it's real name. +* [ ] Multiple cols for dot is failing. +* [ ] Table detection for INSERT INTO is not stopping after it encounters the lparen. * [ ] Bottom status bar is cut-off in half pane. Figure out how to fix that. * [ ] Column completion for nested sql. * [ ] Add JOIN to the list of keywords and provide proper autocompletion for it. diff --git a/pgcli/packages/parseutils.py b/pgcli/packages/parseutils.py index 4ae795af..a3de836e 100644 --- a/pgcli/packages/parseutils.py +++ b/pgcli/packages/parseutils.py @@ -1,7 +1,7 @@ import re import sqlparse from sqlparse.sql import IdentifierList, Identifier, Function -from sqlparse.tokens import Keyword, DML +from sqlparse.tokens import Keyword, DML, Punctuation cleanup_regex = { # This matches only alphanumerics and underscores. @@ -74,7 +74,7 @@ def extract_from_part(parsed): if is_subselect(item): for x in extract_from_part(item): yield x - elif item.ttype is Keyword: + elif item.ttype is Keyword or item.ttype is Punctuation: raise StopIteration else: yield item diff --git a/pgcli/packages/sqlcompletion.py b/pgcli/packages/sqlcompletion.py index ae630859..cb5c51c0 100644 --- a/pgcli/packages/sqlcompletion.py +++ b/pgcli/packages/sqlcompletion.py @@ -11,9 +11,10 @@ def suggest_type(full_text, text_before_cursor): A scope for a column category will be a list of tables. """ - + #word_before_cursor = last_word(text_before_cursor, + #include='all_punctuations') word_before_cursor = last_word(text_before_cursor, - include='all_punctuations') + include='most_punctuations') # If we've partially typed a word then word_before_cursor won't be an empty # string. In that case we want to remove the partially typed string before @@ -21,7 +22,7 @@ def suggest_type(full_text, text_before_cursor): # partially typed string which renders the smart completion useless because # it will always return the list of keywords as completion. if word_before_cursor: - if word_before_cursor[-1] in ('.'): + if word_before_cursor[-1] in ('(', '.'): parsed = sqlparse.parse(text_before_cursor) else: parsed = sqlparse.parse( @@ -34,12 +35,6 @@ def suggest_type(full_text, text_before_cursor): p = parsed[0] if parsed else None last_token = p and p.token_prev(len(p.tokens)) or '' - def is_function_word(word): - return word.endswith('(') - - if is_function_word(word_before_cursor): - return ('columns', extract_tables(full_text)) - return suggest_based_on_last_token(last_token, text_before_cursor, full_text) def suggest_based_on_last_token(token, text_before_cursor, full_text): @@ -74,5 +69,6 @@ def find_prev_keyword(sql): return None for t in reversed(list(sqlparse.parse(sql)[0].flatten())): - if t.is_keyword: + if t.is_keyword or t.value == '(': return t.value + diff --git a/tests/test_naive_completion.py b/tests/test_naive_completion.py index c0dd8e5f..b5798aea 100644 --- a/tests/test_naive_completion.py +++ b/tests/test_naive_completion.py @@ -9,6 +9,7 @@ def completer(): def test_empty_string_completion(completer): #print set(completer.get_completions(Document(text=''))) - print set(map(Completion, completer.all_completions)) - assert False + #print set(map(Completion, completer.all_completions)) + #assert False #assert set(map(Completion, completer.keywords)) == set(completer.get_completions(Document(text=''))) + pass diff --git a/tests/test_sqlcompletion.py b/tests/test_sqlcompletion.py index 394cd697..b816e3d4 100644 --- a/tests/test_sqlcompletion.py +++ b/tests/test_sqlcompletion.py @@ -36,10 +36,22 @@ def test_table_comma_suggests_tables(): assert suggestion == ('tables', []) def test_into_suggests_tables(): - suggestion = suggest_type('INSERT INTO ', - 'INSERT INTO ') + suggestion = suggest_type('INSERT INTO ', 'INSERT INTO ') assert suggestion == ('tables', []) +def test_insert_into_lparen_suggests_cols(): + suggestion = suggest_type('INSERT INTO abc (', 'INSERT INTO abc (') + assert suggestion == ('columns', ['abc']) + +def test_insert_into_lparen_partial_text_suggests_cols(): + suggestion = suggest_type('INSERT INTO abc (i', 'INSERT INTO abc (i') + assert suggestion == ('columns', ['abc']) + +def test_insert_into_lparen_comma_suggests_cols(): + #import pdb; pdb.set_trace() + suggestion = suggest_type('INSERT INTO abc (id,', 'INSERT INTO abc (id,') + assert suggestion == ('columns', ['abc']) + def test_partially_typed_col_name_suggests_col_names(): suggestion = suggest_type('SELECT * FROM tabl WHERE col_n', 'SELECT * FROM tabl WHERE col_n')