From 92de8ad160e04ef529b5b1aec1586d90517dedac Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Sun, 1 Sep 2013 12:07:46 -0400 Subject: [PATCH] much smarter handling of separators (#204). --- autoload/airline/init.vim | 3 +- autoload/airline/parts.vim | 4 ++ autoload/airline/section.vim | 77 +++++++++++++++++++----------------- doc/airline.txt | 6 +++ t/section.vim | 13 +++++- 5 files changed, 65 insertions(+), 38 deletions(-) diff --git a/autoload/airline/init.vim b/autoload/airline/init.vim index 5782228b..8dd176f7 100644 --- a/autoload/airline/init.vim +++ b/autoload/airline/init.vim @@ -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']) diff --git a/autoload/airline/parts.vim b/autoload/airline/parts.vim index 4a2f5427..9abbb895 100644 --- a/autoload/airline/parts.vim +++ b/autoload/airline/parts.vim @@ -57,3 +57,7 @@ function! airline#parts#readonly() return &readonly ? g:airline_symbols.readonly : '' endfunction +function! airline#parts#filetype() + return &filetype +endfunction + diff --git a/autoload/airline/section.vim b/autoload/airline/section.vim index 95b5e5a8..e2d37020 100644 --- a/autoload/airline/section.vim +++ b/autoload/airline/section.vim @@ -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 diff --git a/doc/airline.txt b/doc/airline.txt index 2cb75ec3..62a23deb 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -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* diff --git a/t/section.vim b/t/section.vim index 34f279d8..360ec5f6 100644 --- a/t/section.vim +++ b/t/section.vim @@ -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