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: str, completions: _IC, *, _debug: bool = False)
| 837 | |
| 838 | |
| 839 | def rectify_completions(text: str, completions: _IC, *, _debug: bool = False) -> _IC: |
| 840 | """ |
| 841 | Rectify a set of completions to all have the same ``start`` and ``end`` |
| 842 | |
| 843 | .. warning:: |
| 844 | |
| 845 | Unstable |
| 846 | |
| 847 | This function is unstable, API may change without warning. |
| 848 | It will also raise unless use in proper context manager. |
| 849 | |
| 850 | Parameters |
| 851 | ---------- |
| 852 | text : str |
| 853 | text that should be completed. |
| 854 | completions : Iterator[Completion] |
| 855 | iterator over the completions to rectify |
| 856 | _debug : bool |
| 857 | Log failed completion |
| 858 | |
| 859 | Notes |
| 860 | ----- |
| 861 | :class:`jedi.api.classes.Completion` s returned by Jedi may not have the same start and end, though |
| 862 | the Jupyter Protocol requires them to behave like so. This will readjust |
| 863 | the completion to have the same ``start`` and ``end`` by padding both |
| 864 | extremities with surrounding text. |
| 865 | |
| 866 | During stabilisation should support a ``_debug`` option to log which |
| 867 | completion are return by the IPython completer and not found in Jedi in |
| 868 | order to make upstream bug report. |
| 869 | """ |
| 870 | warnings.warn("`rectify_completions` is a provisional API (as of IPython 6.0). " |
| 871 | "It may change without warnings. " |
| 872 | "Use in corresponding context manager.", |
| 873 | category=ProvisionalCompleterWarning, stacklevel=2) |
| 874 | |
| 875 | completions = list(completions) |
| 876 | if not completions: |
| 877 | return |
| 878 | starts = (c.start for c in completions) |
| 879 | ends = (c.end for c in completions) |
| 880 | |
| 881 | new_start = min(starts) |
| 882 | new_end = max(ends) |
| 883 | |
| 884 | seen_jedi = set() |
| 885 | seen_python_matches = set() |
| 886 | for c in completions: |
| 887 | new_text = text[new_start:c.start] + c.text + text[c.end:new_end] |
| 888 | if c._origin == 'jedi': |
| 889 | seen_jedi.add(new_text) |
| 890 | elif c._origin == "IPCompleter.python_matcher": |
| 891 | seen_python_matches.add(new_text) |
| 892 | yield Completion(new_start, new_end, new_text, type=c.type, _origin=c._origin, signature=c.signature) |
| 893 | diff = seen_python_matches.difference(seen_jedi) |
| 894 | if diff and _debug: |
| 895 | print('IPython.python matches have extras:', diff) |
| 896 |
nothing calls this directly
no test coverage detected
searching dependent graphs…