X Tutup
Skip to content

Commit d21a990

Browse files
committed
Kepp regular expressions close to their use
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent da2a126 commit d21a990

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

bpython/line.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,6 @@
88

99

1010
current_word_re = re.compile(r'[\w_][\w0-9._]*[(]?')
11-
current_dict_key_re = re.compile(r'''[\w_][\w0-9._]*\[([\w0-9._(), '"]*)''')
12-
current_dict_re = re.compile(r'''([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)''')
13-
current_string_re = re.compile(
14-
'''(?P<open>(?:""")|"|(?:''\')|')(?:((?P<closed>.+?)(?P=open))|(?P<unclosed>.+))''')
15-
current_object_re = re.compile(r'([\w_][\w0-9_]*)[.]')
16-
current_object_attribute_re = re.compile(r'([\w_][\w0-9_]*)[.]?')
17-
current_from_import_from_re = re.compile(r'from ([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*')
18-
current_from_import_import_re_1 = re.compile(r'from\s([\w0-9_.]*)\s+import')
19-
current_from_import_import_re_2 = re.compile(r'([\w0-9_]+)')
20-
current_from_import_import_re_3 = re.compile(r'[,][ ]([\w0-9_]*)')
21-
current_import_re_1 = re.compile(r'import')
22-
current_import_re_2 = re.compile(r'([\w0-9_.]+)')
23-
current_import_re_3 = re.compile(r'[,][ ]([\w0-9_.]*)')
24-
current_method_definition_name_re = re.compile("def\s+([a-zA-Z_][\w]*)")
25-
current_single_word_re = re.compile(r"(?<![.])\b([a-zA-Z_][\w]*)")
26-
current_string_literal_attr_re = re.compile(
27-
"('''" +
28-
r'''|"""|'|")((?:(?=([^"'\\]+|\\.|(?!\1)["']))\3)*)\1[.]([a-zA-Z_]?[\w]*)''')
2911

3012
def current_word(cursor_offset, line):
3113
"""the object.attribute.attribute just before or under the cursor"""
@@ -43,6 +25,9 @@ def current_word(cursor_offset, line):
4325
return None
4426
return (start, end, word)
4527

28+
29+
current_dict_key_re = re.compile(r'''[\w_][\w0-9._]*\[([\w0-9._(), '"]*)''')
30+
4631
def current_dict_key(cursor_offset, line):
4732
"""If in dictionary completion, return the current key"""
4833
matches = current_dict_key_re.finditer(line)
@@ -51,6 +36,9 @@ def current_dict_key(cursor_offset, line):
5136
return (m.start(1), m.end(1), m.group(1))
5237
return None
5338

39+
40+
current_dict_re = re.compile(r'''([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)''')
41+
5442
def current_dict(cursor_offset, line):
5543
"""If in dictionary completion, return the dict that should be used"""
5644
matches = current_dict_re.finditer(line)
@@ -59,6 +47,10 @@ def current_dict(cursor_offset, line):
5947
return (m.start(1), m.end(1), m.group(1))
6048
return None
6149

50+
51+
current_string_re = re.compile(
52+
'''(?P<open>(?:""")|"|(?:''\')|')(?:((?P<closed>.+?)(?P=open))|(?P<unclosed>.+))''')
53+
6254
def current_string(cursor_offset, line):
6355
"""If inside a string of nonzero length, return the string (excluding quotes)
6456
@@ -70,6 +62,9 @@ def current_string(cursor_offset, line):
7062
return m.start(i), m.end(i), m.group(i)
7163
return None
7264

65+
66+
current_object_re = re.compile(r'([\w_][\w0-9_]*)[.]')
67+
7368
def current_object(cursor_offset, line):
7469
"""If in attribute completion, the object on which attribute should be looked up"""
7570
match = current_word(cursor_offset, line)
@@ -86,6 +81,9 @@ def current_object(cursor_offset, line):
8681
return None
8782
return start, start+len(s), s
8883

84+
85+
current_object_attribute_re = re.compile(r'([\w_][\w0-9_]*)[.]?')
86+
8987
def current_object_attribute(cursor_offset, line):
9088
"""If in attribute completion, the attribute being completed"""
9189
match = current_word(cursor_offset, line)
@@ -98,6 +96,9 @@ def current_object_attribute(cursor_offset, line):
9896
return m.start(1) + start, m.end(1) + start, m.group(1)
9997
return None
10098

99+
100+
current_from_import_from_re = re.compile(r'from ([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*')
101+
101102
def current_from_import_from(cursor_offset, line):
102103
"""If in from import completion, the word after from
103104
@@ -115,6 +116,11 @@ def current_from_import_from(cursor_offset, line):
115116
return m.start(1), m.end(1), m.group(1)
116117
return None
117118

119+
120+
current_from_import_import_re_1 = re.compile(r'from\s([\w0-9_.]*)\s+import')
121+
current_from_import_import_re_2 = re.compile(r'([\w0-9_]+)')
122+
current_from_import_import_re_3 = re.compile(r'[,][ ]([\w0-9_]*)')
123+
118124
def current_from_import_import(cursor_offset, line):
119125
"""If in from import completion, the word after import being completed
120126
@@ -134,6 +140,11 @@ def current_from_import_import(cursor_offset, line):
134140
return start, end, m.group(1)
135141
return None
136142

143+
144+
current_import_re_1 = re.compile(r'import')
145+
current_import_re_2 = re.compile(r'([\w0-9_.]+)')
146+
current_import_re_3 = re.compile(r'[,][ ]([\w0-9_.]*)')
147+
137148
def current_import(cursor_offset, line):
138149
#TODO allow for multiple as's
139150
baseline = current_import_re_1.search(line)
@@ -149,6 +160,9 @@ def current_import(cursor_offset, line):
149160
if start < cursor_offset and end >= cursor_offset:
150161
return start, end, m.group(1)
151162

163+
164+
current_method_definition_name_re = re.compile("def\s+([a-zA-Z_][\w]*)")
165+
152166
def current_method_definition_name(cursor_offset, line):
153167
"""The name of a method being defined"""
154168
matches = current_method_definition_name_re.finditer(line)
@@ -157,6 +171,9 @@ def current_method_definition_name(cursor_offset, line):
157171
return m.start(1), m.end(1), m.group(1)
158172
return None
159173

174+
175+
current_single_word_re = re.compile(r"(?<![.])\b([a-zA-Z_][\w]*)")
176+
160177
def current_single_word(cursor_offset, line):
161178
"""the un-dotted word just before or under the cursor"""
162179
matches = current_single_word_re.finditer(line)
@@ -165,6 +182,7 @@ def current_single_word(cursor_offset, line):
165182
return m.start(1), m.end(1), m.group(1)
166183
return None
167184

185+
168186
def current_dotted_attribute(cursor_offset, line):
169187
"""The dotted attribute-object pair before the cursor"""
170188
match = current_word(cursor_offset, line)
@@ -173,6 +191,11 @@ def current_dotted_attribute(cursor_offset, line):
173191
if '.' in word[1:]:
174192
return start, end, word
175193

194+
195+
current_string_literal_attr_re = re.compile(
196+
"('''" +
197+
r'''|"""|'|")((?:(?=([^"'\\]+|\\.|(?!\1)["']))\3)*)\1[.]([a-zA-Z_]?[\w]*)''')
198+
176199
def current_string_literal_attr(cursor_offset, line):
177200
"""The attribute following a string literal"""
178201
matches = current_string_literal_attr_re.finditer(line)

0 commit comments

Comments
 (0)
X Tutup