Skip to content

Instantly share code, notes, and snippets.

@galaunay
Created May 15, 2018 21:47
Show Gist options
  • Save galaunay/63fdff4ca49a891698257be898cb4acd to your computer and use it in GitHub Desktop.
Save galaunay/63fdff4ca49a891698257be898cb4acd to your computer and use it in GitHub Desktop.
Test of the Elpy's 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_definition(filename, source, line, column):
locations = run_with_debug('goto_definitions',
source=source, line=line, column=column,
path=filename, encoding='utf-8')
print("Location from 'goto_definition':")
print(" {}".format(locations))
if locations is not None:
print(" " + locations[-1].module_path)
print(" " + locations[-1].full_name)
print(" line: {}, col: {}"
.format(locations[-1].line, locations[-1].column))
if (locations and locations[0].module_path is None):
locations = run_with_debug(jedi, 'goto_assignments',
source=source, line=line,
column=column,
path=filename, encoding='utf-8')
print("Location from 'goto_assignments':")
print(" {}".format(locations))
if locations is not None:
print(" " + locations[-1].module_path)
print(" " + locations[-1].full_name)
print(" line: {}, col: {}"
.format(locations[-1].line, locations[-1].column))
if not locations:
return None
else:
loc = locations[-1]
print("Returned location: {}".format(loc))
return loc.module_path
line = 17
column = 29
filename = "trading.py"
with open('trading.py', 'r') as f:
source = "".join(f.readlines())
source = None
rpc_get_definition(filename=filename, source=source,
line=line, column=column)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment