Show More
@@ -1,1176 +1,1176 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Word completion for IPython. |
|
3 | 3 | |
|
4 | This module stared as fork of the rlcompleter module in the Python standard | |
|
4 | This module started as fork of the rlcompleter module in the Python standard | |
|
5 | 5 | library. The original enhancements made to rlcompleter have been sent |
|
6 | 6 | upstream and were accepted as of Python 2.3, |
|
7 | 7 | |
|
8 | 8 | |
|
9 | 9 | This is now mostly Completer implementation made to function with |
|
10 | 10 | prompt_toolkit, but that should still work with readline. |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | # Copyright (c) IPython Development Team. |
|
14 | 14 | # Distributed under the terms of the Modified BSD License. |
|
15 | 15 | # |
|
16 | 16 | # Some of this code originated from rlcompleter in the Python standard library |
|
17 | 17 | # Copyright (C) 2001 Python Software Foundation, www.python.org |
|
18 | 18 | |
|
19 | 19 | from __future__ import print_function |
|
20 | 20 | |
|
21 | 21 | import __main__ |
|
22 | 22 | import glob |
|
23 | 23 | import inspect |
|
24 | 24 | import itertools |
|
25 | 25 | import keyword |
|
26 | 26 | import os |
|
27 | 27 | import re |
|
28 | 28 | import sys |
|
29 | 29 | import unicodedata |
|
30 | 30 | import string |
|
31 | 31 | |
|
32 | 32 | from traitlets.config.configurable import Configurable |
|
33 | 33 | from IPython.core.error import TryNext |
|
34 | 34 | from IPython.core.inputsplitter import ESC_MAGIC |
|
35 | 35 | from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol |
|
36 | 36 | from IPython.utils import generics |
|
37 | 37 | from IPython.utils.decorators import undoc |
|
38 | 38 | from IPython.utils.dir2 import dir2, get_real_method |
|
39 | 39 | from IPython.utils.process import arg_split |
|
40 | 40 | from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2 |
|
41 | 41 | from traitlets import Bool, Enum, observe |
|
42 | 42 | |
|
43 | 43 | #----------------------------------------------------------------------------- |
|
44 | 44 | # Globals |
|
45 | 45 | #----------------------------------------------------------------------------- |
|
46 | 46 | |
|
47 | 47 | # Public API |
|
48 | 48 | __all__ = ['Completer','IPCompleter'] |
|
49 | 49 | |
|
50 | 50 | if sys.platform == 'win32': |
|
51 | 51 | PROTECTABLES = ' ' |
|
52 | 52 | else: |
|
53 | 53 | PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&' |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | #----------------------------------------------------------------------------- |
|
57 | 57 | # Main functions and classes |
|
58 | 58 | #----------------------------------------------------------------------------- |
|
59 | 59 | |
|
60 | 60 | def has_open_quotes(s): |
|
61 | 61 | """Return whether a string has open quotes. |
|
62 | 62 | |
|
63 | 63 | This simply counts whether the number of quote characters of either type in |
|
64 | 64 | the string is odd. |
|
65 | 65 | |
|
66 | 66 | Returns |
|
67 | 67 | ------- |
|
68 | 68 | If there is an open quote, the quote character is returned. Else, return |
|
69 | 69 | False. |
|
70 | 70 | """ |
|
71 | 71 | # We check " first, then ', so complex cases with nested quotes will get |
|
72 | 72 | # the " to take precedence. |
|
73 | 73 | if s.count('"') % 2: |
|
74 | 74 | return '"' |
|
75 | 75 | elif s.count("'") % 2: |
|
76 | 76 | return "'" |
|
77 | 77 | else: |
|
78 | 78 | return False |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def protect_filename(s): |
|
82 | 82 | """Escape a string to protect certain characters.""" |
|
83 | 83 | if set(s) & set(PROTECTABLES): |
|
84 | 84 | if sys.platform == "win32": |
|
85 | 85 | return '"' + s + '"' |
|
86 | 86 | else: |
|
87 | 87 | return "".join(("\\" + c if c in PROTECTABLES else c) for c in s) |
|
88 | 88 | else: |
|
89 | 89 | return s |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | def expand_user(path): |
|
93 | 93 | """Expand '~'-style usernames in strings. |
|
94 | 94 | |
|
95 | 95 | This is similar to :func:`os.path.expanduser`, but it computes and returns |
|
96 | 96 | extra information that will be useful if the input was being used in |
|
97 | 97 | computing completions, and you wish to return the completions with the |
|
98 | 98 | original '~' instead of its expanded value. |
|
99 | 99 | |
|
100 | 100 | Parameters |
|
101 | 101 | ---------- |
|
102 | 102 | path : str |
|
103 | 103 | String to be expanded. If no ~ is present, the output is the same as the |
|
104 | 104 | input. |
|
105 | 105 | |
|
106 | 106 | Returns |
|
107 | 107 | ------- |
|
108 | 108 | newpath : str |
|
109 | 109 | Result of ~ expansion in the input path. |
|
110 | 110 | tilde_expand : bool |
|
111 | 111 | Whether any expansion was performed or not. |
|
112 | 112 | tilde_val : str |
|
113 | 113 | The value that ~ was replaced with. |
|
114 | 114 | """ |
|
115 | 115 | # Default values |
|
116 | 116 | tilde_expand = False |
|
117 | 117 | tilde_val = '' |
|
118 | 118 | newpath = path |
|
119 | 119 | |
|
120 | 120 | if path.startswith('~'): |
|
121 | 121 | tilde_expand = True |
|
122 | 122 | rest = len(path)-1 |
|
123 | 123 | newpath = os.path.expanduser(path) |
|
124 | 124 | if rest: |
|
125 | 125 | tilde_val = newpath[:-rest] |
|
126 | 126 | else: |
|
127 | 127 | tilde_val = newpath |
|
128 | 128 | |
|
129 | 129 | return newpath, tilde_expand, tilde_val |
|
130 | 130 | |
|
131 | 131 | |
|
132 | 132 | def compress_user(path, tilde_expand, tilde_val): |
|
133 | 133 | """Does the opposite of expand_user, with its outputs. |
|
134 | 134 | """ |
|
135 | 135 | if tilde_expand: |
|
136 | 136 | return path.replace(tilde_val, '~') |
|
137 | 137 | else: |
|
138 | 138 | return path |
|
139 | 139 | |
|
140 | 140 | |
|
141 | 141 | def completions_sorting_key(word): |
|
142 | 142 | """key for sorting completions |
|
143 | 143 | |
|
144 | 144 | This does several things: |
|
145 | 145 | |
|
146 | 146 | - Lowercase all completions, so they are sorted alphabetically with |
|
147 | 147 | upper and lower case words mingled |
|
148 | 148 | - Demote any completions starting with underscores to the end |
|
149 | 149 | - Insert any %magic and %%cellmagic completions in the alphabetical order |
|
150 | 150 | by their name |
|
151 | 151 | """ |
|
152 | 152 | # Case insensitive sort |
|
153 | 153 | word = word.lower() |
|
154 | 154 | |
|
155 | 155 | prio1, prio2 = 0, 0 |
|
156 | 156 | |
|
157 | 157 | if word.startswith('__'): |
|
158 | 158 | prio1 = 2 |
|
159 | 159 | elif word.startswith('_'): |
|
160 | 160 | prio1 = 1 |
|
161 | 161 | |
|
162 | 162 | if word.endswith('='): |
|
163 | 163 | prio1 = -1 |
|
164 | 164 | |
|
165 | 165 | if word.startswith('%%'): |
|
166 | 166 | # If there's another % in there, this is something else, so leave it alone |
|
167 | 167 | if not "%" in word[2:]: |
|
168 | 168 | word = word[2:] |
|
169 | 169 | prio2 = 2 |
|
170 | 170 | elif word.startswith('%'): |
|
171 | 171 | if not "%" in word[1:]: |
|
172 | 172 | word = word[1:] |
|
173 | 173 | prio2 = 1 |
|
174 | 174 | |
|
175 | 175 | return prio1, word, prio2 |
|
176 | 176 | |
|
177 | 177 | |
|
178 | 178 | @undoc |
|
179 | 179 | class Bunch(object): pass |
|
180 | 180 | |
|
181 | 181 | |
|
182 | 182 | DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?' |
|
183 | 183 | GREEDY_DELIMS = ' =\r\n' |
|
184 | 184 | |
|
185 | 185 | |
|
186 | 186 | class CompletionSplitter(object): |
|
187 | 187 | """An object to split an input line in a manner similar to readline. |
|
188 | 188 | |
|
189 | 189 | By having our own implementation, we can expose readline-like completion in |
|
190 | 190 | a uniform manner to all frontends. This object only needs to be given the |
|
191 | 191 | line of text to be split and the cursor position on said line, and it |
|
192 | 192 | returns the 'word' to be completed on at the cursor after splitting the |
|
193 | 193 | entire line. |
|
194 | 194 | |
|
195 | 195 | What characters are used as splitting delimiters can be controlled by |
|
196 | 196 | setting the `delims` attribute (this is a property that internally |
|
197 | 197 | automatically builds the necessary regular expression)""" |
|
198 | 198 | |
|
199 | 199 | # Private interface |
|
200 | 200 | |
|
201 | 201 | # A string of delimiter characters. The default value makes sense for |
|
202 | 202 | # IPython's most typical usage patterns. |
|
203 | 203 | _delims = DELIMS |
|
204 | 204 | |
|
205 | 205 | # The expression (a normal string) to be compiled into a regular expression |
|
206 | 206 | # for actual splitting. We store it as an attribute mostly for ease of |
|
207 | 207 | # debugging, since this type of code can be so tricky to debug. |
|
208 | 208 | _delim_expr = None |
|
209 | 209 | |
|
210 | 210 | # The regular expression that does the actual splitting |
|
211 | 211 | _delim_re = None |
|
212 | 212 | |
|
213 | 213 | def __init__(self, delims=None): |
|
214 | 214 | delims = CompletionSplitter._delims if delims is None else delims |
|
215 | 215 | self.delims = delims |
|
216 | 216 | |
|
217 | 217 | @property |
|
218 | 218 | def delims(self): |
|
219 | 219 | """Return the string of delimiter characters.""" |
|
220 | 220 | return self._delims |
|
221 | 221 | |
|
222 | 222 | @delims.setter |
|
223 | 223 | def delims(self, delims): |
|
224 | 224 | """Set the delimiters for line splitting.""" |
|
225 | 225 | expr = '[' + ''.join('\\'+ c for c in delims) + ']' |
|
226 | 226 | self._delim_re = re.compile(expr) |
|
227 | 227 | self._delims = delims |
|
228 | 228 | self._delim_expr = expr |
|
229 | 229 | |
|
230 | 230 | def split_line(self, line, cursor_pos=None): |
|
231 | 231 | """Split a line of text with a cursor at the given position. |
|
232 | 232 | """ |
|
233 | 233 | l = line if cursor_pos is None else line[:cursor_pos] |
|
234 | 234 | return self._delim_re.split(l)[-1] |
|
235 | 235 | |
|
236 | 236 | |
|
237 | 237 | class Completer(Configurable): |
|
238 | 238 | |
|
239 | 239 | greedy = Bool(False, |
|
240 | 240 | help="""Activate greedy completion |
|
241 | 241 | PENDING DEPRECTION. this is now mostly taken care of with Jedi. |
|
242 | 242 | |
|
243 | 243 | This will enable completion on elements of lists, results of function calls, etc., |
|
244 | 244 | but can be unsafe because the code is actually evaluated on TAB. |
|
245 | 245 | """ |
|
246 | 246 | ).tag(config=True) |
|
247 | 247 | |
|
248 | 248 | |
|
249 | 249 | def __init__(self, namespace=None, global_namespace=None, **kwargs): |
|
250 | 250 | """Create a new completer for the command line. |
|
251 | 251 | |
|
252 | 252 | Completer(namespace=ns, global_namespace=ns2) -> completer instance. |
|
253 | 253 | |
|
254 | 254 | If unspecified, the default namespace where completions are performed |
|
255 | 255 | is __main__ (technically, __main__.__dict__). Namespaces should be |
|
256 | 256 | given as dictionaries. |
|
257 | 257 | |
|
258 | 258 | An optional second namespace can be given. This allows the completer |
|
259 | 259 | to handle cases where both the local and global scopes need to be |
|
260 | 260 | distinguished. |
|
261 | 261 | |
|
262 | 262 | Completer instances should be used as the completion mechanism of |
|
263 | 263 | readline via the set_completer() call: |
|
264 | 264 | |
|
265 | 265 | readline.set_completer(Completer(my_namespace).complete) |
|
266 | 266 | """ |
|
267 | 267 | |
|
268 | 268 | # Don't bind to namespace quite yet, but flag whether the user wants a |
|
269 | 269 | # specific namespace or to use __main__.__dict__. This will allow us |
|
270 | 270 | # to bind to __main__.__dict__ at completion time, not now. |
|
271 | 271 | if namespace is None: |
|
272 | 272 | self.use_main_ns = 1 |
|
273 | 273 | else: |
|
274 | 274 | self.use_main_ns = 0 |
|
275 | 275 | self.namespace = namespace |
|
276 | 276 | |
|
277 | 277 | # The global namespace, if given, can be bound directly |
|
278 | 278 | if global_namespace is None: |
|
279 | 279 | self.global_namespace = {} |
|
280 | 280 | else: |
|
281 | 281 | self.global_namespace = global_namespace |
|
282 | 282 | |
|
283 | 283 | super(Completer, self).__init__(**kwargs) |
|
284 | 284 | |
|
285 | 285 | def complete(self, text, state): |
|
286 | 286 | """Return the next possible completion for 'text'. |
|
287 | 287 | |
|
288 | 288 | This is called successively with state == 0, 1, 2, ... until it |
|
289 | 289 | returns None. The completion should begin with 'text'. |
|
290 | 290 | |
|
291 | 291 | """ |
|
292 | 292 | if self.use_main_ns: |
|
293 | 293 | self.namespace = __main__.__dict__ |
|
294 | 294 | |
|
295 | 295 | if state == 0: |
|
296 | 296 | if "." in text: |
|
297 | 297 | self.matches = self.attr_matches(text) |
|
298 | 298 | else: |
|
299 | 299 | self.matches = self.global_matches(text) |
|
300 | 300 | try: |
|
301 | 301 | return self.matches[state] |
|
302 | 302 | except IndexError: |
|
303 | 303 | return None |
|
304 | 304 | |
|
305 | 305 | def global_matches(self, text): |
|
306 | 306 | """Compute matches when text is a simple name. |
|
307 | 307 | |
|
308 | 308 | Return a list of all keywords, built-in functions and names currently |
|
309 | 309 | defined in self.namespace or self.global_namespace that match. |
|
310 | 310 | |
|
311 | 311 | """ |
|
312 | 312 | matches = [] |
|
313 | 313 | match_append = matches.append |
|
314 | 314 | n = len(text) |
|
315 | 315 | for lst in [keyword.kwlist, |
|
316 | 316 | builtin_mod.__dict__.keys(), |
|
317 | 317 | self.namespace.keys(), |
|
318 | 318 | self.global_namespace.keys()]: |
|
319 | 319 | for word in lst: |
|
320 | 320 | if word[:n] == text and word != "__builtins__": |
|
321 | 321 | match_append(word) |
|
322 | 322 | return [cast_unicode_py2(m) for m in matches] |
|
323 | 323 | |
|
324 | 324 | def attr_matches(self, text): |
|
325 | 325 | """Compute matches when text contains a dot. |
|
326 | 326 | |
|
327 | 327 | Assuming the text is of the form NAME.NAME....[NAME], and is |
|
328 | 328 | evaluatable in self.namespace or self.global_namespace, it will be |
|
329 | 329 | evaluated and its attributes (as revealed by dir()) are used as |
|
330 | 330 | possible completions. (For class instances, class members are are |
|
331 | 331 | also considered.) |
|
332 | 332 | |
|
333 | 333 | WARNING: this can still invoke arbitrary C code, if an object |
|
334 | 334 | with a __getattr__ hook is evaluated. |
|
335 | 335 | |
|
336 | 336 | """ |
|
337 | 337 | |
|
338 | 338 | # Another option, seems to work great. Catches things like ''.<tab> |
|
339 | 339 | m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text) |
|
340 | 340 | |
|
341 | 341 | if m: |
|
342 | 342 | expr, attr = m.group(1, 3) |
|
343 | 343 | elif self.greedy: |
|
344 | 344 | m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer) |
|
345 | 345 | if not m2: |
|
346 | 346 | return [] |
|
347 | 347 | expr, attr = m2.group(1,2) |
|
348 | 348 | else: |
|
349 | 349 | return [] |
|
350 | 350 | |
|
351 | 351 | try: |
|
352 | 352 | obj = eval(expr, self.namespace) |
|
353 | 353 | except: |
|
354 | 354 | try: |
|
355 | 355 | obj = eval(expr, self.global_namespace) |
|
356 | 356 | except: |
|
357 | 357 | return [] |
|
358 | 358 | |
|
359 | 359 | if self.limit_to__all__ and hasattr(obj, '__all__'): |
|
360 | 360 | words = get__all__entries(obj) |
|
361 | 361 | else: |
|
362 | 362 | words = dir2(obj) |
|
363 | 363 | |
|
364 | 364 | try: |
|
365 | 365 | words = generics.complete_object(obj, words) |
|
366 | 366 | except TryNext: |
|
367 | 367 | pass |
|
368 | 368 | except Exception: |
|
369 | 369 | # Silence errors from completion function |
|
370 | 370 | #raise # dbg |
|
371 | 371 | pass |
|
372 | 372 | # Build match list to return |
|
373 | 373 | n = len(attr) |
|
374 | 374 | return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ] |
|
375 | 375 | |
|
376 | 376 | |
|
377 | 377 | def get__all__entries(obj): |
|
378 | 378 | """returns the strings in the __all__ attribute""" |
|
379 | 379 | try: |
|
380 | 380 | words = getattr(obj, '__all__') |
|
381 | 381 | except: |
|
382 | 382 | return [] |
|
383 | 383 | |
|
384 | 384 | return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)] |
|
385 | 385 | |
|
386 | 386 | |
|
387 | 387 | def match_dict_keys(keys, prefix, delims): |
|
388 | 388 | """Used by dict_key_matches, matching the prefix to a list of keys""" |
|
389 | 389 | if not prefix: |
|
390 | 390 | return None, 0, [repr(k) for k in keys |
|
391 | 391 | if isinstance(k, (string_types, bytes))] |
|
392 | 392 | quote_match = re.search('["\']', prefix) |
|
393 | 393 | quote = quote_match.group() |
|
394 | 394 | try: |
|
395 | 395 | prefix_str = eval(prefix + quote, {}) |
|
396 | 396 | except Exception: |
|
397 | 397 | return None, 0, [] |
|
398 | 398 | |
|
399 | 399 | pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$' |
|
400 | 400 | token_match = re.search(pattern, prefix, re.UNICODE) |
|
401 | 401 | token_start = token_match.start() |
|
402 | 402 | token_prefix = token_match.group() |
|
403 | 403 | |
|
404 | 404 | # TODO: support bytes in Py3k |
|
405 | 405 | matched = [] |
|
406 | 406 | for key in keys: |
|
407 | 407 | try: |
|
408 | 408 | if not key.startswith(prefix_str): |
|
409 | 409 | continue |
|
410 | 410 | except (AttributeError, TypeError, UnicodeError): |
|
411 | 411 | # Python 3+ TypeError on b'a'.startswith('a') or vice-versa |
|
412 | 412 | continue |
|
413 | 413 | |
|
414 | 414 | # reformat remainder of key to begin with prefix |
|
415 | 415 | rem = key[len(prefix_str):] |
|
416 | 416 | # force repr wrapped in ' |
|
417 | 417 | rem_repr = repr(rem + '"') |
|
418 | 418 | if rem_repr.startswith('u') and prefix[0] not in 'uU': |
|
419 | 419 | # Found key is unicode, but prefix is Py2 string. |
|
420 | 420 | # Therefore attempt to interpret key as string. |
|
421 | 421 | try: |
|
422 | 422 | rem_repr = repr(rem.encode('ascii') + '"') |
|
423 | 423 | except UnicodeEncodeError: |
|
424 | 424 | continue |
|
425 | 425 | |
|
426 | 426 | rem_repr = rem_repr[1 + rem_repr.index("'"):-2] |
|
427 | 427 | if quote == '"': |
|
428 | 428 | # The entered prefix is quoted with ", |
|
429 | 429 | # but the match is quoted with '. |
|
430 | 430 | # A contained " hence needs escaping for comparison: |
|
431 | 431 | rem_repr = rem_repr.replace('"', '\\"') |
|
432 | 432 | |
|
433 | 433 | # then reinsert prefix from start of token |
|
434 | 434 | matched.append('%s%s' % (token_prefix, rem_repr)) |
|
435 | 435 | return quote, token_start, matched |
|
436 | 436 | |
|
437 | 437 | |
|
438 | 438 | def _safe_isinstance(obj, module, class_name): |
|
439 | 439 | """Checks if obj is an instance of module.class_name if loaded |
|
440 | 440 | """ |
|
441 | 441 | return (module in sys.modules and |
|
442 | 442 | isinstance(obj, getattr(__import__(module), class_name))) |
|
443 | 443 | |
|
444 | 444 | |
|
445 | 445 | def back_unicode_name_matches(text): |
|
446 | 446 | u"""Match unicode characters back to unicode name |
|
447 | 447 | |
|
448 | 448 | This does β -> \\snowman |
|
449 | 449 | |
|
450 | 450 | Note that snowman is not a valid python3 combining character but will be expanded. |
|
451 | 451 | Though it will not recombine back to the snowman character by the completion machinery. |
|
452 | 452 | |
|
453 | 453 | This will not either back-complete standard sequences like \\n, \\b ... |
|
454 | 454 | |
|
455 | 455 | Used on Python 3 only. |
|
456 | 456 | """ |
|
457 | 457 | if len(text)<2: |
|
458 | 458 | return u'', () |
|
459 | 459 | maybe_slash = text[-2] |
|
460 | 460 | if maybe_slash != '\\': |
|
461 | 461 | return u'', () |
|
462 | 462 | |
|
463 | 463 | char = text[-1] |
|
464 | 464 | # no expand on quote for completion in strings. |
|
465 | 465 | # nor backcomplete standard ascii keys |
|
466 | 466 | if char in string.ascii_letters or char in ['"',"'"]: |
|
467 | 467 | return u'', () |
|
468 | 468 | try : |
|
469 | 469 | unic = unicodedata.name(char) |
|
470 | 470 | return '\\'+char,['\\'+unic] |
|
471 | 471 | except KeyError: |
|
472 | 472 | pass |
|
473 | 473 | return u'', () |
|
474 | 474 | |
|
475 | 475 | def back_latex_name_matches(text): |
|
476 | 476 | u"""Match latex characters back to unicode name |
|
477 | 477 | |
|
478 | 478 | This does ->\\sqrt |
|
479 | 479 | |
|
480 | 480 | Used on Python 3 only. |
|
481 | 481 | """ |
|
482 | 482 | if len(text)<2: |
|
483 | 483 | return u'', () |
|
484 | 484 | maybe_slash = text[-2] |
|
485 | 485 | if maybe_slash != '\\': |
|
486 | 486 | return u'', () |
|
487 | 487 | |
|
488 | 488 | |
|
489 | 489 | char = text[-1] |
|
490 | 490 | # no expand on quote for completion in strings. |
|
491 | 491 | # nor backcomplete standard ascii keys |
|
492 | 492 | if char in string.ascii_letters or char in ['"',"'"]: |
|
493 | 493 | return u'', () |
|
494 | 494 | try : |
|
495 | 495 | latex = reverse_latex_symbol[char] |
|
496 | 496 | # '\\' replace the \ as well |
|
497 | 497 | return '\\'+char,[latex] |
|
498 | 498 | except KeyError: |
|
499 | 499 | pass |
|
500 | 500 | return u'', () |
|
501 | 501 | |
|
502 | 502 | |
|
503 | 503 | class IPCompleter(Completer): |
|
504 | 504 | """Extension of the completer class with IPython-specific features""" |
|
505 | 505 | |
|
506 | 506 | @observe('greedy') |
|
507 | 507 | def _greedy_changed(self, change): |
|
508 | 508 | """update the splitter and readline delims when greedy is changed""" |
|
509 | 509 | if change['new']: |
|
510 | 510 | self.splitter.delims = GREEDY_DELIMS |
|
511 | 511 | else: |
|
512 | 512 | self.splitter.delims = DELIMS |
|
513 | 513 | |
|
514 | 514 | if self.readline: |
|
515 | 515 | self.readline.set_completer_delims(self.splitter.delims) |
|
516 | 516 | |
|
517 | 517 | merge_completions = Bool(True, |
|
518 | 518 | help="""Whether to merge completion results into a single list |
|
519 | 519 | |
|
520 | 520 | If False, only the completion results from the first non-empty |
|
521 | 521 | completer will be returned. |
|
522 | 522 | """ |
|
523 | 523 | ).tag(config=True) |
|
524 | 524 | omit__names = Enum((0,1,2), default_value=2, |
|
525 | 525 | help="""Instruct the completer to omit private method names |
|
526 | 526 | |
|
527 | 527 | Specifically, when completing on ``object.<tab>``. |
|
528 | 528 | |
|
529 | 529 | When 2 [default]: all names that start with '_' will be excluded. |
|
530 | 530 | |
|
531 | 531 | When 1: all 'magic' names (``__foo__``) will be excluded. |
|
532 | 532 | |
|
533 | 533 | When 0: nothing will be excluded. |
|
534 | 534 | """ |
|
535 | 535 | ).tag(config=True) |
|
536 | 536 | limit_to__all__ = Bool(False, |
|
537 | 537 | help=""" |
|
538 | 538 | DEPRECATED as of version 5.0. |
|
539 | 539 | |
|
540 | 540 | Instruct the completer to use __all__ for the completion |
|
541 | 541 | |
|
542 | 542 | Specifically, when completing on ``object.<tab>``. |
|
543 | 543 | |
|
544 | 544 | When True: only those names in obj.__all__ will be included. |
|
545 | 545 | |
|
546 | 546 | When False [default]: the __all__ attribute is ignored |
|
547 | 547 | """, |
|
548 | 548 | ).tag(config=True) |
|
549 | 549 | |
|
550 | 550 | def __init__(self, shell=None, namespace=None, global_namespace=None, |
|
551 | 551 | use_readline=True, config=None, **kwargs): |
|
552 | 552 | """IPCompleter() -> completer |
|
553 | 553 | |
|
554 | 554 | Return a completer object suitable for use by the readline library |
|
555 | 555 | via readline.set_completer(). |
|
556 | 556 | |
|
557 | 557 | Inputs: |
|
558 | 558 | |
|
559 | 559 | - shell: a pointer to the ipython shell itself. This is needed |
|
560 | 560 | because this completer knows about magic functions, and those can |
|
561 | 561 | only be accessed via the ipython instance. |
|
562 | 562 | |
|
563 | 563 | - namespace: an optional dict where completions are performed. |
|
564 | 564 | |
|
565 | 565 | - global_namespace: secondary optional dict for completions, to |
|
566 | 566 | handle cases (such as IPython embedded inside functions) where |
|
567 | 567 | both Python scopes are visible. |
|
568 | 568 | |
|
569 | 569 | use_readline : bool, optional |
|
570 | 570 | If true, use the readline library. This completer can still function |
|
571 | 571 | without readline, though in that case callers must provide some extra |
|
572 | 572 | information on each call about the current line.""" |
|
573 | 573 | |
|
574 | 574 | self.magic_escape = ESC_MAGIC |
|
575 | 575 | self.splitter = CompletionSplitter() |
|
576 | 576 | |
|
577 | 577 | # Readline configuration, only used by the rlcompleter method. |
|
578 | 578 | if use_readline: |
|
579 | 579 | # We store the right version of readline so that later code |
|
580 | 580 | import IPython.utils.rlineimpl as readline |
|
581 | 581 | self.readline = readline |
|
582 | 582 | else: |
|
583 | 583 | self.readline = None |
|
584 | 584 | |
|
585 | 585 | # _greedy_changed() depends on splitter and readline being defined: |
|
586 | 586 | Completer.__init__(self, namespace=namespace, global_namespace=global_namespace, |
|
587 | 587 | config=config, **kwargs) |
|
588 | 588 | |
|
589 | 589 | # List where completion matches will be stored |
|
590 | 590 | self.matches = [] |
|
591 | 591 | self.shell = shell |
|
592 | 592 | # Regexp to split filenames with spaces in them |
|
593 | 593 | self.space_name_re = re.compile(r'([^\\] )') |
|
594 | 594 | # Hold a local ref. to glob.glob for speed |
|
595 | 595 | self.glob = glob.glob |
|
596 | 596 | |
|
597 | 597 | # Determine if we are running on 'dumb' terminals, like (X)Emacs |
|
598 | 598 | # buffers, to avoid completion problems. |
|
599 | 599 | term = os.environ.get('TERM','xterm') |
|
600 | 600 | self.dumb_terminal = term in ['dumb','emacs'] |
|
601 | 601 | |
|
602 | 602 | # Special handling of backslashes needed in win32 platforms |
|
603 | 603 | if sys.platform == "win32": |
|
604 | 604 | self.clean_glob = self._clean_glob_win32 |
|
605 | 605 | else: |
|
606 | 606 | self.clean_glob = self._clean_glob |
|
607 | 607 | |
|
608 | 608 | #regexp to parse docstring for function signature |
|
609 | 609 | self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
610 | 610 | self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
611 | 611 | #use this if positional argument name is also needed |
|
612 | 612 | #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)') |
|
613 | 613 | |
|
614 | 614 | # All active matcher routines for completion |
|
615 | 615 | self.matchers = [ |
|
616 | 616 | self.python_matches, |
|
617 | 617 | self.file_matches, |
|
618 | 618 | self.magic_matches, |
|
619 | 619 | self.python_func_kw_matches, |
|
620 | 620 | self.dict_key_matches, |
|
621 | 621 | ] |
|
622 | 622 | |
|
623 | 623 | # This is set externally by InteractiveShell |
|
624 | 624 | self.custom_completers = None |
|
625 | 625 | |
|
626 | 626 | def all_completions(self, text): |
|
627 | 627 | """ |
|
628 | 628 | Wrapper around the complete method for the benefit of emacs. |
|
629 | 629 | """ |
|
630 | 630 | return self.complete(text)[1] |
|
631 | 631 | |
|
632 | 632 | def _clean_glob(self, text): |
|
633 | 633 | return self.glob("%s*" % text) |
|
634 | 634 | |
|
635 | 635 | def _clean_glob_win32(self,text): |
|
636 | 636 | return [f.replace("\\","/") |
|
637 | 637 | for f in self.glob("%s*" % text)] |
|
638 | 638 | |
|
639 | 639 | def file_matches(self, text): |
|
640 | 640 | """Match filenames, expanding ~USER type strings. |
|
641 | 641 | |
|
642 | 642 | Most of the seemingly convoluted logic in this completer is an |
|
643 | 643 | attempt to handle filenames with spaces in them. And yet it's not |
|
644 | 644 | quite perfect, because Python's readline doesn't expose all of the |
|
645 | 645 | GNU readline details needed for this to be done correctly. |
|
646 | 646 | |
|
647 | 647 | For a filename with a space in it, the printed completions will be |
|
648 | 648 | only the parts after what's already been typed (instead of the |
|
649 | 649 | full completions, as is normally done). I don't think with the |
|
650 | 650 | current (as of Python 2.3) Python readline it's possible to do |
|
651 | 651 | better.""" |
|
652 | 652 | |
|
653 | 653 | # chars that require escaping with backslash - i.e. chars |
|
654 | 654 | # that readline treats incorrectly as delimiters, but we |
|
655 | 655 | # don't want to treat as delimiters in filename matching |
|
656 | 656 | # when escaped with backslash |
|
657 | 657 | if text.startswith('!'): |
|
658 | 658 | text = text[1:] |
|
659 | 659 | text_prefix = u'!' |
|
660 | 660 | else: |
|
661 | 661 | text_prefix = u'' |
|
662 | 662 | |
|
663 | 663 | text_until_cursor = self.text_until_cursor |
|
664 | 664 | # track strings with open quotes |
|
665 | 665 | open_quotes = has_open_quotes(text_until_cursor) |
|
666 | 666 | |
|
667 | 667 | if '(' in text_until_cursor or '[' in text_until_cursor: |
|
668 | 668 | lsplit = text |
|
669 | 669 | else: |
|
670 | 670 | try: |
|
671 | 671 | # arg_split ~ shlex.split, but with unicode bugs fixed by us |
|
672 | 672 | lsplit = arg_split(text_until_cursor)[-1] |
|
673 | 673 | except ValueError: |
|
674 | 674 | # typically an unmatched ", or backslash without escaped char. |
|
675 | 675 | if open_quotes: |
|
676 | 676 | lsplit = text_until_cursor.split(open_quotes)[-1] |
|
677 | 677 | else: |
|
678 | 678 | return [] |
|
679 | 679 | except IndexError: |
|
680 | 680 | # tab pressed on empty line |
|
681 | 681 | lsplit = "" |
|
682 | 682 | |
|
683 | 683 | if not open_quotes and lsplit != protect_filename(lsplit): |
|
684 | 684 | # if protectables are found, do matching on the whole escaped name |
|
685 | 685 | has_protectables = True |
|
686 | 686 | text0,text = text,lsplit |
|
687 | 687 | else: |
|
688 | 688 | has_protectables = False |
|
689 | 689 | text = os.path.expanduser(text) |
|
690 | 690 | |
|
691 | 691 | if text == "": |
|
692 | 692 | return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")] |
|
693 | 693 | |
|
694 | 694 | # Compute the matches from the filesystem |
|
695 | 695 | m0 = self.clean_glob(text.replace('\\','')) |
|
696 | 696 | |
|
697 | 697 | if has_protectables: |
|
698 | 698 | # If we had protectables, we need to revert our changes to the |
|
699 | 699 | # beginning of filename so that we don't double-write the part |
|
700 | 700 | # of the filename we have so far |
|
701 | 701 | len_lsplit = len(lsplit) |
|
702 | 702 | matches = [text_prefix + text0 + |
|
703 | 703 | protect_filename(f[len_lsplit:]) for f in m0] |
|
704 | 704 | else: |
|
705 | 705 | if open_quotes: |
|
706 | 706 | # if we have a string with an open quote, we don't need to |
|
707 | 707 | # protect the names at all (and we _shouldn't_, as it |
|
708 | 708 | # would cause bugs when the filesystem call is made). |
|
709 | 709 | matches = m0 |
|
710 | 710 | else: |
|
711 | 711 | matches = [text_prefix + |
|
712 | 712 | protect_filename(f) for f in m0] |
|
713 | 713 | |
|
714 | 714 | # Mark directories in input list by appending '/' to their names. |
|
715 | 715 | return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches] |
|
716 | 716 | |
|
717 | 717 | def magic_matches(self, text): |
|
718 | 718 | """Match magics""" |
|
719 | 719 | # Get all shell magics now rather than statically, so magics loaded at |
|
720 | 720 | # runtime show up too. |
|
721 | 721 | lsm = self.shell.magics_manager.lsmagic() |
|
722 | 722 | line_magics = lsm['line'] |
|
723 | 723 | cell_magics = lsm['cell'] |
|
724 | 724 | pre = self.magic_escape |
|
725 | 725 | pre2 = pre+pre |
|
726 | 726 | |
|
727 | 727 | # Completion logic: |
|
728 | 728 | # - user gives %%: only do cell magics |
|
729 | 729 | # - user gives %: do both line and cell magics |
|
730 | 730 | # - no prefix: do both |
|
731 | 731 | # In other words, line magics are skipped if the user gives %% explicitly |
|
732 | 732 | bare_text = text.lstrip(pre) |
|
733 | 733 | comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)] |
|
734 | 734 | if not text.startswith(pre2): |
|
735 | 735 | comp += [ pre+m for m in line_magics if m.startswith(bare_text)] |
|
736 | 736 | return [cast_unicode_py2(c) for c in comp] |
|
737 | 737 | |
|
738 | 738 | |
|
739 | 739 | def python_matches(self, text): |
|
740 | 740 | """Match attributes or global python names""" |
|
741 | 741 | if "." in text: |
|
742 | 742 | try: |
|
743 | 743 | matches = self.attr_matches(text) |
|
744 | 744 | if text.endswith('.') and self.omit__names: |
|
745 | 745 | if self.omit__names == 1: |
|
746 | 746 | # true if txt is _not_ a __ name, false otherwise: |
|
747 | 747 | no__name = (lambda txt: |
|
748 | 748 | re.match(r'.*\.__.*?__',txt) is None) |
|
749 | 749 | else: |
|
750 | 750 | # true if txt is _not_ a _ name, false otherwise: |
|
751 | 751 | no__name = (lambda txt: |
|
752 | 752 | re.match(r'\._.*?',txt[txt.rindex('.'):]) is None) |
|
753 | 753 | matches = filter(no__name, matches) |
|
754 | 754 | except NameError: |
|
755 | 755 | # catches <undefined attributes>.<tab> |
|
756 | 756 | matches = [] |
|
757 | 757 | else: |
|
758 | 758 | matches = self.global_matches(text) |
|
759 | 759 | return matches |
|
760 | 760 | |
|
761 | 761 | def _default_arguments_from_docstring(self, doc): |
|
762 | 762 | """Parse the first line of docstring for call signature. |
|
763 | 763 | |
|
764 | 764 | Docstring should be of the form 'min(iterable[, key=func])\n'. |
|
765 | 765 | It can also parse cython docstring of the form |
|
766 | 766 | 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'. |
|
767 | 767 | """ |
|
768 | 768 | if doc is None: |
|
769 | 769 | return [] |
|
770 | 770 | |
|
771 | 771 | #care only the firstline |
|
772 | 772 | line = doc.lstrip().splitlines()[0] |
|
773 | 773 | |
|
774 | 774 | #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*') |
|
775 | 775 | #'min(iterable[, key=func])\n' -> 'iterable[, key=func]' |
|
776 | 776 | sig = self.docstring_sig_re.search(line) |
|
777 | 777 | if sig is None: |
|
778 | 778 | return [] |
|
779 | 779 | # iterable[, key=func]' -> ['iterable[' ,' key=func]'] |
|
780 | 780 | sig = sig.groups()[0].split(',') |
|
781 | 781 | ret = [] |
|
782 | 782 | for s in sig: |
|
783 | 783 | #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)') |
|
784 | 784 | ret += self.docstring_kwd_re.findall(s) |
|
785 | 785 | return ret |
|
786 | 786 | |
|
787 | 787 | def _default_arguments(self, obj): |
|
788 | 788 | """Return the list of default arguments of obj if it is callable, |
|
789 | 789 | or empty list otherwise.""" |
|
790 | 790 | call_obj = obj |
|
791 | 791 | ret = [] |
|
792 | 792 | if inspect.isbuiltin(obj): |
|
793 | 793 | pass |
|
794 | 794 | elif not (inspect.isfunction(obj) or inspect.ismethod(obj)): |
|
795 | 795 | if inspect.isclass(obj): |
|
796 | 796 | #for cython embededsignature=True the constructor docstring |
|
797 | 797 | #belongs to the object itself not __init__ |
|
798 | 798 | ret += self._default_arguments_from_docstring( |
|
799 | 799 | getattr(obj, '__doc__', '')) |
|
800 | 800 | # for classes, check for __init__,__new__ |
|
801 | 801 | call_obj = (getattr(obj, '__init__', None) or |
|
802 | 802 | getattr(obj, '__new__', None)) |
|
803 | 803 | # for all others, check if they are __call__able |
|
804 | 804 | elif hasattr(obj, '__call__'): |
|
805 | 805 | call_obj = obj.__call__ |
|
806 | 806 | ret += self._default_arguments_from_docstring( |
|
807 | 807 | getattr(call_obj, '__doc__', '')) |
|
808 | 808 | |
|
809 | 809 | if PY3: |
|
810 | 810 | _keeps = (inspect.Parameter.KEYWORD_ONLY, |
|
811 | 811 | inspect.Parameter.POSITIONAL_OR_KEYWORD) |
|
812 | 812 | signature = inspect.signature |
|
813 | 813 | else: |
|
814 | 814 | import IPython.utils.signatures |
|
815 | 815 | _keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY, |
|
816 | 816 | IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD) |
|
817 | 817 | signature = IPython.utils.signatures.signature |
|
818 | 818 | |
|
819 | 819 | try: |
|
820 | 820 | sig = signature(call_obj) |
|
821 | 821 | ret.extend(k for k, v in sig.parameters.items() if |
|
822 | 822 | v.kind in _keeps) |
|
823 | 823 | except ValueError: |
|
824 | 824 | pass |
|
825 | 825 | |
|
826 | 826 | return list(set(ret)) |
|
827 | 827 | |
|
828 | 828 | def python_func_kw_matches(self,text): |
|
829 | 829 | """Match named parameters (kwargs) of the last open function""" |
|
830 | 830 | |
|
831 | 831 | if "." in text: # a parameter cannot be dotted |
|
832 | 832 | return [] |
|
833 | 833 | try: regexp = self.__funcParamsRegex |
|
834 | 834 | except AttributeError: |
|
835 | 835 | regexp = self.__funcParamsRegex = re.compile(r''' |
|
836 | 836 | '.*?(?<!\\)' | # single quoted strings or |
|
837 | 837 | ".*?(?<!\\)" | # double quoted strings or |
|
838 | 838 | \w+ | # identifier |
|
839 | 839 | \S # other characters |
|
840 | 840 | ''', re.VERBOSE | re.DOTALL) |
|
841 | 841 | # 1. find the nearest identifier that comes before an unclosed |
|
842 | 842 | # parenthesis before the cursor |
|
843 | 843 | # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo" |
|
844 | 844 | tokens = regexp.findall(self.text_until_cursor) |
|
845 | 845 | tokens.reverse() |
|
846 | 846 | iterTokens = iter(tokens); openPar = 0 |
|
847 | 847 | |
|
848 | 848 | for token in iterTokens: |
|
849 | 849 | if token == ')': |
|
850 | 850 | openPar -= 1 |
|
851 | 851 | elif token == '(': |
|
852 | 852 | openPar += 1 |
|
853 | 853 | if openPar > 0: |
|
854 | 854 | # found the last unclosed parenthesis |
|
855 | 855 | break |
|
856 | 856 | else: |
|
857 | 857 | return [] |
|
858 | 858 | # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" ) |
|
859 | 859 | ids = [] |
|
860 | 860 | isId = re.compile(r'\w+$').match |
|
861 | 861 | |
|
862 | 862 | while True: |
|
863 | 863 | try: |
|
864 | 864 | ids.append(next(iterTokens)) |
|
865 | 865 | if not isId(ids[-1]): |
|
866 | 866 | ids.pop(); break |
|
867 | 867 | if not next(iterTokens) == '.': |
|
868 | 868 | break |
|
869 | 869 | except StopIteration: |
|
870 | 870 | break |
|
871 | 871 | # lookup the candidate callable matches either using global_matches |
|
872 | 872 | # or attr_matches for dotted names |
|
873 | 873 | if len(ids) == 1: |
|
874 | 874 | callableMatches = self.global_matches(ids[0]) |
|
875 | 875 | else: |
|
876 | 876 | callableMatches = self.attr_matches('.'.join(ids[::-1])) |
|
877 | 877 | argMatches = [] |
|
878 | 878 | for callableMatch in callableMatches: |
|
879 | 879 | try: |
|
880 | 880 | namedArgs = self._default_arguments(eval(callableMatch, |
|
881 | 881 | self.namespace)) |
|
882 | 882 | except: |
|
883 | 883 | continue |
|
884 | 884 | |
|
885 | 885 | for namedArg in namedArgs: |
|
886 | 886 | if namedArg.startswith(text): |
|
887 | 887 | argMatches.append(u"%s=" %namedArg) |
|
888 | 888 | return argMatches |
|
889 | 889 | |
|
890 | 890 | def dict_key_matches(self, text): |
|
891 | 891 | "Match string keys in a dictionary, after e.g. 'foo[' " |
|
892 | 892 | def get_keys(obj): |
|
893 | 893 | # Objects can define their own completions by defining an |
|
894 | 894 | # _ipy_key_completions_() method. |
|
895 | 895 | method = get_real_method(obj, '_ipython_key_completions_') |
|
896 | 896 | if method is not None: |
|
897 | 897 | return method() |
|
898 | 898 | |
|
899 | 899 | # Special case some common in-memory dict-like types |
|
900 | 900 | if isinstance(obj, dict) or\ |
|
901 | 901 | _safe_isinstance(obj, 'pandas', 'DataFrame'): |
|
902 | 902 | try: |
|
903 | 903 | return list(obj.keys()) |
|
904 | 904 | except Exception: |
|
905 | 905 | return [] |
|
906 | 906 | elif _safe_isinstance(obj, 'numpy', 'ndarray') or\ |
|
907 | 907 | _safe_isinstance(obj, 'numpy', 'void'): |
|
908 | 908 | return obj.dtype.names or [] |
|
909 | 909 | return [] |
|
910 | 910 | |
|
911 | 911 | try: |
|
912 | 912 | regexps = self.__dict_key_regexps |
|
913 | 913 | except AttributeError: |
|
914 | 914 | dict_key_re_fmt = r'''(?x) |
|
915 | 915 | ( # match dict-referring expression wrt greedy setting |
|
916 | 916 | %s |
|
917 | 917 | ) |
|
918 | 918 | \[ # open bracket |
|
919 | 919 | \s* # and optional whitespace |
|
920 | 920 | ([uUbB]? # string prefix (r not handled) |
|
921 | 921 | (?: # unclosed string |
|
922 | 922 | '(?:[^']|(?<!\\)\\')* |
|
923 | 923 | | |
|
924 | 924 | "(?:[^"]|(?<!\\)\\")* |
|
925 | 925 | ) |
|
926 | 926 | )? |
|
927 | 927 | $ |
|
928 | 928 | ''' |
|
929 | 929 | regexps = self.__dict_key_regexps = { |
|
930 | 930 | False: re.compile(dict_key_re_fmt % ''' |
|
931 | 931 | # identifiers separated by . |
|
932 | 932 | (?!\d)\w+ |
|
933 | 933 | (?:\.(?!\d)\w+)* |
|
934 | 934 | '''), |
|
935 | 935 | True: re.compile(dict_key_re_fmt % ''' |
|
936 | 936 | .+ |
|
937 | 937 | ''') |
|
938 | 938 | } |
|
939 | 939 | |
|
940 | 940 | match = regexps[self.greedy].search(self.text_until_cursor) |
|
941 | 941 | if match is None: |
|
942 | 942 | return [] |
|
943 | 943 | |
|
944 | 944 | expr, prefix = match.groups() |
|
945 | 945 | try: |
|
946 | 946 | obj = eval(expr, self.namespace) |
|
947 | 947 | except Exception: |
|
948 | 948 | try: |
|
949 | 949 | obj = eval(expr, self.global_namespace) |
|
950 | 950 | except Exception: |
|
951 | 951 | return [] |
|
952 | 952 | |
|
953 | 953 | keys = get_keys(obj) |
|
954 | 954 | if not keys: |
|
955 | 955 | return keys |
|
956 | 956 | closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims) |
|
957 | 957 | if not matches: |
|
958 | 958 | return matches |
|
959 | 959 | |
|
960 | 960 | # get the cursor position of |
|
961 | 961 | # - the text being completed |
|
962 | 962 | # - the start of the key text |
|
963 | 963 | # - the start of the completion |
|
964 | 964 | text_start = len(self.text_until_cursor) - len(text) |
|
965 | 965 | if prefix: |
|
966 | 966 | key_start = match.start(2) |
|
967 | 967 | completion_start = key_start + token_offset |
|
968 | 968 | else: |
|
969 | 969 | key_start = completion_start = match.end() |
|
970 | 970 | |
|
971 | 971 | # grab the leading prefix, to make sure all completions start with `text` |
|
972 | 972 | if text_start > key_start: |
|
973 | 973 | leading = '' |
|
974 | 974 | else: |
|
975 | 975 | leading = text[text_start:completion_start] |
|
976 | 976 | |
|
977 | 977 | # the index of the `[` character |
|
978 | 978 | bracket_idx = match.end(1) |
|
979 | 979 | |
|
980 | 980 | # append closing quote and bracket as appropriate |
|
981 | 981 | # this is *not* appropriate if the opening quote or bracket is outside |
|
982 | 982 | # the text given to this method |
|
983 | 983 | suf = '' |
|
984 | 984 | continuation = self.line_buffer[len(self.text_until_cursor):] |
|
985 | 985 | if key_start > text_start and closing_quote: |
|
986 | 986 | # quotes were opened inside text, maybe close them |
|
987 | 987 | if continuation.startswith(closing_quote): |
|
988 | 988 | continuation = continuation[len(closing_quote):] |
|
989 | 989 | else: |
|
990 | 990 | suf += closing_quote |
|
991 | 991 | if bracket_idx > text_start: |
|
992 | 992 | # brackets were opened inside text, maybe close them |
|
993 | 993 | if not continuation.startswith(']'): |
|
994 | 994 | suf += ']' |
|
995 | 995 | |
|
996 | 996 | return [leading + k + suf for k in matches] |
|
997 | 997 | |
|
998 | 998 | def unicode_name_matches(self, text): |
|
999 | 999 | u"""Match Latex-like syntax for unicode characters base |
|
1000 | 1000 | on the name of the character. |
|
1001 | 1001 | |
|
1002 | 1002 | This does \\GREEK SMALL LETTER ETA -> Ξ· |
|
1003 | 1003 | |
|
1004 | 1004 | Works only on valid python 3 identifier, or on combining characters that |
|
1005 | 1005 | will combine to form a valid identifier. |
|
1006 | 1006 | |
|
1007 | 1007 | Used on Python 3 only. |
|
1008 | 1008 | """ |
|
1009 | 1009 | slashpos = text.rfind('\\') |
|
1010 | 1010 | if slashpos > -1: |
|
1011 | 1011 | s = text[slashpos+1:] |
|
1012 | 1012 | try : |
|
1013 | 1013 | unic = unicodedata.lookup(s) |
|
1014 | 1014 | # allow combining chars |
|
1015 | 1015 | if ('a'+unic).isidentifier(): |
|
1016 | 1016 | return '\\'+s,[unic] |
|
1017 | 1017 | except KeyError: |
|
1018 | 1018 | pass |
|
1019 | 1019 | return u'', [] |
|
1020 | 1020 | |
|
1021 | 1021 | |
|
1022 | 1022 | |
|
1023 | 1023 | |
|
1024 | 1024 | def latex_matches(self, text): |
|
1025 | 1025 | u"""Match Latex syntax for unicode characters. |
|
1026 | 1026 | |
|
1027 | 1027 | This does both \\alp -> \\alpha and \\alpha -> Ξ± |
|
1028 | 1028 | |
|
1029 | 1029 | Used on Python 3 only. |
|
1030 | 1030 | """ |
|
1031 | 1031 | slashpos = text.rfind('\\') |
|
1032 | 1032 | if slashpos > -1: |
|
1033 | 1033 | s = text[slashpos:] |
|
1034 | 1034 | if s in latex_symbols: |
|
1035 | 1035 | # Try to complete a full latex symbol to unicode |
|
1036 | 1036 | # \\alpha -> Ξ± |
|
1037 | 1037 | return s, [latex_symbols[s]] |
|
1038 | 1038 | else: |
|
1039 | 1039 | # If a user has partially typed a latex symbol, give them |
|
1040 | 1040 | # a full list of options \al -> [\aleph, \alpha] |
|
1041 | 1041 | matches = [k for k in latex_symbols if k.startswith(s)] |
|
1042 | 1042 | return s, matches |
|
1043 | 1043 | return u'', [] |
|
1044 | 1044 | |
|
1045 | 1045 | def dispatch_custom_completer(self, text): |
|
1046 | 1046 | if not self.custom_completers: |
|
1047 | 1047 | return |
|
1048 | 1048 | |
|
1049 | 1049 | line = self.line_buffer |
|
1050 | 1050 | if not line.strip(): |
|
1051 | 1051 | return None |
|
1052 | 1052 | |
|
1053 | 1053 | # Create a little structure to pass all the relevant information about |
|
1054 | 1054 | # the current completion to any custom completer. |
|
1055 | 1055 | event = Bunch() |
|
1056 | 1056 | event.line = line |
|
1057 | 1057 | event.symbol = text |
|
1058 | 1058 | cmd = line.split(None,1)[0] |
|
1059 | 1059 | event.command = cmd |
|
1060 | 1060 | event.text_until_cursor = self.text_until_cursor |
|
1061 | 1061 | |
|
1062 | 1062 | # for foo etc, try also to find completer for %foo |
|
1063 | 1063 | if not cmd.startswith(self.magic_escape): |
|
1064 | 1064 | try_magic = self.custom_completers.s_matches( |
|
1065 | 1065 | self.magic_escape + cmd) |
|
1066 | 1066 | else: |
|
1067 | 1067 | try_magic = [] |
|
1068 | 1068 | |
|
1069 | 1069 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
1070 | 1070 | try_magic, |
|
1071 | 1071 | self.custom_completers.flat_matches(self.text_until_cursor)): |
|
1072 | 1072 | try: |
|
1073 | 1073 | res = c(event) |
|
1074 | 1074 | if res: |
|
1075 | 1075 | # first, try case sensitive match |
|
1076 | 1076 | withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)] |
|
1077 | 1077 | if withcase: |
|
1078 | 1078 | return withcase |
|
1079 | 1079 | # if none, then case insensitive ones are ok too |
|
1080 | 1080 | text_low = text.lower() |
|
1081 | 1081 | return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)] |
|
1082 | 1082 | except TryNext: |
|
1083 | 1083 | pass |
|
1084 | 1084 | |
|
1085 | 1085 | return None |
|
1086 | 1086 | |
|
1087 | 1087 | def complete(self, text=None, line_buffer=None, cursor_pos=None): |
|
1088 | 1088 | """Find completions for the given text and line context. |
|
1089 | 1089 | |
|
1090 | 1090 | Note that both the text and the line_buffer are optional, but at least |
|
1091 | 1091 | one of them must be given. |
|
1092 | 1092 | |
|
1093 | 1093 | Parameters |
|
1094 | 1094 | ---------- |
|
1095 | 1095 | text : string, optional |
|
1096 | 1096 | Text to perform the completion on. If not given, the line buffer |
|
1097 | 1097 | is split using the instance's CompletionSplitter object. |
|
1098 | 1098 | |
|
1099 | 1099 | line_buffer : string, optional |
|
1100 | 1100 | If not given, the completer attempts to obtain the current line |
|
1101 | 1101 | buffer via readline. This keyword allows clients which are |
|
1102 | 1102 | requesting for text completions in non-readline contexts to inform |
|
1103 | 1103 | the completer of the entire text. |
|
1104 | 1104 | |
|
1105 | 1105 | cursor_pos : int, optional |
|
1106 | 1106 | Index of the cursor in the full line buffer. Should be provided by |
|
1107 | 1107 | remote frontends where kernel has no access to frontend state. |
|
1108 | 1108 | |
|
1109 | 1109 | Returns |
|
1110 | 1110 | ------- |
|
1111 | 1111 | text : str |
|
1112 | 1112 | Text that was actually used in the completion. |
|
1113 | 1113 | |
|
1114 | 1114 | matches : list |
|
1115 | 1115 | A list of completion matches. |
|
1116 | 1116 | """ |
|
1117 | 1117 | # if the cursor position isn't given, the only sane assumption we can |
|
1118 | 1118 | # make is that it's at the end of the line (the common case) |
|
1119 | 1119 | if cursor_pos is None: |
|
1120 | 1120 | cursor_pos = len(line_buffer) if text is None else len(text) |
|
1121 | 1121 | |
|
1122 | 1122 | if PY3: |
|
1123 | 1123 | |
|
1124 | 1124 | base_text = text if not line_buffer else line_buffer[:cursor_pos] |
|
1125 | 1125 | latex_text, latex_matches = self.latex_matches(base_text) |
|
1126 | 1126 | if latex_matches: |
|
1127 | 1127 | return latex_text, latex_matches |
|
1128 | 1128 | name_text = '' |
|
1129 | 1129 | name_matches = [] |
|
1130 | 1130 | for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches): |
|
1131 | 1131 | name_text, name_matches = meth(base_text) |
|
1132 | 1132 | if name_text: |
|
1133 | 1133 | return name_text, name_matches |
|
1134 | 1134 | |
|
1135 | 1135 | # if text is either None or an empty string, rely on the line buffer |
|
1136 | 1136 | if not text: |
|
1137 | 1137 | text = self.splitter.split_line(line_buffer, cursor_pos) |
|
1138 | 1138 | |
|
1139 | 1139 | # If no line buffer is given, assume the input text is all there was |
|
1140 | 1140 | if line_buffer is None: |
|
1141 | 1141 | line_buffer = text |
|
1142 | 1142 | |
|
1143 | 1143 | self.line_buffer = line_buffer |
|
1144 | 1144 | self.text_until_cursor = self.line_buffer[:cursor_pos] |
|
1145 | 1145 | |
|
1146 | 1146 | # Start with a clean slate of completions |
|
1147 | 1147 | self.matches[:] = [] |
|
1148 | 1148 | custom_res = self.dispatch_custom_completer(text) |
|
1149 | 1149 | if custom_res is not None: |
|
1150 | 1150 | # did custom completers produce something? |
|
1151 | 1151 | self.matches = custom_res |
|
1152 | 1152 | else: |
|
1153 | 1153 | # Extend the list of completions with the results of each |
|
1154 | 1154 | # matcher, so we return results to the user from all |
|
1155 | 1155 | # namespaces. |
|
1156 | 1156 | if self.merge_completions: |
|
1157 | 1157 | self.matches = [] |
|
1158 | 1158 | for matcher in self.matchers: |
|
1159 | 1159 | try: |
|
1160 | 1160 | self.matches.extend(matcher(text)) |
|
1161 | 1161 | except: |
|
1162 | 1162 | # Show the ugly traceback if the matcher causes an |
|
1163 | 1163 | # exception, but do NOT crash the kernel! |
|
1164 | 1164 | sys.excepthook(*sys.exc_info()) |
|
1165 | 1165 | else: |
|
1166 | 1166 | for matcher in self.matchers: |
|
1167 | 1167 | self.matches = matcher(text) |
|
1168 | 1168 | if self.matches: |
|
1169 | 1169 | break |
|
1170 | 1170 | # FIXME: we should extend our api to return a dict with completions for |
|
1171 | 1171 | # different types of objects. The rlcomplete() method could then |
|
1172 | 1172 | # simply collapse the dict into a list for readline, but we'd have |
|
1173 | 1173 | # richer completion semantics in other evironments. |
|
1174 | 1174 | self.matches = sorted(set(self.matches), key=completions_sorting_key) |
|
1175 | 1175 | |
|
1176 | 1176 | return text, self.matches |
@@ -1,73 +1,74 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """Miscellaneous context managers. |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | import warnings |
|
6 | 6 | |
|
7 | 7 | # Copyright (c) IPython Development Team. |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | |
|
10 | 10 | class preserve_keys(object): |
|
11 | 11 | """Preserve a set of keys in a dictionary. |
|
12 | 12 | |
|
13 | 13 | Upon entering the context manager the current values of the keys |
|
14 | 14 | will be saved. Upon exiting, the dictionary will be updated to |
|
15 | 15 | restore the original value of the preserved keys. Preserved keys |
|
16 | 16 | which did not exist when entering the context manager will be |
|
17 | 17 | deleted. |
|
18 | 18 | |
|
19 | 19 | Examples |
|
20 | 20 | -------- |
|
21 | 21 | |
|
22 | 22 | >>> d = {'a': 1, 'b': 2, 'c': 3} |
|
23 | 23 | >>> with preserve_keys(d, 'b', 'c', 'd'): |
|
24 | 24 | ... del d['a'] |
|
25 | 25 | ... del d['b'] # will be reset to 2 |
|
26 | 26 | ... d['c'] = None # will be reset to 3 |
|
27 | 27 | ... d['d'] = 4 # will be deleted |
|
28 | 28 | ... d['e'] = 5 |
|
29 | 29 | ... print(sorted(d.items())) |
|
30 | 30 | ... |
|
31 | 31 | [('c', None), ('d', 4), ('e', 5)] |
|
32 | 32 | >>> print(sorted(d.items())) |
|
33 | 33 | [('b', 2), ('c', 3), ('e', 5)] |
|
34 | 34 | """ |
|
35 | 35 | |
|
36 | 36 | def __init__(self, dictionary, *keys): |
|
37 | 37 | self.dictionary = dictionary |
|
38 | 38 | self.keys = keys |
|
39 | 39 | |
|
40 | 40 | def __enter__(self): |
|
41 | 41 | # Actions to perform upon exiting. |
|
42 | 42 | to_delete = [] |
|
43 | 43 | to_update = {} |
|
44 | 44 | |
|
45 | 45 | d = self.dictionary |
|
46 | 46 | for k in self.keys: |
|
47 | 47 | if k in d: |
|
48 | 48 | to_update[k] = d[k] |
|
49 | 49 | else: |
|
50 | 50 | to_delete.append(k) |
|
51 | 51 | |
|
52 | 52 | self.to_delete = to_delete |
|
53 | 53 | self.to_update = to_update |
|
54 | 54 | |
|
55 | 55 | def __exit__(self, *exc_info): |
|
56 | 56 | d = self.dictionary |
|
57 | 57 | |
|
58 | 58 | for k in self.to_delete: |
|
59 | 59 | d.pop(k, None) |
|
60 | 60 | d.update(self.to_update) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | class NoOpContext(object): |
|
64 | 64 | """ |
|
65 | 65 | Deprecated |
|
66 | 66 | |
|
67 | 67 | Context manager that does nothing.""" |
|
68 | 68 | |
|
69 | 69 | def __init__(self): |
|
70 |
warnings.warn("""NoOpContext is deprecated since IPython 5.0 """ |
|
|
70 | warnings.warn("""NoOpContext is deprecated since IPython 5.0 """, | |
|
71 | DeprecationWarning, stacklevel=2) | |
|
71 | 72 | |
|
72 | 73 | def __enter__(self): pass |
|
73 | 74 | def __exit__(self, type, value, traceback): pass |
General Comments 0
You need to be logged in to leave comments.
Login now