Skip to content

Commit

Permalink
fix: make Document.end zero-indexed (#4427)
Browse files Browse the repository at this point in the history
Co-authored-by: Darren Burns <[email protected]>
  • Loading branch information
tconbeer and darrenburns authored Apr 19, 2024
1 parent f9b549a commit 76c0992
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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

### Fixed

- Fixed an off-by-one error in the line number of the `Document.end` property https://github.com/Textualize/textual/issues/4426

## [0.57.0] - 2024-04-19

### Fixed
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 76c0992

Please sign in to comment.