Exemple #1
0
    def get_completions(self, document, complete_event):
        if not document.current_line.strip():
            return
        # Some bits of our completion system may print stuff (e.g. if a module
        # is imported). This context manager ensures that doesn't interfere with
        # the prompt.

        with patch_stdout(), provisionalcompleter():
            body = document.text
            cursor_row = document.cursor_position_row
            cursor_col = document.cursor_position_col
            cursor_position = document.cursor_position
            offset = cursor_to_position(body, cursor_row, cursor_col)
            yield from self._get_completions(body, offset, cursor_position, self.ipy_completer)
Exemple #2
0
    def get_completions(self, document, complete_event):
        if not document.current_line.strip():
            return
        # Some bits of our completion system may print stuff (e.g. if a module
        # is imported). This context manager ensures that doesn't interfere with
        # the prompt.

        with self.patch_stdout(), provisionalcompleter():
            body = document.text
            cursor_row = document.cursor_position_row
            cursor_col = document.cursor_position_col
            cursor_position = document.cursor_position
            offset = cursor_to_position(body, cursor_row, cursor_col)
            yield from self._get_completions(body, offset, cursor_position, self.ipy_completer)
    def get_completions(self, document, complete_event):
        if not document.current_line.strip():
            return
        # Some bits of our completion system may print stuff (e.g. if a module
        # is imported). This context manager ensures that doesn't interfere with
        # the prompt.

        with patch_stdout(), provisionalcompleter():
            body = document.text
            cursor_row = document.cursor_position_row
            cursor_col = document.cursor_position_col
            cursor_position = document.cursor_position
            offset = cursor_to_position(body, cursor_row, cursor_col)
            try:
                yield from self._get_completions(body, offset, cursor_position,
                                                 self.ipy_completer)
            except Exception as e:
                try:
                    exc_type, exc_value, exc_tb = sys.exc_info()
                    traceback.print_exception(exc_type, exc_value, exc_tb)
                except AttributeError:
                    print('Unrecoverable Error in completions')
Exemple #4
0
def _jedi_matches(self, cursor_column: int, cursor_line: int,
                  text: str) -> Iterable[Any]:
    """

    Return a list of :any:`jedi.api.Completions` object from a ``text`` and
    cursor position.

    Parameters
    ----------
    cursor_column : int
        column position of the cursor in ``text``, 0-indexed.
    cursor_line : int
        line position of the cursor in ``text``, 0-indexed
    text : str
        text to complete

    Debugging
    ---------

    If ``IPCompleter.debug`` is ``True`` may return a :any:`_FakeJediCompletion`
    object containing a string with the Jedi debug information attached.
    """
    df = _find_df_in_history(self.shell)
    if df and isinstance(self.shell.user_ns.get("_"), Symbolic):
        namespaces = [{**self.namespace, "_": self.namespace[df]}]
    else:
        namespaces = [self.namespace]

    if self.global_namespace is not None:
        namespaces.append(self.global_namespace)

    completion_filter = lambda x: x
    offset = cursor_to_position(text, cursor_line, cursor_column)
    # filter output if we are completing for object members
    if offset:
        pre = text[offset - 1]
        if pre == '.':
            if self.omit__names == 2:
                completion_filter = lambda c: not c.name.startswith('_')
            elif self.omit__names == 1:
                completion_filter = lambda c: not (c.name.startswith('__') and
                                                   c.name.endswith('__'))
            elif self.omit__names == 0:
                completion_filter = lambda x: x
            else:
                raise ValueError(
                    "Don't understand self.omit__names == {}".format(
                        self.omit__names))

    interpreter = jedi.Interpreter(text[:offset], namespaces)
    try_jedi = True

    try:
        # find the first token in the current tree -- if it is a ' or " then we are in a string
        completing_string = False
        try:
            first_child = next(
                c for c in interpreter._get_module().tree_node.children
                if hasattr(c, 'value'))
        except StopIteration:
            pass
        else:
            # note the value may be ', ", or it may also be ''' or """, or
            # in some cases, """what/you/typed..., but all of these are
            # strings.
            completing_string = len(
                first_child.value) > 0 and first_child.value[0] in {"'", '"'}

        # if we are in a string jedi is likely not the right candidate for
        # now. Skip it.
        try_jedi = not completing_string
    except Exception as e:
        # many of things can go wrong, we are using private API just don't crash.
        if self.debug:
            print("Error detecting if completing a non-finished string :", e,
                  '|')

    if not try_jedi:
        return []
    try:
        return filter(
            completion_filter,
            interpreter.complete(column=cursor_column, line=cursor_line + 1))
    except Exception as e:
        if self.debug:
            return [
                _FakeJediCompletion(
                    'Oops Jedi has crashed, please report a bug with the following:\n"""\n%s\ns"""'
                    % (e))
            ]
        else:
            return []