Merge pull request #979 from ZyX-I/fix-add_local_theme

Fix VimPowerline.add_local_theme
This commit is contained in:
Nikolai Aleksandrovich Pavlov 2014-08-13 07:56:43 +04:00
commit 7453e58680
4 changed files with 72 additions and 12 deletions

View File

@ -389,9 +389,16 @@ class Powerline(object):
if interval is not None and not self.config_loader.is_alive(): if interval is not None and not self.config_loader.is_alive():
self.config_loader.start() self.config_loader.start()
self.default_top_theme = self.common_config['default_top_theme']
self.ext_config = config['ext'][self.ext] self.ext_config = config['ext'][self.ext]
self.theme_levels = (
os.path.join('themes', (
self.ext_config.get('top_theme')
or self.common_config['default_top_theme']
)),
os.path.join('themes', self.ext, '__main__'),
)
if self.ext_config != self.prev_ext_config: if self.ext_config != self.prev_ext_config:
ext_config_differs = True ext_config_differs = True
if ( if (
@ -468,8 +475,18 @@ class Powerline(object):
''' '''
return get_config_paths() return get_config_paths()
def _load_config(self, cfg_path, cfg_type): def load_config(self, cfg_path, cfg_type):
'''Load configuration and setup watches.''' '''Load configuration and setup watches
:param str cfg_path:
Path to the configuration file without any powerline configuration
directory or ``.json`` suffix.
:param str cfg_type:
Configuration type. May be one of ``main`` (for ``config.json``
file), ``colors``, ``colorscheme``, ``theme``.
:return: dictionary with loaded configuration.
'''
return load_config( return load_config(
cfg_path, cfg_path,
self.find_config_files, self.find_config_files,
@ -487,7 +504,7 @@ class Powerline(object):
:return: dictionary with :ref:`top-level configuration <config-main>`. :return: dictionary with :ref:`top-level configuration <config-main>`.
''' '''
return self._load_config('config', 'main') return self.load_config('config', 'main')
def _load_hierarhical_config(self, cfg_type, levels, ignore_levels): def _load_hierarhical_config(self, cfg_type, levels, ignore_levels):
'''Load and merge multiple configuration files '''Load and merge multiple configuration files
@ -509,7 +526,7 @@ class Powerline(object):
exceptions = [] exceptions = []
for i, cfg_path in enumerate(levels): for i, cfg_path in enumerate(levels):
try: try:
lvl_config = self._load_config(cfg_path, cfg_type) lvl_config = self.load_config(cfg_path, cfg_type)
except IOError as e: except IOError as e:
if sys.version_info < (3,): if sys.version_info < (3,):
tb = sys.exc_info()[2] tb = sys.exc_info()[2]
@ -553,9 +570,7 @@ class Powerline(object):
:return: dictionary with :ref:`theme configuration <config-themes>` :return: dictionary with :ref:`theme configuration <config-themes>`
''' '''
levels = ( levels = self.theme_levels + (
os.path.join('themes', self.ext_config.get('top_theme') or self.default_top_theme),
os.path.join('themes', self.ext, '__main__'),
os.path.join('themes', self.ext, name), os.path.join('themes', self.ext, name),
) )
return self._load_hierarhical_config('theme', levels, (0, 1,)) return self._load_hierarhical_config('theme', levels, (0, 1,))
@ -565,7 +580,7 @@ class Powerline(object):
:return: dictionary with :ref:`colors configuration <config-colors>`. :return: dictionary with :ref:`colors configuration <config-colors>`.
''' '''
return self._load_config('colors', 'colors') return self.load_config('colors', 'colors')
@staticmethod @staticmethod
def get_local_themes(local_themes): def get_local_themes(local_themes):

View File

@ -51,8 +51,17 @@ class VimPowerline(Powerline):
''' '''
self.update_renderer() self.update_renderer()
key = self.get_matcher(key) key = self.get_matcher(key)
theme_config = {}
for cfg_path in self.theme_levels:
try:
lvl_config = self.load_config(cfg_path, 'theme')
except IOError:
pass
else:
mergedicts(theme_config, lvl_config)
mergedicts(theme_config, config)
try: try:
self.renderer.add_local_theme(key, {'config': config}) self.renderer.add_local_theme(key, {'config': theme_config})
except KeyError: except KeyError:
return False return False
else: else:
@ -69,10 +78,11 @@ class VimPowerline(Powerline):
'powerline_theme_overrides__' + name) 'powerline_theme_overrides__' + name)
def get_local_themes(self, local_themes): def get_local_themes(self, local_themes):
self.get_matcher = gen_matcher_getter(self.ext, self.import_paths)
if not local_themes: if not local_themes:
return {} return {}
self.get_matcher = gen_matcher_getter(self.ext, self.import_paths)
return dict(((None if key == '__tabline__' else self.get_matcher(key), return dict(((None if key == '__tabline__' else self.get_matcher(key),
{'config': self.load_theme_config(val)}) {'config': self.load_theme_config(val)})
for key, val in local_themes.items())) for key, val in local_themes.items()))

7
tests/matchers.py Normal file
View File

@ -0,0 +1,7 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
def always_true(matcher_info):
return True

View File

@ -402,6 +402,34 @@ class TestVim(TestCase):
vim_module._environ['TEST'] = 'def' vim_module._environ['TEST'] = 'def'
self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_3_8404992_4_192_underline#\xa0def%#Pl_4_192_NONE_None_NONE#>>') self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_3_8404992_4_192_underline#\xa0def%#Pl_4_192_NONE_None_NONE#>>')
def test_local_themes(self):
# Regression test: VimPowerline.add_local_theme did not work properly.
from powerline.vim import VimPowerline
import powerline as powerline_module
import vim
with swap_attributes(config, powerline_module):
with get_powerline_raw(config, VimPowerline) as powerline:
powerline.add_local_theme('tests.matchers.always_true', {
'segment_data': {
'foo': {
'contents': '“bar”'
}
},
'segments': {
'left': [
{
'type': 'string',
'name': 'foo',
'highlight_group': ['g1']
}
]
}
})
window = vim_module.current.window
window_id = 1
winnr = window.number
self.assertEqual(powerline.render(window, window_id, winnr), '%#Pl_5_12583104_6_32896_NONE#\xa0\u201cbar\u201d%#Pl_6_32896_NONE_None_NONE#>>')
def setUpModule(): def setUpModule():
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path'))) sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path')))