Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/_pyrepl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ def gen_colors_from_token_stream(
):
span = Span.from_token(token, line_lengths)
yield ColorSpan(span, "soft_keyword")
elif token.string in BUILTINS:
elif (
token.string in BUILTINS
and not (prev_token and prev_token.exact_type == T.DOT)
):
span = Span.from_token(token, line_lengths)
yield ColorSpan(span, "builtin")

Expand Down
23 changes: 22 additions & 1 deletion Lib/test/test_pyrepl/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from _pyrepl.utils import str_width, wlen, prev_next_window
from _pyrepl.utils import str_width, wlen, prev_next_window, gen_colors


class TestUtils(TestCase):
Expand Down Expand Up @@ -60,3 +60,24 @@ def gen_raise():
self.assertEqual(next(pnw), (3, 4, None))
with self.assertRaises(ZeroDivisionError):
next(pnw)

def test_gen_colors_keyword_highlighting(self):
cases = [
# no highlights
("a.set", [(".", "op")]),
("obj.list", [(".", "op")]),
("obj.match", [(".", "op")]),
("b. \\\n format", [(".", "op")]),
# highlights
("set", [("set", "builtin")]),
("list", [("list", "builtin")]),
]
for code, expected_highlights in cases:
with self.subTest(code=code):
colors = list(gen_colors(code))
# Extract (text, tag) pairs for comparison
actual_highlights = []
for color in colors:
span_text = code[color.span.start:color.span.end + 1]
actual_highlights.append((span_text, color.tag))
self.assertEqual(actual_highlights, expected_highlights)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The default REPL now avoids highlighting built-in names (for instance :class:`set`
or :func:`format`) when they are used as attribute names (for instance in ``value.set``
or ``text.format``).

Loading