Refactor init method: add jedi#init_python

- jedi#init_python is used to init the Python environment.
 - jedi#setup_py_version is the single point where PythonJedi is
   defined, and will configure `PythonJedi` to output an error in case
   initialization failed.
 - jedi#force_py_version_switch will throw an error when
   g:jedi#force_py_version is not handled (e.g. invalid or "auto" and
   unresolved).
 - When g:jedi#force_py_version is provided, it will be used always.
 - Use WarningMsg highlight for errors, which are centralized and
   handled through exceptions.
This commit is contained in:
Daniel Hahler
2015-04-14 22:36:11 +02:00
parent 2d74fe6c51
commit e43012afc8
4 changed files with 79 additions and 36 deletions

View File

@@ -1,3 +1,7 @@
if !jedi#init_python()
finish
endif
if g:jedi#auto_initialization if g:jedi#auto_initialization
if g:jedi#completions_enabled if g:jedi#completions_enabled
" We need our own omnifunc, so this overrides the omnifunc set by " We need our own omnifunc, so this overrides the omnifunc set by

View File

@@ -1,6 +1,10 @@
if !jedi#init_python()
finish
endif
if g:jedi#show_call_signatures > 0 && has('conceal') if g:jedi#show_call_signatures > 0 && has('conceal')
" +conceal is the default for vim >= 7.3 " +conceal is the default for vim >= 7.3
let s:e = g:jedi#call_signature_escape let s:e = g:jedi#call_signature_escape
let s:full = s:e.'jedi=.\{-}'.s:e.'.\{-}'.s:e.'jedi'.s:e let s:full = s:e.'jedi=.\{-}'.s:e.'.\{-}'.s:e.'jedi'.s:e
let s:ignore = s:e.'jedi.\{-}'.s:e let s:ignore = s:e.'jedi.\{-}'.s:e
@@ -13,11 +17,11 @@ if g:jedi#show_call_signatures > 0 && has('conceal')
\ .' contains=jediIgnore,jediFat,jediSpace' \ .' contains=jediIgnore,jediFat,jediSpace'
\ .' containedin=pythonComment,pythonString,pythonRawString' \ .' containedin=pythonComment,pythonString,pythonRawString'
unlet! s:e s:full s:ignore unlet! s:e s:full s:ignore
hi def link jediIgnore Ignore hi def link jediIgnore Ignore
hi def link jediFatSymbol Ignore hi def link jediFatSymbol Ignore
hi def link jediSpace Normal hi def link jediSpace Normal
if exists('g:colors_name') if exists('g:colors_name')
hi def link jediFunction CursorLine hi def link jediFunction CursorLine
hi def link jediFat TabLine hi def link jediFat TabLine

View File

@@ -254,21 +254,31 @@ function! jedi#complete_opened()
endfunction endfunction
function! jedi#setup_py_version(py_version)
if a:py_version == 2
let cmd_init = 'pyfile'
let cmd_exec = 'python'
elseif a:py_version == 3
let cmd_init = 'py3file'
let cmd_exec = 'python3'
else
throw "jedi#setup_py_version: invalid py_version: ".a:py_version
endif
try
execute cmd_init.' '.s:script_path.'/initialize.py'
execute 'command! -nargs=1 PythonJedi '.cmd_exec.' <args>'
return 1
catch
execute 'command! -nargs=1 PythonJedi echoerr "Jedi failed to initialize: '.v:exception.'"'
throw "jedi#setup_py_version: ".v:exception
endtry
endfunction
function! jedi#force_py_version(py_version) function! jedi#force_py_version(py_version)
let g:jedi#force_py_version = a:py_version let g:jedi#force_py_version = a:py_version
try return jedi#setup_py_version(a:py_version)
if g:jedi#force_py_version == 2
command! -nargs=1 PythonJedi python <args>
execute 'pyfile '.s:script_path.'/initialize.py'
elseif g:jedi#force_py_version == 3
command! -nargs=1 PythonJedi python3 <args>
execute 'py3file '.s:script_path.'/initialize.py'
endif
catch
if !exists("g:jedi#squelch_py_warning")
echom "jedi#force_py_version failed: " . v:exception
endif
endtry
endfunction endfunction
@@ -277,6 +287,8 @@ function! jedi#force_py_version_switch()
call jedi#force_py_version(3) call jedi#force_py_version(3)
elseif g:jedi#force_py_version == 3 elseif g:jedi#force_py_version == 3
call jedi#force_py_version(2) call jedi#force_py_version(2)
else
throw "Don't know how to switch from ".g:jedi#force_py_version."!"
endif endif
endfunction endfunction
@@ -343,10 +355,16 @@ call s:init()
let s:script_path = fnameescape(expand('<sfile>:p:h:h')) let s:script_path = fnameescape(expand('<sfile>:p:h:h'))
if g:jedi#force_py_version != 'auto' function! s:init_python()
" Always use the user supplied version. if g:jedi#force_py_version != 'auto'
call jedi#force_py_version(g:jedi#force_py_version) " Always use the user supplied version.
else try
return jedi#force_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
@@ -356,33 +374,50 @@ else
let s:def_py = system("python -c 'import sys; sys.stdout.write(str(sys.version_info[0]))'") let s:def_py = system("python -c 'import sys; sys.stdout.write(str(sys.version_info[0]))'")
if v:shell_error != 0 || !len(s:def_py) if v:shell_error != 0 || !len(s:def_py)
if !exists("g:jedi#squelch_py_warning") if !exists("g:jedi#squelch_py_warning")
echomsg "Error: jedi-vim failed to get Python version from sys.version_info: " . s:def_py echohl WarningMsg
echomsg "Falling back to version 2." echom "Warning: jedi-vim failed to get Python version from sys.version_info: " . s:def_py
echom "Falling back to version 2."
echohl None
endif endif
let s:def_py = 2 let s:def_py = 2
elseif &verbose
echom "jedi-vim: auto-detected Python: ".s:def_py
endif endif
" 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))
call jedi#force_py_version(s:def_py) return jedi#force_py_version(s:def_py)
endif endif
end end
if g:jedi#force_py_version == 'auto' if has('python')
" If it's still "auto", it wasn't handled above. call jedi#setup_py_version(2)
if has('python') elseif has('python3')
command! -nargs=1 PythonJedi python <args> call jedi#setup_py_version(3)
execute 'pyfile '.s:script_path.'/initialize.py' else
elseif has('python3') throw "jedi-vim requires Vim with support for Python 2 or 3."
command! -nargs=1 PythonJedi python3 <args> end
execute 'py3file '.s:script_path.'/initialize.py' return 1
else endfunction
function! jedi#init_python()
if !exists('s:_init_python')
try
let s:_init_python = s:init_python()
catch
if !exists("g:jedi#squelch_py_warning") if !exists("g:jedi#squelch_py_warning")
echomsg "Error: jedi-vim requires vim compiled with +python" echohl WarningMsg
echom "Error: jedi-vim failed to initialize Python: ".v:exception
echohl None
endif endif
end let s:_init_python = 0
endtry
endif endif
endif return s:_init_python
endfunction
call jedi#init_python()
"PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False) "PythonJedi jedi_vim.jedi.set_debug_function(jedi_vim.print_to_stdout, speed=True, warnings=False, notices=False)

View File

@@ -1,4 +1,4 @@
if !has('python') && !has('python3') if !jedi#init_python()
finish finish
endif endif
" ------------------------------------------------------------------------ " ------------------------------------------------------------------------