Skip to content

Commit 8be8f73

Browse files
curent_word falls when if obj.attr obj not global
1 parent 6b7f681 commit 8be8f73

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

bpython/line.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
from bpython.lazyre import LazyReCompile
1212

13-
current_word_re = LazyReCompile(r'[\w_][\w0-9._]*[(]?')
1413
LinePart = namedtuple('LinePart', ['start', 'stop', 'word'])
1514

15+
current_word_re = LazyReCompile(
16+
r'(?<![)\]\w_.])'
17+
r'([\w_][\w0-9._]*[(]?)')
18+
1619

1720
def current_word(cursor_offset, line):
1821
"""the object.attribute.attribute just before or under the cursor"""
@@ -22,10 +25,10 @@ def current_word(cursor_offset, line):
2225
end = pos
2326
word = None
2427
for m in matches:
25-
if m.start() < pos and m.end() >= pos:
26-
start = m.start()
27-
end = m.end()
28-
word = m.group()
28+
if m.start(1) < pos and m.end(1) >= pos:
29+
start = m.start(1)
30+
end = m.end(1)
31+
word = m.group(1)
2932
if word is None:
3033
return None
3134
return LinePart(start, end, word)

bpython/test/test_line_properties.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
current_string, current_object, current_object_attribute, \
66
current_from_import_from, current_from_import_import, current_import, \
77
current_method_definition_name, current_single_word, \
8-
current_expression_attribute
8+
current_expression_attribute, current_dotted_attribute
99

1010

1111
def cursor(s):
@@ -129,6 +129,15 @@ def test_dots(self):
129129
self.assertAccess('stuff[<asd|fg>]')
130130
self.assertAccess('stuff[asdf[<asd|fg>]')
131131

132+
def test_non_dots(self):
133+
self.assertAccess('].asdf|')
134+
self.assertAccess(').asdf|')
135+
self.assertAccess('foo[0].asdf|')
136+
self.assertAccess('foo().asdf|')
137+
self.assertAccess('foo().|')
138+
self.assertAccess('foo().asdf.|')
139+
self.assertAccess('foo[0].asdf.|')
140+
132141
def test_open_paren(self):
133142
self.assertAccess('<foo(|>')
134143
# documenting current behavior - TODO is this intended?
@@ -336,5 +345,16 @@ def test_strings(self):
336345
self.assertAccess('"hey".<|>')
337346

338347

348+
class TestCurrentDottedAttribute(LineTestCase):
349+
def setUp(self):
350+
self.func = current_dotted_attribute
351+
352+
def test_simple(self):
353+
self.assertAccess('<obj.attr>|')
354+
self.assertAccess('(<obj.attr>|')
355+
self.assertAccess('[<obj.attr>|')
356+
self.assertAccess('m.body[0].value|')
357+
self.assertAccess('m.body[0].attr.value|')
358+
339359
if __name__ == '__main__':
340360
unittest.main()

0 commit comments

Comments
 (0)