Skip to content

Commit

Permalink
fix: fix crash after shift+delete (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer authored Aug 22, 2023
1 parent 66117f7 commit 1ced15e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Bug Fixes

- Fixes a crash caused by <kbd>shift+delete</kbd> on a buffer with only one line.

## [0.5.0] - 2023-08-22

### Features
Expand Down
18 changes: 11 additions & 7 deletions src/textual_textarea/textarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions tests/functional_tests/test_textarea.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@
None,
Cursor(2, 0),
),
(
["shift+delete"],
["foo "],
None,
Cursor(3, 1),
[" "],
None,
Cursor(0, 0),
),
],
)
@pytest.mark.asyncio
Expand Down

0 comments on commit 1ced15e

Please sign in to comment.