Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Vim plugin for Go test coverage profiles
" cover.vim - Vim plugin for Go test coverage profiles
" install in ftplugin/go
"
" ":Cover coverprofile" will open the current file in a new read-only window,
" highlighting the code regarding to whether it is covered by tests or not.
" You can change the colors by highlighting goTestCovered and goTestNotCovered
" from your vimrc.
if exists("b:did_ftplugin_go_cover")
finish
endif
if &t_Co > 255
highlight default goTestNotCovered ctermfg=124 guifg=#C00000
highlight default goTestCovered ctermfg=42 guifg=#2cd495
else
highlight default goTestNotCovered ctermfg=DarkRed guifg=#C00000
highlight default goTestCovered ctermfg=DarkGreen guifg=#2cd495
endif
command! -buffer -complete=file -nargs=1 Cover call s:GoCover(<f-args>)
function! s:GoCover(fname)
let contents = getbufline("", 1, "$")
let fullname = expand("%:p")
new
setlocal buftype=nofile bufhidden=hide noswapfile
call setline(1, contents[0])
for line in contents[1:]
call append(line("$"), line)
endfor
syn clear
let lines = readfile(a:fname)
for line in lines[1:]
let tokens = matchlist(line, '^\(.\+\):\(\d\+\).\(\d\+\),\(\d\+\).\(\d\+\) \d\+ \(\d\)')
if empty(tokens)
continue
endif
let gopath = split($GOPATH, ":")
for path in gopath
if fullname == path . "/src/" . tokens[1]
if tokens[6] == 0
let group = "goTestNotCovered"
else
let group = "goTestCovered"
endif
execute "syn region " . group . " start=/\\%" . tokens[2] . "l\\%" . tokens[3] . "c/ end=/\\%" . tokens[4] . "l\\%" . tokens[5] . "c/"
endif
endfor
endfor
setlocal noma
endfunction
let b:did_ftplugin_go_cover = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment