Skip to content

Commit 697853c

Browse files
authored
gh-159973: Highlight PyREPL soft keywords after blank lines and dedents (GH-159992)
1 parent 7636e83 commit 697853c

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
@@ -280,29 +280,29 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
280280
trace("is_soft_keyword_used{t}", t=tokens)
281281
match tokens:
282282
case (
283-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
283+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
284284
TI(string="match"),
285285
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
286286
| TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{" | "~" | "...")
287287
):
288288
return True
289289
case (
290-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(string=":"),
290+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
291291
TI(string="match"),
292292
TI(T.NAME, string=s)
293293
):
294294
if keyword.iskeyword(s):
295295
return s in keyword_first_sets_match
296296
return True
297297
case (
298-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
298+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
299299
TI(string="case"),
300300
TI(T.NUMBER | T.STRING | T.FSTRING_START | T.TSTRING_START)
301301
| TI(T.OP, string="(" | "*" | "-" | "+" | "[" | "{")
302302
):
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="case"),
307307
TI(T.NAME, string=s)
308308
):
@@ -312,13 +312,13 @@ def is_soft_keyword_used(*tokens: TI | None) -> bool:
312312
case (TI(string="case"), TI(string="_"), TI(string=":")):
313313
return True
314314
case (
315-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":"),
315+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":"),
316316
TI(string="type"),
317317
TI(T.NAME, string=s)
318318
):
319319
return not keyword.iskeyword(s)
320320
case (
321-
None | TI(T.NEWLINE) | TI(T.INDENT) | TI(T.DEDENT) | TI(string=":" | ";"),
321+
None | TI(T.NEWLINE | T.NL | T.INDENT | T.DEDENT) | TI(string=":" | ";"),
322322
TI(string="lazy"),
323323
TI(string="import") | TI(string="from")
324324
):

‎Lib/test/test_pyrepl/test_utils.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,67 @@ def test_gen_colors_keyword_highlighting(self):
168168
actual_highlights.append((span_text, color.tag))
169169
self.assertEqual(actual_highlights, expected_highlights)
170170

171+
def test_gen_colors_soft_keywords_after_blank_lines(self):
172+
cases = [
173+
(
174+
"def f(): pass\n",
175+
"match -1:\n case _: pass",
176+
["match", "case", "_"],
177+
),
178+
(
179+
"def f(): pass\n",
180+
"match value:\n case _: pass",
181+
["match", "case", "_"],
182+
),
183+
(
184+
"match value:\n case 0: pass\n",
185+
" case -1: pass",
186+
["match", "case", "case"],
187+
),
188+
(
189+
"match value:\n case 0: pass\n",
190+
" case other: pass",
191+
["match", "case", "case"],
192+
),
193+
("def f(): pass\n", "type Alias = int", ["type"]),
194+
("def f(): pass\n", "lazy import mod", ["lazy"]),
195+
("def f(): pass\n", "lazy from mod import name", ["lazy"]),
196+
]
197+
for prefix, statement, expected in cases:
198+
for blank_line in ("\n", " \n", "# comment\n"):
199+
code = prefix + blank_line + statement
200+
with self.subTest(code=code):
201+
actual = [
202+
code[color.span.start:color.span.end + 1]
203+
for color in gen_colors(code)
204+
if color.tag == "soft_keyword"
205+
]
206+
self.assertEqual(actual, expected)
207+
208+
def test_gen_colors_match_after_dedent(self):
209+
for subject in ("-1", "value"):
210+
for separator in ("", "\n", "# comment\n"):
211+
code = (
212+
"if x:\n pass\n"
213+
f"{separator}match {subject}:\n case _: pass"
214+
)
215+
with self.subTest(code=code):
216+
actual = [
217+
code[color.span.start:color.span.end + 1]
218+
for color in gen_colors(code)
219+
if color.tag == "soft_keyword"
220+
]
221+
self.assertEqual(actual, ["match", "case", "_"])
222+
223+
def test_gen_colors_soft_keyword_names_in_brackets(self):
224+
for opening, closing in ("()", "[]", "{}"):
225+
for name in ("match", "case"):
226+
code = f"{opening}\n{name} - 1\n{closing}"
227+
with self.subTest(code=code):
228+
self.assertNotIn(
229+
"soft_keyword", [color.tag for color in gen_colors(code)]
230+
)
231+
171232
def test_gen_colors_command_highlighting(self):
172233
cases = [
173234
# highlights bare command names (after stripping whitespaces)
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)