Skip to content

Commit 3206b95

Browse files
Highlight FuzzyWordCompleter matches.
1 parent d1d5d11 commit 3206b95

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

prompt_toolkit/completion/fuzzy_completer.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,40 @@ def sort_key(fuzzy_match):
7474
yield Completion(
7575
match.word,
7676
-len(word_before_cursor),
77-
display_meta=display_meta)
77+
display_meta=display_meta,
78+
display=self._get_display(match, word_before_cursor))
79+
80+
def _get_display(self, fuzzy_match, word_before_cursor):
81+
"""
82+
Generate formatted text for the display label.
83+
"""
84+
m = fuzzy_match
85+
86+
if m.match_length == 0:
87+
# No highlighting when we have zero length matches (no input text).
88+
return m.word
89+
90+
result = []
91+
92+
# Text before match.
93+
result.append(('class:fuzzymatch.outside', m.word[:m.start_pos]))
94+
95+
# The match itself.
96+
characters = list(word_before_cursor)
97+
98+
for c in m.word[m.start_pos:m.start_pos + m.match_length]:
99+
classname = 'class:fuzzymatch.inside'
100+
if characters and c == characters[0]:
101+
classname += '.character'
102+
del characters[0]
103+
104+
result.append((classname, c))
105+
106+
# Text after match.
107+
result.append(
108+
('class:fuzzymatch.outside', m.word[m.start_pos + m.match_length:]))
109+
110+
return result
78111

79112

80113
_FuzzyMatch = namedtuple('_FuzzyMatch', 'match_length start_pos word')

prompt_toolkit/key_binding/bindings/completion.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ def display(page):
106106
for c in range(column_count):
107107
try:
108108
completion = page_columns[c][r]
109+
style = 'class:readline-like-completions.completion ' + (completion.style or '')
109110

110-
result.extend(to_formatted_text(completion.display, style=completion.style))
111+
result.extend(to_formatted_text(completion.display, style=style))
111112

112113
# Add padding.
113114
padding = max_compl_width - get_cwidth(completion.display_text)
@@ -116,7 +117,7 @@ def display(page):
116117
pass
117118
result.append(('', '\n'))
118119

119-
app.print_text(result)
120+
app.print_text(to_formatted_text(result, 'class:readline-like-completions'))
120121

121122
# User interaction through an application generator function.
122123
def run_compl():

prompt_toolkit/styles/defaults.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@
7979
('completion-menu.meta.completion.current', 'bg:#aaaaaa #000000'),
8080
('completion-menu.multi-column-meta', 'bg:#aaaaaa #000000'),
8181

82+
# Fuzzy matches in completion menu (for FuzzyCompleter).
83+
('completion-menu.completion fuzzymatch.outside', 'fg:#444444'),
84+
('completion-menu.completion fuzzymatch.inside', 'bold'),
85+
('completion-menu.completion fuzzymatch.inside.character', 'underline'),
86+
('completion-menu.completion.current fuzzymatch.outside', 'fg:default'),
87+
('completion-menu.completion.current fuzzymatch.inside', 'nobold'),
88+
89+
# Styling of readline-like completions.
90+
('readline-like-completions', ''),
91+
('readline-like-completions.completion', ''),
92+
('readline-like-completions.completion fuzzymatch.outside', '#888888'),
93+
('readline-like-completions.completion fuzzymatch.inside', ''),
94+
('readline-like-completions.completion fuzzymatch.inside.character', 'underline'),
95+
8296
# Scrollbars.
8397
('scrollbar.background', 'bg:#aaaaaa'),
8498
('scrollbar.button', 'bg:#444444'),

0 commit comments

Comments
 (0)