Convert the position of the cursor in text (0 indexed) to a line number(0-indexed) and a column number (0-indexed) pair Position should be a valid position in ``text``. Parameters ---------- text : str The text in which to calculate the cursor offset offset : i
(text: str, offset: int)
| 1675 | |
| 1676 | |
| 1677 | def position_to_cursor(text: str, offset: int) -> tuple[int, int]: |
| 1678 | """ |
| 1679 | Convert the position of the cursor in text (0 indexed) to a line |
| 1680 | number(0-indexed) and a column number (0-indexed) pair |
| 1681 | |
| 1682 | Position should be a valid position in ``text``. |
| 1683 | |
| 1684 | Parameters |
| 1685 | ---------- |
| 1686 | text : str |
| 1687 | The text in which to calculate the cursor offset |
| 1688 | offset : int |
| 1689 | Position of the cursor in ``text``, 0-indexed. |
| 1690 | |
| 1691 | Returns |
| 1692 | ------- |
| 1693 | (line, column) : (int, int) |
| 1694 | Line of the cursor; 0-indexed, column of the cursor 0-indexed |
| 1695 | |
| 1696 | See Also |
| 1697 | -------- |
| 1698 | cursor_to_position : reciprocal of this function |
| 1699 | |
| 1700 | """ |
| 1701 | |
| 1702 | assert 0 <= offset <= len(text) , "0 <= %s <= %s" % (offset , len(text)) |
| 1703 | |
| 1704 | before = text[:offset] |
| 1705 | blines = before.split('\n') # ! splitnes trim trailing \n |
| 1706 | line = before.count('\n') |
| 1707 | col = len(blines[-1]) |
| 1708 | return line, col |
| 1709 | |
| 1710 | |
| 1711 | def _safe_isinstance(obj, module, class_name, *attrs): |
no outgoing calls
no test coverage detected
searching dependent graphs…