diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index fbc0f5926ea16b..4038f83a98f464 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -39,6 +39,7 @@ from .console import Console as ConsoleType from ._module_completer import ModuleCompleter, make_default_module_completer from .fancycompleter import Completer as FancyCompleter, colorize_matches +from .utils import gen_colors Console: type[ConsoleType] _error: tuple[type[Exception], ...] | type[Exception] @@ -256,23 +257,41 @@ def _get_first_indentation(buffer: list[str]) -> str | None: def _should_auto_indent(buffer: list[str], pos: int) -> bool: - # check if last character before "pos" is a colon, ignoring - # whitespaces and comments. - last_char = None - while pos > 0: - pos -= 1 - if last_char is None: - if buffer[pos] not in " \t\n#": # ignore whitespaces and comments - last_char = buffer[pos] - else: - # even if we found a non-whitespace character before - # original pos, we keep going back until newline is reached - # to make sure we ignore comments - if buffer[pos] == "\n": - break - if buffer[pos] == "#": - last_char = None - return last_char == ":" + buffer_str = ''.join(buffer) + colors = tuple(gen_colors(buffer_str)) + string_spans = tuple(c.span for c in colors if c.tag == "string") + comment_spans = tuple(c.span for c in colors if c.tag == "comment") + def in_span(i, spans): + return any(s.start <= i <= s.end for s in spans) + i = pos - 1 + while i >= 0: + if buffer_str[i] in " \t\n": + i -= 1 + continue + if in_span(i, comment_spans): + i -= 1 + continue + if in_span(i, string_spans): + # A string is an expression, whether already closed or still + # being typed. Stop here instead of skipping past it and + # reusing a colon from an earlier line or one-line suite. + return False + break + if i < 0 or buffer_str[i] != ":": + return False + + # A colon only triggers an indent if the new line is not already deeper + # than the line with the colon. Otherwise a preceding indented line + # (for example a comment) has already entered the block, and reusing the + # colon would add a second level of indentation. + colon_line_start = i + while colon_line_start > 0 and buffer_str[colon_line_start - 1] != "\n": + colon_line_start -= 1 + colon_indent = 0 + while (colon_line_start + colon_indent <= i + and buffer_str[colon_line_start + colon_indent] in " \t"): + colon_indent += 1 + return _get_this_line_indent(buffer, pos) <= colon_indent class maybe_accept(commands.Command): diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index c11c759f559aff..8676e1464b882f 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -616,6 +616,25 @@ def test_auto_indent_with_comment(self): output = multiline_input(reader) self.assertEqual(output, output_code) + # fmt: off + events = code_to_events( + "def f():\n" + "# foo\n" + "pass\n\n" + ) + + output_code = ( + "def f():\n" + " # foo\n" + " pass\n" + " " + ) + # fmt: on + + reader = self.prepare_reader(events) + output = multiline_input(reader) + self.assertEqual(output, output_code) + def test_auto_indent_with_multicomment(self): # fmt: off events = code_to_events( @@ -649,6 +668,80 @@ def test_auto_indent_ignore_comments(self): output = multiline_input(reader) self.assertEqual(output, output_code) + def test_dont_indent_hashtag(self): + # fmt: off + events = code_to_events( + "if ' ' == '#':\n" + "pass\n\n" + ) + + output_code = ( + "if ' ' == '#':\n" + " pass\n" + " " + ) + # fmt: on + + reader = self.prepare_reader(events) + output = multiline_input(reader) + self.assertEqual(output, output_code) + + def test_dont_indent_in_multiline_string(self): + # fmt: off + events = code_to_events( + "s = '''\n" + "Note:\n" + "'''\n\n" + ) + + output_code = ( + "s = '''\n" + "Note:\n" + "'''" + ) + # fmt: on + + reader = self.prepare_reader(events) + output = multiline_input(reader) + self.assertEqual(output, output_code) + + def test_dont_reuse_colon_after_complete_string(self): + # fmt: off + events = code_to_events( + "if True:\n" + "'s'\n\n" + ) + + output_code = ( + "if True:\n" + " 's'\n" + " " + ) + # fmt: on + + reader = self.prepare_reader(events) + output = multiline_input(reader) + self.assertEqual(output, output_code) + + def test_dont_reuse_colon_in_unterminated_string(self): + # The colon of a one-line suite must not trigger indentation while + # the string that follows it is still being typed. + # fmt: off + events = code_to_events( + "if True: '''s\n" + "'''\n\n" + ) + + output_code = ( + "if True: '''s\n" + "'''\n" + ) + # fmt: on + + reader = self.prepare_reader(events) + output = multiline_input(reader) + self.assertEqual(output, output_code) + class TestPyReplOutput(ScreenEqualMixin, TestCase): def prepare_reader(self, events): diff --git a/Misc/NEWS.d/next/Library/2025-10-28-23-14-16.gh-issue-133710.BzRnmu.rst b/Misc/NEWS.d/next/Library/2025-10-28-23-14-16.gh-issue-133710.BzRnmu.rst new file mode 100644 index 00000000000000..35b7e6343583df --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-28-23-14-16.gh-issue-133710.BzRnmu.rst @@ -0,0 +1 @@ +Enhance auto-indent in :mod:`!_pyrepl`.