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 infinite loop in cropping #5154

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ 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
## [0.84.0] - 2024-10-22

### Fixed

- Fixed `RadioSet` not being scrollable https://github.com/Textualize/textual/issues/5100
- Fixed infinite loop in TextArea https://github.com/Textualize/textual/pull/5154

### Added

Expand Down Expand Up @@ -2453,6 +2454,7 @@ https://textual.textualize.io/blog/2022/11/08/version-040/#version-040
- New handler system for messages that doesn't require inheritance
- Improved traceback handling

[0.84.0]: https://github.com/Textualize/textual/compare/v0.83.0...v0.84.0
[0.83.0]: https://github.com/Textualize/textual/compare/v0.82.0...v0.83.0
[0.82.0]: https://github.com/Textualize/textual/compare/v0.81.0...v0.82.0
[0.81.0]: https://github.com/Textualize/textual/compare/v0.80.1...v0.81.0
Expand Down
1,401 changes: 785 additions & 616 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "textual"
version = "0.83.0"
version = "0.84.0"
homepage = "https://github.com/Textualize/textual"
repository = "https://github.com/Textualize/textual"
documentation = "https://textual.textualize.io/"
Expand Down
3 changes: 3 additions & 0 deletions src/textual/strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,13 @@ def crop(self, start: int, end: int | None = None) -> Strip:
Returns:
A new Strip.
"""

start = max(0, start)
end = self.cell_length if end is None else min(self.cell_length, end)
if start == 0 and end == self.cell_length:
return self
if end <= start:
return Strip([], 0)
cache_key = (start, end)
cached = self._crop_cache.get(cache_key)
if cached is not None:
Expand Down
4 changes: 1 addition & 3 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,9 +1236,7 @@ def render_line(self, y: int) -> Strip:

# Crop the line to show only the visible part (some may be scrolled out of view)
if not self.soft_wrap:
text_strip = text_strip.crop(
scroll_x, scroll_x + virtual_width - gutter_width
)
text_strip = text_strip.crop(scroll_x, scroll_x + virtual_width)

# Stylize the line the cursor is currently on.
if cursor_row == line_index:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/snapshot_tests/snapshot_apps/split_segments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from textual.app import App, ComposeResult
from textual.widgets import TextArea

FAIL_TEXT = "x"


class CodeApp(App):
def compose(self) -> ComposeResult:
yield TextArea(FAIL_TEXT, soft_wrap=False, show_line_numbers=True)


if __name__ == "__main__":
app = CodeApp()
app.run()
9 changes: 9 additions & 0 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2417,3 +2417,12 @@ def on_mount(self) -> None:
self.mount(Label("HELLO"))

assert snap_compare(PSApp())


def test_split_segments_infinite_loop(snap_compare):
"""Regression test for https://github.com/Textualize/textual/issues/5151

Should be a bare-bones text editor containing "x"

"""
assert snap_compare(SNAPSHOT_APPS_DIR / "split_segments.py")
2 changes: 1 addition & 1 deletion tests/test_strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_crop():

assert Strip([Segment("foo")]).crop(1, 3) == Strip([Segment("oo")])
assert Strip([Segment("foo")]).crop(1, 2) == Strip([Segment("o")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([Segment("")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([])

assert Strip([Segment("foo💩"), Segment("b💩ar"), Segment("ba💩z")]).crop(
1, 6
Expand Down
Loading