1111from .lazyre import LazyReCompile
1212
1313LinePart = namedtuple ("LinePart" , ["start" ,"stop" ,"word" ])
14- current_word_re = LazyReCompile (r"(?<![)\]\w_.])" r"([\w_][\w0-9._]*[(]?)" )
14+ _current_word_re = LazyReCompile (r"(?<![)\]\w_.])" r"([\w_][\w0-9._]*[(]?)" )
1515
1616
1717def current_word (cursor_offset :int ,line :str )-> Optional [LinePart ]:
1818"""the object.attribute.attribute just before or under the cursor"""
1919start = cursor_offset
2020end = cursor_offset
2121word = None
22- for m in current_word_re .finditer (line ):
22+ for m in _current_word_re .finditer (line ):
2323if m .start (1 )< cursor_offset <= m .end (1 ):
2424start = m .start (1 )
2525end = m .end (1 )
@@ -29,29 +29,29 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
2929return LinePart (start ,end ,word )
3030
3131
32- current_dict_key_re = LazyReCompile (r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)""" )
32+ _current_dict_key_re = LazyReCompile (r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)""" )
3333
3434
3535def current_dict_key (cursor_offset :int ,line :str )-> Optional [LinePart ]:
3636"""If in dictionary completion, return the current key"""
37- for m in current_dict_key_re .finditer (line ):
37+ for m in _current_dict_key_re .finditer (line ):
3838if m .start (1 )<= cursor_offset <= m .end (1 ):
3939return LinePart (m .start (1 ),m .end (1 ),m .group (1 ))
4040return None
4141
4242
43- current_dict_re = LazyReCompile (r"""([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)""" )
43+ _current_dict_re = LazyReCompile (r"""([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)""" )
4444
4545
4646def current_dict (cursor_offset :int ,line :str )-> Optional [LinePart ]:
4747"""If in dictionary completion, return the dict that should be used"""
48- for m in current_dict_re .finditer (line ):
48+ for m in _current_dict_re .finditer (line ):
4949if m .start (2 )<= cursor_offset <= m .end (2 ):
5050return LinePart (m .start (1 ),m .end (1 ),m .group (1 ))
5151return None
5252
5353
54- current_string_re = LazyReCompile (
54+ _current_string_re = LazyReCompile (
5555'''(?P<open>(?:""")|"|(?:''\' )|')(?:((?P<closed>.+?)(?P=open))|'''
5656"""(?P<unclosed>.+))"""
5757)
@@ -63,14 +63,14 @@ def current_string(cursor_offset: int, line: str) -> Optional[LinePart]:
6363
6464 Weaker than bpython.Repl's current_string, because that checks that a
6565 string is a string based on previous lines in the buffer."""
66- for m in current_string_re .finditer (line ):
66+ for m in _current_string_re .finditer (line ):
6767i = 3 if m .group (3 )else 4
6868if m .start (i )<= cursor_offset <= m .end (i ):
6969return LinePart (m .start (i ),m .end (i ),m .group (i ))
7070return None
7171
7272
73- current_object_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]" )
73+ _current_object_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]" )
7474
7575
7676def current_object (cursor_offset :int ,line :str )-> Optional [LinePart ]:
@@ -82,15 +82,15 @@ def current_object(cursor_offset: int, line: str) -> Optional[LinePart]:
8282start ,end ,word = match
8383s = "." .join (
8484m .group (1 )
85- for m in current_object_re .finditer (word )
85+ for m in _current_object_re .finditer (word )
8686if m .end (1 )+ start < cursor_offset
8787 )
8888if not s :
8989return None
9090return LinePart (start ,start + len (s ),s )
9191
9292
93- current_object_attribute_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]?" )
93+ _current_object_attribute_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]?" )
9494
9595
9696def current_object_attribute (
@@ -102,15 +102,15 @@ def current_object_attribute(
102102if match is None :
103103return None
104104start ,end ,word = match
105- matches = current_object_attribute_re .finditer (word )
105+ matches = _current_object_attribute_re .finditer (word )
106106next (matches )
107107for m in matches :
108108if m .start (1 )+ start <= cursor_offset <= m .end (1 )+ start :
109109return LinePart (m .start (1 )+ start ,m .end (1 )+ start ,m .group (1 ))
110110return None
111111
112112
113- current_from_import_from_re = LazyReCompile (
113+ _current_from_import_from_re = LazyReCompile (
114114r"from +([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*"
115115)
116116
@@ -124,17 +124,19 @@ def current_from_import_from(
124124 parts of an import: from (module) import (name1, name2)
125125 """
126126# TODO allow for as's
127- for m in current_from_import_from_re .finditer (line ):
127+ for m in _current_from_import_from_re .finditer (line ):
128128if (m .start (1 )< cursor_offset <= m .end (1 ))or (
129129m .start (2 )< cursor_offset <= m .end (2 )
130130 ):
131131return LinePart (m .start (1 ),m .end (1 ),m .group (1 ))
132132return None
133133
134134
135- current_from_import_import_re_1 = LazyReCompile (r"from\s+([\w0-9_.]*)\s+import" )
136- current_from_import_import_re_2 = LazyReCompile (r"([\w0-9_]+)" )
137- current_from_import_import_re_3 = LazyReCompile (r", *([\w0-9_]*)" )
135+ _current_from_import_import_re_1 = LazyReCompile (
136+ r"from\s+([\w0-9_.]*)\s+import"
137+ )
138+ _current_from_import_import_re_2 = LazyReCompile (r"([\w0-9_]+)" )
139+ _current_from_import_import_re_3 = LazyReCompile (r", *([\w0-9_]*)" )
138140
139141
140142def current_from_import_import (
@@ -144,15 +146,15 @@ def current_from_import_import(
144146
145147 returns None if cursor not in or just after one of these words
146148 """
147- baseline = current_from_import_import_re_1 .search (line )
149+ baseline = _current_from_import_import_re_1 .search (line )
148150if baseline is None :
149151return None
150- match1 = current_from_import_import_re_2 .search (line [baseline .end () :])
152+ match1 = _current_from_import_import_re_2 .search (line [baseline .end () :])
151153if match1 is None :
152154return None
153155for m in chain (
154156 (match1 ,),
155- current_from_import_import_re_3 .finditer (line [baseline .end () :]),
157+ _current_from_import_import_re_3 .finditer (line [baseline .end () :]),
156158 ):
157159start = baseline .end ()+ m .start (1 )
158160end = baseline .end ()+ m .end (1 )
@@ -161,21 +163,21 @@ def current_from_import_import(
161163return None
162164
163165
164- current_import_re_1 = LazyReCompile (r"import" )
165- current_import_re_2 = LazyReCompile (r"([\w0-9_.]+)" )
166- current_import_re_3 = LazyReCompile (r"[,][ ]*([\w0-9_.]*)" )
166+ _current_import_re_1 = LazyReCompile (r"import" )
167+ _current_import_re_2 = LazyReCompile (r"([\w0-9_.]+)" )
168+ _current_import_re_3 = LazyReCompile (r"[,][ ]*([\w0-9_.]*)" )
167169
168170
169171def current_import (cursor_offset :int ,line :str )-> Optional [LinePart ]:
170172# TODO allow for multiple as's
171- baseline = current_import_re_1 .search (line )
173+ baseline = _current_import_re_1 .search (line )
172174if baseline is None :
173175return None
174- match1 = current_import_re_2 .search (line [baseline .end () :])
176+ match1 = _current_import_re_2 .search (line [baseline .end () :])
175177if match1 is None :
176178return None
177179for m in chain (
178- (match1 ,),current_import_re_3 .finditer (line [baseline .end () :])
180+ (match1 ,),_current_import_re_3 .finditer (line [baseline .end () :])
179181 ):
180182start = baseline .end ()+ m .start (1 )
181183end = baseline .end ()+ m .end (1 )
@@ -184,25 +186,25 @@ def current_import(cursor_offset: int, line: str) -> Optional[LinePart]:
184186return None
185187
186188
187- current_method_definition_name_re = LazyReCompile (r"def\s+([a-zA-Z_][\w]*)" )
189+ _current_method_definition_name_re = LazyReCompile (r"def\s+([a-zA-Z_][\w]*)" )
188190
189191
190192def current_method_definition_name (
191193cursor_offset :int ,line :str
192194)-> Optional [LinePart ]:
193195"""The name of a method being defined"""
194- for m in current_method_definition_name_re .finditer (line ):
196+ for m in _current_method_definition_name_re .finditer (line ):
195197if m .start (1 )<= cursor_offset <= m .end (1 ):
196198return LinePart (m .start (1 ),m .end (1 ),m .group (1 ))
197199return None
198200
199201
200- current_single_word_re = LazyReCompile (r"(?<![.])\b([a-zA-Z_][\w]*)" )
202+ _current_single_word_re = LazyReCompile (r"(?<![.])\b([a-zA-Z_][\w]*)" )
201203
202204
203205def current_single_word (cursor_offset :int ,line :str )-> Optional [LinePart ]:
204206"""the un-dotted word just before or under the cursor"""
205- for m in current_single_word_re .finditer (line ):
207+ for m in _current_single_word_re .finditer (line ):
206208if m .start (1 )<= cursor_offset <= m .end (1 ):
207209return LinePart (m .start (1 ),m .end (1 ),m .group (1 ))
208210return None
@@ -221,7 +223,7 @@ def current_dotted_attribute(
221223return None
222224
223225
224- current_expression_attribute_re = LazyReCompile (
226+ _current_expression_attribute_re = LazyReCompile (
225227r"[.]\s*((?:[\w_][\w0-9_]*)|(?:))"
226228)
227229
@@ -231,7 +233,7 @@ def current_expression_attribute(
231233)-> Optional [LinePart ]:
232234"""If after a dot, the attribute being completed"""
233235# TODO replace with more general current_expression_attribute
234- for m in current_expression_attribute_re .finditer (line ):
236+ for m in _current_expression_attribute_re .finditer (line ):
235237if m .start (1 )<= cursor_offset <= m .end (1 ):
236238return LinePart (m .start (1 ),m .end (1 ),m .group (1 ))
237239return None