From de7ca7ab24a2134bda1cf298a9f41aa299ffe999 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 21 Aug 2024 21:10:27 +0100 Subject: [PATCH] simpler matching --- src/textual/command.py | 2 +- src/textual/fuzzy.py | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/textual/command.py b/src/textual/command.py index 07ec5f62c1..226269ac98 100644 --- a/src/textual/command.py +++ b/src/textual/command.py @@ -751,7 +751,7 @@ def _show_no_matches() -> None: _show_no_matches, ) - async def _watch__list_visible(self) -> None: + def _watch__list_visible(self) -> None: """React to the list visible flag being toggled.""" self.query_one(CommandList).set_class(self._list_visible, "--visible") self.query_one("#--input", Horizontal).set_class( diff --git a/src/textual/fuzzy.py b/src/textual/fuzzy.py index b09f8e1604..5b0282b42b 100644 --- a/src/textual/fuzzy.py +++ b/src/textual/fuzzy.py @@ -107,10 +107,15 @@ def highlight(self, candidate: str) -> Text: if match is None: return text assert match.lastindex is not None - offsets = [ - match.span(group_no)[0] for group_no in range(1, match.lastindex + 1) - ] - for offset in offsets: - text.stylize(self._match_style, offset, offset + 1) + if self._query in text.plain: + # Favor complete matches + offset = text.plain.index(self._query) + text.stylize(self._match_style, offset, offset + len(self._query)) + else: + offsets = [ + match.span(group_no)[0] for group_no in range(1, match.lastindex + 1) + ] + for offset in offsets: + text.stylize(self._match_style, offset, offset + 1) return text