Merge pull request #1603 from Cimbali/master

Improve fugitive integration
This commit is contained in:
Christian Brabandt 2017-12-21 09:09:44 +01:00 committed by GitHub
commit bc985301db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 67 deletions

View File

@ -229,6 +229,12 @@ function! airline#extensions#load()
call add(loaded_ext, 'bufferline') call add(loaded_ext, 'bufferline')
endif endif
if get(g:, 'airline#extensions#fugitiveline#enabled', 1)
\ && exists('*fugitive#head')
call airline#extensions#fugitiveline#init(s:ext)
call add(loaded_ext, 'fugitiveline')
endif
if (get(g:, 'airline#extensions#virtualenv#enabled', 1) && (exists(':VirtualEnvList') || isdirectory($VIRTUAL_ENV))) if (get(g:, 'airline#extensions#virtualenv#enabled', 1) && (exists(':VirtualEnvList') || isdirectory($VIRTUAL_ENV)))
call airline#extensions#virtualenv#init(s:ext) call airline#extensions#virtualenv#init(s:ext)
call add(loaded_ext, 'virtualenv') call add(loaded_ext, 'virtualenv')

View File

@ -81,46 +81,39 @@ else
endfunction endfunction
endif endif
let s:git_dirs = {}
function! s:update_git_branch(path) " Fugitive special revisions. call '0' "staging" ?
let s:names = {'0': 'index', '1': 'ancestor', '2':'target', '3':'merged'}
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 = '' let s:vcs_config['git'].branch = ''
return return
endif endif
let name = fugitive#head(7) let name = fugitive#head(s:sha1size)
if empty(name)
if has_key(s:git_dirs, a:path)
let s:vcs_config['git'].branch = s:git_dirs[a:path]
return
endif
let dir = fugitive#extract_git_dir(a:path)
if empty(dir)
let name = ''
else
try try
let line = join(readfile(dir . '/HEAD')) let commit = fugitive#buffer().commit()
if strpart(line, 0, 16) == 'ref: refs/heads/'
let name = strpart(line, 16) if has_key(s:names, commit)
let name = get(s:names, commit)."(".name.")"
elseif !empty(commit)
let ref = fugitive#repo().git_chomp('describe', '--all', '--exact-match', commit)
if ref !~ "^fatal: no tag exactly matches"
let name = s:format_name(substitute(ref, '\v\C^%(heads/|remotes/|tags/)=','',''))."(".name.")"
else else
" raw commit hash let name = commit[0:s:sha1size-1]."(".name.")"
let name = strpart(line, 0, 7) endif
endif endif
catch catch
let name = ''
endtry endtry
endif
endif
let s:git_dirs[a:path] = name
let s:vcs_config['git'].branch = name let s:vcs_config['git'].branch = name
endfunction endfunction
function! s:update_hg_branch(...) function! s:update_hg_branch()
" path argument is not actually used, so we don't actually care about a:1
" it is just needed, because update_git_branch needs it.
if s:has_lawrencium if s:has_lawrencium
let cmd='LC_ALL=C hg qtop' let cmd='LC_ALL=C hg qtop'
let stl=lawrencium#statusline() let stl=lawrencium#statusline()
@ -152,10 +145,8 @@ function! s:update_hg_branch(...)
endfunction endfunction
function! s:update_branch() function! s:update_branch()
let b:airline_fname_path = get(b:, 'airline_fname_path',
\ exists("*fnamemodify") ? fnamemodify(resolve(@%), ":p:h") : expand("%:p:h"))
for vcs in keys(s:vcs_config) for vcs in keys(s:vcs_config)
call {s:vcs_config[vcs].update_branch}(b:airline_fname_path) call {s:vcs_config[vcs].update_branch}()
if b:buffer_vcs_config[vcs].branch != s:vcs_config[vcs].branch if b:buffer_vcs_config[vcs].branch != s:vcs_config[vcs].branch
let b:buffer_vcs_config[vcs].branch = s:vcs_config[vcs].branch let b:buffer_vcs_config[vcs].branch = s:vcs_config[vcs].branch
unlet! b:airline_head unlet! b:airline_head
@ -256,9 +247,6 @@ function! airline#extensions#branch#head()
endif endif
endif endif
if has_key(heads, 'git') && !s:check_in_path()
let b:airline_head = ''
endif
let minwidth = empty(get(b:, 'airline_hunks', '')) ? 14 : 7 let minwidth = empty(get(b:, 'airline_hunks', '')) ? 14 : 7
let b:airline_head = airline#util#shorten(b:airline_head, 120, minwidth) let b:airline_head = airline#util#shorten(b:airline_head, 120, minwidth)
return b:airline_head return b:airline_head
@ -273,35 +261,6 @@ function! airline#extensions#branch#get_head()
\ : printf('%s%s', empty(symbol) ? '' : symbol.(g:airline_symbols.space), head) \ : printf('%s%s', empty(symbol) ? '' : symbol.(g:airline_symbols.space), head)
endfunction endfunction
function! s:check_in_path()
if !exists('b:airline_file_in_root')
let root = get(b:, 'git_dir', get(b:, 'mercurial_dir', ''))
let bufferpath = resolve(fnamemodify(expand('%'), ':p'))
if !filereadable(root) "not a file
" if .git is a directory, it's the old submodule format
if match(root, '\.git$') >= 0
let root = expand(fnamemodify(root, ':h'))
else
" else it's the newer format, and we need to guesstimate
" 1) check for worktrees
if match(root, 'worktrees') > -1
" worktree can be anywhere, so simply assume true here
return 1
endif
" 2) check for submodules
let pattern = '\.git[\\/]\(modules\)[\\/]'
if match(root, pattern) >= 0
let root = substitute(root, pattern, '', '')
endif
endif
endif
let b:airline_file_in_root = stridx(bufferpath, root) > -1
endif
return b:airline_file_in_root
endfunction
function! s:reset_untracked_cache(shellcmdpost) function! s:reset_untracked_cache(shellcmdpost)
" shellcmdpost - whether function was called as a result of ShellCmdPost hook " shellcmdpost - whether function was called as a result of ShellCmdPost hook
if !g:airline#init#vim_async && !has('nvim') if !g:airline#init#vim_async && !has('nvim')
@ -328,9 +287,8 @@ endfunction
function! airline#extensions#branch#init(ext) function! airline#extensions#branch#init(ext)
call airline#parts#define_function('branch', 'airline#extensions#branch#get_head') call airline#parts#define_function('branch', 'airline#extensions#branch#get_head')
autocmd BufReadPost * unlet! b:airline_file_in_root autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check
autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check b:airline_fname_path autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check
autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check b:airline_fname_path
autocmd BufWritePost * call s:reset_untracked_cache(0) autocmd BufWritePost * call s:reset_untracked_cache(0)
autocmd ShellCmdPost * call s:reset_untracked_cache(1) autocmd ShellCmdPost * call s:reset_untracked_cache(1)
endfunction endfunction

