Skip to content

Commit f731b92

Browse files
johnslavikpablogsal
authored andcommitted
Highlight matching delimiters in the REPL
1 parent bd874ac commit f731b92

9 files changed

Lines changed: 689 additions & 15 deletions

File tree

‎Doc/tutorial/appendix.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ control capabilities.
1717
Since Python 3.13, a new interactive shell is used by default.
1818
This one supports color, multiline editing, history browsing, and
1919
paste mode. To disable color, see :ref:`using-on-controlling-color` for
20-
details. Function keys provide some additional functionality.
20+
details. When the cursor touches a parenthesis, bracket, brace, or string
21+
quote, its matching delimiter is highlighted. This includes triple quotes
22+
and expressions in formatted and template strings. Delimiters within
23+
string contents and comments are ignored. Function keys provide some
24+
additional functionality.
2125
:kbd:`F1` enters the interactive help browser :mod:`pydoc`.
2226
:kbd:`F2` allows for browsing command-line history with neither output nor the
2327
:term:`>>>` and :term:`...` prompts. :kbd:`F3` enters "paste mode", which

‎Lib/_colorize.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ class Syntax(ThemeSection):
412412
number: str = ANSIColors.YELLOW
413413
op: str = ANSIColors.RESET
414414
definition: str = ANSIColors.BOLD
415+
matching_delimiter: str = ANSIColors.INTENSE_BACKGROUND_BLACK
415416
soft_keyword: str = ANSIColors.BOLD_BLUE
416417
reset: str = ANSIColors.RESET
417418

‎Lib/_pyrepl/reader.py‎

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
from .layout import LayoutMap, LayoutResult, LayoutRow, WrappedRow, layout_content_lines
3838
from .layout import wrapped_row_end
3939
from .render import RenderCell, RenderLine, RenderedScreen, ScreenOverlay
40-
from .utils import ANSI_ESCAPE_SEQUENCE, ColorSpan, THEME, StyleRef, gen_colors
40+
from .utils import (
41+
ANSI_ESCAPE_SEQUENCE, ColorSpan, THEME, StyleRef,
42+
find_matching_delimiter, gen_colors,
43+
)
4144
from .trace import trace
4245

4346

@@ -304,6 +307,7 @@ class Reader:
304307
cxy: CursorXY = field(init=False)
305308
lxy: CursorXY = field(init=False)
306309
scheduled_commands: list[CommandName] = field(default_factory=list)
310+
_matching_delimiter: tuple[int, int] | None = field(init=False)
307311
can_colorize: bool = False
308312
gen_colors: Callable[[str], Iterator[ColorSpan]] = gen_colors
309313
threading_hook: Callback | None = None
@@ -386,6 +390,7 @@ def __post_init__(self) -> None:
386390
self.input_trans = input.KeymapTranslator(
387391
self.keymap, invalid_cls="invalid-key", character_cls="self-insert"
388392
)
393+
self._matching_delimiter = None
389394
self.layout = LayoutMap.empty()
390395
self.cxy = self.pos2xy()
391396
self.lxy = (self.pos, 0)
@@ -429,9 +434,8 @@ def calc_screen(self) -> RenderedScreen:
429434
self,
430435
reuse_full=True,
431436
)
432-
assert not self.last_refresh_cache.line_end_offsets or (
433-
self.last_refresh_cache.line_end_offsets[-1] >= len(self.buffer)
434-
), "Buffer modified without invalidate_buffer() call"
437+
# A short screen can cache only a prefix of the buffer.
438+
# get_cached_location() then falls back to a full rebuild.
435439
else:
436440
offset, num_common_lines = self.last_refresh_cache.get_cached_location(
437441
self,
@@ -602,8 +606,51 @@ def _render_message_lines(self) -> tuple[RenderLine, ...]:
602606
def get_screen_overlays(self) -> tuple[ScreenOverlay, ...]:
603607
return ()
604608

609+
def _get_matching_delimiter(self) -> tuple[int, int] | None:
610+
if not self.can_colorize or self.finished:
611+
return None
612+
if not any(
613+
0 <= i < len(self.buffer) and self.buffer[i] in "()[]{}\"'"
614+
for i in (self.pos, self.pos - 1)
615+
):
616+
return None
617+
return find_matching_delimiter(self.get_unicode(), self.pos)
618+
619+
def _matching_delimiter_overlays(
620+
self, base_screen: RenderedScreen
621+
) -> tuple[ScreenOverlay, ...]:
622+
if (
623+
self._matching_delimiter is None
624+
or not self.last_refresh_cache.line_end_offsets
625+
):
626+
return ()
627+
628+
changed_rows: dict[int, list[RenderCell]] = {}
629+
start, end = self._matching_delimiter
630+
for offset in range(start, end):
631+
# pos_to_xy() clamps positions beyond the cached layout.
632+
if offset >= self.last_refresh_cache.line_end_offsets[-1]:
633+
break
634+
# The delimiter's end disambiguates a wrap boundary.
635+
x, y = self.layout.pos_to_xy(offset + 1)
636+
cells = changed_rows.setdefault(y, list(base_screen.lines[y].cells))
637+
column = 0
638+
for i, cell in enumerate(cells):
639+
if column == x - 1 and cell.width:
640+
cells[i] = replace(
641+
cell, style=StyleRef.from_tag("matching_delimiter")
642+
)
643+
break
644+
column += cell.width
645+
return tuple(
646+
ScreenOverlay(y, (RenderLine.from_cells(cells),))
647+
for y, cells in changed_rows.items()
648+
)
649+
605650
def compose_rendered_screen(self, base_screen: RenderedScreen) -> RenderedScreen:
606-
overlays = list(self.get_screen_overlays())
651+
self._matching_delimiter = self._get_matching_delimiter()
652+
overlays = list(self._matching_delimiter_overlays(base_screen))
653+
overlays.extend(self.get_screen_overlays())
607654
message_lines = self._render_message_lines()
608655
if message_lines:
609656
overlays.append(ScreenOverlay(len(base_screen.lines), message_lines))
@@ -826,6 +873,7 @@ def prepare(self) -> None:
826873
self.finished = False
827874
del self.buffer[:]
828875
self.pos = 0
876+
self._matching_delimiter = None
829877
self.layout = LayoutMap.empty()
830878
self.cxy = self.pos2xy()
831879
self.lxy = (self.pos, 0)
@@ -892,6 +940,11 @@ def error(self, msg: str = "none") -> None:
892940
self.invalidate_message()
893941

894942
def update_screen(self) -> None:
943+
if (
944+
not self.invalidation.needs_screen_refresh
945+
and self._get_matching_delimiter() != self._matching_delimiter
946+
):
947+
self.invalidate_overlay()
895948
if self.invalidation.is_cursor_only:
896949
self.update_cursor()
897950
self.clear_invalidation()
@@ -939,12 +992,12 @@ def do_cmd(self, cmd: CommandInput) -> None:
939992
and not self.invalidation.is_cursor_only
940993
):
941994
self.invalidate_cursor()
995+
self.finished = bool(command.finish)
942996
self.update_screen()
943997

