refactor builder and reduce state across multiple calls

This commit is contained in:
Bailey Ling 2014-10-18 13:30:21 -04:00
parent 256dec6800
commit 727192ad6a
1 changed files with 50 additions and 31 deletions

View File

@ -4,8 +4,7 @@
let s:prototype = {} let s:prototype = {}
function! s:prototype.split(...) function! s:prototype.split(...)
let self._side = 0 call add(self._sections, ['|', a:0 ? a:1 : '%='])
let self._line .= '%#'.self._curgroup.'#'.(a:0 ? a:1 : '%=')
endfunction endfunction
function! s:prototype.add_section_spaced(group, contents) function! s:prototype.add_section_spaced(group, contents)
@ -13,21 +12,56 @@ function! s:prototype.add_section_spaced(group, contents)
endfunction endfunction
function! s:prototype.add_section(group, contents) function! s:prototype.add_section(group, contents)
if self._curgroup != '' call add(self._sections, [a:group, a:contents])
if self._curgroup == a:group endfunction
let self._line .= self._side ? self._context.left_alt_sep : self._context.right_alt_sep
else function! s:prototype.add_raw(text)
call airline#highlighter#add_separator(self._curgroup, a:group, self._side) call add(self._sections, ['', a:text])
let self._line .= '%#'.self._curgroup.'_to_'.a:group.'#' endfunction
let self._line .= self._side ? self._context.left_sep : self._context.right_sep
function! s:prototype.build()
let side = 1
let prev_group = ''
let line = ''
for section in self._sections
let group = section[0]
let contents = section[1]
if group == '|'
let side = 0
let line .= contents
continue
endif
if prev_group != ''
if prev_group == group
let line .= side ? self._context.left_alt_sep : self._context.right_alt_sep
elseif group != ''
call airline#highlighter#add_separator(prev_group, group, side)
let line .= '%#'.prev_group.'_to_'.group.'#'
let line .= side ? self._context.left_sep : self._context.right_sep
endif endif
endif endif
if self._curgroup != a:group if group != prev_group
let self._line .= '%#'.a:group.'#' let line .= '%#'.group.'#'
endif endif
let line .= s:get_accented_line(self, group, contents)
if self._context.active if group != ''
let prev_group = group
endif
endfor
if !self._context.active
let line = substitute(line, '%#.\{-}\ze#', '\0_inactive', 'g')
endif
return line
endfunction
function! s:get_accented_line(self, group, contents)
if a:self._context.active
let contents = [] let contents = []
let content_parts = split(a:contents, '__accent') let content_parts = split(a:contents, '__accent')
for cpart in content_parts for cpart in content_parts
@ -40,28 +74,13 @@ function! s:prototype.add_section(group, contents)
let line = substitute(a:contents, '%#__accent[^#]*#', '', 'g') let line = substitute(a:contents, '%#__accent[^#]*#', '', 'g')
let line = substitute(line, '%#__restore__#', '', 'g') let line = substitute(line, '%#__restore__#', '', 'g')
endif endif
return line
let self._line .= line
let self._curgroup = a:group
endfunction
function! s:prototype.add_raw(text)
let self._line .= a:text
endfunction
function! s:prototype.build()
if !self._context.active
let self._line = substitute(self._line, '%#.\{-}\ze#', '\0_inactive', 'g')
endif
return self._line
endfunction endfunction
function! airline#builder#new(context) function! airline#builder#new(context)
let builder = copy(s:prototype) let builder = copy(s:prototype)
let builder._context = a:context let builder._context = a:context
let builder._side = 1 let builder._sections = []
let builder._curgroup = ''
let builder._line = ''
call extend(builder._context, { call extend(builder._context, {
\ 'left_sep': g:airline_left_sep, \ 'left_sep': g:airline_left_sep,