8000 Bugfix + little improvement in FuzzyWordCompleter. · mxr/python-prompt-toolkit@d6fc71a · GitHub
[go: up one dir, main page]

Skip to content

Commit d6fc71a

Browse files
Bugfix + little improvement in FuzzyWordCompleter.
1 parent 3206b95 commit d6fc71a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

prompt_toolkit/completion/fuzzy_completer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ def get_completions(self, document, complete_event):
5656
for word in words:
5757
matches = list(regex.finditer(word))
5858
if matches:
59-
best = min(matches, key=lambda x: len(x.group(1))) # find shortest match
59+
# Prefer the match, closest to the left, then shortest.
60+
best = min(matches, key=lambda m: (m.start(), len(m.group(1))))
6061
fuzzy_matches.append(_FuzzyMatch(len(best.group(1)), best.start(), word))
6162

6263
def sort_key(fuzzy_match):
63-
" Sort by start position, then by proportion of word that is covered. "
64+
""" Sort by start position, then by proportion of word that is
65+
covered. (More coverage is better.) """
6466
return (
6567
fuzzy_match.start_pos,
66-
float(fuzzy_match.match_length) / len(fuzzy_match.word)
68+
- float(fuzzy_match.match_length) / len(fuzzy_match.word)
6769
)
6870

6971
fuzzy_matches = sorted(fuzzy_matches, key=sort_key)

0 commit comments

Comments
 (0)
0