From 3fab51da0d268ea84f4221153d7944516f337127 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Wed, 27 Sep 2023 12:21:22 +0200 Subject: [PATCH] Add Document.get_location_from_index(index) --- src/textual/document/_document.py | 47 ++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/textual/document/_document.py b/src/textual/document/_document.py index 9fdb3420b95..6871eaa1003 100644 --- a/src/textual/document/_document.py +++ b/src/textual/document/_document.py @@ -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. @@ -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.