示例#1
0
    def get_completions(self, info):
        '''Get Python completions'''
        # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py
        if jedi is None:
            return []

        text = info['code']
        position = (info['line_num'], info['column'])
        interpreter = Interpreter(text, [self.env])

        lines = split_lines(text)
        name = get_on_completion_name(
            interpreter._module_node,
            lines,
            position
        )
        before = text[:len(text) - len(name)]

        try:
            completions = interpreter.complete()
        except AttributeError:
            completions = interpreter.completions()

        completions = [before + c.name_with_symbols for c in completions]

        self.kernel.log.error(completions)

        return [c[info['start']:] for c in completions]
示例#2
0
    def get_completions(self, info):
        '''Get Python completions'''
        # https://github.com/davidhalter/jedi/blob/master/jedi/utils.py
        if jedi is None:
            return []

        text = info['code']
        position = (info['line_num'], info['column'])
        interpreter = Interpreter(text, [self.env])

        if jedi.__version__ >= LooseVersion('0.12.0'):
            lines = split_lines(text)
            name = get_on_completion_name(interpreter._module_node, lines,
                                          position)
            before = text[:len(text) - len(name)]
        elif jedi.__version__ >= LooseVersion('0.10.0'):
            lines = split_lines(text)
            name = get_on_completion_name(interpreter._get_module_node(),
                                          lines, position)
            before = text[:len(text) - len(name)]
        else:
            path = UserContext(text, position).get_path_until_cursor()
            path, dot, like = completion_parts(path)
            before = text[:len(text) - len(like)]

        try:
            completions = interpreter.complete()
        except AttributeError:
            completions = interpreter.completions()

        completions = [before + c.name_with_symbols for c in completions]

        self.kernel.log.error(completions)

        return [c[info['start']:] for c in completions]
示例#3
0
        def complete(self, text, state):
            """
            This complete stuff is pretty weird, a generator would make
            a lot more sense, but probably due to backwards compatibility
            this is still the way how it works.

            The only important part is stuff in the ``state == 0`` flow,
            everything else has been copied from the ``rlcompleter`` std.
            library module.
            """
            if state == 0:
                sys.path.insert(0, os.getcwd())
                # Calling python doesn't have a path, so add to sys.path.
                try:
                    logging.debug("Start REPL completion: " + repr(text))
                    interpreter = Interpreter(text,
                                              [namespace_module.__dict__])

                    completions = interpreter.complete(fuzzy=fuzzy)
                    logging.debug("REPL completions: %s", completions)

                    self.matches = [
                        text[:len(text) - c._like_name_length] +
                        c.name_with_symbols for c in completions
                    ]
                except:
                    logging.error("REPL Completion error:\n" +
                                  traceback.format_exc())
                    raise
                finally:
                    sys.path.pop(0)
            try:
                return self.matches[state]
            except IndexError:
                return None
示例#4
0
def serialize_suggestion(prompt, from_, to, cursor, frame):
    answer = {"prompt": prompt, "from": from_, "to": to, "suggestion": {}}
    try:
        script = Interpreter(prompt, [frame.f_locals, frame.f_globals])
        completions = script.complete(cursor["line"] + 1, cursor["ch"])
    except Exception:
        log.exception("Completion failed")
        return answer

    params_first_completions = [
        c for c in completions if c.name_with_symbols != c.name
    ] + [c for c in completions if c.name_with_symbols == c.name]

    completions = [
        {
            "text": comp.name_with_symbols,
            "description": comp.description,
            "docstring": comp.docstring(),
            "type": comp.type,
            "base": comp.name_with_symbols[
                : len(comp.name_with_symbols) - len(comp.complete)
            ],
            "complete": comp.complete,
        }
        for comp in params_first_completions
    ]

    suggestion = {"from": from_, "to": to, "list": completions}
    answer["suggestion"] = suggestion
    return answer