much smarter handling of separators (#204).

This commit is contained in:
Bailey Ling 2013-09-01 12:07:46 -04:00
parent 723e721b32
commit 92de8ad160
5 changed files with 65 additions and 38 deletions

View File

@ -58,6 +58,7 @@ function! airline#init#bootstrap()
call airline#parts#define_function('mode', 'airline#parts#mode')
call airline#parts#define_function('iminsert', 'airline#parts#iminsert')
call airline#parts#define_function('paste', 'airline#parts#paste')
call airline#parts#define_function('filetype', 'airline#parts#filetype')
call airline#parts#define('readonly', {
\ 'function': 'airline#parts#readonly',
\ 'highlight': 'airline_file',
@ -83,7 +84,7 @@ function! airline#init#sections()
let g:airline_section_gutter = airline#section#create([' ', 'readonly', '%='])
endif
if !exists('g:airline_section_x')
let g:airline_section_x = airline#section#create_right(['tagbar', '%{&filetype}'])
let g:airline_section_x = airline#section#create_right(['tagbar', 'filetype'])
endif
if !exists('g:airline_section_y')
let g:airline_section_y = airline#section#create_right(['ffenc'])

View File

@ -57,3 +57,7 @@ function! airline#parts#readonly()
return &readonly ? g:airline_symbols.readonly : ''
endfunction
function! airline#parts#filetype()
return &filetype
endfunction

View File

@ -3,54 +3,59 @@
call airline#init#bootstrap()
function! s:get_val(key, append)
let part = airline#parts#get(a:key)
function! s:create(parts, append)
let _ = ''
for idx in range(len(a:parts))
let part = airline#parts#get(a:parts[idx])
let val = ''
if exists('part.highlight')
let val .= '%#'.(part.highlight).'#'
endif
let val = ''
if exists('part.highlight')
let val .= '%#'.(part.highlight).'#'
endif
if exists('part.function')
let func = (part.function).'()'
elseif exists('part.text')
let func = '"'.(part.text).'"'
elseif exists('part.raw')
return val.(part.raw)
else
return a:key
endif
if exists('part.function')
let func = (part.function).'()'
elseif exists('part.text')
let func = '"'.(part.text).'"'
else
if a:append > 0 && idx != 0
let val .= ' '.g:airline_left_alt_sep.' '
endif
if a:append < 0 && idx != 0
let val = ' '.g:airline_right_alt_sep.' '.val
endif
if exists('part.raw')
let _ .= val.(part.raw)
continue
else
let _ .= val.a:parts[idx]
continue
endif
endif
let minwidth = get(part, 'minwidth', 0)
let minwidth = get(part, 'minwidth', 0)
if a:append > 0
let val .= printf('%%{airline#util#append(%s,%s)}', func, minwidth)
elseif a:append < 0
let val .= printf('%%{airline#util#prepend(%s,%s)}', func, minwidth)
else
let val .= printf('%%{airline#util#wrap(%s,%s)}', func, minwidth)
endif
return val
if a:append > 0 && idx != 0
let val .= printf('%%{airline#util#append(%s,%s)}', func, minwidth)
elseif a:append < 0 && idx != len(a:parts) - 1
let val .= printf('%%{airline#util#prepend(%s,%s)}', func, minwidth)
else
let val .= printf('%%{airline#util#wrap(%s,%s)}', func, minwidth)
endif
let _ .= val
endfor
return _
endfunction
function! airline#section#create(parts)
return join(map(a:parts, 's:get_val(v:val, 0)'), '')
return s:create(a:parts, 0)
endfunction
function! airline#section#create_left(parts)
let _ = s:get_val(a:parts[0], 0)
for i in range(1, len(a:parts) - 1)
let _ .= s:get_val(a:parts[i], 1)
endfor
return _
return s:create(a:parts, 1)
endfunction
function! airline#section#create_right(parts)
let _ = ''
for i in range(0, len(a:parts) - 2)
let _ .= s:get_val(a:parts[i], -1)
endfor
let _ .= s:get_val(a:parts[-1], 0)
return _
return s:create(a:parts, -1)
endfunction

View File

@ -319,6 +319,12 @@ Note: The use of `VimEnter` is important, because most extensions are lazily
loaded, so we must give them a chance to define their parts before we can use
them.
Note: The `airline#section#create` function and friends will do its best to
create a section with the appropriate separators, but it only works for
function and text parts. Special |statusline| items like %f or raw parts will
not work as it is not possible to inspect their widths/contents before
rendering to the statusline.
==============================================================================
FUNCREFS *airline-funcrefs*

View File

@ -34,7 +34,7 @@ describe 'section'
Expect s == '%#hlgroup#hello'
end
it 'should parse out a section from the vimrc'
it 'should parse out a section from the distro'
let s = airline#section#create(['whitespace'])
Expect s =~ 'airline#extensions#whitespace#check'
end
@ -43,5 +43,16 @@ describe 'section'
let s = airline#section#create(['asdf', 'func'])
Expect s == 'asdf%{airline#util#wrap(SectionSpec(),0)}'
end
it 'should force add separators for raw and missing keys'
let s = airline#section#create_left(['asdf', 'raw'])
Expect s == 'asdf > raw'
let s = airline#section#create_left(['asdf', 'aaaa', 'raw'])
Expect s == 'asdf > aaaa > raw'
let s = airline#section#create_right(['raw', '%f'])
Expect s == 'raw < %f'
let s = airline#section#create_right(['%t', 'asdf', '%{getcwd()}'])
Expect s == '%t < asdf < %{getcwd()}'
end
end