|
| 1 | +if !exists("g:go_gopath") |
| 2 | + let g:go_gopath = $GOPATH |
| 3 | +endif |
| 4 | + |
| 5 | +augroup plugin-go-coverlay |
| 6 | + autocmd! |
| 7 | + autocmd BufEnter,BufWinEnter,BufFilePost * call go#coverlay#draw() |
| 8 | + autocmd BufWinLeave * call go#coverlay#clear() |
| 9 | +augroup END |
| 10 | + |
| 11 | +function! go#coverlay#draw() |
| 12 | + call go#coverlay#hook() |
| 13 | + call go#coverlay#clear() |
| 14 | + for m in b:go_coverlay_matches |
| 15 | + let id = matchadd(m.group, m.pattern, m.priority) |
| 16 | + call add(b:go_coverlay_match_ids, id) |
| 17 | + endfor |
| 18 | +endfunction |
| 19 | + |
| 20 | +function! go#coverlay#hook() |
| 21 | + "TODO: can we initialize buf local vars more smartly? |
| 22 | + if !exists("b:go_coverlay_matches") |
| 23 | + let b:go_coverlay_matches = [] |
| 24 | + endif |
| 25 | + if !exists("b:go_coverlay_match_ids") |
| 26 | + let b:go_coverlay_match_ids = [] |
| 27 | + endif |
| 28 | +endfunction |
| 29 | + |
| 30 | +"findbufnr look for the number of buffer that opens `file`, |
| 31 | +" as it is displayed by the ":ls" command. |
| 32 | +"If the buffer doesn't exist, -1 is returned. |
| 33 | +function! go#coverlay#findbufnr(file) |
| 34 | + if a:file[0] == "_" |
| 35 | + return bufnr(a:file[1:]) |
| 36 | + endif |
| 37 | + for path in split(g:go_gopath, ':') |
| 38 | + let nr = bufnr(path . '/src/' . a:file) |
| 39 | + if nr != -1 |
| 40 | + return nr |
| 41 | + endif |
| 42 | + endfor |
| 43 | + return -1 |
| 44 | +endfunction |
| 45 | + |
| 46 | +function! go#coverlay#isopenedon(file, bufnr) |
| 47 | + if a:file[0] == "_" |
| 48 | + if bufnr(a:file[1:]) == a:bufnr |
| 49 | + return 1 |
| 50 | + endif |
| 51 | + return 0 |
| 52 | + endif |
| 53 | + for path in split(g:go_gopath, ':') |
| 54 | + if bufnr(path . '/src/' . a:file) == a:bufnr |
| 55 | + return 1 |
| 56 | + endif |
| 57 | + endfor |
| 58 | + return 0 |
| 59 | +endfunction |
| 60 | + |
| 61 | +function! go#coverlay#parsegocoverline(line) |
| 62 | + " file:startline.col,endline.col numstmt count |
| 63 | + let mx = '\([^:]\+\):\(\d\+\)\.\(\d\+\),\(\d\+\)\.\(\d\+\)\s\(\d\+\)\s\(\d\+\)' |
| 64 | + let l = matchstr(a:line, mx) |
| 65 | + let ret = {} |
| 66 | + let ret.file = substitute(l, mx, '\1', '') |
| 67 | + let ret.startline = substitute(l, mx, '\2', '') |
| 68 | + let ret.startcol = substitute(l, mx, '\3', '') |
| 69 | + let ret.endline = substitute(l, mx, '\4', '') |
| 70 | + let ret.endcol = substitute(l, mx, '\5', '') |
| 71 | + let ret.numstmt = substitute(l, mx, '\6', '') |
| 72 | + let ret.cnt = substitute(l, mx, '\7', '') |
| 73 | + return ret |
| 74 | +endfunction |
| 75 | + |
| 76 | +function! go#coverlay#genmatch(cov) |
| 77 | + let pat1 = '\%>' . a:cov.startline . 'l' |
| 78 | + let pat2 = '\%<' . a:cov.endline . 'l' |
| 79 | + let pat3 = '\|\%' . a:cov.startline . 'l\_^\s\+\|\%' . a:cov.endline . 'l\_^\s\+\(\}$\)\@!' |
| 80 | + let color = 'covered' |
| 81 | + let prio = 6 |
| 82 | + if a:cov.cnt == 0 |
| 83 | + let color = 'uncover' |
| 84 | + let prio = 5 |
| 85 | + endif |
| 86 | + return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3, 'priority': prio} |
| 87 | +endfunction |
| 88 | + |
| 89 | +function! go#coverlay#overlay(file) |
| 90 | + call go#coverlay#hook() |
| 91 | + |
| 92 | + highlight covered term=bold ctermbg=green guibg=green |
| 93 | + highlight uncover term=bold ctermbg=red guibg=red |
| 94 | + |
| 95 | + if !filereadable(a:file) |
| 96 | + return |
| 97 | + endif |
| 98 | + let lines = readfile(a:file) |
| 99 | + let mode = lines[0] |
| 100 | + for line in lines[1:] |
| 101 | + let c = go#coverlay#parsegocoverline(line) |
| 102 | + let nr = go#coverlay#findbufnr(c.file) |
| 103 | + if nr == -1 |
| 104 | + "should we records cov data |
| 105 | + " even if it is not opened currently? |
| 106 | + continue |
| 107 | + endif |
| 108 | + let m = go#coverlay#genmatch(c) |
| 109 | + let matches = get(getbufvar(nr, ""), "go_coverlay_matches", []) |
| 110 | + call add(matches, m) |
| 111 | + call setbufvar(nr, "go_coverlay_matches", matches) |
| 112 | + endfor |
| 113 | + "TODO: can we draw other window for split windows mode? |
| 114 | + call go#coverlay#draw() |
| 115 | +endfunction |
| 116 | + |
| 117 | +let s:coverlay_handler_id = '' |
| 118 | +let s:coverlay_handler_jobs = {} |
| 119 | + |
| 120 | +function! s:coverlay_handler(job, exit_status, data) |
| 121 | + if !has_key(s:coverlay_handler_jobs, a:job.id) |
| 122 | + return |
| 123 | + endif |
| 124 | + let l:tmpname = s:coverlay_handler_jobs[a:job.id] |
| 125 | + if a:exit_status == 0 |
| 126 | + call go#coverlay#overlay(l:tmpname) |
| 127 | + endif |
| 128 | + |
| 129 | + call delete(l:tmpname) |
| 130 | + unlet s:coverlay_handler_jobs[a:job.id] |
| 131 | +endfunction |
| 132 | + |
| 133 | +function! go#coverlay#Coverlay(bang, ...) |
| 134 | + call go#coverlay#Clearlay() |
| 135 | + let l:tmpname=tempname() |
| 136 | + let args = [a:bang, 0, "-coverprofile", l:tmpname] |
| 137 | + |
| 138 | + if a:0 |
| 139 | + call extend(args, a:000) |
| 140 | + endif |
| 141 | + "TODO: add -coverpkg options based on current buf list |
| 142 | + let id = call('go#cmd#Test', args) |
| 143 | + if has('nvim') |
| 144 | + if s:coverlay_handler_id == '' |
| 145 | + let s:coverlay_handler_id = go#jobcontrol#AddHandler(function('s:coverlay_handler')) |
| 146 | + endif |
| 147 | + let s:coverlay_handler_jobs[id] = l:tmpname |
| 148 | + return |
| 149 | + endif |
| 150 | + if !v:shell_error |
| 151 | + call go#coverlay#overlay(l:tmpname) |
| 152 | + endif |
| 153 | + call delete(l:tmpname) |
| 154 | +endfunction |
| 155 | + |
| 156 | +function! go#coverlay#Clearlay() |
| 157 | + call go#coverlay#hook() |
| 158 | + call go#coverlay#clear() |
| 159 | + let b:go_coverlay_matches = [] |
| 160 | +endfunction |
| 161 | + |
| 162 | +function! go#coverlay#clear(...) |
| 163 | + for id in b:go_coverlay_match_ids |
| 164 | + call matchdelete(id) |
| 165 | + endfor |
| 166 | + let b:go_coverlay_match_ids = [] |
| 167 | +endfunction |
| 168 | + |
| 169 | +function! go#coverlay#matches() |
| 170 | + call go#coverlay#hook() |
| 171 | + return b:go_coverlay_matches |
| 172 | +endfunction |
| 173 | + |
| 174 | +" vim:ts=4:sw=4:et |
0 commit comments