| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e5cefc5 commit 3db074b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,6 @@ | |||
| 26 | 26 | import abc | |
| 27 | 27 | import keyword | |
| 28 | 28 | import os | |
| 29 | - import re | ||
| 30 | 29 | import rlcompleter | |
| 31 | 30 | ||
| 32 | 31 | from glob import glob | |
@@ -461,17 +460,13 @@ def safe_eval(expr, namespace): | |||
| 461 | 460 | raise EvaluationError | |
| 462 | 461 | ||
| 463 | 462 | ||
| 464 | - attr_matches_re = None | ||
| 463 | + attr_matches_re = lineparts.LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)") | ||
| 465 | 464 | ||
| 466 | 465 | ||
| 467 | 466 | def attr_matches(text, namespace): | |
| 468 | 467 | """Taken from rlcompleter.py and bent to my will. | |
| 469 | 468 | """ | |
| 470 | 469 | ||
| 471 | - global attr_matches_re | ||
| 472 | - if attr_matches_re is None: | ||
| 473 | - attr_matches_re = re.compile(r"(\w+(\.\w+)*)\.(\w*)") | ||
| 474 | - | ||
| 475 | 470 | # Gna, Py 2.6's rlcompleter searches for __call__ inside the | |
| 476 | 471 | # instance instead of the type, so we monkeypatch to prevent | |
| 477 | 472 | # side-effects (__getattr__/__getattribute__) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,34 @@ | |||
| 7 | 7 | from itertools import chain | |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | - current_word_re = re.compile(r'[\w_][\w0-9._]*[(]?') | ||
| 10 | + class LazyReCompile(object): | ||
| 11 | + | ||
| 12 | + def __init__(self, regex): | ||
| 13 | + self.regex = regex | ||
| 14 | + self.compiled = None | ||
| 15 | + | ||
| 16 | + def compile_regex(method): | ||
| 17 | + def _impl(self, *args, **kwargs): | ||
| 18 | + if self.compiled is None: | ||
| 19 | + self.compiled = re.compile(self.regex) | ||
| 20 | + return method(self, *args, **kwargs) | ||
| 21 | + return _impl | ||
| 22 | + | ||
| 23 | + @compile_regex | ||
| 24 | + def finditer(self, *args, **kwargs): | ||
| 25 | + return self.compiled.finditer(*args, **kwargs) | ||
| 26 | + | ||
| 27 | + @compile_regex | ||
| 28 | + def search(self, *args, **kwargs): | ||
| 29 | + return self.compiled.search(*args, **kwargs) | ||
| 30 | + | ||
| 31 | + @compile_regex | ||
| 32 | + def match(self, *args, **kwargs): | ||
| 33 | + return self.compiled.match(*args, **kwargs) | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + current_word_re = LazyReCompile(r'[\w_][\w0-9._]*[(]?') | ||
| 37 | + | ||
| 11 | 38 | ||
| 12 | 39 | def current_word(cursor_offset, line): | |
| 13 | 40 | """the object.attribute.attribute just before or under the cursor""" | |
@@ -26,7 +53,7 @@ def current_word(cursor_offset, line): | |||
| 26 | 53 | return (start, end, word) | |
| 27 | 54 | ||
| 28 | 55 | ||
| 29 | - current_dict_key_re = re.compile(r'''[\w_][\w0-9._]*\[([\w0-9._(), '"]*)''') | ||
| 56 | + current_dict_key_re = LazyReCompile(r'''[\w_][\w0-9._]*\[([\w0-9._(), '"]*)''') | ||
| 30 | 57 | ||
| 31 | 58 | def current_dict_key(cursor_offset, line): | |
| 32 | 59 | """If in dictionary completion, return the current key""" | |
@@ -37,7 +64,8 @@ def current_dict_key(cursor_offset, line): | |||
| 37 | 64 | return None | |
| 38 | 65 | ||
| 39 | 66 | ||
| 40 | - current_dict_re = re.compile(r'''([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)''') | ||
| 67 | + current_dict_re = LazyReCompile(r'''([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)''') | ||
| 68 | + | ||
| 41 | 69 | ||
| 42 | 70 | def current_dict(cursor_offset, line): | |
| 43 | 71 | """If in dictionary completion, return the dict that should be used""" | |
@@ -48,9 +76,10 @@ def current_dict(cursor_offset, line): | |||
| 48 | 76 | return None | |
| 49 | 77 | ||
| 50 | 78 | ||
| 51 | - current_string_re = re.compile( | ||
| 79 | + current_string_re = LazyReCompile( | ||
| 52 | 80 | '''(?P<open>(?:""")|"|(?:''\')|')(?:((?P<closed>.+?)(?P=open))|(?P<unclosed>.+))''') | |
| 53 | 81 | ||
| 82 | + | ||
| 54 | 83 | def current_string(cursor_offset, line): | |
| 55 | 84 | """If inside a string of nonzero length, return the string (excluding quotes) | |
| 56 | 85 | ||
@@ -63,7 +92,7 @@ def current_string(cursor_offset, line): | |||
| 63 | 92 | return None | |
| 64 | 93 | ||
| 65 | 94 | ||
| 66 | - current_object_re = re.compile(r'([\w_][\w0-9_]*)[.]') | ||
| 95 | + current_object_re = LazyReCompile(r'([\w_][\w0-9_]*)[.]') | ||
| 67 | 96 | ||
| 68 | 97 | def current_object(cursor_offset, line): | |
| 69 | 98 | """If in attribute completion, the object on which attribute should be looked up""" | |
@@ -82,7 +111,7 @@ def current_object(cursor_offset, line): | |||
| 82 | 111 | return start, start+len(s), s | |
| 83 | 112 | ||
| 84 | 113 | ||
| 85 | - current_object_attribute_re = re.compile(r'([\w_][\w0-9_]*)[.]?') | ||
| 114 | + current_object_attribute_re = LazyReCompile(r'([\w_][\w0-9_]*)[.]?') | ||
| 86 | 115 | ||
| 87 | 116 | def current_object_attribute(cursor_offset, line): | |
| 88 | 117 | """If in attribute completion, the attribute being completed""" | |
@@ -97,7 +126,7 @@ def current_object_attribute(cursor_offset, line): | |||
| 97 | 126 | return None | |
| 98 | 127 | ||
| 99 | 128 | ||
| 100 | - current_from_import_from_re = re.compile(r'from ([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*') | ||
| 129 | + current_from_import_from_re = LazyReCompile(r'from ([\w0-9_.]*)(?:\s+import\s+([\w0-9_]+[,]?\s*)+)*') | ||
| 101 | 130 | ||
| 102 | 131 | def current_from_import_from(cursor_offset, line): | |
| 103 | 132 | """If in from import completion, the word after from | |
@@ -117,9 +146,9 @@ def current_from_import_from(cursor_offset, line): | |||
| 117 | 146 | return None | |
| 118 | 147 | ||
| 119 | 148 | ||
| 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_]*)') | ||
| 149 | + current_from_import_import_re_1 = LazyReCompile(r'from\s([\w0-9_.]*)\s+import') | ||
| 150 | + current_from_import_import_re_2 = LazyReCompile(r'([\w0-9_]+)') | ||
| 151 | + current_from_import_import_re_3 = LazyReCompile(r'[,][ ]([\w0-9_]*)') | ||
| 123 | 152 | ||
| 124 | 153 | def current_from_import_import(cursor_offset, line): | |
| 125 | 154 | """If in from import completion, the word after import being completed | |
@@ -141,9 +170,9 @@ def current_from_import_import(cursor_offset, line): | |||
| 141 | 170 | return None | |
| 142 | 171 | ||
| 143 | 172 | ||
| 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_.]*)') | ||
| 173 | + current_import_re_1 = LazyReCompile(r'import') | ||
| 174 | + current_import_re_2 = LazyReCompile(r'([\w0-9_.]+)') | ||
| 175 | + current_import_re_3 = LazyReCompile(r'[,][ ]([\w0-9_.]*)') | ||
| 147 | 176 | ||
| 148 | 177 | def current_import(cursor_offset, line): | |
| 149 | 178 | #TODO allow for multiple as's | |
@@ -161,7 +190,7 @@ def current_import(cursor_offset, line): | |||
| 161 | 190 | return start, end, m.group(1) | |
| 162 | 191 | ||
| 163 | 192 | ||
| 164 | - current_method_definition_name_re = re.compile("def\s+([a-zA-Z_][\w]*)") | ||
| 193 | + current_method_definition_name_re = LazyReCompile("def\s+([a-zA-Z_][\w]*)") | ||
| 165 | 194 | ||
| 166 | 195 | def current_method_definition_name(cursor_offset, line): | |
| 167 | 196 | """The name of a method being defined""" | |
@@ -172,7 +201,7 @@ def current_method_definition_name(cursor_offset, line): | |||
| 172 | 201 | return None | |
| 173 | 202 | ||
| 174 | 203 | ||
| 175 | - current_single_word_re = re.compile(r"(?<![.])\b([a-zA-Z_][\w]*)") | ||
| 204 | + current_single_word_re = LazyReCompile(r"(?<![.])\b([a-zA-Z_][\w]*)") | ||
| 176 | 205 | ||
| 177 | 206 | def current_single_word(cursor_offset, line): | |
| 178 | 207 | """the un-dotted word just before or under the cursor""" | |
@@ -192,7 +221,7 @@ def current_dotted_attribute(cursor_offset, line): | |||
| 192 | 221 | return start, end, word | |
| 193 | 222 | ||
| 194 | 223 | ||
| 195 | - current_string_literal_attr_re = re.compile( | ||
| 224 | + current_string_literal_attr_re = LazyReCompile( | ||
| 196 | 225 | "('''" + | |
| 197 | 226 | r'''|"""|'|")((?:(?=([^"'\\]+|\\.|(?!\1)["']))\3)*)\1[.]([a-zA-Z_]?[\w]*)''') | |
| 198 | 227 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments