forked from VimPlug/jedi-vim
Merge pull request #401 from blueyed/reorder-jedi_vim
Reorder jedi_vim.py / improved error handling
This commit is contained in:
@@ -154,6 +154,25 @@ function! jedi#force_py_version_switch()
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
" Helper function instead of `python vim.eval()`, and `.command()` because
|
||||||
|
" these also return error definitions.
|
||||||
|
function! jedi#_vim_exceptions(str, is_eval)
|
||||||
|
let l:result = {}
|
||||||
|
try
|
||||||
|
if a:is_eval
|
||||||
|
let l:result.result = eval(a:str)
|
||||||
|
else
|
||||||
|
execute a:str
|
||||||
|
let l:result.result = ''
|
||||||
|
endif
|
||||||
|
catch
|
||||||
|
let l:result.exception = v:exception
|
||||||
|
let l:result.throwpoint = v:throwpoint
|
||||||
|
endtry
|
||||||
|
return l:result
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
if !jedi#init_python()
|
if !jedi#init_python()
|
||||||
" Do not define any functions when Python initialization failed.
|
" Do not define any functions when Python initialization failed.
|
||||||
finish
|
finish
|
||||||
@@ -324,6 +343,7 @@ function! jedi#configure_call_signatures()
|
|||||||
autocmd CursorMovedI <buffer> PythonJedi jedi_vim.show_call_signatures()
|
autocmd CursorMovedI <buffer> PythonJedi jedi_vim.show_call_signatures()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
" Determine where the current window is on the screen for displaying call
|
" Determine where the current window is on the screen for displaying call
|
||||||
" signatures in the correct column.
|
" signatures in the correct column.
|
||||||
function! s:save_first_col()
|
function! s:save_first_col()
|
||||||
@@ -372,24 +392,6 @@ function! s:save_first_col()
|
|||||||
endtry
|
endtry
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Helper function instead of `python vim.eval()`, and `.command()` because
|
|
||||||
" these also return error definitions.
|
|
||||||
function! jedi#_vim_exceptions(str, is_eval)
|
|
||||||
let l:result = {}
|
|
||||||
try
|
|
||||||
if a:is_eval
|
|
||||||
let l:result.result = eval(a:str)
|
|
||||||
else
|
|
||||||
execute a:str
|
|
||||||
let l:result.result = ''
|
|
||||||
endif
|
|
||||||
catch
|
|
||||||
let l:result.exception = v:exception
|
|
||||||
let l:result.throwpoint = v:throwpoint
|
|
||||||
endtry
|
|
||||||
return l:result
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
|
|
||||||
function! jedi#complete_string(is_popup_on_dot)
|
function! jedi#complete_string(is_popup_on_dot)
|
||||||
|
|
||||||
|
|||||||
132
jedi_vim.py
132
jedi_vim.py
@@ -14,55 +14,31 @@ except ImportError:
|
|||||||
from itertools import izip_longest as zip_longest # Python 2
|
from itertools import izip_longest as zip_longest # Python 2
|
||||||
|
|
||||||
|
|
||||||
def no_jedi_warning():
|
|
||||||
vim.command('echohl WarningMsg | echom "Please install Jedi if you want to use jedi_vim." | echohl None')
|
|
||||||
|
|
||||||
|
|
||||||
def echo_highlight(msg):
|
|
||||||
vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)
|
|
||||||
|
|
||||||
|
|
||||||
import vim
|
|
||||||
try:
|
|
||||||
import jedi
|
|
||||||
except ImportError:
|
|
||||||
no_jedi_warning()
|
|
||||||
jedi = None
|
|
||||||
else:
|
|
||||||
version = jedi.__version__
|
|
||||||
if isinstance(version, str):
|
|
||||||
# the normal use case, now.
|
|
||||||
from jedi import utils
|
|
||||||
version = utils.version_info()
|
|
||||||
if version < (0, 7):
|
|
||||||
echo_highlight('Please update your Jedi version, it is to old.')
|
|
||||||
|
|
||||||
is_py3 = sys.version_info[0] >= 3
|
is_py3 = sys.version_info[0] >= 3
|
||||||
if is_py3:
|
if is_py3:
|
||||||
unicode = str
|
unicode = str
|
||||||
|
|
||||||
|
|
||||||
def catch_and_print_exceptions(func):
|
class PythonToVimStr(unicode):
|
||||||
def wrapper(*args, **kwargs):
|
""" Vim has a different string implementation of single quotes """
|
||||||
try:
|
__slots__ = []
|
||||||
return func(*args, **kwargs)
|
|
||||||
except (Exception, vim.error):
|
|
||||||
print(traceback.format_exc())
|
|
||||||
return None
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
def __new__(cls, obj, encoding='UTF-8'):
|
||||||
def _check_jedi_availability(show_error=False):
|
if is_py3 or isinstance(obj, unicode):
|
||||||
def func_receiver(func):
|
return unicode.__new__(cls, obj)
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
if jedi is None:
|
|
||||||
if show_error:
|
|
||||||
no_jedi_warning()
|
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
return func(*args, **kwargs)
|
return unicode.__new__(cls, obj, encoding)
|
||||||
return wrapper
|
|
||||||
return func_receiver
|
def __repr__(self):
|
||||||
|
# this is totally stupid and makes no sense but vim/python unicode
|
||||||
|
# support is pretty bad. don't ask how I came up with this... It just
|
||||||
|
# works...
|
||||||
|
# It seems to be related to that bug: http://bugs.python.org/issue5876
|
||||||
|
if unicode is str:
|
||||||
|
s = self
|
||||||
|
else:
|
||||||
|
s = self.encode('UTF-8')
|
||||||
|
return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')
|
||||||
|
|
||||||
|
|
||||||
class VimError(Exception):
|
class VimError(Exception):
|
||||||
@@ -88,34 +64,64 @@ def _catch_exception(string, is_eval):
|
|||||||
return result['result']
|
return result['result']
|
||||||
|
|
||||||
|
|
||||||
def vim_eval(string):
|
|
||||||
return _catch_exception(string, 1)
|
|
||||||
|
|
||||||
|
|
||||||
def vim_command(string):
|
def vim_command(string):
|
||||||
_catch_exception(string, 0)
|
_catch_exception(string, 0)
|
||||||
|
|
||||||
|
|
||||||
class PythonToVimStr(unicode):
|
def vim_eval(string):
|
||||||
""" Vim has a different string implementation of single quotes """
|
return _catch_exception(string, 1)
|
||||||
__slots__ = []
|
|
||||||
|
|
||||||
def __new__(cls, obj, encoding='UTF-8'):
|
|
||||||
if is_py3 or isinstance(obj, unicode):
|
|
||||||
return unicode.__new__(cls, obj)
|
|
||||||
else:
|
|
||||||
return unicode.__new__(cls, obj, encoding)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def no_jedi_warning():
|
||||||
# this is totally stupid and makes no sense but vim/python unicode
|
vim.command('echohl WarningMsg | echom "Please install Jedi if you want to use jedi-vim." | echohl None')
|
||||||
# support is pretty bad. don't ask how I came up with this... It just
|
|
||||||
# works...
|
|
||||||
# It seems to be related to that bug: http://bugs.python.org/issue5876
|
def echo_highlight(msg):
|
||||||
if unicode is str:
|
vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)
|
||||||
s = self
|
|
||||||
|
|
||||||
|
import vim
|
||||||
|
try:
|
||||||
|
import jedi
|
||||||
|
except ImportError:
|
||||||
|
no_jedi_warning()
|
||||||
|
jedi = None
|
||||||
else:
|
else:
|
||||||
s = self.encode('UTF-8')
|
try:
|
||||||
return '"%s"' % s.replace('\\', '\\\\').replace('"', r'\"')
|
version = jedi.__version__
|
||||||
|
except Exception as e: # e.g. AttributeError
|
||||||
|
echo_highlight("Could not load jedi python module: {}".format(e))
|
||||||
|
jedi = None
|
||||||
|
else:
|
||||||
|
if isinstance(version, str):
|
||||||
|
# the normal use case, now.
|
||||||
|
from jedi import utils
|
||||||
|
version = utils.version_info()
|
||||||
|
if version < (0, 7):
|
||||||
|
echo_highlight('Please update your Jedi version, it is too old.')
|
||||||
|
|
||||||
|
|
||||||
|
def catch_and_print_exceptions(func):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
try:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
except (Exception, vim.error):
|
||||||
|
print(traceback.format_exc())
|
||||||
|
return None
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def _check_jedi_availability(show_error=False):
|
||||||
|
def func_receiver(func):
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
if jedi is None:
|
||||||
|
if show_error:
|
||||||
|
no_jedi_warning()
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
return func_receiver
|
||||||
|
|
||||||
|
|
||||||
@catch_and_print_exceptions
|
@catch_and_print_exceptions
|
||||||
|
|||||||
Reference in New Issue
Block a user