View File

@ -0,0 +1,37 @@
" MIT License. Copyright (c) 2017 Cimbali.
" vim: et ts=2 sts=2 sw=2
scriptencoding utf-8
if !exists('*fugitive#head')
finish
endif
if exists("+autochdir") && &autochdir == 1
let s:fmod = ':p'
else
let s:fmod = ':.'
endif
function! airline#extensions#fugitiveline#bufname()
try
let buffer = fugitive#buffer()
if buffer.type('blob')
return fnamemodify(buffer.repo().translate(buffer.path()), s:fmod)
endif
catch
endtry
return fnamemodify(bufname('%'), s:fmod)
endfunction
function! airline#extensions#fugitiveline#init(ext)
if exists("+autochdir") && &autochdir == 1
" if 'acd' is set, vim-airline uses the path section, so we need to redefine this here as well
call airline#parts#define_raw('path', '%<%{airline#extensions#fugitiveline#bufname()}%m')
else
call airline#parts#define_raw('file', '%<%{airline#extensions#fugitiveline#bufname()}%m')
endif
endfunction

View File

@ -400,6 +400,13 @@ vim-bufferline <https://github.com/bling/vim-bufferline>
* determine whether bufferline will overwrite customization variables > * determine whether bufferline will overwrite customization variables >
let g:airline#extensions#bufferline#overwrite_variables = 1 let g:airline#extensions#bufferline#overwrite_variables = 1
< <
------------------------------------- *airline-fugitiveline*
This extension hides the fugitive://**// part of the buffer names, to only
show the file name as if it were in the current working tree.
* enable/disable bufferline integration >
let g:airline#extensions#fugitiveline#enabled = 1
<
------------------------------------- *airline-branch* ------------------------------------- *airline-branch*
vim-airline will display the branch-indicator together with the branch name in vim-airline will display the branch-indicator together with the branch name in
@ -446,6 +453,9 @@ notexists symbol will be displayed after the branch name.
return '[' . a:name . ']' return '[' . a:name . ']'
endfunction endfunction
< <
* truncate sha1 commits at this number of characters >
let g:airline#extensions#branch#sha1_len = 10
<
------------------------------------- *airline-syntastic* ------------------------------------- *airline-syntastic*
syntastic <https://github.com/vim-syntastic/syntastic> syntastic <https://github.com/vim-syntastic/syntastic>