MCPcopy Create free account
hub / github.com/ipython/ipython / rectify_completions

Function rectify_completions

IPython/core/completer.py:453–504  ·  view source on GitHub ↗

Rectify a set of completions to all have the same ``start`` and ``end`` .. warning:: Unstable This function is unstable, API may change without warning. It will also raise unless use in proper context manager. Parameters ---------- text: str text that

(text: str, completions: _IC, *, _debug=False)

Source from the content-addressed store, hash-verified

451
452
453def rectify_completions(text: str, completions: _IC, *, _debug=False)->_IC:
454 """
455 Rectify a set of completions to all have the same ``start`` and ``end``
456
457 .. warning:: Unstable
458
459 This function is unstable, API may change without warning.
460 It will also raise unless use in proper context manager.
461
462 Parameters
463 ----------
464 text: str
465 text that should be completed.
466 completions: Iterator[Completion]
467 iterator over the completions to rectify
468
469
470 :any:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though
471 the Jupyter Protocol requires them to behave like so. This will readjust
472 the completion to have the same ``start`` and ``end`` by padding both
473 extremities with surrounding text.
474
475 During stabilisation should support a ``_debug`` option to log which
476 completion are return by the IPython completer and not found in Jedi in
477 order to make upstream bug report.
478 """
479 warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). "
480 "It may change without warnings. "
481 "Use in corresponding context manager.",
482 category=ProvisionalCompleterWarning, stacklevel=2)
483
484 completions = list(completions)
485 if not completions:
486 return
487 starts = (c.start for c in completions)
488 ends = (c.end for c in completions)
489
490 new_start = min(starts)
491 new_end = max(ends)
492
493 seen_jedi = set()
494 seen_python_matches = set()
495 for c in completions:
496 new_text = text[new_start:c.start] + c.text + text[c.end:new_end]
497 if c._origin == 'jedi':
498 seen_jedi.add(new_text)
499 elif c._origin == 'IPCompleter.python_matches':
500 seen_python_matches.add(new_text)
501 yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature)
502 diff = seen_python_matches.difference(seen_jedi)
503 if diff and _debug:
504 print('IPython.python matches have extras:', diff)
505
506
507if sys.platform == 'win32':

Callers

nothing calls this directly

Calls 3

CompletionClass · 0.85
warnMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected