mirror of https://github.com/dbcli/pgcli
Improve the Insert completion.
This commit is contained in:
parent
09c5f28abe
commit
7492594c37
2
TODO
2
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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue