@@ -77,19 +77,25 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
7777 last_lineno = elineno
7878
7979
80- class MatchCaseFinder (ast .NodeVisitor ):
81- """Helper for finding match/case lines."""
80+ class SoftKeywordFinder (ast .NodeVisitor ):
81+ """Helper for finding lines with soft keywords, like match/case lines."""
8282 def __init__ (self , source : str ) -> None :
83- # This will be the set of line numbers that start match or case statements .
84- self .match_case_lines : Set [TLineNo ] = set ()
83+ # This will be the set of line numbers that start with a soft keyword .
84+ self .soft_key_lines : Set [TLineNo ] = set ()
8585 self .visit (ast .parse (source ))
8686
8787 if sys .version_info >= (3 , 10 ):
8888 def visit_Match (self , node : ast .Match ) -> None :
8989 """Invoked by ast.NodeVisitor.visit"""
90- self .match_case_lines .add (node .lineno )
90+ self .soft_key_lines .add (node .lineno )
9191 for case in node .cases :
92- self .match_case_lines .add (case .pattern .lineno )
92+ self .soft_key_lines .add (case .pattern .lineno )
93+ self .generic_visit (node )
94+
95+ if sys .version_info >= (3 , 12 ):
96+ def visit_TypeAlias (self , node : ast .TypeAlias ) -> None :
97+ """Invoked by ast.NodeVisitor.visit"""
98+ self .soft_key_lines .add (node .lineno )
9399 self .generic_visit (node )
94100
95101
@@ -117,7 +123,7 @@ def source_token_lines(source: str) -> TSourceTokenLines:
117123 tokgen = generate_tokens (source )
118124
119125 if env .PYBEHAVIOR .soft_keywords :
120- match_case_lines = MatchCaseFinder (source ).match_case_lines
126+ soft_key_lines = SoftKeywordFinder (source ).soft_key_lines
121127
122128 for ttype , ttext , (sline , scol ), (_ , ecol ), _ in _phys_tokens (tokgen ):
123129 mark_start = True
@@ -152,7 +158,7 @@ def source_token_lines(source: str) -> TSourceTokenLines:
152158 is_start_of_line = True
153159 else :
154160 is_start_of_line = False
155- if is_start_of_line and sline in match_case_lines :
161+ if is_start_of_line and sline in soft_key_lines :
156162 tok_class = "key"
157163 line .append ((tok_class , part ))
158164 mark_end = True
0 commit comments