Skip to content

Commit

Permalink
fixed behaviour when using multiple buffers, moved functions to autol…
Browse files Browse the repository at this point in the history
…oad, other bugfixes
  • Loading branch information
tveskag committed Mar 12, 2019
1 parent 624497b commit 14e361f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 91 deletions.
35 changes: 0 additions & 35 deletions autoload/blameballs.vim

This file was deleted.

93 changes: 93 additions & 0 deletions autoload/blameline.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
function! s:nvimAnnotate(comment, bufN, lineN)
call nvim_buf_clear_namespace(a:bufN, s:ns_id, 0, -1)
call nvim_buf_set_virtual_text(a:bufN, s:ns_id, a:lineN - 1, [[a:comment, "Comment"]], {})
endfunction

function! s:vimEcho(comment, ...)
echo ''
echom a:comment
endfunction

if has('nvim') && has('nvim-0.3.4') && (g:blameLineUseVirtualText)
let s:ns_id = nvim_create_namespace('nvim-blame-line')
let s:annotateLine = function('s:nvimAnnotate')
else
let s:annotateLine = function('s:vimEcho')
endif

function! s:getAnnotation(bufN, lineN)
let l:git_parent = fnamemodify(b:BlameLineGitdir, ':h')
let l:gitcommand = 'cd '.l:git_parent.'; git --git-dir='.b:BlameLineGitdir.' --work-tree='.l:git_parent
let l:blame = systemlist(l:gitcommand.' annotate --contents - '.expand('%:p').' --porcelain -L '.a:lineN.','.a:lineN.' -M', a:bufN)
if v:shell_error > 0
call s:vimEcho(l:blame[-1])
return ''
endif
let l:commit = strpart(l:blame[0], 0, 40)
let l:format = '%an | %ar | %s'
if l:commit ==# '0000000000000000000000000000000000000000'
let l:annotation = ['Not committed yet']
else
let l:annotation = systemlist(l:gitcommand.' show '.l:commit.' --format="'.l:format.'"')
endif
if v:shell_error > 0
call s:vimEcho(l:annotation[-1])
return ''
endif
return l:annotation[0]
endfunction

function! s:createCursorHandler(bufN)
function! s:handler(buffer, lineN) closure
let l:comment = s:getAnnotation(a:buffer, a:lineN)
call s:annotateLine(l:comment, a:buffer, a:lineN)
endfunction

if has('timers') && has('lambda')
let l:cursorTimer = 0

function! s:debouncedHandler(buffer, lineN) closure
call timer_stop(l:cursorTimer)
let l:cursorTimer = timer_start(70, {-> s:handler(a:buffer, a:lineN)})
endfunction

return function('s:debouncedHandler', [a:bufN])
else
return function('s:handler', [a:bufN])
endif
endfunction

function! blameline#InitBlameLine()
if !exists('*b:ToggleBlameLine')
let b:BlameLineGitdir = systemlist('cd '.expand('%:p:h').'; git rev-parse --git-dir')[-1]
if v:shell_error > 0
let b:ToggleBlameLine = function('s:vimEcho', [b:BlameLineGitdir])
return
endif

let l:rel_to_git_parent = substitute(expand('%:p'), fnamemodify(b:BlameLineGitdir, ':h').'/', '', '')
let l:fileExists = systemlist('cd '.expand('%:p:h').'; git cat-file -e HEAD:'.l:rel_to_git_parent)
if v:shell_error > 0
let b:ToggleBlameLine = function('s:vimEcho', l:fileExists)
return
endif

let b:ToggleBlameLine = function('blameline#EnableBlameLine')
endif
endfunction

function! blameline#EnableBlameLine()
let b:onCursorMoved = s:createCursorHandler(bufnr('%'))
augroup showBlameLine
autocmd CursorMoved <buffer> call b:onCursorMoved(line('.'))
augroup END
call b:onCursorMoved(line('.'))

let b:ToggleBlameLine = function('blameline#DisableBlameLine')
endfunction

function! blameline#DisableBlameLine()
call s:annotateLine('', bufnr('%'), 1)
autocmd! showBlameLine * <buffer>
let b:ToggleBlameLine = function('blameline#EnableBlameLine')
endfunction
57 changes: 1 addition & 56 deletions plugin/blameline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,11 @@ let g:blameline_loaded = 1

let g:blameLineUseVirtualText = get(g:, 'blameLineUseVirtualText', 1)

if has('nvim') && has('nvim-0.3.4') && (g:blameLineUseVirtualText)
let s:annotateLine = function('blameballs#nvimAnnotate')
else
let s:annotateLine = function('blameballs#vimEcho')
endif

augroup enableBlameLine
autocmd!
autocmd BufReadPre,FileReadPre,BufEnter * call InitBlameLine()
autocmd BufReadPre,FileReadPre * call blameline#InitBlameLine()
augroup END

function! InitBlameLine()
if !exists('b:git_dir')
let b:BlameLineGitdir = system('cd '.expand(':p:h').'; git rev-parse --git-dir')[0]
else
let b:BlameLineGitdir = b:git_dir
endif

if !exists('*b:ToggleBlameLine')
let b:ToggleBlameLine = function('blameline#EnableBlameLine')
endif
endfunction

function! s:createCursorHandler(bufN)
function! s:handler(lineN) closure
let l:comment = blameballs#getAnnotation(a:bufN, a:lineN)
call s:annotateLine(a:bufN, a:lineN, l:comment)
endfunction

if has('timers') && has('lambda')
let l:cursorTimer = 0

function! s:debouncedHandler(lineN) closure
call timer_stop(l:cursorTimer)
let l:cursorTimer = timer_start(20, {-> s:handler(a:lineN)})
endfunction

return funcref('s:debouncedHandler')
else
return funcref('s:handler')
endif
endfunction


function! blameline#EnableBlameLine()
let s:onCursorMoved = s:createCursorHandler(bufnr('%'))
augroup showBlameLine
autocmd CursorMoved <buffer> call s:onCursorMoved(line('.'))
augroup END
call s:onCursorMoved(line('.'))

let b:ToggleBlameLine = function('blameline#DisableBlameLine')
endfunction

function! blameline#DisableBlameLine()
call s:annotateLine(bufnr('%'), 0, '')
autocmd! showBlameLine * <buffer>
let b:ToggleBlameLine = function('blameline#EnableBlameLine')
endfunction

command! ToggleBlameLine :call b:ToggleBlameLine()
command! EnableBlameLine :call blameline#EnableBlameLine()
command! DisableBlameLine :call blameline#DisableBlameLine()

0 comments on commit 14e361f

Please sign in to comment.