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