diff --git a/src/textual/document/_document.py b/src/textual/document/_document.py index 9fdb3420b95..8856a92ec6d 100644 --- a/src/textual/document/_document.py +++ b/src/textual/document/_document.py @@ -91,15 +91,25 @@ 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 @@ -332,21 +342,38 @@ 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: + """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): + 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.