From dd4cb809111a151b4b9423e9a9b0263d070e01e4 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 1 May 2015 22:53:29 +0200 Subject: [PATCH 1/3] Move jedi#_vim_exceptions before call to jedi#init_python It might be used during setup, in the case of errors and needs to be defined then already. --- autoload/jedi.vim | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/autoload/jedi.vim b/autoload/jedi.vim index a2172bf..3194e1d 100644 --- a/autoload/jedi.vim +++ b/autoload/jedi.vim @@ -154,6 +154,25 @@ function! jedi#force_py_version_switch() 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() " Do not define any functions when Python initialization failed. finish @@ -324,6 +343,7 @@ function! jedi#configure_call_signatures() autocmd CursorMovedI PythonJedi jedi_vim.show_call_signatures() endfunction + " Determine where the current window is on the screen for displaying call " signatures in the correct column. function! s:save_first_col() @@ -372,24 +392,6 @@ function! s:save_first_col() endtry 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) From baa296942738da7b82d2de8110cb9e7854e97a8f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 1 May 2015 23:07:54 +0200 Subject: [PATCH 2/3] minor: use 'jedi-vim' in no_jedi_warning; fix typo 'too old' --- jedi_vim.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jedi_vim.py b/jedi_vim.py index 08ccb91..fda7b61 100644 --- a/jedi_vim.py +++ b/jedi_vim.py @@ -15,7 +15,7 @@ except ImportError: def no_jedi_warning(): - vim.command('echohl WarningMsg | echom "Please install Jedi if you want to use jedi_vim." | echohl None') + vim.command('echohl WarningMsg | echom "Please install Jedi if you want to use jedi-vim." | echohl None') def echo_highlight(msg): @@ -35,7 +35,7 @@ else: from jedi import utils version = utils.version_info() if version < (0, 7): - echo_highlight('Please update your Jedi version, it is to old.') + echo_highlight('Please update your Jedi version, it is too old.') is_py3 = sys.version_info[0] >= 3 if is_py3: From 527562208cb3c78a933f7a18db52a28120bedc26 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 1 May 2015 23:50:06 +0200 Subject: [PATCH 3/3] jedi_vim: reorder to use echo_highlight in try-import block This uses `echo_highlight` for exceptions caused by an empty "jedi" folder. --- jedi_vim.py | 134 +++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/jedi_vim.py b/jedi_vim.py index fda7b61..21a2f3e 100644 --- a/jedi_vim.py +++ b/jedi_vim.py @@ -14,55 +14,31 @@ except ImportError: 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 too old.') - is_py3 = sys.version_info[0] >= 3 if is_py3: unicode = str -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 +class PythonToVimStr(unicode): + """ Vim has a different string implementation of single quotes """ + __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 _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 + 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): @@ -87,34 +63,64 @@ def _catch_exception(string, is_eval): return result['result'] -def vim_eval(string): - return _catch_exception(string, 1) - - def vim_command(string): _catch_exception(string, 0) -class PythonToVimStr(unicode): - """ Vim has a different string implementation of single quotes """ - __slots__ = [] +def vim_eval(string): + return _catch_exception(string, 1) - 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): - # 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'\"') +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: + try: + 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