ViM plug-in to visualize your code coverage in real time line-by-line.

umbrella.vim 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. if exists('g:loaded_umbrella') || &cp || version < 700
  2. finish
  3. endif
  4. let g:loaded_umbrella = 0.1
  5. let s:keepcpo = &cpo
  6. set cpo&vim
  7. " Declare highlights.
  8. highlight UmbrellaCovered ctermfg=0 ctermbg=121 guifg=Black guibg=Green
  9. highlight UmbrellaPartial ctermfg=0 ctermbg=11 guifg=Black guibg=Yellow
  10. highlight UmbrellaNone ctermfg=15 ctermbg=1 guifg=White guibg=Red
  11. " Declare signs.
  12. sign define UmbrellaCovered text=°° texthl=UmbrellaCovered
  13. sign define UmbrellaPartial text=÷÷ texthl=UmbrellaPartial
  14. sign define UmbrellaNone text=×× texthl=UmbrellaNone
  15. function! s:Initialize()
  16. let s:coverage_systems = {
  17. \'umbrella': './umbrella',
  18. \}
  19. if exists('g:umbrella_root')
  20. unlet g:umbrella_root
  21. endif
  22. if exists('g:umbrella_program')
  23. unlet g:umbrella_program
  24. endif
  25. if exists('g:umbrella_coverage')
  26. unlet g:umbrella_coverage
  27. endif
  28. call s:FindProgram(expand('%:p:h'))
  29. endfunction
  30. function! s:FindProgram(dir)
  31. for [l:filename, l:program] in items(s:coverage_systems)
  32. let l:found = globpath(a:dir, l:filename)
  33. if filereadable(l:found)
  34. let g:umbrella_root = a:dir
  35. let g:umbrella_program = l:program
  36. let g:umbrella_coverage = a:dir . "/.umbrella-coverage"
  37. endif
  38. endfor
  39. let l:parent = fnamemodify(a:dir, ':h')
  40. if l:parent != a:dir
  41. call s:FindProgram(l:parent)
  42. endif
  43. endfunction
  44. function! s:SetMakeProgram(program)
  45. if len(a:program)
  46. let &l:makeprg=a:program
  47. endif
  48. endfunction
  49. function! s:RunCoverage()
  50. if exists('g:umbrella_root') && exists('g:umbrella_program')
  51. exec "cd! " . g:umbrella_root
  52. call s:SetMakeProgram(g:umbrella_program)
  53. exec ":silent make"
  54. cd! -
  55. endif
  56. endfunction
  57. function! s:ClearCoverage()
  58. sign unplace *
  59. endfunction
  60. function! s:ShowCoverage()
  61. call s:ClearCoverage()
  62. if !has("signs") || empty(bufname(""))
  63. return
  64. endif
  65. if exists('g:umbrella_root') && exists('g:umbrella_coverage')
  66. exec "cd! " . g:umbrella_root
  67. for line in readfile(g:umbrella_coverage)
  68. let parts = split(l:line, ";")
  69. if bufexists(l:parts[0])
  70. if len(l:parts[1])
  71. for n in split(l:parts[1], ",")
  72. exe ":sign place 1 line=" . l:n . " name=UmbrellaCovered file=" . l:parts[0]
  73. endfor
  74. endif
  75. if len(l:parts[2])
  76. for n in split(l:parts[2], ",")
  77. exe ":sign place 1 line=" . l:n . " name=UmbrellaPartial file=" . l:parts[0]
  78. endfor
  79. endif
  80. if len(l:parts[3])
  81. for n in split(l:parts[3], ",")
  82. exe ":sign place 1 line=" . l:n . " name=UmbrellaNone file=" . l:parts[0]
  83. endfor
  84. endif
  85. endif
  86. endfor
  87. cd! -
  88. endif
  89. endfunction
  90. " Commands
  91. command Umbrella
  92. \ call s:RunCoverage() <bar>
  93. \ call s:ShowCoverage()
  94. command UmbrellaRefresh
  95. \ call s:ShowCoverage()
  96. command UmbrellaClear
  97. \ call s:ClearCoverage()
  98. " Autocommands
  99. augroup umbrella
  100. autocmd BufReadPost * call s:BufReadPostHook()
  101. autocmd BufWritePost * call s:BufWritePostHook()
  102. autocmd BufWinEnter * call s:BufWinEnterHook()
  103. autocmd BufEnter * call s:BufEnterHook()
  104. augroup END
  105. " Hooks
  106. function! s:BufReadPostHook()
  107. call s:Initialize()
  108. call s:ShowCoverage()
  109. endfunction
  110. function! s:BufWritePostHook()
  111. call s:RunCoverage()
  112. call s:ShowCoverage()
  113. endfunction
  114. function! s:BufWinEnterHook()
  115. call s:Initialize()
  116. call s:ShowCoverage()
  117. endfunction
  118. function! s:BufEnterHook()
  119. call s:Initialize()
  120. call s:ShowCoverage()
  121. endfunction
  122. let &cpo=s:keepcpo
  123. unlet s:keepcpo
  124. " vim: et:sw=4:ts=8:ft=vim