Merge pull request #1203 from ZyX-I/cache-tagbar
Add more precise caching for tagbar segment
This commit is contained in:
commit
bf8f4a1496
|
@ -124,6 +124,14 @@ else:
|
|||
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'):
|
||||
def vim_func_exists(f):
|
||||
try:
|
||||
|
@ -145,6 +153,7 @@ if type(vim) is object:
|
|||
|
||||
|
||||
_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
|
||||
|
@ -171,14 +180,20 @@ if hasattr(vim, 'vvars') and vim.vvars['version'] > 703:
|
|||
|
||||
def vim_getwinvar(segment_info, 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:
|
||||
_vim_to_python_types = {
|
||||
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]),
|
||||
}
|
||||
|
||||
_vim_exists = vim_get_func('exists', rettype='int')
|
||||
|
||||
def vim_getvar(varname):
|
||||
varname = 'g:' + varname
|
||||
if _vim_exists(varname):
|
||||
|
@ -201,6 +216,13 @@ else:
|
|||
raise KeyError(varname)
|
||||
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,):
|
||||
getbufvar = _getbufvar
|
||||
|
|
|
@ -62,11 +62,6 @@ vim_modes = {
|
|||
}
|
||||
|
||||
|
||||
eventfuncs = defaultdict(lambda: [])
|
||||
bufeventfuncs = defaultdict(lambda: [])
|
||||
defined_events = set()
|
||||
|
||||
|
||||
# TODO Remove cache when needed
|
||||
def window_cached(func):
|
||||
cache = {}
|
||||
|
|
|
@ -7,6 +7,7 @@ except ImportError:
|
|||
vim = object()
|
||||
|
||||
from powerline.segments.vim import window_cached
|
||||
from powerline.bindings.vim import vim_global_exists
|
||||
|
||||
|
||||
@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``.
|
||||
'''
|
||||
if not int(vim.eval('exists("g:SyntasticLoclist")')):
|
||||
return
|
||||
if not vim_global_exists('SyntasticLoclist'):
|
||||
return None
|
||||
has_errors = int(vim.eval('g:SyntasticLoclist.current().hasErrorsOrWarningsToDisplay()'))
|
||||
if not has_errors:
|
||||
return
|
||||
|
|
|
@ -6,11 +6,16 @@ try:
|
|||
except ImportError:
|
||||
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
|
||||
def current_tag(pl, flags='s'):
|
||||
currenttag = None
|
||||
tag_cache = {}
|
||||
|
||||
|
||||
@requires_segment_info
|
||||
def current_tag(segment_info, pl, flags='s'):
|
||||
'''Return tag that is near the cursor.
|
||||
|
||||
: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
|
||||
'''
|
||||
if not int(vim.eval('exists(":Tagbar")')):
|
||||
return None
|
||||
return vim.eval('tagbar#currenttag("%s", "", "{0}")'.format(flags))
|
||||
global currenttag
|
||||
global tag_cache
|
||||
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
|
||||
|
|
|
@ -412,9 +412,11 @@ def _emul_bufnr(expr):
|
|||
|
||||
|
||||
@_vim
|
||||
def _emul_exists(varname):
|
||||
if varname.startswith('g:'):
|
||||
return varname[2:] in vars
|
||||
def _emul_exists(ident):
|
||||
if ident.startswith('g:'):
|
||||
return ident[2:] in vars
|
||||
elif ident.startswith(':'):
|
||||
return 0
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue