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