diff options
author | Aya Elsayed <ayah.ehab11@gmail.com> | 2024-05-22 05:56:35 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-22 05:56:35 (GMT) |
commit | 5091c4400c9ea2a2d1e4d89a28c9d0de2651fa6d (patch) | |
tree | 8f69df5fef2bea1a94eaf437bcb3b7c9687a714e /Lib/_pyrepl | |
parent | e6572e8f98d33994d2d0dd3afa92a2a72ee642a9 (diff) | |
download | cpython-5091c4400c9ea2a2d1e4d89a28c9d0de2651fa6d.zip cpython-5091c4400c9ea2a2d1e4d89a28c9d0de2651fa6d.tar.gz cpython-5091c4400c9ea2a2d1e4d89a28c9d0de2651fa6d.tar.bz2 |
gh-118911: Trailing whitespace in a block shouldn't prevent the user from terminating the code block (#119355)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Diffstat (limited to 'Lib/_pyrepl')
-rw-r--r-- | Lib/_pyrepl/historical_reader.py | 2 | ||||
-rw-r--r-- | Lib/_pyrepl/readline.py | 17 |
2 files changed, 16 insertions, 3 deletions
diff --git a/Lib/_pyrepl/historical_reader.py b/Lib/_pyrepl/historical_reader.py index eef7d90..121de33 100644 --- a/Lib/_pyrepl/historical_reader.py +++ b/Lib/_pyrepl/historical_reader.py @@ -259,7 +259,7 @@ class HistoricalReader(Reader): self.transient_history[self.historyi] = self.get_unicode() buf = self.transient_history.get(i) if buf is None: - buf = self.history[i] + buf = self.history[i].rstrip() self.buffer = list(buf) self.historyi = i self.pos = len(self.buffer) diff --git a/Lib/_pyrepl/readline.py b/Lib/_pyrepl/readline.py index 796f1ef..ffa14a9 100644 --- a/Lib/_pyrepl/readline.py +++ b/Lib/_pyrepl/readline.py @@ -244,14 +244,27 @@ class maybe_accept(commands.Command): r: ReadlineAlikeReader r = self.reader # type: ignore[assignment] r.dirty = True # this is needed to hide the completion menu, if visible - # + # if there are already several lines and the cursor # is not on the last one, always insert a new \n. text = r.get_unicode() + if "\n" in r.buffer[r.pos :] or ( r.more_lines is not None and r.more_lines(text) ): - # + def _newline_before_pos(): + before_idx = r.pos - 1 + while before_idx > 0 and text[before_idx].isspace(): + before_idx -= 1 + return text[before_idx : r.pos].count("\n") > 0 + + # if there's already a new line before the cursor then + # even if the cursor is followed by whitespace, we assume + # the user is trying to terminate the block + if _newline_before_pos() and text[r.pos:].isspace(): + self.finish = True + return + # auto-indent the next line like the previous line prevlinestart, indent = _get_previous_line_indent(r.buffer, r.pos) r.insert("\n") |