From 09643b0f80a9236f70f0537a350b894c8d1724ad Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 18 Jan 2021 14:57:34 -0800 Subject: [PATCH] fix backspacing an extra blank line at end of file --- babi/file.py | 6 +++++- tests/features/text_editing_test.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/babi/file.py b/babi/file.py index fd99b74..0a3d05c 100644 --- a/babi/file.py +++ b/babi/file.py @@ -476,7 +476,11 @@ class File: if self.buf.y == 0 and self.buf.x == 0: pass # backspace at the end of the file does not change the contents - elif self.buf.y == len(self.buf) - 1: + elif ( + self.buf.y == len(self.buf) - 1 and + # still allow backspace if there are 2+ blank lines + self.buf[self.buf.y - 1] != '' + ): self.buf.left(margin) # at the beginning of the line, we join the current line and # the previous line diff --git a/tests/features/text_editing_test.py b/tests/features/text_editing_test.py index a314108..6a0adc9 100644 --- a/tests/features/text_editing_test.py +++ b/tests/features/text_editing_test.py @@ -50,6 +50,18 @@ def test_backspace_at_end_of_file_still_allows_scrolling_down(run, tmpdir): h.await_text_missing('*') +def test_backspace_deletes_newline_at_end_of_file(run, tmpdir): + f = tmpdir.join('f') + f.write('foo\n\n') + + with run(str(f)) as h, and_exit(h): + h.press('^End') + h.press('BSpace') + h.press('^S') + + assert f.read() == 'foo\n' + + @pytest.mark.parametrize('key', ('BSpace', '^H')) def test_backspace_deletes_text(run, tmpdir, key): f = tmpdir.join('f')