selection fix

This commit is contained in:
Will McGugan
2025-10-01 16:20:02 +01:00
parent bf0adcd58c
commit 8352382faf
2 changed files with 24 additions and 2 deletions

View File

@@ -46,8 +46,8 @@ class Selection(NamedTuple):
start_line, start_offset = self.start.transpose
if self.end is None:
end_line = len(lines) - 1
end_offset = len(lines[end_line])
end_line = len(lines)
end_offset = len(lines[-1])
else:
end_line, end_offset = self.end.transpose
end_line = min(len(lines), end_line)

22
tests/test_selection.py Normal file
View File

@@ -0,0 +1,22 @@
import pytest
from textual.geometry import Offset
from textual.selection import Selection
@pytest.mark.parametrize(
"text,selection,expected",
[
("Hello", Selection(None, None), "Hello"),
("Hello\nWorld", Selection(None, None), "Hello\nWorld"),
("Hello\nWorld", Selection(Offset(0, 1), None), "World"),
("Hello\nWorld", Selection(None, Offset(5, 0)), "Hello"),
("Foo", Selection(Offset(0, 0), Offset(1, 0)), "F"),
("Foo", Selection(Offset(1, 0), Offset(2, 0)), "o"),
("Foo", Selection(Offset(0, 0), Offset(2, 0)), "Fo"),
("Foo", Selection(Offset(0, 0), None), "Foo"),
],
)
def test_extract(text: str, selection: Selection, expected: str) -> None:
"""Test Selection.extract"""
assert selection.extract(text) == expected