improve documentation for writing extensions, also add helper methods.

This commit is contained in:
Bailey Ling 2013-08-25 15:39:11 +00:00
parent 9ca2aa9a8c
commit a91dda01df
5 changed files with 49 additions and 37 deletions

View File

@ -26,6 +26,26 @@ let s:filetype_overrides = {
let s:filetype_regex_overrides = {} let s:filetype_regex_overrides = {}
function! s:check_defined_section(name)
if !exists('w:airline_section_{a:name}')
if g:airline_section_{a:name} == '__'
let w:airline_section_{a:name} = ''
else
let w:airline_section_{a:name} = g:airline_section_{a:name}
endif
endif
endfunction
function! airline#extensions#append_to_section(name, value)
call <sid>check_defined_section(a:name)
let w:airline_section_{a:name} .= a:value
endfunction
function! airline#extensions#prepend_to_section(name, value)
call <sid>check_defined_section(a:name)
let w:airline_section_{a:name} = a:value . w:airline_section_{a:name}
endfunction
function! airline#extensions#apply_left_override(section1, section2) function! airline#extensions#apply_left_override(section1, section2)
let w:airline_section_a = a:section1 let w:airline_section_a = a:section1
let w:airline_section_b = a:section2 let w:airline_section_b = a:section2
@ -166,14 +186,13 @@ function! airline#extensions#load()
endif endif
if g:airline_section_warning == '__' if g:airline_section_warning == '__'
if (get(g:, 'airline#extensions#whitespace#enabled', 1) && get(g:, 'airline_detect_whitespace', 1))
call airline#extensions#whitespace#init(s:ext)
endif
if (get(g:, 'airline#extensions#syntastic#enabled', 1) && get(g:, 'airline_enable_syntastic', 1)) if (get(g:, 'airline#extensions#syntastic#enabled', 1) && get(g:, 'airline_enable_syntastic', 1))
\ && exists(':SyntasticCheck') \ && exists(':SyntasticCheck')
call airline#extensions#syntastic#init(s:ext) call airline#extensions#syntastic#init(s:ext)
endif endif
if (get(g:, 'airline#extensions#whitespace#enabled', 1) && get(g:, 'airline_detect_whitespace', 1))
call airline#extensions#whitespace#init(s:ext)
endif
endif endif
if get(g:, 'airline#extensions#readonly#enabled', 1) if get(g:, 'airline#extensions#readonly#enabled', 1)

View File

@ -16,13 +16,8 @@ endfunction
function! airline#extensions#csv#apply(...) function! airline#extensions#csv#apply(...)
if &ft ==# "csv" if &ft ==# "csv"
if !exists('w:airline_section_gutter') call airline#extensions#prepend_to_section('gutter',
let w:airline_section_gutter = '%=' \ g:airline_left_alt_sep.' %{airline#extensions#csv#get_column()}')
endif
let w:airline_section_gutter =
\ g:airline_left_alt_sep
\ .' %{airline#extensions#csv#get_column()}'
\ .w:airline_section_gutter
endif endif
endfunction endfunction

View File

@ -6,33 +6,35 @@ if !exists('g:airline#extensions#example#number_of_cats')
let g:airline#extensions#example#number_of_cats = 42 let g:airline#extensions#example#number_of_cats = 42
endif endif
" First you should follow the convention and define an 'init' function. " There are predominantly two methods for integrating a plugin into
" It takes a single argument, which is the 'ext'ension manager of sorts, " vim-airline. The first method here simply modifies the global section and
" which you can invoke certain functions. The most important one is " appends information to it. This is useful for cases where the information
" 'add_statusline_func', which as the name implies, adds a function to " should be displayed all the time for all filetypes.
" the collection such that it will be invoked prior to changes being made function! airline#extensions#example#init(ext)
" to the statusline. Finally, invoke this init function in the let g:airline_section_y .= '%{airline#extensions#example#get_cats()}'
" 'extensions.vim' file after a check to a non-autoloaded variable, endfunction
" command, or function.
" The second method involves using the 'ext'ension manager that was passed in
" and appends a name of a function. This function will be invoked just prior
" to updating the statusline. This method is useful for plugin-specific
" statuslines (like NERDTree or Tagbar) or language specific plugins (like
" virtualenv) which do not need to be loaded all the time.
function! airline#extensions#example#init(ext) function! airline#extensions#example#init(ext)
call a:ext.add_statusline_func('airline#extensions#example#apply') call a:ext.add_statusline_func('airline#extensions#example#apply')
" Alternatively, you can also modify the default global section by " There is also the following function for making changes just prior to an
" appending or prepending to it. But read on to see why using the funcref " inactive statusline.
" method is preferred. " call a:ext.add_inactive_statusline_func('airline#extensions#example#unapply')
let g:airline_section_y .= '%{airline#extensions#example#nyancat()}'
endfunction endfunction
" This function will be invoked just prior to the statusline getting modified.
function! airline#extensions#example#apply(...) function! airline#extensions#example#apply(...)
" Here we are checking for the filetype, allowing for the extension to " First we check for the filetype.
" be loaded only in certain cases.
if &filetype == "nyancat" if &filetype == "nyancat"
" Let's use a helper function. It will take care of ensuring that the
" Then we define a window-local variable, which overrides the default " window-local override exists (and create one based on the global
" g: variable. " airline_section if not), and prepend to it.
let w:airline_section_gutter = call airline#extensions#prepend_to_section('x', '%{airline#extensions#example#get_cats()} ')
\ g:airline_section_gutter
\ .' %{airline#extensions#example#get_cats()}'
endif endif
endfunction endfunction

View File

@ -2,8 +2,7 @@
" vim: et ts=2 sts=2 sw=2 " vim: et ts=2 sts=2 sw=2
function! airline#extensions#syntastic#apply(...) function! airline#extensions#syntastic#apply(...)
let w:airline_section_warning = get(w:, 'airline_section_warning', '') call airline#extensions#append_to_section('warning', '%{SyntasticStatuslineFlag()}')
let w:airline_section_warning = ' %{SyntasticStatuslineFlag()}'
endfunction endfunction
function! airline#extensions#syntastic#init(ext) function! airline#extensions#syntastic#init(ext)

View File

@ -54,10 +54,7 @@ function! airline#extensions#whitespace#check()
endfunction! endfunction!
function! airline#extensions#whitespace#apply(...) function! airline#extensions#whitespace#apply(...)
if !exists('w:airline_section_warning') call airline#extensions#append_to_section('warning', ' %{airline#extensions#whitespace#check()}')
let w:airline_section_warning = ' '
endif
let w:airline_section_warning .= '%{airline#extensions#whitespace#check()}'
endfunction endfunction
function! airline#extensions#whitespace#toggle() function! airline#extensions#whitespace#toggle()