Skip to content

Commit

Permalink
fix: make Document.end zero-indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer committed Apr 18, 2024
1 parent 787331e commit 26ef161
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed `Integer` validator missing failure description when not a number https://github.com/Textualize/textual/issues/4413
- Fixed a crash in `DataTable` if you clicked a link in the border https://github.com/Textualize/textual/issues/4410
- Fixed an off-by-one error in the line number of the `Document.end` property https://github.com/Textualize/textual/issues/4426

### Added

Expand Down
2 changes: 1 addition & 1 deletion src/textual/document/_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def start(self) -> Location:
def end(self) -> Location:
"""Returns the location of the end of the document."""
last_line = self._lines[-1]
return (self.line_count, len(last_line))
return (self.line_count - 1, len(last_line))

def get_index_from_location(self, location: Location) -> int:
"""Given a location, returns the index from the document's text.
Expand Down
13 changes: 13 additions & 0 deletions tests/document/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ def test_location_from_index(text):
len(lines) - 1,
len(lines[-1]),
)


@pytest.mark.parametrize(
"text", [TEXT, TEXT_NEWLINE, TEXT_WINDOWS, TEXT_WINDOWS_NEWLINE]
)
def test_document_end(text):
"""The location is always what we expect."""
document = Document(text)
expected_line_number = (
len(text.splitlines()) if text.endswith("\n") else len(text.splitlines()) - 1
)
expected_pos = 0 if text.endswith("\n") else (len(text.splitlines()[-1]))
assert document.end == (expected_line_number, expected_pos)

0 comments on commit 26ef161

Please sign in to comment.