1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-09-11 13:56:36 +03:00

Fix lexical order tiebreaking

This commit is contained in:
Anthony Lai 2016-03-18 17:47:21 -07:00
parent c3a7284bc8
commit 1fe6ff06a2
2 changed files with 18 additions and 11 deletions

View File

@ -236,14 +236,6 @@ class PGCompleter(Completer):
# fuzzy matches
return -float('Infinity'), -match_point
# Lexical order of items in the collection, used for tiebreaking items
# with the same match group length and start position. In Python,
# 'user' < 'users', i.e. the "lower" value comes first. Since we use
# *higher* priority to mean "more important," we need to flip Python's
# usual position ranking, hence -position.
lexical_order = dict([(name, -position) for position, name in
enumerate(sorted(collection))])
if meta_collection:
# Each possible completion in the collection has a corresponding
# meta-display string
@ -261,7 +253,14 @@ class PGCompleter(Completer):
# Truncate meta-text to 50 characters, if necessary
meta = meta[:47] + u'...'
priority = sort_key, priority_func(item), lexical_order[item]
# Lexical order of items in the collection, used for
# tiebreaking items with the same match group length and start
# position. Since we use *higher* priority to mean "more
# important," we use -ord(c) to prioritize "aa" > "ab" and end
# with 1 to prioritize shorter strings (ie "user" > "users")
lexical_priority = tuple(-ord(c) for c in item) + (1,)
priority = sort_key, priority_func(item), lexical_priority
matches.append(Match(
completion=Completion(item, -text_len, display_meta=meta),

View File

@ -379,6 +379,14 @@ def test_table_names_after_from(completer, complete_event):
Completion(text='set_returning_func', start_position=0, display_meta='function')
])
def test_table_names_after_from_are_lexical_ordered_by_text(completer, complete_event):
text = 'SELECT * FROM '
position = len('SELECT * FROM ')
result = completer.get_completions(
Document(text=text, cursor_position=position),
complete_event)
assert result == sorted(result, key=lambda c: c.text)
def test_auto_escaped_col_names(completer, complete_event):
text = 'SELECT from "select"'
position = len('SELECT ')
@ -498,8 +506,8 @@ def test_join_functions_on_suggests_columns(completer, complete_event):
assert set(result) == set([
Completion(text='x', start_position=0, display_meta='column'),
Completion(text='y', start_position=0, display_meta='column')])
def test_learn_keywords(completer, complete_event):
sql = 'CREATE VIEW v AS SELECT 1'
completer.extend_query_history(sql)