1313
1414import rich .repr
1515
16- from textual .cache import LRUCache
1716from textual .content import Content
1817from textual .visual import Style
1918
@@ -58,16 +57,13 @@ class FuzzySearch:
5857 Unlike a regex solution, this will finds all possible matches.
5958 """
6059
61- cache : LRUCache [tuple [str , str , bool ], tuple [float , tuple [int , ...]]] = LRUCache (
62- maxsize = 1024
63- )
64-
6560 def __init__ (self , case_sensitive : bool = False ) -> None :
6661 """Initialize fuzzy search.
6762
6863 Args:
6964 case_sensitive: Is the match case sensitive?
7065 """
66+ self .cache : dict [tuple [str , str , bool ], tuple [float , tuple [int , ...]]] = {}
7167 self .case_sensitive = case_sensitive
7268
7369 def match (self , query : str , candidate : str ) -> tuple [float , tuple [int , ...]]:
@@ -128,8 +124,8 @@ def score(search: _Search) -> float:
128124 """
129125 # This is a heuristic, and can be tweaked for better results
130126 # Boost first letter matches
131- score : float = len ( search . offsets ) + len (
132- first_letters . intersection ( search .offsets )
127+ score : float = sum (
128+ ( 2.0 if offset in first_letters else 1.0 ) for offset in search .offsets
133129 )
134130 # Boost to favor less groups
135131 offset_count = len (search .offsets )
@@ -143,10 +139,9 @@ def score(search: _Search) -> float:
143139 query_size = len (query )
144140 find = candidate .find
145141 # Limit the number of loops out of an abundance of caution.
146- # This should be hard to reach without contrived data.
147- remaining_loops = 10_000
142+ # This would be hard to reach without contrived data.
143+ remaining_loops = 200
148144
149- # TODO: Can this be optimized? The following can be slow for long candidates (can reach ~1ms).
150145 while stack and (remaining_loops := remaining_loops - 1 ):
151146 search = pop ()
152147 offset = find (query [search .query_offset ], search .candidate_offset )
@@ -156,8 +151,8 @@ def score(search: _Search) -> float:
156151 yield score (advance_branch ), advance_branch .offsets
157152 push (branch )
158153 else :
159- push (branch )
160154 push (advance_branch )
155+ push (branch )
161156
162157
163158@rich .repr .auto
0 commit comments