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

Function line_at_cursor

IPython/utils/tokenutil.py:24–57  ·  view source on GitHub ↗

Return the line in a cell at a given cursor position Used for calling line-based APIs that don't support multi-line input, yet. Parameters ---------- cell: str multiline block of text cursor_pos: integer the cursor position Returns ----

(cell, cursor_pos=0)

Source from the content-addressed store, hash-verified

22 return
23
24def line_at_cursor(cell, cursor_pos=0):
25 """Return the line in a cell at a given cursor position
26
27 Used for calling line-based APIs that don't support multi-line input, yet.
28
29 Parameters
30 ----------
31
32 cell: str
33 multiline block of text
34 cursor_pos: integer
35 the cursor position
36
37 Returns
38 -------
39
40 (line, offset): (string, integer)
41 The line with the current cursor, and the character offset of the start of the line.
42 """
43 offset = 0
44 lines = cell.splitlines(True)
45 for line in lines:
46 next_offset = offset + len(line)
47 if not line.endswith('\n'):
48 # If the last line doesn't have a trailing newline, treat it as if
49 # it does so that the cursor at the end of the line still counts
50 # as being on that line.
51 next_offset += 1
52 if next_offset > cursor_pos:
53 break
54 offset = next_offset
55 else:
56 line = ""
57 return (line, offset)
58
59def token_at_cursor(cell, cursor_pos=0):
60 """Get the token at a given cursor

Callers 1

test_line_at_cursorFunction · 0.90

Calls

no outgoing calls

Tested by 1

test_line_at_cursorFunction · 0.72