def trailing_whitespace(pl, segment_info):
'''Return the line number for trailing whitespaces
It is advised not to use this segment in insert mode: in Insert mode it will
iterate over all lines in buffer each time you happen to type a character
which may cause lags. It will also show you whitespace warning each time you
happen to type space.
Highlight groups used: ``trailing_whitespace`` or ``warning``.
'''
global trailing_whitespace_cache
if trailing_whitespace_cache is None:
trailing_whitespace_cache = register_buffer_cache(defaultdict(lambda: (0, None)))
bufnr = segment_info['bufnr']
changedtick = getbufvar(bufnr, 'changedtick')
if trailing_whitespace_cache[bufnr][0] == changedtick:
return trailing_whitespace_cache[bufnr][1]
else:
buf = segment_info['buffer']
bws = b' \t'
sws = str(' \t') # Ignore unicode_literals and use native str.
for i in range(len(buf)):
try:
line = buf[i]
except UnicodeDecodeError: # May happen in Python 3
if hasattr(vim, 'bindeval'):
line = vim.bindeval('getbufline({0}, {1})'.format(
bufnr, i + 1))
has_trailing_ws = (line[-1] in bws)
else:
line = vim.eval('strtrans(getbufline({0}, {1}))'.format(
bufnr, i + 1))
has_trailing_ws = (line[-1] in bws)
else:
has_trailing_ws = (line and line[-1] in sws)
if has_trailing_ws:
break
if has_trailing_ws:
ret = [{
'contents': str(i + 1),
'highlight_groups': ['trailing_whitespace', 'warning'],
}]
else:
ret = None
trailing_whitespace_cache[bufnr] = (changedtick, ret)
return ret
评论列表
文章目录