MCPcopy Create free account
hub / github.com/ipython/ipython / position_to_cursor

Function position_to_cursor

IPython/core/completer.py:845–879  ·  view source on GitHub ↗

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 :

(text:str, offset:int)

Source from the content-addressed store, hash-verified

843 return sum(len(l) + 1 for l in lines[:line]) + column
844
845def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
846 """
847 Convert the position of the cursor in text (0 indexed) to a line
848 number(0-indexed) and a column number (0-indexed) pair
849
850 Position should be a valid position in ``text``.
851
852 Parameters
853 ----------
854
855 text : str
856 The text in which to calculate the cursor offset
857 offset : int
858 Position of the cursor in ``text``, 0-indexed.
859
860 Return
861 ------
862 (line, column) : (int, int)
863 Line of the cursor; 0-indexed, column of the cursor 0-indexed
864
865
866 See Also
867 --------
868 cursor_to_position : reciprocal of this function
869
870
871 """
872
873 assert 0 <= offset <= len(text) , "0 <= %s <= %s" % (offset , len(text))
874
875 before = text[:offset]
876 blines = before.split('\n') # ! splitnes trim trailing \n
877 line = before.count('\n')
878 col = len(blines[-1])
879 return line, col
880
881
882def _safe_isinstance(obj, module, class_name):

Callers 1

_completionsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected