Skip to content

Instantly share code, notes, and snippets.

@galaunay
Last active May 17, 2018 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galaunay/7c4b4b2a33362a30a240e4794ea0b400 to your computer and use it in GitHub Desktop.
Save galaunay/7c4b4b2a33362a30a240e4794ea0b400 to your computer and use it in GitHub Desktop.
Test of the Elpy's get_completions jedi implementation
# -*- coding: utf-8 -*-
#!/usr/env python3
import jedi
def run_with_debug(name, *args, **kwargs):
re_raise = kwargs.pop('re_raise', ())
try:
script = jedi.Script(*args, **kwargs)
return getattr(script, name)()
except Exception as e:
if isinstance(e, re_raise):
raise
# Bug jedi#485
if (isinstance(e, ValueError) and "invalid \\x escape" in str(e)):
return None
# Bug jedi#485 in Python 3
if (isinstance(e, SyntaxError) and "truncated \\xXX escape" in str(e)):
return None
def rpc_get_completions(filename, source, line, column):
proposals = run_with_debug('completions',
source=source, line=line, column=column,
path=filename, encoding='utf-8')
if proposals is None:
return []
return [{'name': proposal.name.rstrip("="),
'suffix': proposal.complete.rstrip("="),
'annotation': proposal.type,
'meta': proposal.description}
for proposal in proposals]
line = 10
column = 13
filename = "script.py"
with open(filename, 'r') as f:
source = "".join(f.readlines())
completions = rpc_get_completions(source=source, filename=filename,
line=line, column=column)
for c in completions:
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment