@@ -77,19 +77,25 @@ def _phys_tokens(toks: TokenInfos) -> TokenInfos:
77
77
last_lineno = elineno
78
78
79
79
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."""
82
82
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 ()
85
85
self .visit (ast .parse (source ))
86
86
87
87
if sys .version_info >= (3 , 10 ):
88
88
def visit_Match (self , node : ast .Match ) -> None :
89
89
"""Invoked by ast.NodeVisitor.visit"""
90
- self .match_case_lines .add (node .lineno )
90
+ self .soft_key_lines .add (node .lineno )
91
91
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 )
93
99
self .generic_visit (node )
94
100
95
101
@@ -117,7 +123,7 @@ def source_token_lines(source: str) -> TSourceTokenLines:
117
123
tokgen = generate_tokens (source )
118
124
119
125
if env .PYBEHAVIOR .soft_keywords :
120
- match_case_lines = MatchCaseFinder (source ).match_case_lines
126
+ soft_key_lines = SoftKeywordFinder (source ).soft_key_lines
121
127
122
128
for ttype , ttext , (sline , scol ), (_ , ecol ), _ in _phys_tokens (tokgen ):
123
129
mark_start = True
@@ -152,7 +158,7 @@ def source_token_lines(source: str) -> TSourceTokenLines:
152
158
is_start_of_line = True
153
159
else :
154
160
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 :
156
162
tok_class = "key"
157
163
line .append ((tok_class , part ))
158
164
mark_end = True
0 commit comments