Last active
May 17, 2018 21:57
-
-
Save galaunay/7c4b4b2a33362a30a240e4794ea0b400 to your computer and use it in GitHub Desktop.
Test of the Elpy's get_completions jedi implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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