944998
if command_type is not commands.digit_arg:
945999
self.last_command = command_type
9461000

947-
self.finished = bool(command.finish)
9481001
if self.finished:
9491002
self.console.finish()
9501003
self.finish()

‎Lib/_pyrepl/simple_interact.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def maybe_run_command(statement: str) -> bool:
164164
r.cmpltn_reset()
165165
if r.input_trans is r.isearch_trans:
166166
r.do_cmd(("isearch-end", [""]))
167+
r.finished = True
167168
r.pos = len(r.get_unicode())
168169
r.invalidate_full()
169170
r.refresh()

‎Lib/_pyrepl/utils.py‎

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,7 @@ def unbracket(s: str, including_content: bool = False) -> str:
130130
return s.translate(ZERO_WIDTH_TRANS)
131131

132132

133-
def gen_colors(buffer: str) -> Iterator[ColorSpan]:
134-
"""Returns a list of index spans to color using the given color tag.
135-
136-
The input `buffer` should be a valid start of a Python code block, i.e.
137-
it cannot be a block starting in the middle of a multiline string.
138-
"""
133+
def _get_token_stream(buffer: str) -> tuple[Iterator[TI], list[int]]:
139134
sio = StringIO(buffer)
140135
line_lengths = [0] + [len(line) for line in sio.readlines()]
141136
# make line_lengths cumulative
@@ -144,6 +139,87 @@ def gen_colors(buffer: str) -> Iterator[ColorSpan]:
144139

