Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
34 changes: 17 additions & 17 deletions Lib/_pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from .completing_reader import CompletingReader
from .console import Console as ConsoleType
from ._module_completer import ModuleCompleter, make_default_module_completer
from .utils import gen_colors

Console: type[ConsoleType]
_error: tuple[type[Exception], ...] | type[Exception]
Expand Down Expand Up @@ -253,23 +254,22 @@ 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, string_spans) or in_span(i, comment_spans):
i -= 1
continue
break
return i >= 0 and buffer_str[i] == ":"


class maybe_accept(commands.Command):
Expand Down
58 changes: 58 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,27 @@ def test_auto_indent_with_comment(self):
output = multiline_input(reader)
self.assertEqual(output, output_code)

# fmt: off
events = itertools.chain(
code_to_events("def f():\n"),
[
Event(evt="key", data="backspace", raw=b"\x08"),
],
code_to_events("# foo\npass\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(
Expand Down Expand Up @@ -627,6 +648,43 @@ 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)


class TestPyReplOutput(ScreenEqualMixin, TestCase):
def prepare_reader(self, events):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enhance auto-indent in :mod:`!_pyrepl`.
Loading