@@ -34,8 +34,32 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
34
34
return LinePart (start , end , word )
35
35
36
36
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._]*\["""
37
55
_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
+ ')'
39
63
)
40
64
41
65
@@ -47,15 +71,23 @@ def current_dict_key(cursor_offset: int, line: str) -> Optional[LinePart]:
47
71
return None
48
72
49
73
74
+ _current_dict_re_base = r"""([\w_][\w0-9._]*)\["""
50
75
_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
+ ')'
52
83
)
53
84
54
85
55
86
def current_dict (cursor_offset : int , line : str ) -> Optional [LinePart ]:
56
87
"""If in dictionary completion, return the dict that should be used"""
57
88
for m in _current_dict_re .finditer (line ):
58
89
if m .start (2 ) <= cursor_offset <= m .end (2 ):
90
+ print (LinePart (m .start (1 ), m .end (1 ), m .group (1 ))) # TODO
59
91
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
60
92
return None
61
93
0 commit comments