Skip to content

Commit 5548211

Browse files
authored
[3.14] gh-159973: Highlight PyREPL soft keywords after blank lines and dedents (GH-159992) (GH-162677)
1 parent f10d09d commit 5548211

3 files changed

Lines changed: 67 additions & 5 deletions

File tree

‎Lib/_pyrepl/utils.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,29 +253,29 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
253253
trace("is_soft_keyword_used{t}", t=tokens)
254254
match tokens:
255255
case (
256-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
256+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
257257
TI(string="match"),
258258
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
259259
| TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{" | "~" | "...")
260260
):
261261
return True
262262
case (
263-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
263+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
264264
TI(string="match"),
265265
TI(T.NAME, string=s)
266266
):
267267
if keyword.iskeyword(s):
268268
return s in keyword_first_sets_match
269269
return True
270270
case (
271-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
271+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
272272
TI(string="case"),
273273
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
274274
| TI(T.OP, string="(" | "*" | "-" | "[" | "{")
275275
):
276276
return True
277277
case (
278-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
278+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
279279
TI(string="case"),
280280
TI(T.NAME, string=s)
281281
):
@@ -285,7 +285,7 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
285285
case (TI(string="case"), TI(string="_"), TI(string=":")):
286286
return True
287287
case (
288-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
288+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
289289
TI(string="type"),
290290
TI(T.NAME, string=s)
291291
):

‎Lib/test/test_pyrepl/test_utils.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,62 @@ def test_gen_colors_keyword_highlighting(self):
127127
span_text = code[color.span.start:color.span.end + 1]
128128
actual_highlights.append((span_text, color.tag))
129129
self.assertEqual(actual_highlights, expected_highlights)
130+
131+
def test_gen_colors_soft_keywords_after_blank_lines(self):
132+
cases = [
133+
(
134+
"def f(): pass\n",
135+
"match -1:\n case _: pass",
136+
["match", "case", "_"],
137+
),
138+
(
139+
"def f(): pass\n",
140+
"match value:\n case _: pass",
141+
["match", "case", "_"],
142+
),
143+
(
144+
"match value:\n case 0: pass\n",
145+
" case -1: pass",
146+
["match", "case", "case"],
147+
),
148+
(
149+
"match value:\n case 0: pass\n",
150+
" case other: pass",
151+
["match", "case", "case"],
152+
),
153+
("def f(): pass\n", "type Alias = int", ["type"]),
154+
]
155+
for prefix, statement, expected in cases:
156+
for blank_line in ("\n", " \n", "# comment\n"):
157+
code = prefix + blank_line + statement
158+
with self.subTest(code=code):
159+
actual = [
160+
code[color.span.start:color.span.end + 1]
161+
for color in gen_colors(code)
162+
if color.tag == "soft_keyword"
163+
]
164+
self.assertEqual(actual, expected)
165+
166+
def test_gen_colors_match_after_dedent(self):
167+
for subject in ("-1", "value"):
168+
for separator in ("", "\n", "# comment\n"):
169+
code = (
170+
"if x:\n pass\n"
171+
f"{separator}match {subject}:\n case _: pass"
172+
)
173+
with self.subTest(code=code):
174+
actual = [
175+
code[color.span.start:color.span.end + 1]
176+
for color in gen_colors(code)
177+
if color.tag == "soft_keyword"
178+
]
179+
self.assertEqual(actual, ["match", "case", "_"])
180+
181+
def test_gen_colors_soft_keyword_names_in_brackets(self):
182+
for opening, closing in ("()", "[]", "{}"):
183+
for name in ("match", "case"):
184+
code = f"{opening}\n{name} - 1\n{closing}"
185+
with self.subTest(code=code):
186+
self.assertNotIn(
187+
"soft_keyword", [color.tag for color in gen_colors(code)]
188+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix soft keyword highlighting after blank lines, comment-only lines, and
2+
dedents in the :ref:`interactive shell <tut-interactive>`. Patch by Bartosz
3+
Sławecki.

0 commit comments

Comments
 (0)