mirror of
https://github.com/davidhalter/jedi-vim.git
synced 2025-12-07 11:14:32 +08:00
Trying to improve jedi-vim, when Jedi is not found. Fixes #263.
This commit is contained in:
56
jedi_vim.py
56
jedi_vim.py
@@ -13,8 +13,29 @@ try:
|
||||
except ImportError:
|
||||
from itertools import izip_longest as zip_longest # Python 2
|
||||
|
||||
|
||||
def no_jedi_warning():
|
||||
vim.command('echoerr "Please install Jedi if you want to use jedi_vim."')
|
||||
|
||||
|
||||
def echo_highlight(msg):
|
||||
vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)
|
||||
|
||||
|
||||
import vim
|
||||
import jedi
|
||||
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
|
||||
if is_py3:
|
||||
@@ -31,6 +52,19 @@ def catch_and_print_exceptions(func):
|
||||
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
|
||||
|
||||
|
||||
class VimError(Exception):
|
||||
def __init__(self, message, throwpoint, executing):
|
||||
super(type(self), self).__init__(message)
|
||||
@@ -61,10 +95,6 @@ def vim_command(string):
|
||||
_catch_exception(string, 0)
|
||||
|
||||
|
||||
def echo_highlight(msg):
|
||||
vim_command('echohl WarningMsg | echom "%s" | echohl None' % msg)
|
||||
|
||||
|
||||
class PythonToVimStr(unicode):
|
||||
""" Vim has a different string implementation of single quotes """
|
||||
__slots__ = []
|
||||
@@ -101,6 +131,7 @@ def get_script(source=None, column=None):
|
||||
return jedi.Script(source, row, column, buf_path, encoding)
|
||||
|
||||
|
||||
@_check_jedi_availability(show_error=False)
|
||||
@catch_and_print_exceptions
|
||||
def completions():
|
||||
row, column = vim.current.window.cursor
|
||||
@@ -156,6 +187,7 @@ def completions():
|
||||
vim.command('return ' + strout)
|
||||
|
||||
|
||||
@_check_jedi_availability(show_error=True)
|
||||
@catch_and_print_exceptions
|
||||
def goto(is_definition=False, is_related_name=False, no_output=False):
|
||||
definitions = []
|
||||
@@ -208,6 +240,7 @@ def goto(is_definition=False, is_related_name=False, no_output=False):
|
||||
return definitions
|
||||
|
||||
|
||||
@_check_jedi_availability(show_error=True)
|
||||
@catch_and_print_exceptions
|
||||
def show_documentation():
|
||||
script = get_script()
|
||||
@@ -230,6 +263,7 @@ def show_documentation():
|
||||
text = ('\n' + '-' * 79 + '\n').join(docs)
|
||||
vim.command('let l:doc = %s' % repr(PythonToVimStr(text)))
|
||||
vim.command('let l:doc_lines = %s' % len(text.split('\n')))
|
||||
return True
|
||||
|
||||
|
||||
@catch_and_print_exceptions
|
||||
@@ -257,6 +291,7 @@ def clear_call_signatures():
|
||||
vim.current.window.cursor = cursor
|
||||
|
||||
|
||||
@_check_jedi_availability(show_error=False)
|
||||
@catch_and_print_exceptions
|
||||
def show_call_signatures(signatures=()):
|
||||
if vim_eval("has('conceal') && g:jedi#show_call_signatures") == '0':
|
||||
@@ -376,6 +411,7 @@ def cmdline_call_signatures(signatures):
|
||||
% (spaces, signatures[0].call_name, text))
|
||||
|
||||
|
||||
@_check_jedi_availability(show_error=True)
|
||||
@catch_and_print_exceptions
|
||||
def rename():
|
||||
if not int(vim.eval('a:0')):
|
||||
@@ -428,6 +464,7 @@ def rename():
|
||||
echo_highlight('Jedi did %s renames!' % len(temp_rename))
|
||||
|
||||
|
||||
@_check_jedi_availability(show_error=True)
|
||||
@catch_and_print_exceptions
|
||||
def py_import():
|
||||
# args are the same as for the :edit command
|
||||
@@ -538,12 +575,3 @@ def escape_file_path(path):
|
||||
|
||||
def print_to_stdout(level, str_out):
|
||||
print(str_out)
|
||||
|
||||
|
||||
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.')
|
||||
|
||||
Reference in New Issue
Block a user