145140
sio.seek(0)
146141
gen = tokenize.generate_tokens(sio.readline)
142+
return gen, line_lengths
143+
144+
145+
def find_matching_delimiter(buffer: str, pos: int) -> tuple[int, int] | None:
146+
"""Return the half-open span of the partner touching the cursor."""
147+
# A delimiter under the cursor takes precedence, even inside literal data.
148+
candidate = next((
149+
i for i in (pos, pos - 1)
150+
if 0 <= i < len(buffer) and buffer[i] in "()[]{}\"'"
151+
), None)
152+
if candidate is None:
153+
return None
154+
155+
gen, line_lengths = _get_token_stream(buffer)
156+
try:
157+
return _find_matching_delimiter_from_token_stream(
158+
gen, line_lengths, candidate
159+
)
160+
except SyntaxError, tokenize.TokenError:
161+
# Incomplete input can still contain complete pairs.
162+
return None
163+
164+
165+
def _find_matching_delimiter_from_token_stream(
166+
token_generator: Iterator[TI],
167+
line_lengths: list[int],
168+
candidate: int,
169+
) -> tuple[int, int] | None:
170+
brackets: list[tuple[str, int]] = []
171+
strings: list[tuple[int, int]] = []
172+
closing = {")": "(", "]": "[", "}": "{"}
173+
for token in token_generator:
174+
if token.type == T.ERRORTOKEN and token.string in {"\"", "'"}:
175+
# An unfinished short string can expose apparent operators.
176+
break
177+
offset = line_lengths[token.start[0] - 1] + token.start[1]
178+
pair = None
179+
match token.type:
180+
case T.STRING | T.FSTRING_START | T.TSTRING_START:
181+
# Token boundaries already account for prefixes and escapes.
182+
quote_index = next(
183+
i for i, c in enumerate(token.string) if c in "\"'"
184+
)
185+
delimiter = token.string[quote_index]
186+
width = 3 if token.string.startswith(delimiter * 3, quote_index) else 1
187+
opening = (offset + quote_index, offset + quote_index + width)
188+
if token.type == T.STRING:
189+
end = line_lengths[token.end[0] - 1] + token.end[1]
190+
pair = (opening, (end - width, end))
191+
else:
192+
strings.append(opening)
193+
case T.FSTRING_END | T.TSTRING_END:
194+
opening = strings.pop()
195+
pair = (opening, (offset, offset + len(token.string)))
196+
case T.OP if token.string in "()[]{}":
197+
if token.string in "([{":
198+
brackets.append((token.string, offset))
199+
elif brackets and brackets[-1][0] == closing[token.string]:
200+
_, opening_offset = brackets.pop()
201+
pair = (
202+
(opening_offset, opening_offset + 1), (offset, offset + 1)
203+
)
204+
else:
205+
# Do not pair across an incorrectly nested delimiter.
206+
brackets.clear()
207+
if pair is not None:
208+
opening, ending = pair
209+
if opening[0] <= candidate < opening[1]:
210+
return ending
211+
if ending[0] <= candidate < ending[1]:
212+
return opening
213+
return None
214+
215+
216+
def gen_colors(buffer: str) -> Iterator[ColorSpan]:
217+
"""Returns a list of index spans to color using the given color tag.
218+
219+
The input `buffer` should be a valid start of a Python code block, i.e.
220+
it cannot be a block starting in the middle of a multiline string.
221+
"""
222+
gen, line_lengths = _get_token_stream(buffer)
147223
last_emitted: ColorSpan | None = None
148224
try:
149225
for color in gen_colors_from_token_stream(gen, line_lengths):

‎Lib/test/test_pyrepl/test_interact.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
_more_lines,
1414
run_multiline_interactive_console,
1515
)
16+
from .support import FakeConsole, prepare_reader
17+
1618

1719
class TestSimpleInteract(unittest.TestCase):
1820
def test_prompts_are_converted_to_strings(self):
@@ -53,6 +55,48 @@ def __str__(self):
5355

5456
multiline_input.assert_called_once_with(ANY, ">>> ", ANY)
5557

58+
def test_keyboard_interrupt_clears_matching_delimiter_overlay(self):
59+
backend = FakeConsole(())
60+
reader = prepare_reader(backend)
61+
reader.can_colorize = True
62+
reader.buffer = list("(x)")
63+
reader.pos = 0
64+
reader.refresh()
65+
66+
matching_cells = [
67+
cell
68+
for overlay in reader.rendered_screen.overlays
69+
for line in overlay.lines
70+
for cell in line.cells
71+
if cell.style.tag == "matching_delimiter"
72+
]
73+
self.assertEqual([cell.text for cell in matching_cells], [")"])
74+
75+
console = InteractiveColoredConsole()
76+
reader.push_input_trans(reader.isearch_trans)
77+
with (
78+
patch("_pyrepl.readline._setup"),
79+
patch("_pyrepl.simple_interact._get_reader", return_value=reader),
80+
patch(
81+
"_pyrepl.simple_interact.multiline_input",
82+
side_effect=[KeyboardInterrupt, EOFError],
83+
),
84+
patch.object(console, "runsource", wraps=console.runsource) as runsource,
85+
contextlib.redirect_stderr(io.StringIO()) as stderr,
86+
):
87+
run_multiline_interactive_console(console)
88+
89+
self.assertEqual("".join(reader.buffer), "(x)")
90+
self.assertFalse(
91+
any(
92+
cell.style.tag == "matching_delimiter"
93+
for line in reader.rendered_screen.composed_lines
94+
for cell in line.cells
95+
)
96+
)
97+
self.assertIn("KeyboardInterrupt", stderr.getvalue())
98+
runsource.assert_not_called()
99+
56100
def test_multiple_statements(self):
57101
namespace = {}
58102
code = dedent("""\

0 commit comments

Comments
 (0)