Skip to content

Commit 6c70847

Browse files
committed
fix: xx.keyword that keyword should not be highlight
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
1 parent 11a5fc8 commit 6c70847

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

Lib/_pyrepl/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ def gen_colors_from_token_stream(
196196
is_def_name = False
197197
span = Span.from_token(token, line_lengths)
198198
yield ColorSpan(span, "definition")
199-
elif keyword.iskeyword(token.string):
199+
elif (keyword.iskeyword(token.string)
200+
and not (prev_token and prev_token.type == T.OP and prev_token.string == ".")):
200201
span = Span.from_token(token, line_lengths)
201202
yield ColorSpan(span, "keyword")
202203
if token.string in IDENTIFIERS_AFTER:
@@ -208,7 +209,8 @@ def gen_colors_from_token_stream(
208209
):
209210
span = Span.from_token(token, line_lengths)
210211
yield ColorSpan(span, "soft_keyword")
211-
elif token.string in BUILTINS:
212+
elif (token.string in BUILTINS
213+
and not (prev_token and prev_token.type == T.OP and prev_token.string == ".")):
212214
span = Span.from_token(token, line_lengths)
213215
yield ColorSpan(span, "builtin")
214216

Lib/test/test_pyrepl/test_utils.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22

3-
from _pyrepl.utils import str_width, wlen, prev_next_window
3+
from _pyrepl.utils import str_width, wlen, prev_next_window, gen_colors
44

55

66
class TestUtils(TestCase):
@@ -60,3 +60,38 @@ def gen_raise():
6060
self.assertEqual(next(pnw), (3, 4, None))
6161
with self.assertRaises(ZeroDivisionError):
6262
next(pnw)
63+
64+
def test_gen_colors_keyword_highlighting(self):
65+
no_highlight_cases = [
66+
("a.set", [(".", "op")]), # 'set' should not be highlighted after '.'
67+
("a.def", [(".", "op")]), # 'def' should not be highlighted after '.'
68+
("obj.class", [(".", "op")]), # 'class' should not be highlighted after '.'
69+
("obj.list", [(".", "op")]), # 'list' should not be highlighted after '.'
70+
("obj.match", [(".", "op")]), # 'match' should not be highlighted after '.'
71+
]
72+
for code, expected_highlights in no_highlight_cases:
73+
with self.subTest(code=code):
74+
colors = list(gen_colors(code))
75+
# Extract (text, tag) pairs for comparison
76+
actual_highlights = []
77+
for color in colors:
78+
span_text = code[color.span.start:color.span.end + 1]
79+
actual_highlights.append((span_text, color.tag))
80+
self.assertEqual(actual_highlights, expected_highlights,
81+
f"In '{code}', expected {expected_highlights}, got {actual_highlights}")
82+
highlight_cases = [
83+
("set", [("set", "builtin")]),
84+
("list", [("list", "builtin")]),
85+
("def func():", [("def", "keyword"), ("func", "definition"), ("(", "op"), (")", "op"), (":", "op")]),
86+
("class A:", [("class", "keyword"), ("A", "definition"), (":", "op")]),
87+
]
88+
for code, expected_highlights in highlight_cases:
89+
with self.subTest(code=code):
90+
colors = list(gen_colors(code))
91+
# Extract (text, tag) pairs for comparison
92+
actual_highlights = []
93+
for color in colors:
94+
span_text = code[color.span.start:color.span.end + 1]
95+
actual_highlights.append((span_text, color.tag))
96+
self.assertEqual(actual_highlights, expected_highlights,
97+
f"In '{code}', expected {expected_highlights}, got {actual_highlights}")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: xxx.keyword the keyword should not highlight in new repl

0 commit comments

Comments
 (0)