@@ -21,6 +21,27 @@ def safe_getattr(obj, name):
2121 return getattr (obj , name , None )
2222
2323
24+ def colorize_matches (names , values , theme ):
25+ return [
26+ _color_for_obj (name , obj , theme )
27+ for name , obj in zip (names , values )
28+ ]
29+
30+ def _color_for_obj (name , value , theme ):
31+ t = type (value )
32+ color = _color_by_type (t , theme )
33+ return f"{ color } { name } { ANSIColors .RESET } "
34+
35+
36+ def _color_by_type (t , theme ):
37+ typename = t .__name__
38+ # this is needed e.g. to turn method-wrapper into method_wrapper,
39+ # because if we want _colorize.FancyCompleter to be "dataclassable"
40+ # our keys need to be valid identifiers.
41+ typename = typename .replace ('-' , '_' ).replace ('.' , '_' )
42+ return getattr (theme .fancycompleter , typename , ANSIColors .RESET )
43+
44+
2445class Completer (rlcompleter .Completer ):
2546 """
2647 When doing something like a.b.<tab>, keep the full a.b.attr completion
@@ -169,23 +190,7 @@ def _attr_matches(self, text):
169190 return expr , attr , names , values
170191
171192 def colorize_matches (self , names , values ):
172- return [
173- self ._color_for_obj (name , obj )
174- for name , obj in zip (names , values )
175- ]
176-
177- def _color_for_obj (self , name , value ):
178- t = type (value )
179- color = self ._color_by_type (t )
180- return f"{ color } { name } { ANSIColors .RESET } "
181-
182- def _color_by_type (self , t ):
183- typename = t .__name__
184- # this is needed e.g. to turn method-wrapper into method_wrapper,
185- # because if we want _colorize.FancyCompleter to be "dataclassable"
186- # our keys need to be valid identifiers.
187- typename = typename .replace ('-' , '_' ).replace ('.' , '_' )
188- return getattr (self .theme .fancycompleter , typename , ANSIColors .RESET )
193+ return colorize_matches (names , values , self .theme )
189194
190195
191196def commonprefix (names ):
0 commit comments