diff --git a/CHANGELOG.md b/CHANGELOG.md index 5992822..e756867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +### Bug Fixes + +- Fixes a crash caused by shift+delete on a buffer with only one line. + ## [0.5.0] - 2023-08-22 ### Features diff --git a/src/textual_textarea/textarea.py b/src/textual_textarea/textarea.py index 1c26072..9b086b0 100644 --- a/src/textual_textarea/textarea.py +++ b/src/textual_textarea/textarea.py @@ -406,14 +406,18 @@ def on_key(self, event: events.Key) -> None: if selection_before is None: # delete whole line cursor_before = self.cursor - if self.cursor.lno == len(self.lines) - 1: - self.cursor = Cursor(lno=self.cursor.lno - 1, pos=0) + if len(self.lines) > 1: + if self.cursor.lno == len(self.lines) - 1: + self.cursor = Cursor(lno=self.cursor.lno - 1, pos=0) + else: + self.cursor = Cursor(lno=self.cursor.lno, pos=0) + self.lines = ( + self.lines[0 : cursor_before.lno] + + self.lines[cursor_before.lno + 1 :] + ) else: - self.cursor = Cursor(lno=self.cursor.lno, pos=0) - self.lines = ( - self.lines[0 : cursor_before.lno] - + self.lines[cursor_before.lno + 1 :] - ) + self.lines = [" "] + self.cursor = Cursor(0, 0) else: # delete selection, same as plain delete self._delete_selection(selection_before, self.cursor) diff --git a/tests/functional_tests/test_textarea.py b/tests/functional_tests/test_textarea.py index ea39b55..ab15371 100644 --- a/tests/functional_tests/test_textarea.py +++ b/tests/functional_tests/test_textarea.py @@ -126,6 +126,15 @@ None, Cursor(2, 0), ), + ( + ["shift+delete"], + ["foo "], + None, + Cursor(3, 1), + [" "], + None, + Cursor(0, 0), + ), ], ) @pytest.mark.asyncio