##// END OF EJS Templates
prioritize function token for inspection...
Min RK -
Show More
@@ -48,13 +48,28 b' def test_multiline():'
48 48 start = cell.index(expected) + 1
49 49 for i in range(start, start + len(expected)):
50 50 expect_token(expected, cell, i)
51 expected = 'there'
51 expected = 'hello'
52 52 start = cell.index(expected) + 1
53 53 for i in range(start, start + len(expected)):
54 54 expect_token(expected, cell, i)
55 55
56 def test_nested_call():
57 cell = "foo(bar(a=5), b=10)"
58 expected = 'foo'
59 start = cell.index('bar') + 1
60 for i in range(start, start + 3):
61 expect_token(expected, cell, i)
62 expected = 'bar'
63 start = cell.index('a=')
64 for i in range(start, start + 3):
65 expect_token(expected, cell, i)
66 expected = 'foo'
67 start = cell.index(')') + 1
68 for i in range(start, len(cell)-1):
69 expect_token(expected, cell, i)
70
56 71 def test_attrs():
57 cell = "foo(a=obj.attr.subattr)"
72 cell = "a = obj.attr.subattr"
58 73 expected = 'obj'
59 74 idx = cell.find('obj') + 1
60 75 for i in range(idx, idx + 3):
@@ -58,6 +58,9 b' def token_at_cursor(cell, cursor_pos=0):'
58 58
59 59 Used for introspection.
60 60
61 Function calls are prioritized, so the token for the callable will be returned
62 if the cursor is anywhere inside the call.
63
61 64 Parameters
62 65 ----------
63 66
@@ -70,6 +73,7 b' def token_at_cursor(cell, cursor_pos=0):'
70 73 names = []
71 74 tokens = []
72 75 offset = 0
76 call_names = []
73 77 for tup in generate_tokens(StringIO(cell).readline):
74 78
75 79 tok = Token(*tup)
@@ -93,6 +97,11 b' def token_at_cursor(cell, cursor_pos=0):'
93 97 if tok.text == '=' and names:
94 98 # don't inspect the lhs of an assignment
95 99 names.pop(-1)
100 if tok.text == '(' and names:
101 # if we are inside a function call, inspect the function
102 call_names.append(names[-1])
103 elif tok.text == ')' and call_names:
104 call_names.pop(-1)
96 105
97 106 if offset + end_col > cursor_pos:
98 107 # we found the cursor, stop reading
@@ -102,7 +111,9 b' def token_at_cursor(cell, cursor_pos=0):'
102 111 if tok.token == tokenize2.NEWLINE:
103 112 offset += len(tok.line)
104 113
105 if names:
114 if call_names:
115 return call_names[-1]
116 elif names:
106 117 return names[-1]
107 118 else:
108 119 return ''
General Comments 0
You need to be logged in to leave comments. Login now