FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bpython/bpython/line.py at master · benkrig/bpython · GitHub
benkrig
/
bpython
Public
forked from
bpython/bpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bpython
/
bpython
/
line.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (178 loc) · 7.68 KB
Breadcrumbs
bpython
/
bpython
/
line.py
Copy path
File metadata and controls
235 lines (178 loc) · 7.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# encoding: utf-8
"""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."""
from
__future__
import
unicode_literals
,
absolute_import
from
itertools
import
chain
from
collections
import
namedtuple
from
.
lazyre
import
LazyReCompile
LinePart
=
namedtuple
(
'LinePart'
, [
'start'
,
'stop'
,
'word'
])
current_word_re
=
LazyReCompile
(
r'(?<![)\]\w_.])'
r'([\w_][\w0-9._]*[(]?)'
)
def
current_word
(
cursor_offset
,
line
):
"""the object.attribute.attribute just before or under the cursor"""
pos
=
cursor_offset
matches
=
current_word_re
.
finditer
(
line
)
start
=
pos
end
=
pos
word
=
None
for
m
in
matches
:
if
m
.
start
(
1
)
<
pos
and
m
.
end
(
1
)
>=
pos
:
start
=
m
.
start
(
1
)
end
=
m
.
end
(
1
)
word
=
m
.
group
(
1
)
if
word
is
None
:
return
None
return
LinePart
(
start
,
end
,
word
)
current_dict_key_re
=
LazyReCompile
(
r'''[\w_][\w0-9._]*\[([\w0-9._(), '"]*)'''
)
def
current_dict_key
(
cursor_offset
,
line
):
"""If in dictionary completion, return the current key"""
matches
=
current_dict_key_re
.
finditer
(
line
)
for
m
in
matches
:
if
m
.
start
(
1
)
<=
cursor_offset
and
m
.
end
(
1
)
>=
cursor_offset
:
return
LinePart
(
m
.
start
(
1
),
m
.
end
(
1
),
m
.
group
(
1
))
return
None
current_dict_re
=
LazyReCompile
(
r'''([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)'''
)
def
current_dict
(
cursor_offset
,
line
):
"""If in dictionary completion, return the dict that should be used"""
matches
=
current_dict_re
.
finditer
(
line
)
for
m
in
matches
:
if
m
.
start
(
2
)
<=
cursor_offset
and
m
.
end
(
2
)
>=
cursor_offset
:
return
LinePart
(
m
.
start
(
1
),
m
.
end
(
1
),
m
.
group
(
1
))
return
None
current_string_re
=
LazyReCompile
(
'''(?P<open>(?:""")|"|(?:''
\'
)|')(?:((?P<closed>.+?)(?P=open))|'''
'''(?P<unclosed>.+))'''
)
def
current_string
(
cursor_offset
,
line
):
"""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."""
for
m
in
current_string_re
.
finditer
(
line
):
i
=
3
if
m
.
group
(
3
)
else
4
if
m
.
start
(
i
)
<=
cursor_offset
and
m
.
end
(
i
)
>=
cursor_offset
:
return
LinePart
(
m
.
start
(
i
),
m
.
end
(
i
),
m
.
group
(
i
))
return
None
current_object_re
=
LazyReCompile
(
r'([\w_][\w0-9_]*)[.]'
)
def
current_object
(
cursor_offset
,
line
):
"""If in attribute completion, the object on which attribute should be
looked up."""
match
=
current_word
(
cursor_offset
,
line
)
if
match
is
None
:
return
None
start
,
end
,
word
=
match
matches
=
current_object_re
.
finditer
(
word
)
s
=
''
for
m
in
matches
:
if
m
.
end
(
1
)
+
start
<
cursor_offset
:
if
s
:
s
+=
'.'
s
+=
m
.
group
(
1
)
if
not
s
:
return
None
return
LinePart
(
start
,
start
+
len
(
s
),
s
)
current_object_attribute_re
=
LazyReCompile
(
r'([\w_][\w0-9_]*)[.]?'
)
def
current_object_attribute
(
cursor_offset
,
line
):
"""If in attribute completion, the attribute being completed"""
# TODO replace with more general current_expression_attribute
match
=
current_word
(
cursor_offset
,
line
)
if
match
is
None
:
return
None
start
,
end
,
word
=
match
matches
=
current_object_attribute_re
.
finditer
(
word
)
next
(
matches
)
for
m
in
matches
:
if
(
m
.
start
(
1
)
+
start
<=
cursor_offset
and
m
.
end
(
1
)
+
start
>=
cursor_offset
):
return
LinePart
(
m
.
start
(
1
)
+
start
,
m
.
end
(
1
)
+
start
,
m
.
group
(
1
))
return
None
current_from_import_from_re
=
LazyReCompile
(
r'from ([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*'
)
def
current_from_import_from
(
cursor_offset
,
line
):
"""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
tokens
=
line
.
split
()
if
not
(
'from'
in
tokens
or
'import'
in
tokens
):
return
None
matches
=
current_from_import_from_re
.
finditer
(
line
)
for
m
in
matches
:
if
((
m
.
start
(
1
)
<
cursor_offset
and
m
.
end
(
1
)
>=
cursor_offset
)
or
(
m
.
start
(
2
)
<
cursor_offset
and
m
.
end
(
2
)
>=
cursor_offset
)):
return
LinePart
(
m
.
start
(
1
),
m
.
end
(
1
),
m
.
group
(
1
))
return
None
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_]*)'
)
def
current_from_import_import
(
cursor_offset
,
line
):
"""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
)
if
baseline
is
None
:
return
None
match1
=
current_from_import_import_re_2
.
search
(
line
[
baseline
.
end
():])
if
match1
is
None
:
return
None
matches
=
current_from_import_import_re_3
.
finditer
(
line
[
baseline
.
end
():])
for
m
in
chain
((
match1
, ),
matches
):
start
=
baseline
.
end
()
+
m
.
start
(
1
)
end
=
baseline
.
end
()
+
m
.
end
(
1
)
if
start
<
cursor_offset
and
end
>=
cursor_offset
:
return
LinePart
(
start
,
end
,
m
.
group
(
1
))
return
None
current_import_re_1
=
LazyReCompile
(
r'import'
)
current_import_re_2
=
LazyReCompile
(
r'([\w0-9_.]+)'
)
current_import_re_3
=
LazyReCompile
(
r'[,][ ]([\w0-9_.]*)'
)
def
current_import
(
cursor_offset
,
line
):
# TODO allow for multiple as's
baseline
=
current_import_re_1
.
search
(
line
)
if
baseline
is
None
:
return
None
match1
=
current_import_re_2
.
search
(
line
[
baseline
.
end
():])
if
match1
is
None
:
return
None
matches
=
current_import_re_3
.
finditer
(
line
[
baseline
.
end
():])
for
m
in
chain
((
match1
, ),
matches
):
start
=
baseline
.
end
()
+
m
.
start
(
1
)
end
=
baseline
.
end
()
+
m
.
end
(
1
)
if
start
<
cursor_offset
and
end
>=
cursor_offset
:
return
LinePart
(
start
,
end
,
m
.
group
(
1
))
current_method_definition_name_re
=
LazyReCompile
(
"def\s+([a-zA-Z_][\w]*)"
)
def
current_method_definition_name
(
cursor_offset
,
line
):
"""The name of a method being defined"""
matches
=
current_method_definition_name_re
.
finditer
(
line
)
for
m
in
matches
:
if
(
m
.
start
(
1
)
<=
cursor_offset
and
m
.
end
(
1
)
>=
cursor_offset
):
return
LinePart
(
m
.
start
(
1
),
m
.
end
(
1
),
m
.
group
(
1
))
return
None
current_single_word_re
=
LazyReCompile
(
r"(?<![.])\b([a-zA-Z_][\w]*)"
)
def
current_single_word
(
cursor_offset
,
line
):
"""the un-dotted word just before or under the cursor"""
matches
=
current_single_word_re
.
finditer
(
line
)
for
m
in
matches
:
if
m
.
start
(
1
)
<=
cursor_offset
and
m
.
end
(
1
)
>=
cursor_offset
:
return
LinePart
(
m
.
start
(
1
),
m
.
end
(
1
),
m
.
group
(
1
))
return
None
def
current_dotted_attribute
(
cursor_offset
,
line
):
"""The dotted attribute-object pair before the cursor"""
match
=
current_word
(
cursor_offset
,
line
)
if
match
is
None
:
return
None
start
,
end
,
word
=
match
if
'.'
in
word
[
1
:]:
return
LinePart
(
start
,
end
,
word
)
current_expression_attribute_re
=
LazyReCompile
(
r'[.]\s*((?:[\w_][\w0-9_]*)|(?:))'
)
def
current_expression_attribute
(
cursor_offset
,
line
):
"""If after a dot, the attribute being completed"""
# TODO replace with more general current_expression_attribute
matches
=
current_expression_attribute_re
.
finditer
(
line
)
for
m
in
matches
:
if
(
m
.
start
(
1
)
<=
cursor_offset
and
m
.
end
(
1
)
>=
cursor_offset
):
return
LinePart
(
m
.
start
(
1
),
m
.
end
(
1
),
m
.
group
(
1
))
return
None
Back
|
FazBrowse Home
|
New Git URL