Skip to content

Commit f92fac2

Browse files
t-yukifatih
authored andcommitted
Add vim-go-coverlay
Merge @t-yuki's https://github.com/t-yuki/vim-go-coverlay plugin into vim-go
1 parent 1a82fb7 commit f92fac2

20 files changed

+522
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ IMPROVEMENTS:
1111

1212
BUG FIXES:
1313

14-
* Term mode: fix closing location list if result is succesfull after a failed attempt [gh-768]
14+
* Term mode: fix closing location list if result is successful after a failed attempt [gh-768]
1515
* Syntax: fix gotexttmpl identifier highlighting [gh-778]
1616

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'vim-flavor', '~> 2.2.1'

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ 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+
6062
## Install
6163

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

219221
```vim
222+
<<<<<<< HEAD
220223
let g:go_bin_path = expand("~/.gotools")
221224
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
222231
```
223232

224233
### Using with Neovim (beta)

Rakefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env rake
2+
3+
task :ci => [:dump, :test]
4+
5+
task :dump do
6+
sh 'vim --version'
7+
end
8+
9+
# Firstly, `bundle install; bundle install --deployment`
10+
# Then, `rake test`
11+
task :test do
12+
sh 'bundle exec vim-flavor test'
13+
end

VimFlavor

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flavor 'fatih/vim-go', '~> 1.5'

autoload/go/coverlay.vim

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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

ftplugin/go/coverlay.vim

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
if exists("b:did_ftplugin_go_coverlay")
2+
finish
3+
endif
4+
let b:did_ftplugin_go_coverlay = 1
5+
6+
" Some handy plug mappings
7+
nnoremap <silent> <Plug>(go-coverlay) :<C-u>call go#coverlay#Coverlay(!g:go_jump_to_error)<CR>
8+
nnoremap <silent> <Plug>(go-clearlay) :<C-u>call go#coverlay#Clearlay()<CR>
9+
10+
" coverlay
11+
command! -nargs=* -bang GoCoverlay call go#coverlay#Coverlay(<bang>0, <f-args>)
12+
command! -nargs=* GoClearlay call go#coverlay#Clearlay(<f-args>)
13+
14+
" vim:ts=4:sw=4:et

plugin/vim-go-coverlay.vim

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
" vim-go-coverlay - Go (golang) code coverage overlays support
2+
"
3+
if exists("g:loaded_vim_go_coverlay")
4+
finish
5+
endif
6+
7+
let g:loaded_vim_go_coverlay = 1
8+
" vim:ts=4:sw=4:et

0 commit comments

Comments
 (0)