@@ -11,7 +11,6 @@ def suggest_type(full_text, text_before_cursor):
1111 A scope for a column category will be a list of tables.
1212 """
1313
14- tables = extract_tables (full_text )
1514
1615 word_before_cursor = last_word (text_before_cursor ,
1716 include = 'all_punctuations' )
@@ -22,45 +21,53 @@ def suggest_type(full_text, text_before_cursor):
2221 # partially typed string which renders the smart completion useless because
2322 # it will always return the list of keywords as completion.
2423 if word_before_cursor :
25- parsed = sqlparse .parse (
26- text_before_cursor [:- len (word_before_cursor )])
24+ if word_before_cursor [- 1 ] in ('.' ):
25+ parsed = sqlparse .parse (text_before_cursor )
26+ else :
27+ parsed = sqlparse .parse (
28+ text_before_cursor [:- len (word_before_cursor )])
2729 else :
2830 parsed = sqlparse .parse (text_before_cursor )
2931
3032 # Need to check if `p` is not empty, since an empty string will result in
3133 # an empty tuple.
3234 p = parsed [0 ] if parsed else None
33- n = p and len (p .tokens ) or 0
34- last_token = p and p .token_prev (n ) or ''
35- last_token_v = last_token .value if last_token else ''
35+ last_token = p and p .token_prev (len (p .tokens )) or ''
3636
3737 def is_function_word (word ):
38- return word and len (word ) > 1 and word [- 1 ] == '('
38+ return word .endswith ('(' )
39+
3940 if is_function_word (word_before_cursor ):
40- return ('columns' , tables )
41+ return ('columns' , extract_tables ( full_text ) )
4142
42- return (suggest_based_on_last_token (last_token_v , text_before_cursor ),
43- tables )
43+ return suggest_based_on_last_token (last_token , text_before_cursor , full_text )
4444
45+ def suggest_based_on_last_token (token , text_before_cursor , full_text ):
46+ if isinstance (token , basestring ):
47+ token_v = token
48+ else :
49+ token_v = token .value
4550
46- def suggest_based_on_last_token (last_token_v , text_before_cursor ):
47- if last_token_v .lower ().endswith ('(' ):
48- return 'columns'
49- if last_token_v .lower () in ('set' , 'by' , 'distinct' ):
50- return 'columns'
51- elif last_token_v .lower () in ('select' , 'where' , 'having' ):
52- return 'columns-and-functions'
53- elif last_token_v .lower () in ('from' , 'update' , 'into' , 'describe' ):
54- return 'tables'
55- elif last_token_v in ('d' ,): # \d
56- return 'tables'
57- elif last_token_v .lower () in ('c' , 'use' ): # \c
58- return 'databases'
59- elif last_token_v .endswith (',' ):
51+ if token_v .lower ().endswith ('(' ):
52+ return 'columns' , extract_tables (full_text )
53+ if token_v .lower () in ('set' , 'by' , 'distinct' ):
54+ return 'columns' , extract_tables (full_text )
55+ elif token_v .lower () in ('select' , 'where' , 'having' ):
56+ return 'columns-and-functions' , extract_tables (full_text )
57+ elif token_v .lower () in ('from' , 'update' , 'into' , 'describe' ):
58+ return 'tables' , []
59+ elif token_v in ('d' ,): # \d
60+ return 'tables' , []
61+ elif token_v .lower () in ('c' , 'use' ): # \c
62+ return 'databases' , []
63+ elif token_v .endswith (',' ):
6064 prev_keyword = find_prev_keyword (text_before_cursor )
61- return suggest_based_on_last_token (prev_keyword , text_before_cursor )
65+ return suggest_based_on_last_token (prev_keyword , text_before_cursor , full_text )
66+ elif token_v .endswith ('.' ):
67+ tables = extract_tables (full_text , include_alias = True )
68+ return 'columns' , [tables .get (token .token_first ().value )]
6269 else :
63- return 'keywords'
70+ return 'keywords' , []
6471
6572def find_prev_keyword (sql ):
6673 if not sql .strip ():
0 commit comments