Skip to content

Commit 97d6b19

Browse files
Updated regex to handle str, bytes, int, float, tuple dict keys
1 parent 3f20d8e commit 97d6b19

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

bpython/line.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,32 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
3434
return LinePart(start, end, word)
3535

3636

37+
# pieces of regex to match repr() of several hashable built-in types
38+
_match_identifiers_and_functions = r"""[a-zA-Z_][a-zA-Z0-9_.(),\[\] ]*"""
39+
_match_single_quote_str_bytes = \
40+
r"""b?'(?:\\(?:\\\\)*['"nabfrtvxuU]|[^'\\])*(?:\\\\)*\\?'?"""
41+
_match_double_quote_str_bytes = r"""b?"[^"]*"?"""
42+
_match_int_and_float = r"""\-?\d+(?:\.\d*)?|\-"""
43+
_match_str_bytes_int_float = \
44+
f'(?:{_match_single_quote_str_bytes}|{_match_double_quote_str_bytes}|' \
45+
f'{_match_int_and_float})'
46+
_match_tuple = r"""\((?:""" \
47+
f'{_match_str_bytes_int_float}' \
48+
r""", )*(?:""" \
49+
f'{_match_str_bytes_int_float}' \
50+
r"""\,|""" \
51+
f'{_match_str_bytes_int_float}' \
52+
r""")?\)?"""
53+
54+
_current_dict_key_re_base = r"""[\w_][\w0-9._]*\["""
3755
_current_dict_key_re = LazyReCompile(
38-
r"""[\w_][\w0-9._]*\[('[^']*|"[^"]*|[\w0-9._(), '"]*)"""
56+
f'{_current_dict_key_re_base}('
57+
f'{_match_identifiers_and_functions}|'
58+
f'{_match_single_quote_str_bytes}|'
59+
f'{_match_double_quote_str_bytes}|'
60+
f'{_match_int_and_float}|'
61+
f'{_match_tuple}|'
62+
')'
3963
)
4064

4165

@@ -47,15 +71,23 @@ def current_dict_key(cursor_offset: int, line: str) -> Optional[LinePart]:
4771
return None
4872

4973

74+
_current_dict_re_base = r"""([\w_][\w0-9._]*)\["""
5075
_current_dict_re = LazyReCompile(
51-
r"""([\w_][\w0-9._]*)\[('[^']*|"[^"]*|[\w0-9._(), '"]*)"""
76+
f'{_current_dict_re_base}('
77+
f'{_match_identifiers_and_functions}|'
78+
f'{_match_single_quote_str_bytes}|'
79+
f'{_match_double_quote_str_bytes}|'
80+
f'{_match_int_and_float}|'
81+
f'{_match_tuple}|'
82+
')'
5283
)
5384

5485

5586
def current_dict(cursor_offset: int, line: str) -> Optional[LinePart]:
5687
"""If in dictionary completion, return the dict that should be used"""
5788
for m in _current_dict_re.finditer(line):
5889
if m.start(2) <= cursor_offset <= m.end(2):
90+
print(LinePart(m.start(1), m.end(1), m.group(1))) # TODO
5991
return LinePart(m.start(1), m.end(1), m.group(1))
6092
return None
6193

0 commit comments

Comments
 (0)