Implement caching for tagbar segment

Ref #700
This commit is contained in:
ZyX 2014-11-30 01:52:20 +03:00
parent 2304242ca2
commit f7d7e274f4
1 changed files with 20 additions and 4 deletions

View File

@ -1,15 +1,21 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
from powerline.segments.vim import window_cached
try:
import vim
except ImportError:
vim = object()
from powerline.bindings.vim import vim_command_exists, vim_get_autoload_func
from powerline.theme import requires_segment_info
currenttag = None
tag_cache = {}
@window_cached
def current_tag(pl, flags='s'):
@requires_segment_info
def current_tag(segment_info, pl, flags='s'):
'''Return tag that is near the cursor.
:param str flags:
@ -25,6 +31,10 @@ def current_tag(pl, flags='s'):
.. _`official documentation`: https://github.com/majutsushi/tagbar/blob/master/doc/tagbar.txt
'''
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')
@ -32,4 +42,10 @@ def current_tag(pl, flags='s'):
return None
else:
return None
return currenttag('%s', '', flags)
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