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