Skip to content

Commit

Permalink
Add Document.get_location_from_index(index)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Sep 27, 2023
1 parent 92da0cf commit 3fab51d
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/textual/document/_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,27 @@ def newline(self) -> Newline:
"""Return the line separator used in the document."""

@abstractmethod
def get_index(self, at: Location) -> int:
"""Given a location in the document, returns the index
in the document seen as a flat string.
def get_index_from_location(self, location: Location) -> int:
"""Given a location, returns the index from the document's text.
Args:
at: The location in the document.
location: The location in the document.
Returns:
The index in the document seen as a flat string.
The index in the document's text.
"""

@abstractmethod
def get_location_from_index(self, index: int) -> Location:
"""Given an index in the document's text, returns the corresponding location.
Args:
index: The index in the document's text.
Returns:
The corresponding location.
"""

@abstractmethod
def get_line(self, index: int) -> str:
"""Returns the line with the given index from the document.
Expand Down Expand Up @@ -332,21 +342,30 @@ def line_count(self) -> int:
"""Returns the number of lines in the document."""
return len(self._lines)

def get_index(self, at: Location) -> int:
"""Given a location in the document, returns the index
in the document seen as a flat string.
def get_index_from_location(self, location: Location) -> int:
"""Given a location, returns the index from the document's text.
Args:
at: The location in the document.
location: The location in the document.
Returns:
The index in the document seen as a flat string.
The index in the document's text.
"""
row, col = at
idx = row * len(self.newline) + col
row, col = location
index = row * len(self.newline) + col
for i in range(row):
idx += len(self.get_line(i))
return idx
index += len(self.get_line(i))
return index

def get_location_from_index(self, index: int) -> Location:
idx = 0
newline_len = len(self.newline)
for i in range(self.line_count):
idx += len(self.get_line(i)) + newline_len
if index < idx:
return (i, idx - index)
elif index == idx:
return (i + 1, 0)

def get_line(self, index: int) -> str:
"""Returns the line with the given index from the document.
Expand Down

0 comments on commit 3fab51d

Please sign in to comment.