Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a TextArea crash #4126

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed

- Fixed a crash in the TextArea when performing a backward replace https://github.com/Textualize/textual/pull/4126
- Fixed selection not updating correctly when pasting while there's a non-zero selection https://github.com/Textualize/textual/pull/4126

## [0.48.2] - 2024-02-02

### Fixed
Expand Down
24 changes: 15 additions & 9 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,6 @@ def render_line(self, widget_y: int) -> Strip:
# Get the line from the Document.
line_string = document.get_line(line_index)
line = Text(line_string, end="")

line_character_count = len(line)
line.tab_size = self.indent_width
line.set_length(line_character_count + 1) # space at end for cursor
Expand Down Expand Up @@ -1195,8 +1194,8 @@ def edit(self, edit: Edit) -> EditResult:
self.wrapped_document.wrap(self.wrap_width, self.indent_width)
else:
self.wrapped_document.wrap_range(
edit.from_location,
edit.to_location,
edit.top,
edit.bottom,
result.end_location,
)

Expand Down Expand Up @@ -1338,7 +1337,8 @@ async def _on_mouse_up(self, event: events.MouseUp) -> None:

async def _on_paste(self, event: events.Paste) -> None:
"""When a paste occurs, insert the text from the paste event into the document."""
self.replace(event.text, *self.selection)
result = self.replace(event.text, *self.selection)
self.move_cursor(result.end_location)

def cell_width_to_column_index(self, cell_width: int, row_index: int) -> int:
"""Return the column that the cell width corresponds to on the given row.
Expand Down Expand Up @@ -1811,8 +1811,7 @@ def delete(
Returns:
An `EditResult` containing information about the edit.
"""
top, bottom = sorted((start, end))
return self.edit(Edit("", top, bottom, maintain_selection_offset))
return self.edit(Edit("", start, end, maintain_selection_offset))

def replace(
self,
Expand Down Expand Up @@ -1986,14 +1985,13 @@ def do(self, text_area: TextArea) -> EditResult:
# position in the document even if an insert happens before
# their cursor position.

edit_top, edit_bottom = sorted((edit_from, edit_to))
edit_bottom_row, edit_bottom_column = edit_bottom
edit_bottom_row, edit_bottom_column = self.bottom

selection_start, selection_end = text_area.selection
selection_start_row, selection_start_column = selection_start
selection_end_row, selection_end_column = selection_end

replace_result = text_area.document.replace_range(edit_from, edit_to, text)
replace_result = text_area.document.replace_range(self.top, self.bottom, text)

new_edit_to_row, new_edit_to_column = replace_result.end_location

Expand Down Expand Up @@ -2048,6 +2046,14 @@ def after(self, text_area: TextArea) -> None:
text_area.selection = self._updated_selection
text_area.record_cursor_width()

@property
def top(self) -> Location:
darrenburns marked this conversation as resolved.
Show resolved Hide resolved
return min([self.from_location, self.to_location])

@property
def bottom(self) -> Location:
return max([self.from_location, self.to_location])


@runtime_checkable
class Undoable(Protocol):
Expand Down
53 changes: 53 additions & 0 deletions tests/text_area/test_edit_via_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

Note that more extensive testing for editing is done at the Document level.
"""

import pytest

from textual.app import App, ComposeResult
from textual.events import Paste
from textual.widgets import TextArea
from textual.widgets.text_area import Selection

Expand Down Expand Up @@ -416,3 +418,54 @@ async def test_delete_word_right_at_end_of_line():

assert text_area.text == "0123456789"
assert text_area.selection == Selection.cursor((0, 5))


@pytest.mark.parametrize(
"selection",
[
Selection(start=(1, 0), end=(3, 0)),
Selection(start=(3, 0), end=(1, 0)),
],
)
async def test_replace_lines_with_fewer_lines(selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.text = SIMPLE_TEXT
text_area.selection = selection

await pilot.press("a")

expected_text = """\
ABCDE
aPQRST
UVWXY
Z"""
assert text_area.text == expected_text
assert text_area.selection == Selection.cursor((1, 1))


@pytest.mark.parametrize(
"selection",
[
Selection(start=(1, 0), end=(3, 0)),
Selection(start=(3, 0), end=(1, 0)),
],
)
async def test_paste(selection):
app = TextAreaApp()
async with app.run_test() as pilot:
text_area = app.query_one(TextArea)
text_area.text = SIMPLE_TEXT
text_area.selection = selection

app.post_message(Paste("a"))
await pilot.pause()

expected_text = """\
ABCDE
aPQRST
UVWXY
Z"""
assert text_area.text == expected_text
assert text_area.selection == Selection.cursor((1, 1))
Loading