Factor out tab insertion into its own function

This commit is contained in:
mrmr1993 2018-03-15 18:09:18 +00:00
parent 9b41fe5de9
commit 6db0af6f8d

View File

@ -12,6 +12,17 @@ function! s:prototype.insert_tabs(curtab) dict
let self._right_position = self._left_position
endfunction
function! s:prototype.try_insert_tab(tab, pos, tab_nr_type, sep_size, force) dict
let tab_title = self.get_title(a:tab_nr_type, a:tab)
let self._remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + a:sep_size
if a:force || self._remaining_space >= 0
call self.insert_section(self.get_group(a:tab), tab_title, a:pos)
let self._right_position += 1
return 1
endif
return 0
endfunction
function! s:prototype.build() dict
if has_key(self, '_left_position')
let tab_nr_type = get(g:, 'airline#extensions#tabline#tab_nr_type', 0)
@ -25,16 +36,14 @@ function! s:prototype.build() dict
let skipped_tabs_marker_size = s:strchars(s:evaluate_tabline(skipped_tabs_marker))
" The left marker will have left_alt_sep, and the right will have left_sep.
let self._remaining_space -= 2 * skipped_tabs_marker_size + left_sep_size + left_alt_sep_size
" Add the current tab
let tab_title = self.get_title(tab_nr_type, self._left_tab)
let self._remaining_space -= s:strchars(s:evaluate_tabline(tab_title))
"
" There are always two left_seps (either side of the selected tab) and all
" other seperators are left_alt_seps.
let self._remaining_space -= 2 * left_sep_size - left_alt_sep_size
call self.insert_section(self.get_group(self._left_tab), tab_title, self._left_position)
let self._right_position += 1
let self._left_tab -= 1
let self._remaining_space -= left_sep_size - left_alt_sep_size
" Add the current tab
let self._left_tab -=
\ self.try_insert_tab(self._left_tab, self._left_position, tab_nr_type, left_sep_size, 1)
if get(g:, 'airline#extensions#tabline#current_first', 0)
" always have current tabpage first
@ -43,30 +52,17 @@ function! s:prototype.build() dict
" Add the tab to the right
if self._right_tab <= num_tabs
let tab_title = self.get_title(tab_nr_type, self._right_tab)
let self._remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + left_alt_sep_size
call self.insert_section(self.get_group(self._right_tab), tab_title, self._right_position)
let self._right_position += 1
let self._right_tab += 1
let self._right_tab +=
\ self.try_insert_tab(self._right_tab, self._right_position, tab_nr_type, left_alt_sep_size, 1)
endif
while self._remaining_space > 0
if self._left_tab > 0
let tab_title = self.get_title(tab_nr_type, self._left_tab)
let self._remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + left_alt_sep_size
if self._remaining_space >= 0
call self.insert_section(self.get_group(self._left_tab), tab_title, self._left_position)
let self._right_position += 1
let self._left_tab -= 1
endif
let self._left_tab -=
\ self.try_insert_tab(self._left_tab, self._left_position, tab_nr_type, left_alt_sep_size, 0)
elseif self._right_tab <= num_tabs
let tab_title = self.get_title(tab_nr_type, self._right_tab)
let self._remaining_space -= s:strchars(s:evaluate_tabline(tab_title)) + left_alt_sep_size
if self._remaining_space >= 0
call self.insert_section(self.get_group(self._right_tab), tab_title, self._right_position)
let self._right_position += 1
let self._right_tab += 1
endif
let self._right_tab +=
\ self.try_insert_tab(self._right_tab, self._right_position, tab_nr_type, left_alt_sep_size, 0)
else
break
endif