Python jedi.Interpreter() Examples

The following are 4 code examples of jedi.Interpreter(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jedi , or try the search function .
Example #1
Source File: pyodide.py    From pyodide with Mozilla Public License 2.0 6 votes vote down vote up
def get_completions(code, cursor=None, namespaces=None):
    """
    Get code autocompletion candidates.
    """
    import jedi
    import __main__

    if namespaces is None:
        namespaces = [__main__.__dict__]

    if cursor is None:
        cursor = len(code)
    code = code[:cursor]
    interp = jedi.Interpreter(source=code, namespaces=namespaces)
    completions = interp.completions()

    return [x.name for x in completions] 
Example #2
Source File: backend.py    From thonny with MIT License 6 votes vote down vote up
def _cmd_shell_autocomplete(self, cmd):
        error = None
        try:
            import jedi
        except ImportError:
            completions = []
            error = "Could not import jedi"
        else:
            try:
                # with warnings.catch_warnings():
                interpreter = jedi.Interpreter(cmd.source, [__main__.__dict__])
                completions = self._export_completions(interpreter.completions())
            except Exception as e:
                completions = []
                error = "Autocomplete error: " + str(e)

        return InlineResponse(
            "shell_autocomplete", source=cmd.source, completions=completions, error=error
        ) 
Example #3
Source File: console.py    From pyqtconsole with MIT License 5 votes vote down vote up
def get_completions(self, line):
        """Get completions. Used by the ``autocomplete`` extension."""
        script = jedi.Interpreter(line, [self.interpreter.locals])
        return [comp.name for comp in script.completions()] 
Example #4
Source File: python_magic.py    From metakernel with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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)]

        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]