##// END OF EJS Templates
Limit number of completions returned
Thomas Kluyver -
Show More
@@ -1,2017 +1,2021 b''
1 1 """Completion for IPython.
2 2
3 3 This module started as fork of the rlcompleter module in the Python standard
4 4 library. The original enhancements made to rlcompleter have been sent
5 5 upstream and were accepted as of Python 2.3,
6 6
7 7 This module now support a wide variety of completion mechanism both available
8 8 for normal classic Python code, as well as completer for IPython specific
9 9 Syntax like magics.
10 10
11 11 Latex and Unicode completion
12 12 ============================
13 13
14 14 IPython and compatible frontends not only can complete your code, but can help
15 15 you to input a wide range of characters. In particular we allow you to insert
16 16 a unicode character using the tab completion mechanism.
17 17
18 18 Forward latex/unicode completion
19 19 --------------------------------
20 20
21 21 Forward completion allows you to easily type a unicode character using its latex
22 22 name, or unicode long description. To do so type a backslash follow by the
23 23 relevant name and press tab:
24 24
25 25
26 26 Using latex completion:
27 27
28 28 .. code::
29 29
30 30 \\alpha<tab>
31 31 Ξ±
32 32
33 33 or using unicode completion:
34 34
35 35
36 36 .. code::
37 37
38 38 \\greek small letter alpha<tab>
39 39 Ξ±
40 40
41 41
42 42 Only valid Python identifiers will complete. Combining characters (like arrow or
43 43 dots) are also available, unlike latex they need to be put after the their
44 44 counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
45 45
46 46 Some browsers are known to display combining characters incorrectly.
47 47
48 48 Backward latex completion
49 49 -------------------------
50 50
51 51 It is sometime challenging to know how to type a character, if you are using
52 52 IPython, or any compatible frontend you can prepend backslash to the character
53 53 and press `<tab>` to expand it to its latex form.
54 54
55 55 .. code::
56 56
57 57 \\Ξ±<tab>
58 58 \\alpha
59 59
60 60
61 61 Both forward and backward completions can be deactivated by setting the
62 62 ``Completer.backslash_combining_completions`` option to ``False``.
63 63
64 64
65 65 Experimental
66 66 ============
67 67
68 68 Starting with IPython 6.0, this module can make use of the Jedi library to
69 69 generate completions both using static analysis of the code, and dynamically
70 70 inspecting multiple namespaces. The APIs attached to this new mechanism is
71 71 unstable and will raise unless use in an :any:`provisionalcompleter` context
72 72 manager.
73 73
74 74 You will find that the following are experimental:
75 75
76 76 - :any:`provisionalcompleter`
77 77 - :any:`IPCompleter.completions`
78 78 - :any:`Completion`
79 79 - :any:`rectify_completions`
80 80
81 81 .. note::
82 82
83 83 better name for :any:`rectify_completions` ?
84 84
85 85 We welcome any feedback on these new API, and we also encourage you to try this
86 86 module in debug mode (start IPython with ``--Completer.debug=True``) in order
87 87 to have extra logging information is :any:`jedi` is crashing, or if current
88 88 IPython completer pending deprecations are returning results not yet handled
89 89 by :any:`jedi`
90 90
91 91 Using Jedi for tab completion allow snippets like the following to work without
92 92 having to execute any code:
93 93
94 94 >>> myvar = ['hello', 42]
95 95 ... myvar[1].bi<tab>
96 96
97 97 Tab completion will be able to infer that ``myvar[1]`` is a real number without
98 98 executing any code unlike the previously available ``IPCompleter.greedy``
99 99 option.
100 100
101 101 Be sure to update :any:`jedi` to the latest stable version or to try the
102 102 current development version to get better completions.
103 103 """
104 104
105 105
106 106 # Copyright (c) IPython Development Team.
107 107 # Distributed under the terms of the Modified BSD License.
108 108 #
109 109 # Some of this code originated from rlcompleter in the Python standard library
110 110 # Copyright (C) 2001 Python Software Foundation, www.python.org
111 111
112 112
113 113 import __main__
114 114 import builtins as builtin_mod
115 115 import glob
116 116 import time
117 117 import inspect
118 118 import itertools
119 119 import keyword
120 120 import os
121 121 import re
122 122 import sys
123 123 import unicodedata
124 124 import string
125 125 import warnings
126 126
127 127 from contextlib import contextmanager
128 128 from importlib import import_module
129 129 from typing import Iterator, List, Tuple, Iterable, Union
130 130 from types import SimpleNamespace
131 131
132 132 from traitlets.config.configurable import Configurable
133 133 from IPython.core.error import TryNext
134 134 from IPython.core.inputsplitter import ESC_MAGIC
135 135 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
136 136 from IPython.core.oinspect import InspectColors
137 137 from IPython.utils import generics
138 138 from IPython.utils.dir2 import dir2, get_real_method
139 139 from IPython.utils.process import arg_split
140 140 from traitlets import Bool, Enum, observe, Int
141 141
142 142 # skip module docstests
143 143 skip_doctest = True
144 144
145 145 try:
146 146 import jedi
147 147 import jedi.api.helpers
148 148 import jedi.api.classes
149 149 JEDI_INSTALLED = True
150 150 except ImportError:
151 151 JEDI_INSTALLED = False
152 152 #-----------------------------------------------------------------------------
153 153 # Globals
154 154 #-----------------------------------------------------------------------------
155 155
156 156 # Public API
157 157 __all__ = ['Completer','IPCompleter']
158 158
159 159 if sys.platform == 'win32':
160 160 PROTECTABLES = ' '
161 161 else:
162 162 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
163 163
164 # Protect against returning an enormous number of completions which the frontend
165 # may have trouble processing.
166 MATCHES_LIMIT = 500
164 167
165 168 _deprecation_readline_sentinel = object()
166 169
167 170
168 171 class ProvisionalCompleterWarning(FutureWarning):
169 172 """
170 173 Exception raise by an experimental feature in this module.
171 174
172 175 Wrap code in :any:`provisionalcompleter` context manager if you
173 176 are certain you want to use an unstable feature.
174 177 """
175 178 pass
176 179
177 180 warnings.filterwarnings('error', category=ProvisionalCompleterWarning)
178 181
179 182 @contextmanager
180 183 def provisionalcompleter(action='ignore'):
181 184 """
182 185
183 186
184 187 This contest manager has to be used in any place where unstable completer
185 188 behavior and API may be called.
186 189
187 190 >>> with provisionalcompleter():
188 191 ... completer.do_experimetal_things() # works
189 192
190 193 >>> completer.do_experimental_things() # raises.
191 194
192 195 .. note:: Unstable
193 196
194 197 By using this context manager you agree that the API in use may change
195 198 without warning, and that you won't complain if they do so.
196 199
197 200 You also understand that if the API is not to you liking you should report
198 201 a bug to explain your use case upstream and improve the API and will loose
199 202 credibility if you complain after the API is make stable.
200 203
201 204 We'll be happy to get your feedback , feature request and improvement on
202 205 any of the unstable APIs !
203 206 """
204 207 with warnings.catch_warnings():
205 208 warnings.filterwarnings(action, category=ProvisionalCompleterWarning)
206 209 yield
207 210
208 211
209 212 def has_open_quotes(s):
210 213 """Return whether a string has open quotes.
211 214
212 215 This simply counts whether the number of quote characters of either type in
213 216 the string is odd.
214 217
215 218 Returns
216 219 -------
217 220 If there is an open quote, the quote character is returned. Else, return
218 221 False.
219 222 """
220 223 # We check " first, then ', so complex cases with nested quotes will get
221 224 # the " to take precedence.
222 225 if s.count('"') % 2:
223 226 return '"'
224 227 elif s.count("'") % 2:
225 228 return "'"
226 229 else:
227 230 return False
228 231
229 232
230 233 def protect_filename(s, protectables=PROTECTABLES):
231 234 """Escape a string to protect certain characters."""
232 235 if set(s) & set(protectables):
233 236 if sys.platform == "win32":
234 237 return '"' + s + '"'
235 238 else:
236 239 return "".join(("\\" + c if c in protectables else c) for c in s)
237 240 else:
238 241 return s
239 242
240 243
241 244 def expand_user(path:str) -> Tuple[str, bool, str]:
242 245 """Expand ``~``-style usernames in strings.
243 246
244 247 This is similar to :func:`os.path.expanduser`, but it computes and returns
245 248 extra information that will be useful if the input was being used in
246 249 computing completions, and you wish to return the completions with the
247 250 original '~' instead of its expanded value.
248 251
249 252 Parameters
250 253 ----------
251 254 path : str
252 255 String to be expanded. If no ~ is present, the output is the same as the
253 256 input.
254 257
255 258 Returns
256 259 -------
257 260 newpath : str
258 261 Result of ~ expansion in the input path.
259 262 tilde_expand : bool
260 263 Whether any expansion was performed or not.
261 264 tilde_val : str
262 265 The value that ~ was replaced with.
263 266 """
264 267 # Default values
265 268 tilde_expand = False
266 269 tilde_val = ''
267 270 newpath = path
268 271
269 272 if path.startswith('~'):
270 273 tilde_expand = True
271 274 rest = len(path)-1
272 275 newpath = os.path.expanduser(path)
273 276 if rest:
274 277 tilde_val = newpath[:-rest]
275 278 else:
276 279 tilde_val = newpath
277 280
278 281 return newpath, tilde_expand, tilde_val
279 282
280 283
281 284 def compress_user(path:str, tilde_expand:bool, tilde_val:str) -> str:
282 285 """Does the opposite of expand_user, with its outputs.
283 286 """
284 287 if tilde_expand:
285 288 return path.replace(tilde_val, '~')
286 289 else:
287 290 return path
288 291
289 292
290 293 def completions_sorting_key(word):
291 294 """key for sorting completions
292 295
293 296 This does several things:
294 297
295 298 - Lowercase all completions, so they are sorted alphabetically with
296 299 upper and lower case words mingled
297 300 - Demote any completions starting with underscores to the end
298 301 - Insert any %magic and %%cellmagic completions in the alphabetical order
299 302 by their name
300 303 """
301 304 # Case insensitive sort
302 305 word = word.lower()
303 306
304 307 prio1, prio2 = 0, 0
305 308
306 309 if word.startswith('__'):
307 310 prio1 = 2
308 311 elif word.startswith('_'):
309 312 prio1 = 1
310 313
311 314 if word.endswith('='):
312 315 prio1 = -1
313 316
314 317 if word.startswith('%%'):
315 318 # If there's another % in there, this is something else, so leave it alone
316 319 if not "%" in word[2:]:
317 320 word = word[2:]
318 321 prio2 = 2
319 322 elif word.startswith('%'):
320 323 if not "%" in word[1:]:
321 324 word = word[1:]
322 325 prio2 = 1
323 326
324 327 return prio1, word, prio2
325 328
326 329
327 330 class _FakeJediCompletion:
328 331 """
329 332 This is a workaround to communicate to the UI that Jedi has crashed and to
330 333 report a bug. Will be used only id :any:`IPCompleter.debug` is set to true.
331 334
332 335 Added in IPython 6.0 so should likely be removed for 7.0
333 336
334 337 """
335 338
336 339 def __init__(self, name):
337 340
338 341 self.name = name
339 342 self.complete = name
340 343 self.type = 'crashed'
341 344 self.name_with_symbols = name
342 345 self.signature = ''
343 346 self._origin = 'fake'
344 347
345 348 def __repr__(self):
346 349 return '<Fake completion object jedi has crashed>'
347 350
348 351
349 352 class Completion:
350 353 """
351 354 Completion object used and return by IPython completers.
352 355
353 356 .. warning:: Unstable
354 357
355 358 This function is unstable, API may change without warning.
356 359 It will also raise unless use in proper context manager.
357 360
358 361 This act as a middle ground :any:`Completion` object between the
359 362 :any:`jedi.api.classes.Completion` object and the Prompt Toolkit completion
360 363 object. While Jedi need a lot of information about evaluator and how the
361 364 code should be ran/inspected, PromptToolkit (and other frontend) mostly
362 365 need user facing information.
363 366
364 367 - Which range should be replaced replaced by what.
365 368 - Some metadata (like completion type), or meta informations to displayed to
366 369 the use user.
367 370
368 371 For debugging purpose we can also store the origin of the completion (``jedi``,
369 372 ``IPython.python_matches``, ``IPython.magics_matches``...).
370 373 """
371 374
372 375 __slots__ = ['start', 'end', 'text', 'type', 'signature', '_origin']
373 376
374 377 def __init__(self, start: int, end: int, text: str, *, type: str=None, _origin='', signature='') -> None:
375 378 warnings.warn("``Completion`` is a provisional API (as of IPython 6.0). "
376 379 "It may change without warnings. "
377 380 "Use in corresponding context manager.",
378 381 category=ProvisionalCompleterWarning, stacklevel=2)
379 382
380 383 self.start = start
381 384 self.end = end
382 385 self.text = text
383 386 self.type = type
384 387 self.signature = signature
385 388 self._origin = _origin
386 389
387 390 def __repr__(self):
388 391 return '<Completion start=%s end=%s text=%r type=%r, signature=%r,>' % \
389 392 (self.start, self.end, self.text, self.type or '?', self.signature or '?')
390 393
391 394 def __eq__(self, other)->Bool:
392 395 """
393 396 Equality and hash do not hash the type (as some completer may not be
394 397 able to infer the type), but are use to (partially) de-duplicate
395 398 completion.
396 399
397 400 Completely de-duplicating completion is a bit tricker that just
398 401 comparing as it depends on surrounding text, which Completions are not
399 402 aware of.
400 403 """
401 404 return self.start == other.start and \
402 405 self.end == other.end and \
403 406 self.text == other.text
404 407
405 408 def __hash__(self):
406 409 return hash((self.start, self.end, self.text))
407 410
408 411
409 412 _IC = Iterable[Completion]
410 413
411 414
412 415 def _deduplicate_completions(text: str, completions: _IC)-> _IC:
413 416 """
414 417 Deduplicate a set of completions.
415 418
416 419 .. warning:: Unstable
417 420
418 421 This function is unstable, API may change without warning.
419 422
420 423 Parameters
421 424 ----------
422 425 text: str
423 426 text that should be completed.
424 427 completions: Iterator[Completion]
425 428 iterator over the completions to deduplicate
426 429
427 430 Yields
428 431 ------
429 432 `Completions` objects
430 433
431 434
432 435 Completions coming from multiple sources, may be different but end up having
433 436 the same effect when applied to ``text``. If this is the case, this will
434 437 consider completions as equal and only emit the first encountered.
435 438
436 439 Not folded in `completions()` yet for debugging purpose, and to detect when
437 440 the IPython completer does return things that Jedi does not, but should be
438 441 at some point.
439 442 """
440 443 completions = list(completions)
441 444 if not completions:
442 445 return
443 446
444 447 new_start = min(c.start for c in completions)
445 448 new_end = max(c.end for c in completions)
446 449
447 450 seen = set()
448 451 for c in completions:
449 452 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
450 453 if new_text not in seen:
451 454 yield c
452 455 seen.add(new_text)
453 456
454 457
455 458 def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
456 459 """
457 460 Rectify a set of completions to all have the same ``start`` and ``end``
458 461
459 462 .. warning:: Unstable
460 463
461 464 This function is unstable, API may change without warning.
462 465 It will also raise unless use in proper context manager.
463 466
464 467 Parameters
465 468 ----------
466 469 text: str
467 470 text that should be completed.
468 471 completions: Iterator[Completion]
469 472 iterator over the completions to rectify
470 473
471 474
472 475 :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
473 476 the Jupyter Protocol requires them to behave like so. This will readjust
474 477 the completion to have the same ``start`` and ``end`` by padding both
475 478 extremities with surrounding text.
476 479
477 480 During stabilisation should support a ``_debug`` option to log which
478 481 completion are return by the IPython completer and not found in Jedi in
479 482 order to make upstream bug report.
480 483 """
481 484 warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
482 485 "It may change without warnings. "
483 486 "Use in corresponding context manager.",
484 487 category=ProvisionalCompleterWarning, stacklevel=2)
485 488
486 489 completions = list(completions)
487 490 if not completions:
488 491 return
489 492 starts = (c.start for c in completions)
490 493 ends = (c.end for c in completions)
491 494
492 495 new_start = min(starts)
493 496 new_end = max(ends)
494 497
495 498 seen_jedi = set()
496 499 seen_python_matches = set()
497 500 for c in completions:
498 501 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
499 502 if c._origin == 'jedi':
500 503 seen_jedi.add(new_text)
501 504 elif c._origin == 'IPCompleter.python_matches':
502 505 seen_python_matches.add(new_text)
503 506 yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
504 507 diff = seen_python_matches.difference(seen_jedi)
505 508 if diff and _debug:
506 509 print('IPython.python matches have extras:', diff)
507 510
508 511
509 512 if sys.platform == 'win32':
510 513 DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
511 514 else:
512 515 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
513 516
514 517 GREEDY_DELIMS = ' =\r\n'
515 518
516 519
517 520 class CompletionSplitter(object):
518 521 """An object to split an input line in a manner similar to readline.
519 522
520 523 By having our own implementation, we can expose readline-like completion in
521 524 a uniform manner to all frontends. This object only needs to be given the
522 525 line of text to be split and the cursor position on said line, and it
523 526 returns the 'word' to be completed on at the cursor after splitting the
524 527 entire line.
525 528
526 529 What characters are used as splitting delimiters can be controlled by
527 530 setting the ``delims`` attribute (this is a property that internally
528 531 automatically builds the necessary regular expression)"""
529 532
530 533 # Private interface
531 534
532 535 # A string of delimiter characters. The default value makes sense for
533 536 # IPython's most typical usage patterns.
534 537 _delims = DELIMS
535 538
536 539 # The expression (a normal string) to be compiled into a regular expression
537 540 # for actual splitting. We store it as an attribute mostly for ease of
538 541 # debugging, since this type of code can be so tricky to debug.
539 542 _delim_expr = None
540 543
541 544 # The regular expression that does the actual splitting
542 545 _delim_re = None
543 546
544 547 def __init__(self, delims=None):
545 548 delims = CompletionSplitter._delims if delims is None else delims
546 549 self.delims = delims
547 550
548 551 @property
549 552 def delims(self):
550 553 """Return the string of delimiter characters."""
551 554 return self._delims
552 555
553 556 @delims.setter
554 557 def delims(self, delims):
555 558 """Set the delimiters for line splitting."""
556 559 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
557 560 self._delim_re = re.compile(expr)
558 561 self._delims = delims
559 562 self._delim_expr = expr
560 563
561 564 def split_line(self, line, cursor_pos=None):
562 565 """Split a line of text with a cursor at the given position.
563 566 """
564 567 l = line if cursor_pos is None else line[:cursor_pos]
565 568 return self._delim_re.split(l)[-1]
566 569
567 570
568 571
569 572 class Completer(Configurable):
570 573
571 574 greedy = Bool(False,
572 575 help="""Activate greedy completion
573 576 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
574 577
575 578 This will enable completion on elements of lists, results of function calls, etc.,
576 579 but can be unsafe because the code is actually evaluated on TAB.
577 580 """
578 581 ).tag(config=True)
579 582
580 583 use_jedi = Bool(default_value=JEDI_INSTALLED,
581 584 help="Experimental: Use Jedi to generate autocompletions. "
582 585 "Default to True if jedi is installed").tag(config=True)
583 586
584 587 jedi_compute_type_timeout = Int(default_value=400,
585 588 help="""Experimental: restrict time (in milliseconds) during which Jedi can compute types.
586 589 Set to 0 to stop computing types. Non-zero value lower than 100ms may hurt
587 590 performance by preventing jedi to build its cache.
588 591 """).tag(config=True)
589 592
590 593 debug = Bool(default_value=False,
591 594 help='Enable debug for the Completer. Mostly print extra '
592 595 'information for experimental jedi integration.')\
593 596 .tag(config=True)
594 597
595 598 backslash_combining_completions = Bool(True,
596 599 help="Enable unicode completions, e.g. \\alpha<tab> . "
597 600 "Includes completion of latex commands, unicode names, and expanding "
598 601 "unicode characters back to latex commands.").tag(config=True)
599 602
600 603
601 604
602 605 def __init__(self, namespace=None, global_namespace=None, **kwargs):
603 606 """Create a new completer for the command line.
604 607
605 608 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
606 609
607 610 If unspecified, the default namespace where completions are performed
608 611 is __main__ (technically, __main__.__dict__). Namespaces should be
609 612 given as dictionaries.
610 613
611 614 An optional second namespace can be given. This allows the completer
612 615 to handle cases where both the local and global scopes need to be
613 616 distinguished.
614 617 """
615 618
616 619 # Don't bind to namespace quite yet, but flag whether the user wants a
617 620 # specific namespace or to use __main__.__dict__. This will allow us
618 621 # to bind to __main__.__dict__ at completion time, not now.
619 622 if namespace is None:
620 623 self.use_main_ns = True
621 624 else:
622 625 self.use_main_ns = False
623 626 self.namespace = namespace
624 627
625 628 # The global namespace, if given, can be bound directly
626 629 if global_namespace is None:
627 630 self.global_namespace = {}
628 631 else:
629 632 self.global_namespace = global_namespace
630 633
631 634 super(Completer, self).__init__(**kwargs)
632 635
633 636 def complete(self, text, state):
634 637 """Return the next possible completion for 'text'.
635 638
636 639 This is called successively with state == 0, 1, 2, ... until it
637 640 returns None. The completion should begin with 'text'.
638 641
639 642 """
640 643 if self.use_main_ns:
641 644 self.namespace = __main__.__dict__
642 645
643 646 if state == 0:
644 647 if "." in text:
645 648 self.matches = self.attr_matches(text)
646 649 else:
647 650 self.matches = self.global_matches(text)
648 651 try:
649 652 return self.matches[state]
650 653 except IndexError:
651 654 return None
652 655
653 656 def global_matches(self, text):
654 657 """Compute matches when text is a simple name.
655 658
656 659 Return a list of all keywords, built-in functions and names currently
657 660 defined in self.namespace or self.global_namespace that match.
658 661
659 662 """
660 663 matches = []
661 664 match_append = matches.append
662 665 n = len(text)
663 666 for lst in [keyword.kwlist,
664 667 builtin_mod.__dict__.keys(),
665 668 self.namespace.keys(),
666 669 self.global_namespace.keys()]:
667 670 for word in lst:
668 671 if word[:n] == text and word != "__builtins__":
669 672 match_append(word)
670 673
671 674 snake_case_re = re.compile(r"[^_]+(_[^_]+)+?\Z")
672 675 for lst in [self.namespace.keys(),
673 676 self.global_namespace.keys()]:
674 677 shortened = {"_".join([sub[0] for sub in word.split('_')]) : word
675 678 for word in lst if snake_case_re.match(word)}
676 679 for word in shortened.keys():
677 680 if word[:n] == text and word != "__builtins__":
678 681 match_append(shortened[word])
679 682 return matches
680 683
681 684 def attr_matches(self, text):
682 685 """Compute matches when text contains a dot.
683 686
684 687 Assuming the text is of the form NAME.NAME....[NAME], and is
685 688 evaluatable in self.namespace or self.global_namespace, it will be
686 689 evaluated and its attributes (as revealed by dir()) are used as
687 690 possible completions. (For class instances, class members are are
688 691 also considered.)
689 692
690 693 WARNING: this can still invoke arbitrary C code, if an object
691 694 with a __getattr__ hook is evaluated.
692 695
693 696 """
694 697
695 698 # Another option, seems to work great. Catches things like ''.<tab>
696 699 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
697 700
698 701 if m:
699 702 expr, attr = m.group(1, 3)
700 703 elif self.greedy:
701 704 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
702 705 if not m2:
703 706 return []
704 707 expr, attr = m2.group(1,2)
705 708 else:
706 709 return []
707 710
708 711 try:
709 712 obj = eval(expr, self.namespace)
710 713 except:
711 714 try:
712 715 obj = eval(expr, self.global_namespace)
713 716 except:
714 717 return []
715 718
716 719 if self.limit_to__all__ and hasattr(obj, '__all__'):
717 720 words = get__all__entries(obj)
718 721 else:
719 722 words = dir2(obj)
720 723
721 724 try:
722 725 words = generics.complete_object(obj, words)
723 726 except TryNext:
724 727 pass
725 728 except AssertionError:
726 729 raise
727 730 except Exception:
728 731 # Silence errors from completion function
729 732 #raise # dbg
730 733 pass
731 734 # Build match list to return
732 735 n = len(attr)
733 736 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
734 737
735 738
736 739 def get__all__entries(obj):
737 740 """returns the strings in the __all__ attribute"""
738 741 try:
739 742 words = getattr(obj, '__all__')
740 743 except:
741 744 return []
742 745
743 746 return [w for w in words if isinstance(w, str)]
744 747
745 748
746 749 def match_dict_keys(keys: List[str], prefix: str, delims: str):
747 750 """Used by dict_key_matches, matching the prefix to a list of keys
748 751
749 752 Parameters
750 753 ==========
751 754 keys:
752 755 list of keys in dictionary currently being completed.
753 756 prefix:
754 757 Part of the text already typed by the user. e.g. `mydict[b'fo`
755 758 delims:
756 759 String of delimiters to consider when finding the current key.
757 760
758 761 Returns
759 762 =======
760 763
761 764 A tuple of three elements: ``quote``, ``token_start``, ``matched``, with
762 765 ``quote`` being the quote that need to be used to close current string.
763 766 ``token_start`` the position where the replacement should start occurring,
764 767 ``matches`` a list of replacement/completion
765 768
766 769 """
767 770 if not prefix:
768 771 return None, 0, [repr(k) for k in keys
769 772 if isinstance(k, (str, bytes))]
770 773 quote_match = re.search('["\']', prefix)
771 774 quote = quote_match.group()
772 775 try:
773 776 prefix_str = eval(prefix + quote, {})
774 777 except Exception:
775 778 return None, 0, []
776 779
777 780 pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
778 781 token_match = re.search(pattern, prefix, re.UNICODE)
779 782 token_start = token_match.start()
780 783 token_prefix = token_match.group()
781 784
782 785 matched = []
783 786 for key in keys:
784 787 try:
785 788 if not key.startswith(prefix_str):
786 789 continue
787 790 except (AttributeError, TypeError, UnicodeError):
788 791 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
789 792 continue
790 793
791 794 # reformat remainder of key to begin with prefix
792 795 rem = key[len(prefix_str):]
793 796 # force repr wrapped in '
794 797 rem_repr = repr(rem + '"') if isinstance(rem, str) else repr(rem + b'"')
795 798 if rem_repr.startswith('u') and prefix[0] not in 'uU':
796 799 # Found key is unicode, but prefix is Py2 string.
797 800 # Therefore attempt to interpret key as string.
798 801 try:
799 802 rem_repr = repr(rem.encode('ascii') + '"')
800 803 except UnicodeEncodeError:
801 804 continue
802 805
803 806 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
804 807 if quote == '"':
805 808 # The entered prefix is quoted with ",
806 809 # but the match is quoted with '.
807 810 # A contained " hence needs escaping for comparison:
808 811 rem_repr = rem_repr.replace('"', '\\"')
809 812
810 813 # then reinsert prefix from start of token
811 814 matched.append('%s%s' % (token_prefix, rem_repr))
812 815 return quote, token_start, matched
813 816
814 817
815 818 def cursor_to_position(text:str, line:int, column:int)->int:
816 819 """
817 820
818 821 Convert the (line,column) position of the cursor in text to an offset in a
819 822 string.
820 823
821 824 Parameters
822 825 ----------
823 826
824 827 text : str
825 828 The text in which to calculate the cursor offset
826 829 line : int
827 830 Line of the cursor; 0-indexed
828 831 column : int
829 832 Column of the cursor 0-indexed
830 833
831 834 Return
832 835 ------
833 836 Position of the cursor in ``text``, 0-indexed.
834 837
835 838 See Also
836 839 --------
837 840 position_to_cursor: reciprocal of this function
838 841
839 842 """
840 843 lines = text.split('\n')
841 844 assert line <= len(lines), '{} <= {}'.format(str(line), str(len(lines)))
842 845
843 846 return sum(len(l) + 1 for l in lines[:line]) + column
844 847
845 848 def position_to_cursor(text:str, offset:int)->Tuple[int, int]:
846 849 """
847 850 Convert the position of the cursor in text (0 indexed) to a line
848 851 number(0-indexed) and a column number (0-indexed) pair
849 852
850 853 Position should be a valid position in ``text``.
851 854
852 855 Parameters
853 856 ----------
854 857
855 858 text : str
856 859 The text in which to calculate the cursor offset
857 860 offset : int
858 861 Position of the cursor in ``text``, 0-indexed.
859 862
860 863 Return
861 864 ------
862 865 (line, column) : (int, int)
863 866 Line of the cursor; 0-indexed, column of the cursor 0-indexed
864 867
865 868
866 869 See Also
867 870 --------
868 871 cursor_to_position : reciprocal of this function
869 872
870 873
871 874 """
872 875
873 876 assert 0 < offset <= len(text) , "0 < %s <= %s" % (offset , len(text))
874 877
875 878 before = text[:offset]
876 879 blines = before.split('\n') # ! splitnes trim trailing \n
877 880 line = before.count('\n')
878 881 col = len(blines[-1])
879 882 return line, col
880 883
881 884
882 885 def _safe_isinstance(obj, module, class_name):
883 886 """Checks if obj is an instance of module.class_name if loaded
884 887 """
885 888 return (module in sys.modules and
886 889 isinstance(obj, getattr(import_module(module), class_name)))
887 890
888 891
889 892 def back_unicode_name_matches(text):
890 893 u"""Match unicode characters back to unicode name
891 894
892 895 This does ``β˜ƒ`` -> ``\\snowman``
893 896
894 897 Note that snowman is not a valid python3 combining character but will be expanded.
895 898 Though it will not recombine back to the snowman character by the completion machinery.
896 899
897 900 This will not either back-complete standard sequences like \\n, \\b ...
898 901
899 902 Used on Python 3 only.
900 903 """
901 904 if len(text)<2:
902 905 return u'', ()
903 906 maybe_slash = text[-2]
904 907 if maybe_slash != '\\':
905 908 return u'', ()
906 909
907 910 char = text[-1]
908 911 # no expand on quote for completion in strings.
909 912 # nor backcomplete standard ascii keys
910 913 if char in string.ascii_letters or char in ['"',"'"]:
911 914 return u'', ()
912 915 try :
913 916 unic = unicodedata.name(char)
914 917 return '\\'+char,['\\'+unic]
915 918 except KeyError:
916 919 pass
917 920 return u'', ()
918 921
919 922 def back_latex_name_matches(text:str):
920 923 """Match latex characters back to unicode name
921 924
922 925 This does ``\\β„΅`` -> ``\\aleph``
923 926
924 927 Used on Python 3 only.
925 928 """
926 929 if len(text)<2:
927 930 return u'', ()
928 931 maybe_slash = text[-2]
929 932 if maybe_slash != '\\':
930 933 return u'', ()
931 934
932 935
933 936 char = text[-1]
934 937 # no expand on quote for completion in strings.
935 938 # nor backcomplete standard ascii keys
936 939 if char in string.ascii_letters or char in ['"',"'"]:
937 940 return u'', ()
938 941 try :
939 942 latex = reverse_latex_symbol[char]
940 943 # '\\' replace the \ as well
941 944 return '\\'+char,[latex]
942 945 except KeyError:
943 946 pass
944 947 return u'', ()
945 948
946 949
947 950 def _formatparamchildren(parameter) -> str:
948 951 """
949 952 Get parameter name and value from Jedi Private API
950 953
951 954 Jedi does not expose a simple way to get `param=value` from its API.
952 955
953 956 Prameter
954 957 ========
955 958
956 959 parameter:
957 960 Jedi's function `Param`
958 961
959 962 Returns
960 963 =======
961 964
962 965 A string like 'a', 'b=1', '*args', '**kwargs'
963 966
964 967
965 968 """
966 969 description = parameter.description
967 970 if not description.startswith('param '):
968 971 raise ValueError('Jedi function parameter description have change format.'
969 972 'Expected "param ...", found %r".' % description)
970 973 return description[6:]
971 974
972 975 def _make_signature(completion)-> str:
973 976 """
974 977 Make the signature from a jedi completion
975 978
976 979 Parameter
977 980 =========
978 981
979 982 completion: jedi.Completion
980 983 object does not complete a function type
981 984
982 985 Returns
983 986 =======
984 987
985 988 a string consisting of the function signature, with the parenthesis but
986 989 without the function name. example:
987 990 `(a, *args, b=1, **kwargs)`
988 991
989 992 """
990 993
991 994 return '(%s)'% ', '.join([f for f in (_formatparamchildren(p) for p in completion.params) if f])
992 995
993 996 class IPCompleter(Completer):
994 997 """Extension of the completer class with IPython-specific features"""
995 998
996 999 @observe('greedy')
997 1000 def _greedy_changed(self, change):
998 1001 """update the splitter and readline delims when greedy is changed"""
999 1002 if change['new']:
1000 1003 self.splitter.delims = GREEDY_DELIMS
1001 1004 else:
1002 1005 self.splitter.delims = DELIMS
1003 1006
1004 1007 merge_completions = Bool(True,
1005 1008 help="""Whether to merge completion results into a single list
1006 1009
1007 1010 If False, only the completion results from the first non-empty
1008 1011 completer will be returned.
1009 1012 """
1010 1013 ).tag(config=True)
1011 1014 omit__names = Enum((0,1,2), default_value=2,
1012 1015 help="""Instruct the completer to omit private method names
1013 1016
1014 1017 Specifically, when completing on ``object.<tab>``.
1015 1018
1016 1019 When 2 [default]: all names that start with '_' will be excluded.
1017 1020
1018 1021 When 1: all 'magic' names (``__foo__``) will be excluded.
1019 1022
1020 1023 When 0: nothing will be excluded.
1021 1024 """
1022 1025 ).tag(config=True)
1023 1026 limit_to__all__ = Bool(False,
1024 1027 help="""
1025 1028 DEPRECATED as of version 5.0.
1026 1029
1027 1030 Instruct the completer to use __all__ for the completion
1028 1031
1029 1032 Specifically, when completing on ``object.<tab>``.
1030 1033
1031 1034 When True: only those names in obj.__all__ will be included.
1032 1035
1033 1036 When False [default]: the __all__ attribute is ignored
1034 1037 """,
1035 1038 ).tag(config=True)
1036 1039
1037 1040 @observe('limit_to__all__')
1038 1041 def _limit_to_all_changed(self, change):
1039 1042 warnings.warn('`IPython.core.IPCompleter.limit_to__all__` configuration '
1040 1043 'value has been deprecated since IPython 5.0, will be made to have '
1041 1044 'no effects and then removed in future version of IPython.',
1042 1045 UserWarning)
1043 1046
1044 1047 def __init__(self, shell=None, namespace=None, global_namespace=None,
1045 1048 use_readline=_deprecation_readline_sentinel, config=None, **kwargs):
1046 1049 """IPCompleter() -> completer
1047 1050
1048 1051 Return a completer object.
1049 1052
1050 1053 Parameters
1051 1054 ----------
1052 1055
1053 1056 shell
1054 1057 a pointer to the ipython shell itself. This is needed
1055 1058 because this completer knows about magic functions, and those can
1056 1059 only be accessed via the ipython instance.
1057 1060
1058 1061 namespace : dict, optional
1059 1062 an optional dict where completions are performed.
1060 1063
1061 1064 global_namespace : dict, optional
1062 1065 secondary optional dict for completions, to
1063 1066 handle cases (such as IPython embedded inside functions) where
1064 1067 both Python scopes are visible.
1065 1068
1066 1069 use_readline : bool, optional
1067 1070 DEPRECATED, ignored since IPython 6.0, will have no effects
1068 1071 """
1069 1072
1070 1073 self.magic_escape = ESC_MAGIC
1071 1074 self.splitter = CompletionSplitter()
1072 1075
1073 1076 if use_readline is not _deprecation_readline_sentinel:
1074 1077 warnings.warn('The `use_readline` parameter is deprecated and ignored since IPython 6.0.',
1075 1078 DeprecationWarning, stacklevel=2)
1076 1079
1077 1080 # _greedy_changed() depends on splitter and readline being defined:
1078 1081 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
1079 1082 config=config, **kwargs)
1080 1083
1081 1084 # List where completion matches will be stored
1082 1085 self.matches = []
1083 1086 self.shell = shell
1084 1087 # Regexp to split filenames with spaces in them
1085 1088 self.space_name_re = re.compile(r'([^\\] )')
1086 1089 # Hold a local ref. to glob.glob for speed
1087 1090 self.glob = glob.glob
1088 1091
1089 1092 # Determine if we are running on 'dumb' terminals, like (X)Emacs
1090 1093 # buffers, to avoid completion problems.
1091 1094 term = os.environ.get('TERM','xterm')
1092 1095 self.dumb_terminal = term in ['dumb','emacs']
1093 1096
1094 1097 # Special handling of backslashes needed in win32 platforms
1095 1098 if sys.platform == "win32":
1096 1099 self.clean_glob = self._clean_glob_win32
1097 1100 else:
1098 1101 self.clean_glob = self._clean_glob
1099 1102
1100 1103 #regexp to parse docstring for function signature
1101 1104 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1102 1105 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1103 1106 #use this if positional argument name is also needed
1104 1107 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
1105 1108
1106 1109 # All active matcher routines for completion
1107 1110 self.matchers = [
1108 1111 self.python_matches,
1109 1112 self.file_matches,
1110 1113 self.magic_matches,
1111 1114 self.python_func_kw_matches,
1112 1115 self.dict_key_matches,
1113 1116 ]
1114 1117 self.magic_arg_matchers = [
1115 1118 self.magic_config_matches,
1116 1119 self.magic_color_matches,
1117 1120 ]
1118 1121
1119 1122 # This is set externally by InteractiveShell
1120 1123 self.custom_completers = None
1121 1124
1122 1125 def all_completions(self, text):
1123 1126 """
1124 1127 Wrapper around the complete method for the benefit of emacs.
1125 1128 """
1126 1129 return self.complete(text)[1]
1127 1130
1128 1131 def _clean_glob(self, text):
1129 1132 return self.glob("%s*" % text)
1130 1133
1131 1134 def _clean_glob_win32(self,text):
1132 1135 return [f.replace("\\","/")
1133 1136 for f in self.glob("%s*" % text)]
1134 1137
1135 1138 def file_matches(self, text):
1136 1139 """Match filenames, expanding ~USER type strings.
1137 1140
1138 1141 Most of the seemingly convoluted logic in this completer is an
1139 1142 attempt to handle filenames with spaces in them. And yet it's not
1140 1143 quite perfect, because Python's readline doesn't expose all of the
1141 1144 GNU readline details needed for this to be done correctly.
1142 1145
1143 1146 For a filename with a space in it, the printed completions will be
1144 1147 only the parts after what's already been typed (instead of the
1145 1148 full completions, as is normally done). I don't think with the
1146 1149 current (as of Python 2.3) Python readline it's possible to do
1147 1150 better."""
1148 1151
1149 1152 # chars that require escaping with backslash - i.e. chars
1150 1153 # that readline treats incorrectly as delimiters, but we
1151 1154 # don't want to treat as delimiters in filename matching
1152 1155 # when escaped with backslash
1153 1156 if text.startswith('!'):
1154 1157 text = text[1:]
1155 1158 text_prefix = u'!'
1156 1159 else:
1157 1160 text_prefix = u''
1158 1161
1159 1162 text_until_cursor = self.text_until_cursor
1160 1163 # track strings with open quotes
1161 1164 open_quotes = has_open_quotes(text_until_cursor)
1162 1165
1163 1166 if '(' in text_until_cursor or '[' in text_until_cursor:
1164 1167 lsplit = text
1165 1168 else:
1166 1169 try:
1167 1170 # arg_split ~ shlex.split, but with unicode bugs fixed by us
1168 1171 lsplit = arg_split(text_until_cursor)[-1]
1169 1172 except ValueError:
1170 1173 # typically an unmatched ", or backslash without escaped char.
1171 1174 if open_quotes:
1172 1175 lsplit = text_until_cursor.split(open_quotes)[-1]
1173 1176 else:
1174 1177 return []
1175 1178 except IndexError:
1176 1179 # tab pressed on empty line
1177 1180 lsplit = ""
1178 1181
1179 1182 if not open_quotes and lsplit != protect_filename(lsplit):
1180 1183 # if protectables are found, do matching on the whole escaped name
1181 1184 has_protectables = True
1182 1185 text0,text = text,lsplit
1183 1186 else:
1184 1187 has_protectables = False
1185 1188 text = os.path.expanduser(text)
1186 1189
1187 1190 if text == "":
1188 1191 return [text_prefix + protect_filename(f) for f in self.glob("*")]
1189 1192
1190 1193 # Compute the matches from the filesystem
1191 1194 if sys.platform == 'win32':
1192 1195 m0 = self.clean_glob(text)
1193 1196 else:
1194 1197 m0 = self.clean_glob(text.replace('\\', ''))
1195 1198
1196 1199 if has_protectables:
1197 1200 # If we had protectables, we need to revert our changes to the
1198 1201 # beginning of filename so that we don't double-write the part
1199 1202 # of the filename we have so far
1200 1203 len_lsplit = len(lsplit)
1201 1204 matches = [text_prefix + text0 +
1202 1205 protect_filename(f[len_lsplit:]) for f in m0]
1203 1206 else:
1204 1207 if open_quotes:
1205 1208 # if we have a string with an open quote, we don't need to
1206 1209 # protect the names beyond the quote (and we _shouldn't_, as
1207 1210 # it would cause bugs when the filesystem call is made).
1208 1211 matches = m0 if sys.platform == "win32" else\
1209 1212 [protect_filename(f, open_quotes) for f in m0]
1210 1213 else:
1211 1214 matches = [text_prefix +
1212 1215 protect_filename(f) for f in m0]
1213 1216
1214 1217 # Mark directories in input list by appending '/' to their names.
1215 1218 return [x+'/' if os.path.isdir(x) else x for x in matches]
1216 1219
1217 1220 def magic_matches(self, text):
1218 1221 """Match magics"""
1219 1222 # Get all shell magics now rather than statically, so magics loaded at
1220 1223 # runtime show up too.
1221 1224 lsm = self.shell.magics_manager.lsmagic()
1222 1225 line_magics = lsm['line']
1223 1226 cell_magics = lsm['cell']
1224 1227 pre = self.magic_escape
1225 1228 pre2 = pre+pre
1226 1229
1227 1230 # Completion logic:
1228 1231 # - user gives %%: only do cell magics
1229 1232 # - user gives %: do both line and cell magics
1230 1233 # - no prefix: do both
1231 1234 # In other words, line magics are skipped if the user gives %% explicitly
1232 1235 #
1233 1236 # We also exclude magics that match any currently visible names:
1234 1237 # https://github.com/ipython/ipython/issues/4877
1235 1238 bare_text = text.lstrip(pre)
1236 1239 global_matches = self.global_matches(bare_text)
1237 1240 matches = lambda magic: magic.startswith(bare_text) \
1238 1241 and magic not in global_matches
1239 1242 comp = [ pre2+m for m in cell_magics if matches(m)]
1240 1243 if not text.startswith(pre2):
1241 1244 comp += [ pre+m for m in line_magics if matches(m)]
1242 1245
1243 1246 return comp
1244 1247
1245 1248 def magic_config_matches(self, text:str) -> List[str]:
1246 1249 """ Match class names and attributes for %config magic """
1247 1250 texts = text.strip().split()
1248 1251
1249 1252 if len(texts) > 0 and (texts[0] == 'config' or texts[0] == '%config'):
1250 1253 # get all configuration classes
1251 1254 classes = sorted(set([ c for c in self.shell.configurables
1252 1255 if c.__class__.class_traits(config=True)
1253 1256 ]), key=lambda x: x.__class__.__name__)
1254 1257 classnames = [ c.__class__.__name__ for c in classes ]
1255 1258
1256 1259 # return all classnames if config or %config is given
1257 1260 if len(texts) == 1:
1258 1261 return classnames
1259 1262
1260 1263 # match classname
1261 1264 classname_texts = texts[1].split('.')
1262 1265 classname = classname_texts[0]
1263 1266 classname_matches = [ c for c in classnames
1264 1267 if c.startswith(classname) ]
1265 1268
1266 1269 # return matched classes or the matched class with attributes
1267 1270 if texts[1].find('.') < 0:
1268 1271 return classname_matches
1269 1272 elif len(classname_matches) == 1 and \
1270 1273 classname_matches[0] == classname:
1271 1274 cls = classes[classnames.index(classname)].__class__
1272 1275 help = cls.class_get_help()
1273 1276 # strip leading '--' from cl-args:
1274 1277 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
1275 1278 return [ attr.split('=')[0]
1276 1279 for attr in help.strip().splitlines()
1277 1280 if attr.startswith(texts[1]) ]
1278 1281 return []
1279 1282
1280 1283 def magic_color_matches(self, text:str) -> List[str] :
1281 1284 """ Match color schemes for %colors magic"""
1282 1285 texts = text.strip().split()
1283 1286
1284 1287 if len(texts) > 0 and (texts[0] == 'colors' or texts[0] == '%colors'):
1285 1288 prefix = texts[1] if len(texts) > 1 else ''
1286 1289 return [ color for color in InspectColors.keys()
1287 1290 if color.startswith(prefix) ]
1288 1291 return []
1289 1292
1290 1293 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
1291 1294 """
1292 1295
1293 1296 Return a list of :any:`jedi.api.Completions` object from a ``text`` and
1294 1297 cursor position.
1295 1298
1296 1299 Parameters
1297 1300 ----------
1298 1301 cursor_column : int
1299 1302 column position of the cursor in ``text``, 0-indexed.
1300 1303 cursor_line : int
1301 1304 line position of the cursor in ``text``, 0-indexed
1302 1305 text : str
1303 1306 text to complete
1304 1307
1305 1308 Debugging
1306 1309 ---------
1307 1310
1308 1311 If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion`
1309 1312 object containing a string with the Jedi debug information attached.
1310 1313 """
1311 1314 namespaces = [self.namespace]
1312 1315 if self.global_namespace is not None:
1313 1316 namespaces.append(self.global_namespace)
1314 1317
1315 1318 completion_filter = lambda x:x
1316 1319 # cursor_pos is an it, jedi wants line and column
1317 1320 offset = cursor_to_position(text, cursor_line, cursor_column)
1318 1321 # filter output if we are completing for object members
1319 1322 if offset:
1320 1323 pre = text[offset-1]
1321 1324 if pre == '.':
1322 1325 if self.omit__names == 2:
1323 1326 completion_filter = lambda c:not c.name.startswith('_')
1324 1327 elif self.omit__names == 1:
1325 1328 completion_filter = lambda c:not (c.name.startswith('__') and c.name.endswith('__'))
1326 1329 elif self.omit__names == 0:
1327 1330 completion_filter = lambda x:x
1328 1331 else:
1329 1332 raise ValueError("Don't understand self.omit__names == {}".format(self.omit__names))
1330 1333
1331 1334 interpreter = jedi.Interpreter(
1332 1335 text, namespaces, column=cursor_column, line=cursor_line + 1)
1333 1336
1334 1337 try_jedi = False
1335 1338
1336 1339 try:
1337 1340 # should we check the type of the node is Error ?
1338 1341 from jedi.parser.tree import ErrorLeaf
1339 1342 next_to_last_tree = interpreter._get_module().tree_node.children[-2]
1340 1343 completing_string = False
1341 1344 if isinstance(next_to_last_tree, ErrorLeaf):
1342 1345 completing_string = interpreter._get_module().tree_node.children[-2].value[0] in {'"', "'"}
1343 1346 # if we are in a string jedi is likely not the right candidate for
1344 1347 # now. Skip it.
1345 1348 try_jedi = not completing_string
1346 1349 except Exception as e:
1347 1350 # many of things can go wrong, we are using private API just don't crash.
1348 1351 if self.debug:
1349 1352 print("Error detecting if completing a non-finished string :", e, '|')
1350 1353
1351 1354 if not try_jedi:
1352 1355 return []
1353 1356 try:
1354 1357 return filter(completion_filter, interpreter.completions())
1355 1358 except Exception as e:
1356 1359 if self.debug:
1357 1360 return [_FakeJediCompletion('Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""' % (e))]
1358 1361 else:
1359 1362 return []
1360 1363
1361 1364 def python_matches(self, text):
1362 1365 """Match attributes or global python names"""
1363 1366 if "." in text:
1364 1367 try:
1365 1368 matches = self.attr_matches(text)
1366 1369 if text.endswith('.') and self.omit__names:
1367 1370 if self.omit__names == 1:
1368 1371 # true if txt is _not_ a __ name, false otherwise:
1369 1372 no__name = (lambda txt:
1370 1373 re.match(r'.*\.__.*?__',txt) is None)
1371 1374 else:
1372 1375 # true if txt is _not_ a _ name, false otherwise:
1373 1376 no__name = (lambda txt:
1374 1377 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
1375 1378 matches = filter(no__name, matches)
1376 1379 except NameError:
1377 1380 # catches <undefined attributes>.<tab>
1378 1381 matches = []
1379 1382 else:
1380 1383 matches = self.global_matches(text)
1381 1384 return matches
1382 1385
1383 1386 def _default_arguments_from_docstring(self, doc):
1384 1387 """Parse the first line of docstring for call signature.
1385 1388
1386 1389 Docstring should be of the form 'min(iterable[, key=func])\n'.
1387 1390 It can also parse cython docstring of the form
1388 1391 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
1389 1392 """
1390 1393 if doc is None:
1391 1394 return []
1392 1395
1393 1396 #care only the firstline
1394 1397 line = doc.lstrip().splitlines()[0]
1395 1398
1396 1399 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
1397 1400 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
1398 1401 sig = self.docstring_sig_re.search(line)
1399 1402 if sig is None:
1400 1403 return []
1401 1404 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
1402 1405 sig = sig.groups()[0].split(',')
1403 1406 ret = []
1404 1407 for s in sig:
1405 1408 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
1406 1409 ret += self.docstring_kwd_re.findall(s)
1407 1410 return ret
1408 1411
1409 1412 def _default_arguments(self, obj):
1410 1413 """Return the list of default arguments of obj if it is callable,
1411 1414 or empty list otherwise."""
1412 1415 call_obj = obj
1413 1416 ret = []
1414 1417 if inspect.isbuiltin(obj):
1415 1418 pass
1416 1419 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
1417 1420 if inspect.isclass(obj):
1418 1421 #for cython embededsignature=True the constructor docstring
1419 1422 #belongs to the object itself not __init__
1420 1423 ret += self._default_arguments_from_docstring(
1421 1424 getattr(obj, '__doc__', ''))
1422 1425 # for classes, check for __init__,__new__
1423 1426 call_obj = (getattr(obj, '__init__', None) or
1424 1427 getattr(obj, '__new__', None))
1425 1428 # for all others, check if they are __call__able
1426 1429 elif hasattr(obj, '__call__'):
1427 1430 call_obj = obj.__call__
1428 1431 ret += self._default_arguments_from_docstring(
1429 1432 getattr(call_obj, '__doc__', ''))
1430 1433
1431 1434 _keeps = (inspect.Parameter.KEYWORD_ONLY,
1432 1435 inspect.Parameter.POSITIONAL_OR_KEYWORD)
1433 1436
1434 1437 try:
1435 1438 sig = inspect.signature(call_obj)
1436 1439 ret.extend(k for k, v in sig.parameters.items() if
1437 1440 v.kind in _keeps)
1438 1441 except ValueError:
1439 1442 pass
1440 1443
1441 1444 return list(set(ret))
1442 1445
1443 1446 def python_func_kw_matches(self,text):
1444 1447 """Match named parameters (kwargs) of the last open function"""
1445 1448
1446 1449 if "." in text: # a parameter cannot be dotted
1447 1450 return []
1448 1451 try: regexp = self.__funcParamsRegex
1449 1452 except AttributeError:
1450 1453 regexp = self.__funcParamsRegex = re.compile(r'''
1451 1454 '.*?(?<!\\)' | # single quoted strings or
1452 1455 ".*?(?<!\\)" | # double quoted strings or
1453 1456 \w+ | # identifier
1454 1457 \S # other characters
1455 1458 ''', re.VERBOSE | re.DOTALL)
1456 1459 # 1. find the nearest identifier that comes before an unclosed
1457 1460 # parenthesis before the cursor
1458 1461 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
1459 1462 tokens = regexp.findall(self.text_until_cursor)
1460 1463 iterTokens = reversed(tokens); openPar = 0
1461 1464
1462 1465 for token in iterTokens:
1463 1466 if token == ')':
1464 1467 openPar -= 1
1465 1468 elif token == '(':
1466 1469 openPar += 1
1467 1470 if openPar > 0:
1468 1471 # found the last unclosed parenthesis
1469 1472 break
1470 1473 else:
1471 1474 return []
1472 1475 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
1473 1476 ids = []
1474 1477 isId = re.compile(r'\w+$').match
1475 1478
1476 1479 while True:
1477 1480 try:
1478 1481 ids.append(next(iterTokens))
1479 1482 if not isId(ids[-1]):
1480 1483 ids.pop(); break
1481 1484 if not next(iterTokens) == '.':
1482 1485 break
1483 1486 except StopIteration:
1484 1487 break
1485 1488
1486 1489 # Find all named arguments already assigned to, as to avoid suggesting
1487 1490 # them again
1488 1491 usedNamedArgs = set()
1489 1492 par_level = -1
1490 1493 for token, next_token in zip(tokens, tokens[1:]):
1491 1494 if token == '(':
1492 1495 par_level += 1
1493 1496 elif token == ')':
1494 1497 par_level -= 1
1495 1498
1496 1499 if par_level != 0:
1497 1500 continue
1498 1501
1499 1502 if next_token != '=':
1500 1503 continue
1501 1504
1502 1505 usedNamedArgs.add(token)
1503 1506
1504 1507 # lookup the candidate callable matches either using global_matches
1505 1508 # or attr_matches for dotted names
1506 1509 if len(ids) == 1:
1507 1510 callableMatches = self.global_matches(ids[0])
1508 1511 else:
1509 1512 callableMatches = self.attr_matches('.'.join(ids[::-1]))
1510 1513 argMatches = []
1511 1514 for callableMatch in callableMatches:
1512 1515 try:
1513 1516 namedArgs = self._default_arguments(eval(callableMatch,
1514 1517 self.namespace))
1515 1518 except:
1516 1519 continue
1517 1520
1518 1521 # Remove used named arguments from the list, no need to show twice
1519 1522 for namedArg in set(namedArgs) - usedNamedArgs:
1520 1523 if namedArg.startswith(text):
1521 1524 argMatches.append(u"%s=" %namedArg)
1522 1525 return argMatches
1523 1526
1524 1527 def dict_key_matches(self, text):
1525 1528 "Match string keys in a dictionary, after e.g. 'foo[' "
1526 1529 def get_keys(obj):
1527 1530 # Objects can define their own completions by defining an
1528 1531 # _ipy_key_completions_() method.
1529 1532 method = get_real_method(obj, '_ipython_key_completions_')
1530 1533 if method is not None:
1531 1534 return method()
1532 1535
1533 1536 # Special case some common in-memory dict-like types
1534 1537 if isinstance(obj, dict) or\
1535 1538 _safe_isinstance(obj, 'pandas', 'DataFrame'):
1536 1539 try:
1537 1540 return list(obj.keys())
1538 1541 except Exception:
1539 1542 return []
1540 1543 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
1541 1544 _safe_isinstance(obj, 'numpy', 'void'):
1542 1545 return obj.dtype.names or []
1543 1546 return []
1544 1547
1545 1548 try:
1546 1549 regexps = self.__dict_key_regexps
1547 1550 except AttributeError:
1548 1551 dict_key_re_fmt = r'''(?x)
1549 1552 ( # match dict-referring expression wrt greedy setting
1550 1553 %s
1551 1554 )
1552 1555 \[ # open bracket
1553 1556 \s* # and optional whitespace
1554 1557 ([uUbB]? # string prefix (r not handled)
1555 1558 (?: # unclosed string
1556 1559 '(?:[^']|(?<!\\)\\')*
1557 1560 |
1558 1561 "(?:[^"]|(?<!\\)\\")*
1559 1562 )
1560 1563 )?
1561 1564 $
1562 1565 '''
1563 1566 regexps = self.__dict_key_regexps = {
1564 1567 False: re.compile(dict_key_re_fmt % '''
1565 1568 # identifiers separated by .
1566 1569 (?!\d)\w+
1567 1570 (?:\.(?!\d)\w+)*
1568 1571 '''),
1569 1572 True: re.compile(dict_key_re_fmt % '''
1570 1573 .+
1571 1574 ''')
1572 1575 }
1573 1576
1574 1577 match = regexps[self.greedy].search(self.text_until_cursor)
1575 1578 if match is None:
1576 1579 return []
1577 1580
1578 1581 expr, prefix = match.groups()
1579 1582 try:
1580 1583 obj = eval(expr, self.namespace)
1581 1584 except Exception:
1582 1585 try:
1583 1586 obj = eval(expr, self.global_namespace)
1584 1587 except Exception:
1585 1588 return []
1586 1589
1587 1590 keys = get_keys(obj)
1588 1591 if not keys:
1589 1592 return keys
1590 1593 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
1591 1594 if not matches:
1592 1595 return matches
1593 1596
1594 1597 # get the cursor position of
1595 1598 # - the text being completed
1596 1599 # - the start of the key text
1597 1600 # - the start of the completion
1598 1601 text_start = len(self.text_until_cursor) - len(text)
1599 1602 if prefix:
1600 1603 key_start = match.start(2)
1601 1604 completion_start = key_start + token_offset
1602 1605 else:
1603 1606 key_start = completion_start = match.end()
1604 1607
1605 1608 # grab the leading prefix, to make sure all completions start with `text`
1606 1609 if text_start > key_start:
1607 1610 leading = ''
1608 1611 else:
1609 1612 leading = text[text_start:completion_start]
1610 1613
1611 1614 # the index of the `[` character
1612 1615 bracket_idx = match.end(1)
1613 1616
1614 1617 # append closing quote and bracket as appropriate
1615 1618 # this is *not* appropriate if the opening quote or bracket is outside
1616 1619 # the text given to this method
1617 1620 suf = ''
1618 1621 continuation = self.line_buffer[len(self.text_until_cursor):]
1619 1622 if key_start > text_start and closing_quote:
1620 1623 # quotes were opened inside text, maybe close them
1621 1624 if continuation.startswith(closing_quote):
1622 1625 continuation = continuation[len(closing_quote):]
1623 1626 else:
1624 1627 suf += closing_quote
1625 1628 if bracket_idx > text_start:
1626 1629 # brackets were opened inside text, maybe close them
1627 1630 if not continuation.startswith(']'):
1628 1631 suf += ']'
1629 1632
1630 1633 return [leading + k + suf for k in matches]
1631 1634
1632 1635 def unicode_name_matches(self, text):
1633 1636 u"""Match Latex-like syntax for unicode characters base
1634 1637 on the name of the character.
1635 1638
1636 1639 This does ``\\GREEK SMALL LETTER ETA`` -> ``Ξ·``
1637 1640
1638 1641 Works only on valid python 3 identifier, or on combining characters that
1639 1642 will combine to form a valid identifier.
1640 1643
1641 1644 Used on Python 3 only.
1642 1645 """
1643 1646 slashpos = text.rfind('\\')
1644 1647 if slashpos > -1:
1645 1648 s = text[slashpos+1:]
1646 1649 try :
1647 1650 unic = unicodedata.lookup(s)
1648 1651 # allow combining chars
1649 1652 if ('a'+unic).isidentifier():
1650 1653 return '\\'+s,[unic]
1651 1654 except KeyError:
1652 1655 pass
1653 1656 return u'', []
1654 1657
1655 1658
1656 1659 def latex_matches(self, text):
1657 1660 u"""Match Latex syntax for unicode characters.
1658 1661
1659 1662 This does both ``\\alp`` -> ``\\alpha`` and ``\\alpha`` -> ``Ξ±``
1660 1663
1661 1664 Used on Python 3 only.
1662 1665 """
1663 1666 slashpos = text.rfind('\\')
1664 1667 if slashpos > -1:
1665 1668 s = text[slashpos:]
1666 1669 if s in latex_symbols:
1667 1670 # Try to complete a full latex symbol to unicode
1668 1671 # \\alpha -> Ξ±
1669 1672 return s, [latex_symbols[s]]
1670 1673 else:
1671 1674 # If a user has partially typed a latex symbol, give them
1672 1675 # a full list of options \al -> [\aleph, \alpha]
1673 1676 matches = [k for k in latex_symbols if k.startswith(s)]
1674 1677 return s, matches
1675 1678 return u'', []
1676 1679
1677 1680 def dispatch_custom_completer(self, text):
1678 1681 if not self.custom_completers:
1679 1682 return
1680 1683
1681 1684 line = self.line_buffer
1682 1685 if not line.strip():
1683 1686 return None
1684 1687
1685 1688 # Create a little structure to pass all the relevant information about
1686 1689 # the current completion to any custom completer.
1687 1690 event = SimpleNamespace()
1688 1691 event.line = line
1689 1692 event.symbol = text
1690 1693 cmd = line.split(None,1)[0]
1691 1694 event.command = cmd
1692 1695 event.text_until_cursor = self.text_until_cursor
1693 1696
1694 1697 # for foo etc, try also to find completer for %foo
1695 1698 if not cmd.startswith(self.magic_escape):
1696 1699 try_magic = self.custom_completers.s_matches(
1697 1700 self.magic_escape + cmd)
1698 1701 else:
1699 1702 try_magic = []
1700 1703
1701 1704 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1702 1705 try_magic,
1703 1706 self.custom_completers.flat_matches(self.text_until_cursor)):
1704 1707 try:
1705 1708 res = c(event)
1706 1709 if res:
1707 1710 # first, try case sensitive match
1708 1711 withcase = [r for r in res if r.startswith(text)]
1709 1712 if withcase:
1710 1713 return withcase
1711 1714 # if none, then case insensitive ones are ok too
1712 1715 text_low = text.lower()
1713 1716 return [r for r in res if r.lower().startswith(text_low)]
1714 1717 except TryNext:
1715 1718 pass
1716 1719
1717 1720 return None
1718 1721
1719 1722 def completions(self, text: str, offset: int)->Iterator[Completion]:
1720 1723 """
1721 1724 Returns an iterator over the possible completions
1722 1725
1723 1726 .. warning:: Unstable
1724 1727
1725 1728 This function is unstable, API may change without warning.
1726 1729 It will also raise unless use in proper context manager.
1727 1730
1728 1731 Parameters
1729 1732 ----------
1730 1733
1731 1734 text:str
1732 1735 Full text of the current input, multi line string.
1733 1736 offset:int
1734 1737 Integer representing the position of the cursor in ``text``. Offset
1735 1738 is 0-based indexed.
1736 1739
1737 1740 Yields
1738 1741 ------
1739 1742 :any:`Completion` object
1740 1743
1741 1744
1742 1745 The cursor on a text can either be seen as being "in between"
1743 1746 characters or "On" a character depending on the interface visible to
1744 1747 the user. For consistency the cursor being on "in between" characters X
1745 1748 and Y is equivalent to the cursor being "on" character Y, that is to say
1746 1749 the character the cursor is on is considered as being after the cursor.
1747 1750
1748 1751 Combining characters may span more that one position in the
1749 1752 text.
1750 1753
1751 1754
1752 1755 .. note::
1753 1756
1754 1757 If ``IPCompleter.debug`` is :any:`True` will yield a ``--jedi/ipython--``
1755 1758 fake Completion token to distinguish completion returned by Jedi
1756 1759 and usual IPython completion.
1757 1760
1758 1761 .. note::
1759 1762
1760 1763 Completions are not completely deduplicated yet. If identical
1761 1764 completions are coming from different sources this function does not
1762 1765 ensure that each completion object will only be present once.
1763 1766 """
1764 1767 warnings.warn("_complete is a provisional API (as of IPython 6.0). "
1765 1768 "It may change without warnings. "
1766 1769 "Use in corresponding context manager.",
1767 1770 category=ProvisionalCompleterWarning, stacklevel=2)
1768 1771
1769 1772 seen = set()
1770 1773 for c in self._completions(text, offset, _timeout=self.jedi_compute_type_timeout/1000):
1771 1774 if c and (c in seen):
1772 1775 continue
1773 1776 yield c
1774 1777 seen.add(c)
1775 1778
1776 1779 def _completions(self, full_text: str, offset: int, *, _timeout)->Iterator[Completion]:
1777 1780 """
1778 1781 Core completion module.Same signature as :any:`completions`, with the
1779 1782 extra `timeout` parameter (in seconds).
1780 1783
1781 1784
1782 1785 Computing jedi's completion ``.type`` can be quite expensive (it is a
1783 1786 lazy property) and can require some warm-up, more warm up than just
1784 1787 computing the ``name`` of a completion. The warm-up can be :
1785 1788
1786 1789 - Long warm-up the first time a module is encountered after
1787 1790 install/update: actually build parse/inference tree.
1788 1791
1789 1792 - first time the module is encountered in a session: load tree from
1790 1793 disk.
1791 1794
1792 1795 We don't want to block completions for tens of seconds so we give the
1793 1796 completer a "budget" of ``_timeout`` seconds per invocation to compute
1794 1797 completions types, the completions that have not yet been computed will
1795 1798 be marked as "unknown" an will have a chance to be computed next round
1796 1799 are things get cached.
1797 1800
1798 1801 Keep in mind that Jedi is not the only thing treating the completion so
1799 1802 keep the timeout short-ish as if we take more than 0.3 second we still
1800 1803 have lots of processing to do.
1801 1804
1802 1805 """
1803 1806 deadline = time.monotonic() + _timeout
1804 1807
1805 1808
1806 1809 before = full_text[:offset]
1807 1810 cursor_line, cursor_column = position_to_cursor(full_text, offset)
1808 1811
1809 1812 matched_text, matches, matches_origin, jedi_matches = self._complete(
1810 1813 full_text=full_text, cursor_line=cursor_line, cursor_pos=cursor_column)
1811 1814
1812 1815 iter_jm = iter(jedi_matches)
1813 1816 if _timeout:
1814 1817 for jm in iter_jm:
1815 1818 try:
1816 1819 type_ = jm.type
1817 1820 except Exception:
1818 1821 if self.debug:
1819 1822 print("Error in Jedi getting type of ", jm)
1820 1823 type_ = None
1821 1824 delta = len(jm.name_with_symbols) - len(jm.complete)
1822 1825 if type_ == 'function':
1823 1826 signature = _make_signature(jm)
1824 1827 else:
1825 1828 signature = ''
1826 1829 yield Completion(start=offset - delta,
1827 1830 end=offset,
1828 1831 text=jm.name_with_symbols,
1829 1832 type=type_,
1830 1833 signature=signature,
1831 1834 _origin='jedi')
1832 1835
1833 1836 if time.monotonic() > deadline:
1834 1837 break
1835 1838
1836 1839 for jm in iter_jm:
1837 1840 delta = len(jm.name_with_symbols) - len(jm.complete)
1838 1841 yield Completion(start=offset - delta,
1839 1842 end=offset,
1840 1843 text=jm.name_with_symbols,
1841 1844 type='<unknown>', # don't compute type for speed
1842 1845 _origin='jedi',
1843 1846 signature='')
1844 1847
1845 1848
1846 1849 start_offset = before.rfind(matched_text)
1847 1850
1848 1851 # TODO:
1849 1852 # Supress this, right now just for debug.
1850 1853 if jedi_matches and matches and self.debug:
1851 1854 yield Completion(start=start_offset, end=offset, text='--jedi/ipython--',
1852 1855 _origin='debug', type='none', signature='')
1853 1856
1854 1857 # I'm unsure if this is always true, so let's assert and see if it
1855 1858 # crash
1856 1859 assert before.endswith(matched_text)
1857 1860 for m, t in zip(matches, matches_origin):
1858 1861 yield Completion(start=start_offset, end=offset, text=m, _origin=t, signature='', type='<unknown>')
1859 1862
1860 1863
1861 1864 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1862 1865 """Find completions for the given text and line context.
1863 1866
1864 1867 Note that both the text and the line_buffer are optional, but at least
1865 1868 one of them must be given.
1866 1869
1867 1870 Parameters
1868 1871 ----------
1869 1872 text : string, optional
1870 1873 Text to perform the completion on. If not given, the line buffer
1871 1874 is split using the instance's CompletionSplitter object.
1872 1875
1873 1876 line_buffer : string, optional
1874 1877 If not given, the completer attempts to obtain the current line
1875 1878 buffer via readline. This keyword allows clients which are
1876 1879 requesting for text completions in non-readline contexts to inform
1877 1880 the completer of the entire text.
1878 1881
1879 1882 cursor_pos : int, optional
1880 1883 Index of the cursor in the full line buffer. Should be provided by
1881 1884 remote frontends where kernel has no access to frontend state.
1882 1885
1883 1886 Returns
1884 1887 -------
1885 1888 text : str
1886 1889 Text that was actually used in the completion.
1887 1890
1888 1891 matches : list
1889 1892 A list of completion matches.
1890 1893
1891 1894
1892 1895 .. note::
1893 1896
1894 1897 This API is likely to be deprecated and replaced by
1895 1898 :any:`IPCompleter.completions` in the future.
1896 1899
1897 1900
1898 1901 """
1899 1902 warnings.warn('`Completer.complete` is pending deprecation since '
1900 1903 'IPython 6.0 and will be replaced by `Completer.completions`.',
1901 1904 PendingDeprecationWarning)
1902 1905 # potential todo, FOLD the 3rd throw away argument of _complete
1903 1906 # into the first 2 one.
1904 1907 return self._complete(line_buffer=line_buffer, cursor_pos=cursor_pos, text=text, cursor_line=0)[:2]
1905 1908
1906 1909 def _complete(self, *, cursor_line, cursor_pos, line_buffer=None, text=None,
1907 1910 full_text=None, return_jedi_results=True) -> Tuple[str, List[str], List[str], Iterable[_FakeJediCompletion]]:
1908 1911 """
1909 1912
1910 1913 Like complete but can also returns raw jedi completions as well as the
1911 1914 origin of the completion text. This could (and should) be made much
1912 1915 cleaner but that will be simpler once we drop the old (and stateful)
1913 1916 :any:`complete` API.
1914 1917
1915 1918
1916 1919 With current provisional API, cursor_pos act both (depending on the
1917 1920 caller) as the offset in the ``text`` or ``line_buffer``, or as the
1918 1921 ``column`` when passing multiline strings this could/should be renamed
1919 1922 but would add extra noise.
1920 1923 """
1921 1924
1922 1925 # if the cursor position isn't given, the only sane assumption we can
1923 1926 # make is that it's at the end of the line (the common case)
1924 1927 if cursor_pos is None:
1925 1928 cursor_pos = len(line_buffer) if text is None else len(text)
1926 1929
1927 1930 if self.use_main_ns:
1928 1931 self.namespace = __main__.__dict__
1929 1932
1930 1933 # if text is either None or an empty string, rely on the line buffer
1931 1934 if (not line_buffer) and full_text:
1932 1935 line_buffer = full_text.split('\n')[cursor_line]
1933 1936 if not text:
1934 1937 text = self.splitter.split_line(line_buffer, cursor_pos)
1935 1938
1936 1939 if self.backslash_combining_completions:
1937 1940 # allow deactivation of these on windows.
1938 1941 base_text = text if not line_buffer else line_buffer[:cursor_pos]
1939 1942 latex_text, latex_matches = self.latex_matches(base_text)
1940 1943 if latex_matches:
1941 1944 return latex_text, latex_matches, ['latex_matches']*len(latex_matches), ()
1942 1945 name_text = ''
1943 1946 name_matches = []
1944 1947 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
1945 1948 name_text, name_matches = meth(base_text)
1946 1949 if name_text:
1947 return name_text, name_matches, [meth.__qualname__]*len(name_matches), ()
1950 return name_text, name_matches[:MATCHES_LIMIT], \
1951 [meth.__qualname__]*min(len(name_matches), MATCHES_LIMIT), ()
1948 1952
1949 1953
1950 1954 # If no line buffer is given, assume the input text is all there was
1951 1955 if line_buffer is None:
1952 1956 line_buffer = text
1953 1957
1954 1958 self.line_buffer = line_buffer
1955 1959 self.text_until_cursor = self.line_buffer[:cursor_pos]
1956 1960
1957 1961 # Do magic arg matches
1958 1962 for matcher in self.magic_arg_matchers:
1959 matches = [(m, matcher.__qualname__) for m in matcher(line_buffer)]
1963 matches = list(matcher(line_buffer))[:MATCHES_LIMIT]
1960 1964 if matches:
1961 matches2 = [m[0] for m in matches]
1962 origins = [m[1] for m in matches]
1963 return text, matches2, origins, ()
1965 origins = [matcher.__qualname__] * len(matches)
1966 return text, matches, origins, ()
1964 1967
1965 1968 # Start with a clean slate of completions
1966 1969 matches = []
1967 1970 custom_res = self.dispatch_custom_completer(text)
1968 1971 # FIXME: we should extend our api to return a dict with completions for
1969 1972 # different types of objects. The rlcomplete() method could then
1970 1973 # simply collapse the dict into a list for readline, but we'd have
1971 1974 # richer completion semantics in other evironments.
1972 1975 completions = ()
1973 1976 if self.use_jedi and return_jedi_results:
1974 1977 if not full_text:
1975 1978 full_text = line_buffer
1976 1979 completions = self._jedi_matches(
1977 1980 cursor_pos, cursor_line, full_text)
1978 1981 if custom_res is not None:
1979 1982 # did custom completers produce something?
1980 1983 matches = [(m, 'custom') for m in custom_res]
1981 1984 else:
1982 1985 # Extend the list of completions with the results of each
1983 1986 # matcher, so we return results to the user from all
1984 1987 # namespaces.
1985 1988 if self.merge_completions:
1986 1989 matches = []
1987 1990 for matcher in self.matchers:
1988 1991 try:
1989 1992 matches.extend([(m, matcher.__qualname__)
1990 1993 for m in matcher(text)])
1991 1994 except:
1992 1995 # Show the ugly traceback if the matcher causes an
1993 1996 # exception, but do NOT crash the kernel!
1994 1997 sys.excepthook(*sys.exc_info())
1995 1998 else:
1996 1999 for matcher in self.matchers:
1997 2000 matches = [(m, matcher.__qualname__)
1998 2001 for m in matcher(text)]
1999 2002 if matches:
2000 2003 break
2001 2004 seen = set()
2002 2005 filtered_matches = set()
2003 2006 for m in matches:
2004 2007 t, c = m
2005 2008 if t not in seen:
2006 2009 filtered_matches.add(m)
2007 2010 seen.add(t)
2008 2011
2009 2012 _filtered_matches = sorted(
2010 set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))
2013 set(filtered_matches), key=lambda x: completions_sorting_key(x[0]))\
2014 [:MATCHES_LIMIT]
2011 2015
2012 2016 _matches = [m[0] for m in _filtered_matches]
2013 2017 origins = [m[1] for m in _filtered_matches]
2014 2018
2015 2019 self.matches = _matches
2016 2020
2017 2021 return text, _matches, origins, completions
General Comments 0
You need to be logged in to leave comments. Login now