2017-02-07 22:53:28 -05:00
|
|
|
"""Jedi-based completer for Python-mode."""
|
|
|
|
import builtins
|
|
|
|
import importlib
|
|
|
|
|
|
|
|
from xonsh.lazyasd import lazyobject, lazybool
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ()
|
|
|
|
|
|
|
|
|
|
|
|
@lazybool
|
|
|
|
def HAS_JEDI():
|
|
|
|
"""``True`` if `jedi` is available, else ``False``."""
|
|
|
|
spec = importlib.util.find_spec('jedi')
|
|
|
|
return (spec is not None)
|
|
|
|
|
|
|
|
|
|
|
|
@lazyobject
|
|
|
|
def jedi():
|
|
|
|
if HAS_JEDI:
|
|
|
|
import jedi as m
|
|
|
|
else:
|
|
|
|
m = None
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
|
|
def complete_jedi(prefix, line, start, end, ctx):
|
|
|
|
"""Jedi-based completer for Python-mode."""
|
|
|
|
if not HAS_JEDI:
|
|
|
|
return set()
|
2018-09-13 17:08:01 -04:00
|
|
|
src = builtins.__xonsh__.shell.shell.accumulated_inputs + line
|
2017-02-07 22:53:28 -05:00
|
|
|
script = jedi.api.Interpreter(src, [ctx], column=end)
|
2019-10-02 15:01:05 -05:00
|
|
|
script_comp = set()
|
|
|
|
try:
|
|
|
|
script_comp = script.completions()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2018-09-13 17:08:01 -04:00
|
|
|
if builtins.__xonsh__.env.get('CASE_SENSITIVE_COMPLETIONS'):
|
2019-10-02 15:01:05 -05:00
|
|
|
rtn = {x.name_with_symbols for x in script_comp
|
|
|
|
if x.name_with_symbols.startswith(prefix)}
|
2017-02-07 22:53:28 -05:00
|
|
|
else:
|
2019-10-02 15:01:05 -05:00
|
|
|
rtn = {x.name_with_symbols for x in script_comp}
|
2017-02-07 22:53:28 -05:00
|
|
|
return rtn
|
|
|
|
|
|
|
|
|
|
|
|
# register the completer
|
2018-09-13 17:08:01 -04:00
|
|
|
builtins.__xonsh__.ctx['complete_jedi'] = complete_jedi
|
2017-02-21 11:56:33 -05:00
|
|
|
completer add jedi complete_jedi end
|
|
|
|
completer remove python_mode
|
2018-09-13 17:08:01 -04:00
|
|
|
del builtins.__xonsh__.ctx['complete_jedi']
|