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
1
function ! go#coverlay#draw ()
12
2
call go#coverlay#hook ()
13
3
call go#coverlay#clear ()
14
4
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 )
16
6
call add (b: go_coverlay_match_ids , id)
17
7
endfor
18
8
endfunction
@@ -22,19 +12,20 @@ function! go#coverlay#hook()
22
12
if ! exists (" b:go_coverlay_matches" )
23
13
let b: go_coverlay_matches = []
24
14
endif
15
+
25
16
if ! exists (" b:go_coverlay_match_ids" )
26
17
let b: go_coverlay_match_ids = []
27
18
endif
28
19
endfunction
29
20
30
- " findbufnr look for the number of buffer that opens `file`,
21
+ " findbufnr look for the number of buffer that opens `file`,
31
22
" 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.
33
24
function ! go#coverlay#findbufnr (file )
34
25
if a: file [0 ] == " _"
35
26
return bufnr (a: file [1 :])
36
27
endif
37
- for path in split (g: go_gopath , ' :' )
28
+ for path in split (go#path#Default () , ' :' )
38
29
let nr = bufnr (path . ' /src/' . a: file )
39
30
if nr != -1
40
31
return nr
@@ -61,37 +52,51 @@ endfunction
61
52
function ! go#coverlay#parsegocoverline (line )
62
53
" file:startline.col,endline.col numstmt count
63
54
let mx = ' \([^:]\+\):\(\d\+\)\.\(\d\+\),\(\d\+\)\.\(\d\+\)\s\(\d\+\)\s\(\d\+\)'
64
- let l = matchstr (a: line , mx)
55
+ let tokens = matchlist (a: line , mx)
65
56
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 ]
73
64
return ret
74
65
endfunction
75
66
76
67
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
68
let color = ' covered'
81
- let prio = 6
82
69
if a: cov .cnt == 0
83
70
let color = ' uncover'
84
- let prio = 5
85
71
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
87
95
endfunction
88
96
89
97
function ! go#coverlay#overlay (file )
90
98
call go#coverlay#hook ()
91
99
92
- highlight covered term = bold ctermbg= green guibg= green
93
- highlight uncover term = bold ctermbg= red guibg= red
94
-
95
100
if ! filereadable (a: file )
96
101
return
97
102
endif
@@ -105,11 +110,16 @@ function! go#coverlay#overlay(file)
105
110
" even if it is not opened currently?
106
111
continue
107
112
endif
108
- let m = go#coverlay#genmatch (c )
113
+ let gen_matches = go#coverlay#genmatch (c )
109
114
let matches = get (getbufvar (nr, " " ), " go_coverlay_matches" , [])
110
- call add (matches, m )
115
+ call extend (matches, gen_matches )
111
116
call setbufvar (nr, " go_coverlay_matches" , matches)
112
117
endfor
118
+
119
+ syntax clear
120
+ highlight covered term = bold ctermfg= 118 guifg= #A6E22E
121
+ highlight uncover term = bold ctermfg= 197 guifg= #F92672
122
+
113
123
" TODO: can we draw other window for split windows mode?
114
124
call go#coverlay#draw ()
115
125
endfunction
@@ -154,6 +164,8 @@ function! go#coverlay#Coverlay(bang, ...)
154
164
endfunction
155
165
156
166
function ! go#coverlay#Clearlay ()
167
+ syntax reset
168
+ syntax on
157
169
call go#coverlay#hook ()
158
170
call go#coverlay#clear ()
159
171
let b: go_coverlay_matches = []
0 commit comments