diff --git a/src/textual/document/_document.py b/src/textual/document/_document.py index 5e8e37d8d0..69a7b35d60 100644 --- a/src/textual/document/_document.py +++ b/src/textual/document/_document.py @@ -90,6 +90,28 @@ def text(self) -> str: def newline(self) -> Newline: """Return the line separator used in the document.""" + @abstractmethod + def get_index_from_location(self, location: Location) -> int: + """Given a location, returns the index from the document's text. + + Args: + location: The location in the document. + + Returns: + 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. @@ -320,6 +342,40 @@ def line_count(self) -> int: """Returns the number of lines in the document.""" return len(self._lines) + def get_index_from_location(self, location: Location) -> int: + """Given a location, returns the index from the document's text. + + Args: + location: The location in the document. + + Returns: + The index in the document's text. + """ + row, col = location + index = row * len(self.newline) + col + for i in range(row): + index += len(self.get_line(i)) + return index + + 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. + """ + idx = 0 + newline_len = len(self.newline) + for i in range(self.line_count): + next_idx = idx + len(self.get_line(i)) + newline_len + if index < next_idx: + return (i, index - idx) + elif index == next_idx: + return (i + 1, 0) + idx = next_idx + def get_line(self, index: int) -> str: """Returns the line with the given index from the document.