Skip to content

Commit 89b6e3f

Browse files
committed
feat(TextDocument): add utility function for getting a position from an offset
1 parent 0f29949 commit 89b6e3f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pygls/workspace/text_document.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ def offset_at_position(self, client_position: types.Position) -> int:
192192
)
193193
return self.offset_at_server_position(server_position)
194194

195+
def position_at_offset(self, offset: int) -> ServerPosition:
196+
remaining_offset = offset
197+
for lineno, line in enumerate(self.lines):
198+
if remaining_offset < len(line):
199+
return ServerPosition(lineno, remaining_offset)
200+
remaining_offset -= len(line)
201+
return ServerPosition(lineno, len(line))
202+
195203
def range_from_client_units(self, range: types.Range) -> ServerRange:
196204
"""
197205
Convert a range from client units into code points, suitable for indexing into `self.lines`.

0 commit comments

Comments
 (0)