11
11
from .lazyre import LazyReCompile
12
12
13
13
LinePart = 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._]*[(]?)" )
15
15
16
16
17
17
def current_word (cursor_offset : int , line : str ) -> Optional [LinePart ]:
18
18
"""the object.attribute.attribute just before or under the cursor"""
19
19
start = cursor_offset
20
20
end = cursor_offset
21
21
word = None
22
- for m in current_word_re .finditer (line ):
22
+ for m in _current_word_re .finditer (line ):
23
23
if m .start (1 ) < cursor_offset <= m .end (1 ):
24
24
start = m .start (1 )
25
25
end = m .end (1 )
@@ -29,29 +29,29 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
29
29
return LinePart (start , end , word )
30
30
31
31
32
- current_dict_key_re = LazyReCompile (r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)""" )
32
+ _current_dict_key_re = LazyReCompile (r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)""" )
33
33
34
34
35
35
def current_dict_key (cursor_offset : int , line : str ) -> Optional [LinePart ]:
36
36
"""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 ):
38
38
if m .start (1 ) <= cursor_offset <= m .end (1 ):
39
39
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
40
40
return None
41
41
42
42
43
- current_dict_re = LazyReCompile (r"""([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)""" )
43
+ _current_dict_re = LazyReCompile (r"""([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)""" )
44
44
45
45
46
46
def current_dict (cursor_offset : int , line : str ) -> Optional [LinePart ]:
47
47
"""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 ):
49
49
if m .start (2 ) <= cursor_offset <= m .end (2 ):
50
50
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
51
51
return None
52
52
53
53
54
- current_string_re = LazyReCompile (
54
+ _current_string_re = LazyReCompile (
55
55
'''(?P<open>(?:""")|"|(?:''\' )|')(?:((?P<closed>.+?)(?P=open))|'''
56
56
"""(?P<unclosed>.+))"""
57
57
)
@@ -63,14 +63,14 @@ def current_string(cursor_offset: int, line: str) -> Optional[LinePart]:
63
63
64
64
Weaker than bpython.Repl's current_string, because that checks that a
65
65
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 ):
67
67
i = 3 if m .group (3 ) else 4
68
68
if m .start (i ) <= cursor_offset <= m .end (i ):
69
69
return LinePart (m .start (i ), m .end (i ), m .group (i ))
70
70
return None
71
71
72
72
73
- current_object_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]" )
73
+ _current_object_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]" )
74
74
75
75
76
76
def current_object (cursor_offset : int , line : str ) -> Optional [LinePart ]:
@@ -82,15 +82,15 @@ def current_object(cursor_offset: int, line: str) -> Optional[LinePart]:
82
82
start , end , word = match
83
83
s = "." .join (
84
84
m .group (1 )
85
- for m in current_object_re .finditer (word )
85
+ for m in _current_object_re .finditer (word )
86
86
if m .end (1 ) + start < cursor_offset
87
87
)
88
88
if not s :
89
89
return None
90
90
return LinePart (start , start + len (s ), s )
91
91
92
92
93
- current_object_attribute_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]?" )
93
+ _current_object_attribute_re = LazyReCompile (r"([\w_][\w0-9_]*)[.]?" )
94
94
95
95
96
96
def current_object_attribute (
@@ -102,15 +102,15 @@ def current_object_attribute(
102
102
if match is None :
103
103
return None
104
104
start , end , word = match
105
- matches = current_object_attribute_re .finditer (word )
105
+ matches = _current_object_attribute_re .finditer (word )
106
106
next (matches )
107
107
for m in matches :
108
108
if m .start (1 ) + start <= cursor_offset <= m .end (1 ) + start :
109
109
return LinePart (m .start (1 ) + start , m .end (1 ) + start , m .group (1 ))
110
110
return None
111
111
112
112
113
- current_from_import_from_re = LazyReCompile (
113
+ _current_from_import_from_re = LazyReCompile (
114
114
r"from +([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*"
115
115
)
116
116
@@ -124,17 +124,19 @@ def current_from_import_from(
124
124
parts of an import: from (module) import (name1, name2)
125
125
"""
126
126
# 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 ):
128
128
if (m .start (1 ) < cursor_offset <= m .end (1 )) or (
129
129
m .start (2 ) < cursor_offset <= m .end (2 )
130
130
):
131
131
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
132
132
return None
133
133
134
134
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_]*)" )
138
140
139
141
140
142
def current_from_import_import (
@@ -144,15 +146,15 @@ def current_from_import_import(
144
146
145
147
returns None if cursor not in or just after one of these words
146
148
"""
147
- baseline = current_from_import_import_re_1 .search (line )
149
+ baseline = _current_from_import_import_re_1 .search (line )
148
150
if baseline is None :
149
151
return 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 () :])
151
153
if match1 is None :
152
154
return None
153
155
for m in chain (
154
156
(match1 ,),
155
- current_from_import_import_re_3 .finditer (line [baseline .end () :]),
157
+ _current_from_import_import_re_3 .finditer (line [baseline .end () :]),
156
158
):
157
159
start = baseline .end () + m .start (1 )
158
160
end = baseline .end () + m .end (1 )
@@ -161,21 +163,21 @@ def current_from_import_import(
161
163
return None
162
164
163
165
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_.]*)" )
167
169
168
170
169
171
def current_import (cursor_offset : int , line : str ) -> Optional [LinePart ]:
170
172
# TODO allow for multiple as's
171
- baseline = current_import_re_1 .search (line )
173
+ baseline = _current_import_re_1 .search (line )
172
174
if baseline is None :
173
175
return None
174
- match1 = current_import_re_2 .search (line [baseline .end () :])
176
+ match1 = _current_import_re_2 .search (line [baseline .end () :])
175
177
if match1 is None :
176
178
return None
177
179
for m in chain (
178
- (match1 ,), current_import_re_3 .finditer (line [baseline .end () :])
180
+ (match1 ,), _current_import_re_3 .finditer (line [baseline .end () :])
179
181
):
180
182
start = baseline .end () + m .start (1 )
181
183
end = baseline .end () + m .end (1 )
@@ -184,25 +186,25 @@ def current_import(cursor_offset: int, line: str) -> Optional[LinePart]:
184
186
return None
185
187
186
188
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]*)" )
188
190
189
191
190
192
def current_method_definition_name (
191
193
cursor_offset : int , line : str
192
194
) -> Optional [LinePart ]:
193
195
"""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 ):
195
197
if m .start (1 ) <= cursor_offset <= m .end (1 ):
196
198
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
197
199
return None
198
200
199
201
200
- current_single_word_re = LazyReCompile (r"(?<![.])\b([a-zA-Z_][\w]*)" )
202
+ _current_single_word_re = LazyReCompile (r"(?<![.])\b([a-zA-Z_][\w]*)" )
201
203
202
204
203
205
def current_single_word (cursor_offset : int , line : str ) -> Optional [LinePart ]:
204
206
"""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 ):
206
208
if m .start (1 ) <= cursor_offset <= m .end (1 ):
207
209
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
208
210
return None
@@ -221,7 +223,7 @@ def current_dotted_attribute(
221
223
return None
222
224
223
225
224
- current_expression_attribute_re = LazyReCompile (
226
+ _current_expression_attribute_re = LazyReCompile (
225
227
r"[.]\s*((?:[\w_][\w0-9_]*)|(?:))"
226
228
)
227
229
@@ -231,7 +233,7 @@ def current_expression_attribute(
231
233
) -> Optional [LinePart ]:
232
234
"""If after a dot, the attribute being completed"""
233
235
# 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 ):
235
237
if m .start (1 ) <= cursor_offset <= m .end (1 ):
236
238
return LinePart (m .start (1 ), m .end (1 ), m .group (1 ))
237
239
return None
0 commit comments