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('mode', 'airline#parts#mode')
call airline#parts#define_function('iminsert', 'airline#parts#iminsert') call airline#parts#define_function('iminsert', 'airline#parts#iminsert')
call airline#parts#define_function('paste', 'airline#parts#paste') call airline#parts#define_function('paste', 'airline#parts#paste')
call airline#parts#define_function('filetype', 'airline#parts#filetype')
call airline#parts#define('readonly', { call airline#parts#define('readonly', {
\ 'function': 'airline#parts#readonly', \ 'function': 'airline#parts#readonly',
\ 'highlight': 'airline_file', \ 'highlight': 'airline_file',
@ -83,7 +84,7 @@ function! airline#init#sections()
let g:airline_section_gutter = airline#section#create([' ', 'readonly', '%=']) let g:airline_section_gutter = airline#section#create([' ', 'readonly', '%='])
endif endif
if !exists('g:airline_section_x') 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 endif
if !exists('g:airline_section_y') if !exists('g:airline_section_y')
let g:airline_section_y = airline#section#create_right(['ffenc']) 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 : '' return &readonly ? g:airline_symbols.readonly : ''
endfunction endfunction
function! airline#parts#filetype()
return &filetype
endfunction

View File

@ -3,54 +3,59 @@
call airline#init#bootstrap() call airline#init#bootstrap()
function! s:get_val(key, append) function! s:create(parts, append)
let part = airline#parts#get(a:key) let _ = ''
for idx in range(len(a:parts))
let part = airline#parts#get(a:parts[idx])
let val = '' let val = ''
if exists('part.highlight') if exists('part.highlight')
let val .= '%#'.(part.highlight).'#' let val .= '%#'.(part.highlight).'#'
endif endif
if exists('part.function') if exists('part.function')
let func = (part.function).'()' let func = (part.function).'()'
elseif exists('part.text') elseif exists('part.text')
let func = '"'.(part.text).'"' let func = '"'.(part.text).'"'
elseif exists('part.raw') else
return val.(part.raw) if a:append > 0 && idx != 0
else let val .= ' '.g:airline_left_alt_sep.' '
return a:key endif
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 if a:append > 0 && idx != 0
let val .= printf('%%{airline#util#append(%s,%s)}', func, minwidth) let val .= printf('%%{airline#util#append(%s,%s)}', func, minwidth)
elseif a:append < 0 elseif a:append < 0 && idx != len(a:parts) - 1
let val .= printf('%%{airline#util#prepend(%s,%s)}', func, minwidth) let val .= printf('%%{airline#util#prepend(%s,%s)}', func, minwidth)
else else
let val .= printf('%%{airline#util#wrap(%s,%s)}', func, minwidth) let val .= printf('%%{airline#util#wrap(%s,%s)}', func, minwidth)
endif endif
return val let _ .= val
endfor
return _
endfunction endfunction
function! airline#section#create(parts) function! airline#section#create(parts)
return join(map(a:parts, 's:get_val(v:val, 0)'), '') return s:create(a:parts, 0)
endfunction endfunction
function! airline#section#create_left(parts) function! airline#section#create_left(parts)
let _ = s:get_val(a:parts[0], 0) return s:create(a:parts, 1)
for i in range(1, len(a:parts) - 1)
let _ .= s:get_val(a:parts[i], 1)
endfor
return _
endfunction endfunction
function! airline#section#create_right(parts) function! airline#section#create_right(parts)
let _ = '' return s:create(a:parts, -1)
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 _
endfunction 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 loaded, so we must give them a chance to define their parts before we can use
them. 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* FUNCREFS *airline-funcrefs*

View File

@ -34,7 +34,7 @@ describe 'section'
Expect s == '%#hlgroup#hello' Expect s == '%#hlgroup#hello'
end 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']) let s = airline#section#create(['whitespace'])
Expect s =~ 'airline#extensions#whitespace#check' Expect s =~ 'airline#extensions#whitespace#check'
end end
@ -43,5 +43,16 @@ describe 'section'
let s = airline#section#create(['asdf', 'func']) let s = airline#section#create(['asdf', 'func'])
Expect s == 'asdf%{airline#util#wrap(SectionSpec(),0)}' Expect s == 'asdf%{airline#util#wrap(SectionSpec(),0)}'
end 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 end