forked from VimPlug/jedi-vim
Use the environment for completions.
This commit is contained in:
@@ -58,15 +58,6 @@ endfor
|
|||||||
let s:script_path = fnameescape(expand('<sfile>:p:h:h'))
|
let s:script_path = fnameescape(expand('<sfile>:p:h:h'))
|
||||||
|
|
||||||
function! s:init_python() abort
|
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.
|
" Handle "auto" version.
|
||||||
if has('nvim') || (has('python') && has('python3'))
|
if has('nvim') || (has('python') && has('python3'))
|
||||||
" Neovim usually has both python providers. Skipping the `has` check
|
" 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.
|
" Make sure that the auto-detected version is available in Vim.
|
||||||
if !has('nvim') || has('python'.(s:def_py == 2 ? '' : s:def_py))
|
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
|
endif
|
||||||
|
|
||||||
" Add a warning in case the auto-detected version is not available,
|
" Add a warning in case the auto-detected version is not available,
|
||||||
@@ -105,9 +96,9 @@ function! s:init_python() abort
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
if has('python')
|
if has('python')
|
||||||
call jedi#setup_py_version(2)
|
call jedi#setup_python_imports(2)
|
||||||
elseif has('python3')
|
elseif has('python3')
|
||||||
call jedi#setup_py_version(3)
|
call jedi#setup_python_imports(3)
|
||||||
else
|
else
|
||||||
throw 'jedi-vim requires Vim with support for Python 2 or 3.'
|
throw 'jedi-vim requires Vim with support for Python 2 or 3.'
|
||||||
endif
|
endif
|
||||||
@@ -139,7 +130,7 @@ endfunction
|
|||||||
|
|
||||||
|
|
||||||
let s:python_version = 'null'
|
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
|
if a:py_version == 2
|
||||||
let cmd_exec = 'python'
|
let cmd_exec = 'python'
|
||||||
let s:python_version = 2
|
let s:python_version = 2
|
||||||
@@ -147,7 +138,7 @@ function! jedi#setup_py_version(py_version) abort
|
|||||||
let cmd_exec = 'python3'
|
let cmd_exec = 'python3'
|
||||||
let s:python_version = 3
|
let s:python_version = 3
|
||||||
else
|
else
|
||||||
throw 'jedi#setup_py_version: invalid py_version: '.a:py_version
|
throw 'jedi#setup_python_imports: invalid py_version: '.a:py_version
|
||||||
endif
|
endif
|
||||||
|
|
||||||
execute 'command! -nargs=1 PythonJedi '.cmd_exec.' <args>'
|
execute 'command! -nargs=1 PythonJedi '.cmd_exec.' <args>'
|
||||||
@@ -164,12 +155,12 @@ function! jedi#setup_py_version(py_version) abort
|
|||||||
try
|
try
|
||||||
exe 'PythonJedi exec('''.escape(join(init_lines, '\n'), "'").''')'
|
exe 'PythonJedi exec('''.escape(join(init_lines, '\n'), "'").''')'
|
||||||
catch
|
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
|
endtry
|
||||||
if s:init_outcome is 0
|
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
|
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
|
endif
|
||||||
return 1
|
return 1
|
||||||
endfunction
|
endfunction
|
||||||
@@ -252,23 +243,6 @@ function! jedi#debug_info() abort
|
|||||||
endif
|
endif
|
||||||
endfunction
|
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
|
" Helper function instead of `python vim.eval()`, and `.command()` because
|
||||||
" these also return error definitions.
|
" these also return error definitions.
|
||||||
function! jedi#_vim_exceptions(str, is_eval) abort
|
function! jedi#_vim_exceptions(str, is_eval) abort
|
||||||
|
|||||||
@@ -157,6 +157,17 @@ def _check_jedi_availability(show_error=False):
|
|||||||
return func_receiver
|
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
|
@catch_and_print_exceptions
|
||||||
def get_script(source=None, column=None):
|
def get_script(source=None, column=None):
|
||||||
jedi.settings.additional_dynamic_modules = [
|
jedi.settings.additional_dynamic_modules = [
|
||||||
@@ -170,8 +181,12 @@ def get_script(source=None, column=None):
|
|||||||
if column is None:
|
if column is None:
|
||||||
column = vim.current.window.cursor[1]
|
column = vim.current.window.cursor[1]
|
||||||
buf_path = vim.current.buffer.name
|
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)
|
@_check_jedi_availability(show_error=False)
|
||||||
@@ -636,7 +651,7 @@ def py_import():
|
|||||||
args = shsplit(vim.eval('a:args'))
|
args = shsplit(vim.eval('a:args'))
|
||||||
import_path = args.pop()
|
import_path = args.pop()
|
||||||
text = 'import %s' % import_path
|
text = 'import %s' % import_path
|
||||||
scr = jedi.Script(text, 1, len(text), '')
|
scr = jedi.Script(text, 1, len(text), '', environment=_get_environment())
|
||||||
try:
|
try:
|
||||||
completion = scr.goto_assignments()[0]
|
completion = scr.goto_assignments()[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
@@ -659,7 +674,7 @@ def py_import_completions():
|
|||||||
comps = []
|
comps = []
|
||||||
else:
|
else:
|
||||||
text = 'import %s' % argl
|
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()]
|
comps = ['%s%s' % (argl, c.complete) for c in script.completions()]
|
||||||
vim.command("return '%s'" % '\n'.join(comps))
|
vim.command("return '%s'" % '\n'.join(comps))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user