Merge pull request #1203 from ZyX-I/cache-tagbar

Add more precise caching for tagbar segment
This commit is contained in:
Nikolai Aleksandrovich Pavlov 2014-11-30 03:13:41 +03:00
commit bf8f4a1496
5 changed files with 59 additions and 18 deletions

View File

@ -124,6 +124,14 @@ else:
vim_get_func = VimFunc vim_get_func = VimFunc
def vim_get_autoload_func(f, rettype=None):
func = vim_get_func(f)
if not func:
vim.command('runtime! ' + f.replace('#', '/')[:f.rindex('#')] + '.vim')
func = vim_get_func(f)
return func
if hasattr(vim, 'Function'): if hasattr(vim, 'Function'):
def vim_func_exists(f): def vim_func_exists(f):
try: try:
@ -145,6 +153,7 @@ if type(vim) is object:
_getbufvar = vim_get_func('getbufvar') _getbufvar = vim_get_func('getbufvar')
_vim_exists = vim_get_func('exists', rettype='int')
# It may crash on some old vim versions and I do not remember in which patch # It may crash on some old vim versions and I do not remember in which patch
@ -171,14 +180,20 @@ if hasattr(vim, 'vvars') and vim.vvars['version'] > 703:
def vim_getwinvar(segment_info, varname): def vim_getwinvar(segment_info, varname):
return _vim_to_python(segment_info['window'].vars[str(varname)]) return _vim_to_python(segment_info['window'].vars[str(varname)])
def vim_global_exists(name):
try:
vim.vars[name]
except KeyError:
return False
else:
return True
else: else:
_vim_to_python_types = { _vim_to_python_types = {
dict: (lambda value: dict(((k, _vim_to_python(v)) for k, v in value.items()))), dict: (lambda value: dict(((k, _vim_to_python(v)) for k, v in value.items()))),
list: (lambda value: [_vim_to_python(i) for i in value]), list: (lambda value: [_vim_to_python(i) for i in value]),
} }
_vim_exists = vim_get_func('exists', rettype='int')
def vim_getvar(varname): def vim_getvar(varname):
varname = 'g:' + varname varname = 'g:' + varname
if _vim_exists(varname): if _vim_exists(varname):
@ -201,6 +216,13 @@ else:
raise KeyError(varname) raise KeyError(varname)
return result return result
def vim_global_exists(name):
return int(vim.eval('exists("g:' + name + '")'))
def vim_command_exists(name):
return _vim_exists(':' + name)
if sys.version_info < (3,): if sys.version_info < (3,):
getbufvar = _getbufvar getbufvar = _getbufvar

View File

@ -62,11 +62,6 @@ vim_modes = {
} }
eventfuncs = defaultdict(lambda: [])
bufeventfuncs = defaultdict(lambda: [])
defined_events = set()
# TODO Remove cache when needed # TODO Remove cache when needed
def window_cached(func): def window_cached(func):
cache = {} cache = {}

View File

@ -7,6 +7,7 @@ except ImportError:
vim = object() vim = object()
from powerline.segments.vim import window_cached from powerline.segments.vim import window_cached
from powerline.bindings.vim import vim_global_exists
@window_cached @window_cached
@ -21,8 +22,8 @@ def syntastic(pl, err_format='ERR:  {first_line} ({num}) ', warn_format='WARN
Highlight groups used: ``syntastic.warning`` or ``warning``, ``syntastic.error`` or ``error``. Highlight groups used: ``syntastic.warning`` or ``warning``, ``syntastic.error`` or ``error``.
''' '''
if not int(vim.eval('exists("g:SyntasticLoclist")')): if not vim_global_exists('SyntasticLoclist'):
return return None
has_errors = int(vim.eval('g:SyntasticLoclist.current().hasErrorsOrWarningsToDisplay()')) has_errors = int(vim.eval('g:SyntasticLoclist.current().hasErrorsOrWarningsToDisplay()'))
if not has_errors: if not has_errors:
return return

View File

@ -6,11 +6,16 @@ try:
except ImportError: except ImportError:
vim = object() vim = object()
from powerline.segments.vim import window_cached from powerline.bindings.vim import vim_command_exists, vim_get_autoload_func
from powerline.theme import requires_segment_info
@window_cached currenttag = None
def current_tag(pl, flags='s'): tag_cache = {}
@requires_segment_info
def current_tag(segment_info, pl, flags='s'):
'''Return tag that is near the cursor. '''Return tag that is near the cursor.
:param str flags: :param str flags:
@ -25,6 +30,22 @@ def current_tag(pl, flags='s'):
.. _`official documentation`: https://github.com/majutsushi/tagbar/blob/master/doc/tagbar.txt .. _`official documentation`: https://github.com/majutsushi/tagbar/blob/master/doc/tagbar.txt
''' '''
if not int(vim.eval('exists(":Tagbar")')): global currenttag
return None global tag_cache
return vim.eval('tagbar#currenttag("%s", "", "{0}")'.format(flags)) window_id = segment_info['window_id']
if segment_info['mode'] == 'nc':
return tag_cache.get(window_id, (None,))[-1]
if not currenttag:
if vim_command_exists('Tagbar'):
currenttag = vim_get_autoload_func('tagbar#currenttag')
if not currenttag:
return None
else:
return None
prev_key, r = tag_cache.get(window_id, (None, None))
key = (int(vim.eval('b:changedtick')), segment_info['window'].cursor[0])
if prev_key and key == prev_key:
return r
r = currenttag('%s', '', flags)
tag_cache[window_id] = (key, r)
return r

View File

@ -412,9 +412,11 @@ def _emul_bufnr(expr):
@_vim @_vim
def _emul_exists(varname): def _emul_exists(ident):
if varname.startswith('g:'): if ident.startswith('g:'):
return varname[2:] in vars return ident[2:] in vars
elif ident.startswith(':'):
return 0
raise NotImplementedError raise NotImplementedError