Make branch detection customizable

Instead of requiring each version control plugin to modify airline to
show the current branch, provide a customization function we can check
instead.

Following the example of airline_theme_patch_func, you define the
variable like so:

    let g:airline#extensions#branch#custom_head = 'david#svn#get_branch'

Custom functions should cache their value. They may need an autocmd to
invalidate their cache:

    " Use a buffer-unique group name to prevent clearing autocmds for other
    " buffers.
    exec 'augroup svndavid-'. bufnr("%")
        au!
        autocmd BufWinLeave <buffer> unlet! b:svndavid_branch
    augroup END

This change lets me integrate with vc.vim (I couldn't get VCSCommand
working for svn) or write my own thing for perforce.

Additionally, always load whole file and check for existence.

Instead of determining up front whether various scm plugins are
installed, check for them on use so they can be added after this script
is sourced.

This also mitigates the problem of checking for existence of autoload
functions (which are not loaded by exist()). Since we're checking
root-level functions, they're likely to be loaded once we're using any
part of the plugin.
This commit is contained in:
David Briscoe 2018-03-09 11:10:45 -08:00
parent 958f78335e
commit 8209ca7da1
2 changed files with 41 additions and 10 deletions

View File

@ -3,13 +3,18 @@
scriptencoding utf-8
let s:has_fugitive = exists('*fugitive#head')
let s:has_lawrencium = exists('*lawrencium#statusline')
let s:has_vcscommand = get(g:, 'airline#extensions#branch#use_vcscommand', 0) && exists('*VCSCommandGetStatusLine')
if !s:has_fugitive && !s:has_lawrencium && !s:has_vcscommand
finish
endif
function! s:has_fugitive()
return exists('*fugitive#head')
endfunction
function! s:has_lawrencium()
return exists('*lawrencium#statusline')
endfunction
function! s:has_vcscommand()
return get(g:, 'airline#extensions#branch#use_vcscommand', 0) && exists('*VCSCommandGetStatusLine')
endfunction
function! s:has_custom_scm()
return !empty(get(g:, 'airline#extensions#branch#custom_head', ''))
endfunction
" s:vcs_config contains static configuration of VCSes and their status relative
" to the active file.
@ -89,7 +94,7 @@ let s:names = {'0': 'index', '1': 'orig', '2':'fetch', '3':'merge'}
let s:sha1size = get(g:, 'airline#extensions#branch#sha1_len', 7)
function! s:update_git_branch()
if !s:has_fugitive
if !s:has_fugitive()
let s:vcs_config['git'].branch = ''
return
endif
@ -123,7 +128,7 @@ function! s:display_git_branch()
endfunction
function! s:update_hg_branch()
if s:has_lawrencium
if s:has_lawrencium()
let cmd='LC_ALL=C hg qtop'
let stl=lawrencium#statusline()
let file=expand('%:p')
@ -248,7 +253,7 @@ function! airline#extensions#branch#head()
endfor
if empty(heads)
if s:has_vcscommand
if s:has_vcscommand()
call VCSCommandEnableBufferSetup()
if exists('b:VCSCommandBufferInfo')
let b:airline_head = s:format_name(get(b:VCSCommandBufferInfo, 0, ''))
@ -256,6 +261,15 @@ function! airline#extensions#branch#head()
endif
endif
if empty(heads)
if s:has_custom_scm()
try
let Fn = function(g:airline#extensions#branch#custom_head)
let b:airline_head = Fn()
endtry
endif
endif
if exists("g:airline#extensions#branch#displayed_head_limit")
let w:displayed_head_limit = g:airline#extensions#branch#displayed_head_limit
if len(b:airline_head) > w:displayed_head_limit - 1

View File

@ -459,6 +459,23 @@ notexists symbol will be displayed after the branch name.
<
* truncate sha1 commits at this number of characters >
let g:airline#extensions#branch#sha1_len = 10
* customize branch name retrieval for any version control system >
let g:airline#extensions#branch#custom_head = 'GetScmBranch'
function! GetScmBranch()
if !exists('b:perforce_client')
let b:perforce_client = system('p4 client -o | grep Client')
" Invalidate cache to prevent stale data when switching clients. Use a
" buffer-unique group name to prevent clearing autocmds for other
" buffers.
exec 'augroup perforce_client-'. bufnr("%")
au!
autocmd BufWinLeave <buffer> silent! unlet! b:perforce_client
augroup END
endif
return b:perforce_client
endfunction
<
------------------------------------- *airline-syntastic*
syntastic <https://github.com/vim-syntastic/syntastic>