From c9263d945e96b54b62702e42057a2b5b91551eec Mon Sep 17 00:00:00 2001 From: itchyny Date: Sun, 16 Sep 2018 23:12:53 +0900 Subject: [PATCH] fix highlighting separator when component group is skipped due to expanding components (close #289, #290) --- autoload/lightline.vim | 19 +++++++++++++------ test/expand.vim | 20 ++++++++++---------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/autoload/lightline.vim b/autoload/lightline.vim index 4571c1d..edcae03 100644 --- a/autoload/lightline.vim +++ b/autoload/lightline.vim @@ -2,7 +2,7 @@ " Filename: autoload/lightline.vim " Author: itchyny " License: MIT License -" Last Change: 2018/06/22 08:50:00. +" Last Change: 2018/09/16 23:00:00. " ============================================================================= let s:save_cpo = &cpo @@ -343,9 +343,9 @@ function! s:convert(name, index) abort 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 + ' . is_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 . '", "' . a:index . '"]'), 'v:val[0] != []') else - return [[[a:name], 0, a:index]] + return [[[a:name], 0, a:index, a:index]] endif endfunction @@ -375,16 +375,23 @@ function! s:expand(components) abort let components = [] let expanded = [] let indices = [] + let prevtype = '' let previndex = -1 let xs = s:flatten_twice(s:map(deepcopy(a:components), 'map(v:val, "s:convert(v:val, ''" . v:key . "'')")')) - for [component, expand, index] in xs - if previndex != index - call add(indices, index) + for [component, expand, type, index] in xs + if prevtype !=# type + for i in range(previndex + 1, max([previndex, index - 1])) + call add(indices, string(i)) + call add(components, []) + call add(expanded, []) + endfor + call add(indices, type) call add(components, []) call add(expanded, []) endif call extend(components[-1], component) call extend(expanded[-1], repeat([expand], len(component))) + let prevtype = type let previndex = index endfor call add(indices, string(len(a:components))) diff --git a/test/expand.vim b/test/expand.vim index a9c8577..0a132e2 100644 --- a/test/expand.vim +++ b/test/expand.vim @@ -156,7 +156,7 @@ function! s:suite.custom_void_string() let g:lightline = { 'component_expand': { 'custom': 'Custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -169,7 +169,7 @@ function! s:suite.custom_type_void_string() let g:lightline = { 'component_expand': { 'custom': 'Custom' }, 'component_type': { 'custom': 'custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -208,7 +208,7 @@ function! s:suite.custom_void_string_array() let g:lightline = { 'component_expand': { 'custom': 'Custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -221,7 +221,7 @@ function! s:suite.custom_type_void_string_array() let g:lightline = { 'component_expand': { 'custom': 'Custom' }, 'component_type': { 'custom': 'custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -234,7 +234,7 @@ function! s:suite.custom_void_string_array_2() let g:lightline = { 'component_expand': { 'custom': 'Custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -247,7 +247,7 @@ function! s:suite.custom_type_void_string_array_2() let g:lightline = { 'component_expand': { 'custom': 'Custom' }, 'component_type': { 'custom': 'custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -469,7 +469,7 @@ function! s:suite.custom_error() let g:lightline = { 'component_expand': { 'custom': 'Custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -483,7 +483,7 @@ function! s:suite.custom_type_error() let g:lightline = { 'component_expand': { 'custom': 'Custom' }, 'component_type': { 'custom': 'custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) delfunction Custom @@ -493,7 +493,7 @@ function! s:suite.notfound() let g:lightline = { 'component_expand': { 'custom': 'NotFound' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) endfunction @@ -502,7 +502,7 @@ function! s:suite.custom_type_notfound() let g:lightline = { 'component_expand': { 'custom': 'NotFound' }, 'component_type': { 'custom': 'custom' } } call lightline#init() call s:assert.equals(s:expand([['readonly', 'filename'], ['custom'], ['modified']]), - \ [[['readonly', 'filename'], ['modified']], [[0, 0], [0]], ['0', '2', '3']]) + \ [[['readonly', 'filename'], [], ['modified']], [[0, 0], [], [0]], ['0', '1', '2', '3']]) call s:assert.equals(s:expand([['readonly', 'filename', 'custom', 'modified']]), \ [[['readonly', 'filename', 'modified']], [[0, 0, 0]], ['0', '1']]) endfunction