Skip to content

Commit

Permalink
Fix some problems
Browse files Browse the repository at this point in the history
This variant also supports Neovim, ref powerline#1287.
  • Loading branch information
ZyX-I committed Jun 4, 2017
1 parent f4c69b0 commit 4fbadcf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 34 deletions.
11 changes: 9 additions & 2 deletions powerline/editors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ def __rxor__(self, other):
def __getitem__(self, index):
return EditorIndex(self, index)

def __call__(self, *args):
return EditorFunc(self, *args)

def __eq__(self, other):
return (self is other or (type(self) is type(other) and self.__dict__ == other.__dict__))

Expand All @@ -194,15 +197,19 @@ def __repr__(self):
class EditorNumber(int, EditorObj):
'''Class representing integer literal
'''
pass
def __repr__(self):
return '{0}({1})'.format(
self.__class__.__name__, super(EditorNumber, self).__repr__())


class EditorStr(unicode, EditorObj):
'''Class representing text string literal
.. note: Not a binary string.
'''
pass
def __repr__(self):
return '{0}({1})'.format(
self.__class__.__name__, super(EditorStr, self).__repr__())


class EditorList(EditorObj):
Expand Down
42 changes: 28 additions & 14 deletions powerline/editors/vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from itertools import chain

from powerline.lib.dict import updated
from powerline.lib.unicode import unicode
from powerline.editors import (Editor, param_updated, iterparam_updated,
EditorObj, EditorBinaryOp, EditorTernaryOp,
EditorEmpty, EditorIndex, EditorFunc, EditorStr,
Expand All @@ -20,7 +21,7 @@
EditorBufferNameBase, EditorMap,
EditorWindowList, EditorTabList,
EditorWindowBuffer, EditorTabWindowBuffer,
EditorTabWindow, EditorTabAmount,
EditorTabWindow, EditorTabAmount, EditorNumber,
toedobj)


