Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathline.py
More file actions
306 lines (240 loc) · 9.84 KB
/
line.py
File metadata and controls
306 lines (240 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""Extracting and changing portions of the current line
All functions take cursor offset from the beginning of the line and the line of
Python code, and return None, or a tuple of the start index, end index, and the
word."""
importre
fromdataclassesimportdataclass
fromitertoolsimportchain
from .lazyreimportLazyReCompile
@dataclass
classLinePart:
start:int
stop:int
word:str
_current_word_re=LazyReCompile(r"(?<![)\]\w_.])"r"([\w_][\w0-9._]*[(]?)")
CHARACTER_PAIR_MAP= {"(":")","{":"}","[":"]","'":"'",'"':'"'}
defcurrent_word(cursor_offset:int,line:str)->LinePart|None:
"""the object.attribute.attribute just before or under the cursor"""
start=cursor_offset
end=cursor_offset
word=None
formin_current_word_re.finditer(line):
ifm.start(1)<cursor_offset<=m.end(1):
start=m.start(1)
end=m.end(1)
word=m.group(1)
ifwordisNone:
returnNone
returnLinePart(start,end,word)
# pieces of regex to match repr() of several hashable built-in types
_match_all_dict_keys=r"""[^\]]*"""
# https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
_match_single_quote_str_bytes=r"""
# bytes repr() begins with `b` character; bytes and str begin with `'`
b?'
# match escape sequence; this handles `\'` in the string repr()
(?:\\['"nabfrtvxuU\\]|
# or match any non-`\` and non-single-quote character (most of the string)
[^'\\])*
# matches hanging `\` or ending `'` if one is present
[\\']?
"""
# bytes and str repr() only uses double quotes if the string contains 1 or more
# `'` character and exactly 0 `"` characters
_match_double_quote_str_bytes=r"""
# bytes repr() begins with `b` character
b?"
# string continues until a `"` character is reached
[^"]*
# end matching at closing double-quote if one is present
"?"""
# match valid identifier name followed by `[` character
_match_dict_before_key=r"""[\w_][\w0-9._]*\["""
_current_dict_key_re=LazyReCompile(
f"{_match_dict_before_key}((?:"
f"{_match_single_quote_str_bytes}|"
f"{_match_double_quote_str_bytes}|"
f"{_match_all_dict_keys}|)*)",
re.VERBOSE,
)
defcurrent_dict_key(cursor_offset:int,line:str)->LinePart|None:
"""If in dictionary completion, return the current key"""
formin_current_dict_key_re.finditer(line):
ifm.start(1)<=cursor_offset<=m.end(1):
returnLinePart(m.start(1),m.end(1),m.group(1))
returnNone
# capture valid identifier name if followed by `[` character
_capture_dict_name=r"""([\w_][\w0-9._]*)\["""
_current_dict_re=LazyReCompile(
f"{_capture_dict_name}((?:"
f"{_match_single_quote_str_bytes}|"
f"{_match_double_quote_str_bytes}|"
f"{_match_all_dict_keys}|)*)",
re.VERBOSE,
)
defcurrent_dict(cursor_offset:int,line:str)->LinePart|None:
"""If in dictionary completion, return the dict that should be used"""
formin_current_dict_re.finditer(line):
ifm.start(2)<=cursor_offset<=m.end(2):
returnLinePart(m.start(1),m.end(1),m.group(1))
returnNone
_current_string_re=LazyReCompile(
'''(?P<open>(?:""")|"|(?:''\')|')(?:((?P<closed>.+?)(?P=open))|'''
"""(?P<unclosed>.+))"""
)
defcurrent_string(cursor_offset:int,line:str)->LinePart|None:
"""If inside a string of nonzero length, return the string (excluding
quotes)
Weaker than bpython.Repl's current_string, because that checks that a
string is a string based on previous lines in the buffer."""
formin_current_string_re.finditer(line):
i=3ifm.group(3)else4
ifm.start(i)<=cursor_offset<=m.end(i):
returnLinePart(m.start(i),m.end(i),m.group(i))
returnNone
_current_object_re=LazyReCompile(r"([\w_][\w0-9_]*)[.]")
defcurrent_object(cursor_offset:int,line:str)->LinePart|None:
"""If in attribute completion, the object on which attribute should be
looked up."""
match=current_word(cursor_offset,line)
ifmatchisNone:
returnNone
s=".".join(
m.group(1)
formin_current_object_re.finditer(match.word)
ifm.end(1)+match.start<cursor_offset
)
ifnots:
returnNone
returnLinePart(match.start,match.start+len(s),s)
_current_object_attribute_re=LazyReCompile(r"([\w_][\w0-9_]*)[.]?")
defcurrent_object_attribute(cursor_offset:int,line:str)->LinePart|None:
"""If in attribute completion, the attribute being completed"""
# TODO replace with more general current_expression_attribute
match=current_word(cursor_offset,line)
ifmatchisNone:
returnNone
matches=_current_object_attribute_re.finditer(match.word)
next(matches)
forminmatches:
ifm.start(1)+match.start<=cursor_offset<=m.end(1)+match.start:
returnLinePart(
m.start(1)+match.start,m.end(1)+match.start,m.group(1)
)
returnNone
_current_from_import_from_re=LazyReCompile(
r"from +([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*"
)
defcurrent_from_import_from(cursor_offset:int,line:str)->LinePart|None:
"""If in from import completion, the word after from
returns None if cursor not in or just after one of the two interesting
parts of an import: from (module) import (name1, name2)
"""
# TODO allow for as's
formin_current_from_import_from_re.finditer(line):
if (m.start(1)<cursor_offset<=m.end(1))or (
m.start(2)<cursor_offset<=m.end(2)
):
returnLinePart(m.start(1),m.end(1),m.group(1))
returnNone
_current_from_import_import_re_1=LazyReCompile(
r"from\s+([\w0-9_.]*)\s+import"
)
_current_from_import_import_re_2=LazyReCompile(r"([\w0-9_]+)")
_current_from_import_import_re_3=LazyReCompile(r", *([\w0-9_]*)")
defcurrent_from_import_import(
cursor_offset:int,line:str
)->LinePart|None:
"""If in from import completion, the word after import being completed
returns None if cursor not in or just after one of these words
"""
baseline=_current_from_import_import_re_1.search(line)
ifbaselineisNone:
returnNone
match1=_current_from_import_import_re_2.search(line[baseline.end() :])
ifmatch1isNone:
returnNone
forminchain(
(match1,),
_current_from_import_import_re_3.finditer(line[baseline.end() :]),
):
start=baseline.end()+m.start(1)
end=baseline.end()+m.end(1)
ifstart<cursor_offset<=end:
returnLinePart(start,end,m.group(1))
returnNone
_current_import_re_1=LazyReCompile(r"import")
_current_import_re_2=LazyReCompile(r"([\w0-9_.]+)")
_current_import_re_3=LazyReCompile(r"[,][ ]*([\w0-9_.]*)")
defcurrent_import(cursor_offset:int,line:str)->LinePart|None:
# TODO allow for multiple as's
baseline=_current_import_re_1.search(line)
ifbaselineisNone:
returnNone
match1=_current_import_re_2.search(line[baseline.end() :])
ifmatch1isNone:
returnNone
forminchain(
(match1,),_current_import_re_3.finditer(line[baseline.end() :])
):
start=baseline.end()+m.start(1)
end=baseline.end()+m.end(1)
ifstart<cursor_offset<=end:
returnLinePart(start,end,m.group(1))
returnNone
_current_method_definition_name_re=LazyReCompile(r"def\s+([a-zA-Z_][\w]*)")
defcurrent_method_definition_name(
cursor_offset:int,line:str
)->LinePart|None:
"""The name of a method being defined"""
formin_current_method_definition_name_re.finditer(line):
ifm.start(1)<=cursor_offset<=m.end(1):
returnLinePart(m.start(1),m.end(1),m.group(1))
returnNone
_current_single_word_re=LazyReCompile(r"(?<![.])\b([a-zA-Z_][\w]*)")
defcurrent_single_word(cursor_offset:int,line:str)->LinePart|None:
"""the un-dotted word just before or under the cursor"""
formin_current_single_word_re.finditer(line):
ifm.start(1)<=cursor_offset<=m.end(1):
returnLinePart(m.start(1),m.end(1),m.group(1))
returnNone
defcurrent_dotted_attribute(cursor_offset:int,line:str)->LinePart|None:
"""The dotted attribute-object pair before the cursor"""
match=current_word(cursor_offset,line)
ifmatchisnotNoneand"."inmatch.word[1:]:
returnmatch
returnNone
_current_expression_attribute_re=LazyReCompile(
r"[.]\s*((?:[\w_][\w0-9_]*)|(?:))"
)
defcurrent_expression_attribute(
cursor_offset:int,line:str
)->LinePart|None:
"""If after a dot, the attribute being completed"""
# TODO replace with more general current_expression_attribute
formin_current_expression_attribute_re.finditer(line):
ifm.start(1)<=cursor_offset<=m.end(1):
returnLinePart(m.start(1),m.end(1),m.group(1))
returnNone
defcursor_on_closing_char_pair(
cursor_offset:int,line:str,ch:str|None=None
)->tuple[bool,bool]:
"""Checks if cursor sits on closing character of a pair
and whether its pair character is directly behind it
"""
on_closing_char,pair_close=False,False
iflineisNone:
returnon_closing_char,pair_close
ifcursor_offset<len(line):
cur_char=line[cursor_offset]
ifcur_charinCHARACTER_PAIR_MAP.values():
on_closing_char=TrueifchisNoneelsecur_char==ch
ifcursor_offset>0:
prev_char=line[cursor_offset-1]
if (
on_closing_char
andprev_charinCHARACTER_PAIR_MAP
andCHARACTER_PAIR_MAP[prev_char]==cur_char
):
pair_close=TrueifchisNoneelseprev_char==ch
returnon_closing_char,pair_close