diff options
author | Arnon Yaari <wiggin15@yahoo.com> | 2024-05-31 09:02:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-31 09:02:54 (GMT) |
commit | dae0375bd97f3821c5db1602a0653a3c5dc53c5b (patch) | |
tree | b2725a7e2a82ffbd9bfb0a5c164db99a11b4faf8 /Lib/_pyrepl | |
parent | 94e9585e99abc2d060cedc77b3c03e06b4a0a9c4 (diff) | |
download | cpython-dae0375bd97f3821c5db1602a0653a3c5dc53c5b.zip cpython-dae0375bd97f3821c5db1602a0653a3c5dc53c5b.tar.gz cpython-dae0375bd97f3821c5db1602a0653a3c5dc53c5b.tar.bz2 |
gh-111201: Improve pyrepl auto indentation (#119606)
- auto-indent when editing multi-line block
- ignore comments
Diffstat (limited to 'Lib/_pyrepl')
-rw-r--r-- | Lib/_pyrepl/readline.py | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 248f385..7d811bf 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -237,13 +237,24 @@ def _get_first_indentation(buffer: list[str]) -> str | None: return None -def _is_last_char_colon(buffer: list[str]) -> bool: - i = len(buffer) - while i > 0: - i -= 1 - if buffer[i] not in " \t\n": # ignore whitespaces - return buffer[i] == ":" - return False +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 + 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 == ":" class maybe_accept(commands.Command): @@ -280,7 +291,7 @@ class maybe_accept(commands.Command): for i in range(prevlinestart, prevlinestart + indent): r.insert(r.buffer[i]) r.update_last_used_indentation() - if _is_last_char_colon(r.buffer): + if _should_auto_indent(r.buffer, r.pos): if r.last_used_indentation is not None: indentation = r.last_used_indentation else: |