From fc7d25e7965cfab307103820a6d1dc7f4e75ceb3 Mon Sep 17 00:00:00 2001 From: itchyny Date: Mon, 21 Aug 2017 08:21:01 +0900 Subject: [PATCH] implement g:lightline.component_raw to specify raw type components. (close #240, #134) Having raw type configuration in component_type is a kind of implementation design mistake. We sometimes want to specify both the type and the raw attribute of a component. The raw component feature shouldn't be included as one of the types of components. Now we can specify the raw attribute with component_raw and the type with component_type. --- autoload/lightline.vim | 12 +++++++----- doc/lightline.txt | 13 ++++++++++++- test/expand.vim | 13 +++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/autoload/lightline.vim b/autoload/lightline.vim index 8fd42fe..9004efd 100644 --- a/autoload/lightline.vim +++ b/autoload/lightline.vim @@ -2,7 +2,7 @@ " Filename: autoload/lightline.vim " Author: itchyny " License: MIT License -" Last Change: 2017/08/10 04:53:19. +" Last Change: 2017/08/21 08:19:52. " ============================================================================= let s:save_cpo = &cpo @@ -118,6 +118,7 @@ let s:_lightline = { \ 'component_type': { \ 'tabs': 'tabsel', 'close': 'raw' \ }, + \ 'component_raw': {}, \ 'tab_component': {}, \ 'tab_component_function': { \ 'filename': 'lightline#tab#filename', 'modified': 'lightline#tab#modified', @@ -338,8 +339,9 @@ endfunction function! s:convert(name, index) abort if has_key(s:lightline.component_expand, a:name) let type = get(s:lightline.component_type, a:name, a:index) + let is_raw = get(s:lightline.component_raw, a:name) || type ==# 'raw' return filter(s:map(s:evaluate_expand(s:lightline.component_expand[a:name]), - \ '[v:val, 1 + ' . (type ==# 'raw') . ', v:key == 1 && ' . (type !=# 'raw') . ' ? "' . type . '" : "' . a:index . '"]'), 'v:val[0] != []') + \ '[v:val, 1 + ' . is_raw . ', v:key == 1 && ' . (type !=# 'raw') . ' ? "' . type . '" : "' . a:index . '"]'), 'v:val[0] != []') else return [[[a:name], 0, a:index]] endif @@ -394,7 +396,7 @@ function! s:line(tabline, inactive) abort endif let [l, r] = a:tabline ? [s:lightline.tab_llen, s:lightline.tab_rlen] : [s:lightline.llen, s:lightline.rlen] let [p, s] = a:tabline ? [s:lightline.tabline_separator, s:lightline.tabline_subseparator] : [s:lightline.separator, s:lightline.subseparator] - let [c, f, t] = [s:lightline.component, s:lightline.component_function, s:lightline.component_type] + let [c, f, t, w] = [s:lightline.component, s:lightline.component_function, s:lightline.component_type, s:lightline.component_raw] let mode = a:tabline ? 'tabline' : a:inactive ? 'inactive' : 'active' let l_ = has_key(s:lightline, mode) ? s:lightline[mode].left : s:lightline.active.left let [lt, lc, ll] = s:expand(copy(l_)) @@ -404,7 +406,7 @@ function! s:line(tabline, inactive) abort let _ .= '%#LightlineLeft_' . mode . '_' . ll[i] . '#' for j in range(len(lt[i])) let x = lc[i][j] ? lt[i][j] : has_key(f, lt[i][j]) ? (exists('*' . f[lt[i][j]]) ? '%{' . f[lt[i][j]] . '()}' : '%{exists("*' . f[lt[i][j]] . '")?' . f[lt[i][j]] . '():""}') : get(c, lt[i][j], '') - let _ .= has_key(t, lt[i][j]) && t[lt[i][j]] ==# 'raw' || lc[i][j] ==# 2 || x ==# '' ? x : '%( ' . x . ' %)' + let _ .= has_key(t, lt[i][j]) && t[lt[i][j]] ==# 'raw' || get(w, lt[i][j]) || lc[i][j] ==# 2 || x ==# '' ? x : '%( ' . x . ' %)' if j < len(lt[i]) - 1 && s.left !=# '' let _ .= s:subseparator(lt[i][(j):], s.left, lc[i][(j):]) endif @@ -419,7 +421,7 @@ function! s:line(tabline, inactive) abort let _ .= '%#LightlineRight_' . mode . '_' . rl[i] . '#' for j in range(len(rt[i])) let x = rc[i][j] ? rt[i][j] : has_key(f, rt[i][j]) ? (exists('*' . f[rt[i][j]]) ? '%{' . f[rt[i][j]] . '()}' : '%{exists("*' . f[rt[i][j]] . '")?' . f[rt[i][j]] . '():""}') : get(c, rt[i][j], '') - let _ .= has_key(t, rt[i][j]) && t[rt[i][j]] ==# 'raw' || rc[i][j] ==# 2 || x ==# '' ? x : '%( ' . x . ' %)' + let _ .= has_key(t, rt[i][j]) && t[rt[i][j]] ==# 'raw' || get(w, rt[i][j]) || rc[i][j] ==# 2 || x ==# '' ? x : '%( ' . x . ' %)' if j < len(rt[i]) - 1 && s.right !=# '' let _ .= s:subseparator(rt[i][(j):], s.right, rc[i][(j):]) endif diff --git a/doc/lightline.txt b/doc/lightline.txt index 0b41c12..5f645e4 100644 --- a/doc/lightline.txt +++ b/doc/lightline.txt @@ -4,7 +4,7 @@ Version: 0.1 Author: itchyny (https://github.com/itchyny) License: MIT License Repository: https://github.com/itchyny/lightline.vim -Last Change: 2017/05/28 01:07:02. +Last Change: 2017/08/21 08:18:42. CONTENTS *lightline-contents* @@ -188,11 +188,22 @@ OPTIONS *lightline-option* |g:lightline.component_expand|. The types are used to specify the color. Specifically, the type raw is used to specify a component which should not be wrapped by item group: %(...%). + If you want to specify the type of a raw component, please use + |g:lightline.component_raw|. The default value is: > let g:lightline.component_type = { \ 'tabs': 'tabsel', \ 'close': 'raw' } +< + g:lightline.component_raw *g:lightline.component_raw* + A dictionary to specify the raw type components. When you + register a component to this dictionary (like > + g:lightline.component_raw = { 'example': 1 } +< ), the example component is not wrapped by item group: %(...%). + The default value is: > + + let g:lightline.component_raw = {} < g:lightline.tab_component *g:lightline.tab_component* A dictionary for components in one tab. diff --git a/test/expand.vim b/test/expand.vim index b8e5843..a9c8577 100644 --- a/test/expand.vim +++ b/test/expand.vim @@ -58,6 +58,19 @@ function! s:suite.raw_type() delfunction Custom endfunction +function! s:suite.component_raw() + function! Custom() + return [ ['left'], ['middle'], ['right'] ] + endfunction + let g:lightline = { 'component_expand': { 'custom': 'Custom' }, 'component_type': { 'custom': 'custom' }, 'component_raw': { 'custom': 1 } } + call lightline#init() + call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), + \ [[['readonly', 'filename'], ['left'], ['middle'], ['right'], ['modified']], [[0, 0], [2], [2], [2], [0]], ['0', '1', 'custom', '1', '2', '3']]) + call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), + \ [[['readonly', 'filename', 'left'], ['middle'], ['right', 'modified']], [[0, 0, 2], [2], [2, 0]], ['0', 'custom', '0', '1']]) + delfunction Custom +endfunction + function! s:suite.multiple() function! Custom() return [ ['x0', 'x1', 'x2'], ['y0', 'y1', 'y2'], ['z0', 'z1', 'z2'] ]