Skip to content

Commit 4e786a4

Browse files
authored
[3.15] gh-159973: Highlight PyREPL soft keywords after blank lines and dedents (GH-159992) (GH-162676)
1 parent ed0d9aa commit 4e786a4

3 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎Lib/_pyrepl/utils.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,29 +270,29 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
270270
trace("is_soft_keyword_used{t}", t=tokens)
271271
match tokens:
272272
case (
273-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
273+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
274274
TI(string="match"),
275275
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
276276
| TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{" | "~" | "...")
277277
):
278278
return True
279279
case (
280-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
280+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
281281
TI(string="match"),
282282
TI(T.NAME, string=s)
283283
):
284284
if keyword.iskeyword(s):
285285
return s in keyword_first_sets_match
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="case"),
290290
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
291291
| TI(T.OP, string="(" | "*" | "-" | "[" | "{")
292292
):
293293
return True
294294
case (
295-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
295+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
296296
TI(string="case"),
297297
TI(T.NAME, string=s)
298298
):
@@ -302,13 +302,13 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
302302
case (TI(string="case"), TI(string="_"), TI(string=":")):
303303
return True
304304
case (
305-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
305+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
306306
TI(string="type"),
307307
TI(T.NAME, string=s)
308308
):
309309
return not keyword.iskeyword(s)
310310
case (
311-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":" | ";"),
311+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":" | ";"),
312312
TI(string="lazy"),
313313
TI(string="import") | TI(string="from")
314314
):

‎Lib/test/test_pyrepl/test_utils.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,64 @@ def test_gen_colors_keyword_highlighting(self):
159159
span_text = code[color.span.start:color.span.end + 1]
160160
actual_highlights.append((span_text, color.tag))
161161
self.assertEqual(actual_highlights, expected_highlights)
162+
163+
def test_gen_colors_soft_keywords_after_blank_lines(self):
164+
cases = [
165+
(
166+
"def f(): pass\n",
167+
"match -1:\n case _: pass",
168+
["match", "case", "_"],
169+
),
170+
(
171+
"def f(): pass\n",
172+
"match value:\n case _: pass",
173+
["match", "case", "_"],
174+
),
175+
(
176+
"match value:\n case 0: pass\n",
177+
" case -1: pass",
178+
["match", "case", "case"],
179+
),
180+
(
181+
"match value:\n case 0: pass\n",
182+
" case other: pass",
183+
["match", "case", "case"],
184+
),
185+
("def f(): pass\n", "type Alias = int", ["type"]),
186+
("def f(): pass\n", "lazy import mod", ["lazy"]),
187+
("def f(): pass\n", "lazy from mod import name", ["lazy"]),
188+
]
189+
for prefix, statement, expected in cases:
190+
for blank_line in ("\n", " \n", "# comment\n"):
191+
code = prefix + blank_line + statement
192+
with self.subTest(code=code):
193+
actual = [
194+
code[color.span.start:color.span.end + 1]
195+
for color in gen_colors(code)
196+
if color.tag == "soft_keyword"
197+
]
198+
self.assertEqual(actual, expected)
199+
200+
def test_gen_colors_match_after_dedent(self):
201+
for subject in ("-1", "value"):
202+
for separator in ("", "\n", "# comment\n"):
203+
code = (
204+
"if x:\n pass\n"
205+
f"{separator}match {subject}:\n case _: pass"
206+
)
207+
with self.subTest(code=code):
208+
actual = [
209+
code[color.span.start:color.span.end + 1]
210+
for color in gen_colors(code)
211+
if color.tag == "soft_keyword"
212+
]
213+
self.assertEqual(actual, ["match", "case", "_"])
214+
215+
def test_gen_colors_soft_keyword_names_in_brackets(self):
216+
for opening, closing in ("()", "[]", "{}"):
217+
for name in ("match", "case"):
218+
code = f"{opening}\n{name} - 1\n{closing}"
219+
with self.subTest(code=code):
220+
self.assertNotIn(
221+
"soft_keyword", [color.tag for color in gen_colors(code)]
222+
)
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)