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