Use the environment for completions.

This commit is contained in:
Dave Halter
2018-04-04 09:46:43 +02:00
parent 57cbf1421f
commit 72a2643f7c
2 changed files with 27 additions and 38 deletions

View File

@@ -58,15 +58,6 @@ endfor
let s:script_path = fnameescape(expand('<sfile>:p:h:h'))
function! s:init_python() abort
if g:jedi#force_py_version !=# 'auto'
" Always use the user supplied version.
try
return jedi#setup_py_version(g:jedi#force_py_version)
catch
throw 'Could not setup g:jedi#force_py_version: '.v:exception
endtry
endif
" Handle "auto" version.
if has('nvim') || (has('python') && has('python3'))
" Neovim usually has both python providers. Skipping the `has` check
@@ -88,7 +79,7 @@ function! s:init_python() abort
" Make sure that the auto-detected version is available in Vim.
if !has('nvim') || has('python'.(s:def_py == 2 ? '' : s:def_py))
return jedi#setup_py_version(s:def_py)
return jedi#setup_python_imports(s:def_py)
endif
" Add a warning in case the auto-detected version is not available,
@@ -105,9 +96,9 @@ function! s:init_python() abort
endif
if has('python')
call jedi#setup_py_version(2)
call jedi#setup_python_imports(2)
elseif has('python3')
call jedi#setup_py_version(3)
call jedi#setup_python_imports(3)
else
throw 'jedi-vim requires Vim with support for Python 2 or 3.'
endif
@@ -139,7 +130,7 @@ endfunction
let s:python_version = 'null'
function! jedi#setup_py_version(py_version) abort
function! jedi#setup_python_imports(py_version) abort
if a:py_version == 2
let cmd_exec = 'python'
let s:python_version = 2
@@ -147,7 +138,7 @@ function! jedi#setup_py_version(py_version) abort
let cmd_exec = 'python3'
let s:python_version = 3
else
throw 'jedi#setup_py_version: invalid py_version: '.a:py_version
throw 'jedi#setup_python_imports: invalid py_version: '.a:py_version
endif
execute 'command! -nargs=1 PythonJedi '.cmd_exec.' <args>'
@@ -164,12 +155,12 @@ function! jedi#setup_py_version(py_version) abort
try
exe 'PythonJedi exec('''.escape(join(init_lines, '\n'), "'").''')'
catch
throw printf('jedi#setup_py_version: failed to run Python for initialization: %s.', v:exception)
throw printf('jedi#setup_python_imports: failed to run Python for initialization: %s.', v:exception)
endtry
if s:init_outcome is 0
throw 'jedi#setup_py_version: failed to run Python for initialization.'
throw 'jedi#setup_python_imports: failed to run Python for initialization.'
elseif s:init_outcome isnot 1
throw printf('jedi#setup_py_version: %s.', s:init_outcome)
throw printf('jedi#setup_python_imports: %s.', s:init_outcome)
endif
return 1
endfunction
@@ -252,23 +243,6 @@ function! jedi#debug_info() abort
endif
endfunction
function! jedi#force_py_version(py_version) abort
let g:jedi#force_py_version = a:py_version
return jedi#setup_py_version(a:py_version)
endfunction
function! jedi#force_py_version_switch() abort
if g:jedi#force_py_version == 2
call jedi#force_py_version(3)
elseif g:jedi#force_py_version == 3
call jedi#force_py_version(2)
else
throw "Don't know how to switch from ".g:jedi#force_py_version.'!'
endif
endfunction
" Helper function instead of `python vim.eval()`, and `.command()` because
" these also return error definitions.
function! jedi#_vim_exceptions(str, is_eval) abort

View File

@@ -157,6 +157,17 @@ def _check_jedi_availability(show_error=False):
return func_receiver
def _get_environment():
force_python_version = vim_eval("g:jedi#force_py_version")
environment = jedi.get_default_environment()
if force_python_version != "auto":
try:
environment = jedi.api.environment.get_python_environment('python' + force_python_version)
except jedi.InvalidPythonEnvironment:
pass
return environment
@catch_and_print_exceptions
def get_script(source=None, column=None):
jedi.settings.additional_dynamic_modules = [
@@ -170,8 +181,12 @@ def get_script(source=None, column=None):
if column is None:
column = vim.current.window.cursor[1]
buf_path = vim.current.buffer.name
encoding = vim_eval('&encoding') or 'latin1'
return jedi.Script(source, row, column, buf_path, encoding)
return jedi.Script(
source, row, column, buf_path,
encoding=vim_eval('&encoding') or 'latin1',
environment=_get_environment(),
)
@_check_jedi_availability(show_error=False)
@@ -636,7 +651,7 @@ def py_import():
args = shsplit(vim.eval('a:args'))
import_path = args.pop()
text = 'import %s' % import_path
scr = jedi.Script(text, 1, len(text), '')
scr = jedi.Script(text, 1, len(text), '', environment=_get_environment())
try:
completion = scr.goto_assignments()[0]
except IndexError:
@@ -659,7 +674,7 @@ def py_import_completions():
comps = []
else:
text = 'import %s' % argl
script = jedi.Script(text, 1, len(text), '')
script = jedi.Script(text, 1, len(text), '', environment=_get_environment())
comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
vim.command("return '%s'" % '\n'.join(comps))