3131
3232from . import rich_utils as ru
3333
34- # Regular expression to identify strings which we should sort numerically
35- NUMERIC_RE = re .compile (
34+ # Regular expression to identify strings that we should sort numerically
35+ _NUMERIC_RE = re .compile (
3636 r"""
3737 ^ # Start of string
3838 [-+]? # Optional sign
4646 re .VERBOSE ,
4747)
4848
49+ # Regular expression to identify whitespace characters that are rendered as
50+ # control sequences (like ^J or ^I) in the completion menu.
51+ _CONTROL_WHITESPACE_RE = re .compile (r'\r\n|[\n\r\t\f\v]' )
52+
4953
5054@dataclass (frozen = True , slots = True , kw_only = True )
5155class CompletionItem :
5256 """A single completion result."""
5357
54- _SANITIZE_RE = re .compile (r'\r\n|[\n\r\t\f\v]' )
55-
5658 # The underlying object this completion represents (e.g., str, int, Path).
5759 # This is used to support argparse choices validation.
5860 value : Any = field (kw_only = False )
@@ -78,15 +80,6 @@ class CompletionItem:
7880 display_plain : str = field (init = False )
7981 display_meta_plain : str = field (init = False )
8082
81- @classmethod
82- def _sanitize_display_string (cls , val : str ) -> str :
83- """Sanitize a string for display in the completion menu.
84-
85- This replaces whitespace characters that are rendered as
86- control sequences (like ^J or ^I) with spaces.
87- """
88- return cls ._SANITIZE_RE .sub (' ' , val )
89-
9083 def __post_init__ (self ) -> None :
9184 """Finalize the object after initialization."""
9285 # Derive text from value if it wasn't explicitly provided
@@ -98,8 +91,8 @@ def __post_init__(self) -> None:
9891 object .__setattr__ (self , "display" , self .text )
9992
10093 # Sanitize display and display_meta
101- object .__setattr__ (self , "display" , self . _sanitize_display_string (self .display ))
102- object .__setattr__ (self , "display_meta" , self . _sanitize_display_string (self .display_meta ))
94+ object .__setattr__ (self , "display" , sanitize_display_string (self .display ))
95+ object .__setattr__ (self , "display_meta" , sanitize_display_string (self .display_meta ))
10396
10497 # Create plain text versions by stripping ANSI sequences.
10598 # These are stored as attributes for fast access during sorting/filtering.
@@ -268,4 +261,16 @@ class Completions(CompletionResultsBase):
268261
269262def all_display_numeric (items : Collection [CompletionItem ]) -> bool :
270263 """Return True if items is non-empty and every item.display_plain value is a numeric string."""
271- return bool (items ) and all (NUMERIC_RE .match (item .display_plain ) for item in items )
264+ return bool (items ) and all (_NUMERIC_RE .match (item .display_plain ) for item in items )
265+
266+
267+ def sanitize_display_string (val : str ) -> str :
268+ """Sanitize a string for display in the completion menu.
269+
270+ This replaces whitespace characters that are rendered as
271+ control sequences (like ^J or ^I) with spaces.
272+
273+ :param val: string to be sanitized
274+ :return: the sanitized string
275+ """
276+ return _CONTROL_WHITESPACE_RE .sub (' ' , val )
0 commit comments