MCPcopy Index your code
hub / github.com/ipython/ipython / token_at_cursor

Function token_at_cursor

IPython/utils/tokenutil.py:98–200  ·  view source on GitHub ↗

Get the token at a given cursor Used for introspection. Function calls are prioritized, so the token for the callable will be returned if the cursor is anywhere inside the call. Parameters ---------- cell : str A block of Python code cursor_pos : int Th

(cell: str, cursor_pos: int = 0)

Source from the content-addressed store, hash-verified

96
97
98def token_at_cursor(cell: str, cursor_pos: int = 0) -> str:
99 """Get the token at a given cursor
100
101 Used for introspection.
102
103 Function calls are prioritized, so the token for the callable will be returned
104 if the cursor is anywhere inside the call.
105
106 Parameters
107 ----------
108 cell : str
109 A block of Python code
110 cursor_pos : int
111 The location of the cursor in the block where the token should be found
112 """
113 names: list[str] = []
114 call_names: list[str] = []
115 closing_call_name: str | None = None
116 most_recent_outer_name: str | None = None
117
118 offsets = {1: 0} # lines start at 1
119 intersects_with_cursor = False
120 cur_token_is_name = False
121 tokens: list[Token | None] = [
122 Token(*tup) for tup in generate_tokens(StringIO(cell).readline)
123 ]
124 if not tokens:
125 return ""
126 for prev_tok, (tok, next_tok) in zip(
127 [None] + tokens, itertools.pairwise(tokens + [None])
128 ):
129 # token, text, start, end, line = tup
130 start_line, start_col = tok.start
131 end_line, end_col = tok.end
132 if end_line + 1 not in offsets:
133 # keep track of offsets for each line
134 lines = tok.line.splitlines(True)
135 for lineno, line in enumerate(lines, start_line + 1):
136 if lineno not in offsets:
137 offsets[lineno] = offsets[lineno - 1] + len(line)
138
139 closing_call_name = None
140
141 offset = offsets[start_line]
142 if offset + start_col > cursor_pos:
143 # current token starts after the cursor,
144 # don't consume it
145 break
146
147 if cur_token_is_name := tok.token == tokenize.NAME and not iskeyword(tok.text):
148 if (
149 names
150 and prev_tok
151 and prev_tok.token == tokenize.OP
152 and prev_tok.text == "."
153 ):
154 names[-1] = "%s.%s" % (names[-1], tok.text)
155 else:

Callers 1

expect_tokenFunction · 0.90

Calls 3

TokenClass · 0.85
generate_tokensFunction · 0.85
popMethod · 0.80

Tested by 1

expect_tokenFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…