Expand Down Expand Up @@ -209,6 +210,11 @@ def raising(exc):
'tovim': lambda self, toed, **kw: (
"'" + self.replace("'", "''") + "'"
),
'tovimpy': lambda self, toed, **kw: unicode.__repr__(self),
},
EditorNumber: {
'tovim': lambda self, toed, **kw: int.__repr__(self),
'tovimpy': lambda self, toed, **kw: int.__repr__(self),
},
EditorList: {
'toed': lambda self, toed, **kw: (
Expand All @@ -229,7 +235,7 @@ def raising(exc):
if_true=toed(self.if_true, **kw),
if_false=toed(self.if_false, **kw),
),
'tovimpy': lambda self, toed, **kw: '({if_true}) if ({condition}) else ({if_false})'.format(
'tovimpy': lambda self, toed, **kw: '(({if_true}) if ({condition}) else ({if_false}))'.format(
condition=toed(self.condition, **kw),
if_true=toed(self.if_true, **kw),
if_false=toed(self.if_false, **kw),
Expand All @@ -241,17 +247,25 @@ def raising(exc):
EditorIndex: {
'toed': lambda self, toed, **kw: (
toed(self.obj, **kw) + (
'[' + ' : '.join((toed(i, **kw) for i in self.index)) + ' + 1]'
'[' + ' : '.join((toed(idx if i == 0 else toedobj(idx - 1), **kw)
for i, idx in enumerate(self.index))) + ']'
)
),
},
EditorFunc: {
'tovim': lambda self, toed, **kw: (
self.name + '(' + ', '.join((toed(arg, **kw) for arg in self.args)) + ')'
(
'(' + toed(self.name, **kw) + ')'
if isinstance(self.name, EditorObj) else
self.name
) + '(' + ', '.join((toed(arg, **kw) for arg in self.args)) + ')'
),
'tovimpy': lambda self, toed, **kw: (
'vim_funcs[' + repr(self.name) + ']'
+ '(' + ', '.join((toed(arg, **kw) for arg in self.args)) + ')'
(
'(' + toed(self.name, **kw) + ')'
if isinstance(self.name, EditorObj) else
'vim_funcs[' + repr(self.name) + ']'
) + '(' + ', '.join((toed(arg, **kw) for arg in self.args)) + ')'
),
},
VimGlobalOption: {
Expand Down Expand Up @@ -759,14 +773,12 @@ def compile_themes_getter(cls, local_themes, **kwargs):
pycode = ['lambda pl, matcher_info: (']
for i, m in enumerate(local_themes):
matcher = m[0]
if callable(matcher):
if isinstance(matcher, EditorObj):
code += [cls.toed(matcher, **kwargs) + '?' + str(i + 1) + ':']
elif matcher is not None:
pycode += [
'{i} if local_themes[{i}][0](pl, matcher_info) else'.format(i=i)
]
elif matcher is None:
pass
else:
code += [cls.toed(matcher, **kwargs) + '?' + str(i + 1) + ':']
pycode += ['0)']
code += ['0']
return ''.join(code), eval('\n'.join(pycode), {'local_themes': local_themes})
Expand Down Expand Up @@ -831,6 +843,8 @@ def compile_reqs_dict(cls, reqs_dict, vim_funcs, vim, **kwargs):
','
]
code[-1] = '}'
# for i, v in enumerate(code):
# print(i + 1, v)
ret = eval('\n'.join(code), gvars)
ret.source = code
return ret
Expand Down Expand Up @@ -873,15 +887,15 @@ def compile_themes_getter(cls, local_themes, vim_funcs, vim, **kwargs):
code = ['lambda pl, matcher_info, theme, buffer, window, tabpage: (']
for i, m in enumerate(local_themes):
matcher, theme = m
if callable(matcher):
if isinstance(matcher, EditorObj):
code += [
'local_themes[{0}][1]'.format(i),
'if local_themes[i][0](pl=pl, matcher_info=matcher_info) else',
'if ' + cls.toed(matcher, **kwargs) + ' else',
]
elif matcher is not None:
code += [
'local_themes[{0}][1]'.format(i),
'if ' + cls.toed(matcher, **kwargs) + ' else',
'if local_themes[{0}][0](pl=pl, matcher_info=matcher_info) else'.format(i),
]
code += ['theme', ')']
return eval('\n'.join(code), updated(init_globals, {
Expand Down
33 changes: 19 additions & 14 deletions powerline/segments/vim/plugin/syntastic.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

from powerline.theme import requires_segment_info
from powerline.segments.vim import window_cached
from powerline.bindings.vim import vim_global_exists
from powerline.editors import EditorFunc, EditorTernaryOp, with_input


@window_cached
@requires_segment_info
@with_input((
'syntastic_errors',
(EditorTernaryOp(
EditorFunc('eval',
'exists("g:SyntasticLoclist") '
'&& g:SyntasticLoclist.current().hasErrorsOrWarningsToDisplay()'),
EditorFunc('eval',
'[g:SyntasticLoclist.current().errors(),'
' g:SyntasticLoclist.current().warnings()]'),
0,
),),
'literal',
))
def syntastic(pl, segment_info,
err_format='ERR:  {first_line} ({num}) ',
warn_format='WARN:  {first_line} ({num}) '):
Expand All @@ -21,23 +30,19 @@ def syntastic(pl, segment_info,
Highlight groups used: ``syntastic:warning`` or ``warning``, ``syntastic:error`` or ``error``.
'''
vim = segment_info['vim']
if not vim_global_exists(vim, 'SyntasticLoclist'):
errs = segment_info['input']['syntastic_errors']
if not isinstance(errs, list):
return None
has_errors = int(vim.eval('g:SyntasticLoclist.current().hasErrorsOrWarningsToDisplay()'))
if not has_errors:
return None
errors = vim.eval('g:SyntasticLoclist.current().errors()')
warnings = vim.eval('g:SyntasticLoclist.current().warnings()')
errors, warnings = errs
segments = []
if errors:
segments.append({
'contents': err_format.format(first_line=errors[0]['lnum'], num=len(errors)),
'contents': err_format.format(first_line=int(errors[0]['lnum']), num=len(errors)),
'highlight_groups': ['syntastic:error', 'error'],
})
if warnings:
segments.append({
'contents': warn_format.format(first_line=warnings[0]['lnum'], num=len(warnings)),
'contents': warn_format.format(first_line=int(warnings[0]['lnum']), num=len(warnings)),
'highlight_groups': ['syntastic:warning', 'warning'],
})
return segments
14 changes: 10 additions & 4 deletions powerline/vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ class VimPowerline(Powerline):
def init(self, pyeval='pyeval', **kwargs):
import vim
self.vim = vim
self.is_old_vim = bool(int(self.vim.eval('v:version < 704 || exists("g:powerline_old_vim")')))
self.is_old_vim = bool(int(self.vim.eval(
'v:version < 704 '
'|| exists("g:powerline_old_vim") '
'|| has("nvim")')))
self.vim_funcs = VimFuncsDict(vim)

if self.is_old_vim:
Expand Down Expand Up @@ -360,8 +363,7 @@ def viml_return_str(self, s):
'''
self.vim.command(b'return "' + s.replace(b'\\', b'\\\\').replace(b'"', b'\\"') + b'"')

def do_setup(self, pyeval=None, pycmd=None, can_replace_pyeval=True, _local_themes=()):
import __main__
def do_setup(self, pyeval=None, pycmd=None, can_replace_pyeval=True, _local_themes=(), gvars=None):
if not pyeval:
pyeval = 'pyeval' if sys.version_info < (3,) else 'py3eval'
if not pycmd:
Expand All @@ -387,7 +389,11 @@ def do_setup(self, pyeval=None, pycmd=None, can_replace_pyeval=True, _local_them
self.pyeval = pyeval
self.construct_window_statusline = self.create_window_statusline_constructor()

__main__.powerline = self
if gvars is None:
import __main__
__main__.powerline = self
else:
gvars['powerline'] = self

try:
if (
Expand Down

0 comments on commit 4fbadcf

Please sign in to comment.