Repository: asottile/babi

Issue:
backspacing blank line at end of file doesn't work
- `echo -en 'foo\n\n' > t`
- `babi t`
- <kbd>ctrl+end</kbd>
- <kbd>backspace</kbd>
- <kbd>ctrl+s</kbd>
- <kbd>ctrl+x</kbd> (close babi)
- `babi t`  (reopen the file)
- the extra blank line is still there

this used to work, hmmm

Discussion:

Patch:
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


Test patch:
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')