Skip to content

Commit 7c4157b

Browse files
committed
coverage: improvements and integration into vim-go
1 parent f92fac2 commit 7c4157b

File tree

8 files changed

+77
-69
lines changed

8 files changed

+77
-69
lines changed

README.md

-9
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ People have asked for this for a long time, now you can be a fully supporter by
5757

5858
[https://www.patreon.com/fatih](https://www.patreon.com/fatih)
5959

60-
![ss-vim-go-coverlay](https://cloud.githubusercontent.com/assets/3804806/5319001/81a3b89a-7ce8-11e4-9fbd-2f0fd00ce1c7.gif)
61-
6260
## Install
6361

6462
Vim-go follows the standard runtime path structure, so I highly recommend to
@@ -219,15 +217,8 @@ By default when `:GoInstallBinaries` is called, the binaries are installed to
219217
`$GOBIN` or `$GOPATH/bin`. To change it:
220218

221219
```vim
222-
<<<<<<< HEAD
223220
let g:go_bin_path = expand("~/.gotools")
224221
let g:go_bin_path = "/home/fatih/.mypath" "or give absolute path
225-
=======
226-
au FileType go nmap <leader>c <Plug>(go-coverlay) "test coverage then overlay covered lines
227-
au FileType go nmap <leader>C <Plug>(go-clearlay) "clear overlay
228-
229-
au BufWritePost *.go call go#coverlay#Coverlay() "run test and cover on file save
230-
>>>>>>> 70c8cf8... code cleanup
231222
```
232223

233224
### Using with Neovim (beta)

autoload/go/coverlay.vim

+45-33
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
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-
111
function! go#coverlay#draw()
122
call go#coverlay#hook()
133
call go#coverlay#clear()
144
for m in b:go_coverlay_matches
15-
let id = matchadd(m.group, m.pattern, m.priority)
5+
let id = matchaddpos(m.group, m.pos)
166
call add(b:go_coverlay_match_ids, id)
177
endfor
188
endfunction
@@ -22,19 +12,20 @@ function! go#coverlay#hook()
2212
if !exists("b:go_coverlay_matches")
2313
let b:go_coverlay_matches = []
2414
endif
15+
2516
if !exists("b:go_coverlay_match_ids")
2617
let b:go_coverlay_match_ids = []
2718
endif
2819
endfunction
2920

30-
"findbufnr look for the number of buffer that opens `file`,
21+
" findbufnr look for the number of buffer that opens `file`,
3122
" as it is displayed by the ":ls" command.
32-
"If the buffer doesn't exist, -1 is returned.
23+
" If the buffer doesn't exist, -1 is returned.
3324
function! go#coverlay#findbufnr(file)
3425
if a:file[0] == "_"
3526
return bufnr(a:file[1:])
3627
endif
37-
for path in split(g:go_gopath, ':')
28+
for path in split(go#path#Default(), ':')
3829
let nr = bufnr(path . '/src/' . a:file)
3930
if nr != -1
4031
return nr
@@ -61,37 +52,51 @@ endfunction
6152
function! go#coverlay#parsegocoverline(line)
6253
" file:startline.col,endline.col numstmt count
6354
let mx = '\([^:]\+\):\(\d\+\)\.\(\d\+\),\(\d\+\)\.\(\d\+\)\s\(\d\+\)\s\(\d\+\)'
64-
let l = matchstr(a:line, mx)
55+
let tokens = matchlist(a:line, mx)
6556
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', '')
57+
let ret.file = tokens[1]
58+
let ret.startline = str2nr(tokens[2])
59+
let ret.startcol = str2nr(tokens[3])
60+
let ret.endline = str2nr(tokens[4])
61+
let ret.endcol = str2nr(tokens[5])
62+
let ret.numstmt = tokens[6]
63+
let ret.cnt = tokens[7]
7364
return ret
7465
endfunction
7566

7667
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\+\(\}$\)\@!'
8068
let color = 'covered'
81-
let prio = 6
8269
if a:cov.cnt == 0
8370
let color = 'uncover'
84-
let prio = 5
8571
endif
86-
return {'group': color, 'pattern': pat1 . '\_^\s\+' . pat2 . pat3, 'priority': prio}
72+
73+
let matches = []
74+
75+
" if start and end are the same, also specify the byte length
76+
" example: foo.go:92.2,92.65 1 0
77+
if a:cov.startline == a:cov.endline
78+
call add(matches, {'group': color, 'pos': [[a:cov.startline, a:cov.startcol, a:cov.endcol - a:cov.startcol]] })
79+
return matches
80+
endif
81+
82+
" add start and end columns
83+
call add(matches, {'group': color, 'pos': [[a:cov.startline, a:cov.startcol]] })
84+
85+
" and then the remaining lines
86+
let start_line = a:cov.startline
87+
while start_line < a:cov.endline
88+
let start_line += 1
89+
call add(matches, {'group': color, 'pos': [[start_line]] })
90+
endwhile
91+
92+
call add(matches, {'group': color, 'pos': [[a:cov.endline, a:cov.endcol-1]] })
93+
94+
return matches
8795
endfunction
8896

8997
function! go#coverlay#overlay(file)
9098
call go#coverlay#hook()
9199

92-
highlight covered term=bold ctermbg=green guibg=green
93-
highlight uncover term=bold ctermbg=red guibg=red
94-
95100
if !filereadable(a:file)
96101
return
97102
endif
@@ -105,11 +110,16 @@ function! go#coverlay#overlay(file)
105110
" even if it is not opened currently?
106111
continue
107112
endif
108-
let m = go#coverlay#genmatch(c)
113+
let gen_matches = go#coverlay#genmatch(c)
109114
let matches = get(getbufvar(nr, ""), "go_coverlay_matches", [])
110-
call add(matches, m)
115+
call extend(matches, gen_matches)
111116
call setbufvar(nr, "go_coverlay_matches", matches)
112117
endfor
118+
119+
syntax clear
120+
highlight covered term=bold ctermfg=118 guifg=#A6E22E
121+
highlight uncover term=bold ctermfg=197 guifg=#F92672
122+
113123
"TODO: can we draw other window for split windows mode?
114124
call go#coverlay#draw()
115125
endfunction
@@ -154,6 +164,8 @@ function! go#coverlay#Coverlay(bang, ...)
154164
endfunction
155165

156166
function! go#coverlay#Clearlay()
167+
syntax reset
168+
syntax on
157169
call go#coverlay#hook()
158170
call go#coverlay#clear()
159171
let b:go_coverlay_matches = []

doc/vim-go.txt

+17-3
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,27 @@ COMMANDS *go-commands*
323323

324324
If [!] is not given the first error is jumped to.
325325

326-
If using neovim `:GoTestCompile` will run in a new terminal or run asynchronously
327-
in the background according to |g:go_term_enabled|. You can set the mode of
328-
the new terminal with |g:go_term_mode|.
326+
If using neovim `:GoTestCompile` will run in a new terminal or run
327+
asynchronously in the background according to |g:go_term_enabled|. You can
328+
set the mode of the new terminal with |g:go_term_mode|.
329329

330330
*:GoCoverage*
331331
:GoCoverage[!] [options]
332332

333+
Create a coverage profile and annotates the current file's source code.
334+
To clear the annotation use |:GoCoverageClear|.
335+
336+
If [!] is not given the first error is jumped to.
337+
338+
*:GoCoverageClear*
339+
:GoCoverageClear[!] [options]
340+
341+
Clears the go cover annotation of the current file.
342+
343+
344+
*:GoCoverageBrowser*
345+
:GoCoverageBrowser[!] [options]
346+
333347
Create a coverage profile and open a browser to display the annotated
334348
source code of the current package.
335349

ftplugin/go/commands.vim

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ command! -nargs=* -bang GoInstall call go#cmd#Install(<bang>0, <f-args>)
2626
command! -nargs=* -bang GoTest call go#cmd#Test(<bang>0, 0, <f-args>)
2727
command! -nargs=* -bang GoTestFunc call go#cmd#TestFunc(<bang>0, <f-args>)
2828
command! -nargs=* -bang GoTestCompile call go#cmd#Test(<bang>0, 1, <f-args>)
29-
command! -nargs=* -bang GoCoverage call go#cmd#Coverage(<bang>0, <f-args>)
29+
30+
" -- cover
31+
command! -nargs=* -bang GoCoverage call go#coverlay#Coverlay(<bang>0, <f-args>)
32+
command! -nargs=* GoCoverageClear call go#coverlay#Clearlay(<f-args>)
33+
command! -nargs=* -bang GoCoverageBrowser call go#cmd#Coverage(<bang>0, <f-args>)
3034

3135
" -- play
3236
command! -nargs=0 -range=% GoPlay call go#play#Share(<count>, <line1>, <line2>)

ftplugin/go/coverlay.vim

-14
This file was deleted.

ftplugin/go/mappings.vim

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ nnoremap <silent> <Plug>(go-install) :<C-u>call go#cmd#Install(!g:go_jump_to_err
2323
nnoremap <silent> <Plug>(go-test) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 0)<CR>
2424
nnoremap <silent> <Plug>(go-test-func) :<C-u>call go#cmd#TestFunc(!g:go_jump_to_error)<CR>
2525
nnoremap <silent> <Plug>(go-test-compile) :<C-u>call go#cmd#Test(!g:go_jump_to_error, 1)<CR>
26-
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error)<CR>
26+
27+
nnoremap <silent> <Plug>(go-coverage) :<C-u>call go#coverlay#Coverlay(!g:go_jump_to_error)<CR>
28+
nnoremap <silent> <Plug>(go-coverage-clear) :<C-u>call go#coverlay#Clearlay()<CR>
29+
nnoremap <silent> <Plug>(go-coverage-browser) :<C-u>call go#cmd#Coverage(!g:go_jump_to_error)<CR>
2730
2831
nnoremap <silent> <Plug>(go-files) :<C-u>call go#tool#Files()<CR>
2932
nnoremap <silent> <Plug>(go-deps) :<C-u>call go#tool#Deps()<CR>

plugin/go.vim

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ augroup vim-go
167167
if get(g:, "go_metalinter_autosave", 0)
168168
autocmd BufWritePost *.go call go#lint#Gometa(1)
169169
endif
170+
171+
" redraw coverage for all window, experimental
172+
if get(g:, "go_coverage_draw", 0)
173+
autocmd BufEnter,BufWinEnter,BufFilePost * call go#coverlay#draw()
174+
autocmd BufWinLeave * call go#coverlay#clear()
175+
endif
170176
augroup END
171177

172178

plugin/vim-go-coverlay.vim

-8
This file was deleted.

0 commit comments

Comments
 (0)