blib/lib/Devel/Cover/Report/Vim.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 58 CC 2 SCAR 9.4
CC
2
Cov
0%
get_options
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.6
1 # Copyright 2012-2026, Paul Johnson (paul@pjcj.net)
2  
3 # This software is free. It is licensed under the same terms as Perl itself.
4  
5 # The latest version of this software should be available from my homepage:
6 # https://pjcj.net
7  
8 package Devel::Cover::Report::Vim;
9  
10 use 5.20.0;
11 use warnings;
12 use feature "signatures";
13 no warnings "experimental::signatures";
14  
15 our $VERSION;
16  
17 BEGIN {
18 our $VERSION = '1.52'; # VERSION
19 }
20  
21 use parent "Devel::Cover::Base::Editor";
22  
23 use Devel::Cover::Inc ();
24  
25 BEGIN { $VERSION //= $Devel::Cover::Inc::VERSION }
26  
27 use Getopt::Long qw( GetOptions );
28  
29 my %Templates;
30  
31 sub get_options ($self, $opt) {
32 $opt->{outputfile} = "coverage.vim";
33 die "Invalid command line options"
34 unless GetOptions($opt, qw( outputfile=s ));
35 }
36  
37 sub template_name { "vim" }
38 sub templates { \%Templates }
39 sub output_message ($self, $out) { "Vim script written to $out" }
40  
41 $Templates{vim} = <<'EOT';
42 " This file was generated by Devel::Cover Version [% version %]
43 " Devel::Cover is copyright 2001-2026, Paul Johnson (paul@pjcj.net)
44 " Devel::Cover is free. It is licensed under the same terms as Perl itself.
45 " The latest version of Devel::Cover should be available from my homepage:
46 " https://pjcj.net
47  
48 [% FOREACH r = runs %]
49 " Run: [% r.run %]
50 " Perl version: [% r.perl %]
51 " OS: [% r.OS %]
52 " Start: [% r.start %]
53 " Finish: [% r.finish %]
54  
55 [% END %]
56  
57 highlight cov_pod ctermfg=Green cterm=bold gui=bold guifg=Green
58 highlight cov_pod_error ctermfg=Red cterm=bold gui=bold guifg=Red
59 highlight cov_subroutine ctermfg=Green cterm=bold gui=bold guifg=Green
60 highlight cov_subroutine_error ctermfg=Red cterm=bold gui=bold guifg=Red
61 highlight cov_statement ctermfg=Green cterm=bold gui=bold guifg=Green
62 highlight cov_statement_error ctermfg=Red cterm=bold gui=bold guifg=Red
63 highlight cov_branch ctermfg=Green cterm=bold gui=bold guifg=Green
64 highlight cov_branch_error ctermfg=Red cterm=bold gui=bold guifg=Red
65 highlight cov_condition ctermfg=Green cterm=bold gui=bold guifg=Green
66 highlight cov_condition_error ctermfg=Red cterm=bold gui=bold guifg=Red
67  
68 sign define pod linehl=cov texthl=cov_pod text=P
69 sign define pod_error linehl=err texthl=cov_pod_error text=P
70 sign define subroutine linehl=cov texthl=cov_subroutine text=R
71 sign define subroutine_error linehl=err texthl=cov_subroutine_error text=R
72 sign define statement linehl=cov texthl=cov_statement text=S
73 sign define statement_error linehl=err texthl=cov_statement_error text=S
74 sign define branch linehl=cov texthl=cov_branch text=B
75 sign define branch_error linehl=err texthl=cov_branch_error text=B
76 sign define condition linehl=cov texthl=cov_condition text=C
77 sign define condition_error linehl=err texthl=cov_condition_error text=C
78  
79 function! CoverageOld(filename)
80 endfunction
81  
82 function! CoverageValid(filename)
83 endfunction
84  
85 " The signs definitions can be overridden. To do this add a file called
86 " devel-cover.vim at some appropriate point in your ~/.vim directory and add
87 " your local configuration commands there.
88 " For example, I use the vim solarized theme and I have the following commands
89 " in my local configuration file ~/.vim/local/devel-cover.vim:
90 "
91 " ----------------------------------------------------------------------------
92 "
93 " let s:fg_cover = "#859900"
94 " let s:fg_error = "#dc322f"
95 " let s:bg_valid = "#073642"
96 " let s:bg_old = "#342a2a"
97 "
98 " let s:types = [ "pod", "subroutine", "statement", "branch", "condition", ]
99 "
100 " for s:type in s:types
101 " exe "highlight cov_" . s:type . " ctermbg=0 cterm=bold gui=NONE guifg=" . s:fg_cover
102 " exe "highlight cov_" . s:type . "_error ctermbg=0 cterm=bold gui=NONE guifg=" . s:fg_error
103 " endfor
104 " exe "highlight SignColumn ctermbg=0 guibg=" . s:bg_valid
105 "
106 " " highlight cov ctermbg=8 guibg=#002b36
107 " " highlight err ctermbg=0 guibg=#073642
108 "
109 " function! s:set_bg(bg)
110 " for s:type in s:types
111 " exe "highlight cov_" . s:type . " guibg=" . a:bg
112 " exe "highlight cov_" . s:type . "_error guibg=" . a:bg
113 " endfor
114 " exe "highlight SignColumn ctermbg=0 guibg=" . a:bg
115 " endfunction
116 "
117 " function! CoverageValid(filename)
118 " call s:set_bg(s:bg_valid)
119 " endfunction
120 "
121 " function! CoverageOld(filename)
122 " call s:set_bg(s:bg_old)
123 " endfunction
124 "
125 " ----------------------------------------------------------------------------
126  
127 let s:config = findfile("devel-cover.vim", expand('$HOME/.vim') . "/**")
128 if strlen(s:config)
129 echom "Reading local config from " . s:config
130 exe "source " . s:config
131 endif
132  
133 let s:types = [
134 [%- FOREACH type = types -%] "[%- type -%]",[%- END -%]
135 [%- FOREACH type = types -%] "[%- type -%]_error",[%- END -%]
136 ]
137  
138 [%- MACRO criterion(file, crit, error) BLOCK %]
139 \ '[% crit %][% error ? "_error" : "" %]': [
140 [%- criteria = cover.file("$file").$crit -%]
141 [%- FOREACH loc = criteria.items.nsort -%]
142 [%- cov = 0 -%]
143 [%- FOREACH l = criteria.location("$loc") -%]
144 [%- IF error ? l.error : l.covered -%] [% loc -%],[%- cov = 1; LAST; END -%]
145 [%- LAST IF cov; END -%]
146 [%- END -%]
147 ],
148 [%- END %]
149  
150 let s:coverage =
151 \ {
152 \[% FOREACH file = files %]
153 \ '[% file %]':
154 \ {
155 [%- FOREACH type = types; criterion(file, type, 0); criterion(file, type, 1); END %]
156 \ },
157 \[% END %]
158 \ }
159  
160 let s:cov_time = [% cov_time %]
161  
162 function! s:coverage_for(filename)
163 let s:fn_len = strlen(a:filename)
164 for s:cf in keys(s:coverage)
165 let s:f = substitute(s:cf, "^blib/", "", "")
166 let s:match_pos = match(a:filename, s:f . "$")
167 if s:match_pos >= 0 && s:match_pos < s:fn_len
168 return s:coverage[s:cf]
169 endif
170 endfor
171  
172 echom "No coverage recorded for " . a:filename
173 return {}
174 endfunction
175  
176 let s:signs = {}
177 let s:sign_num = 1
178  
179 function! s:add_coverage_signs(filename)
180 let s:cov = s:coverage_for(a:filename)
181 if !len(keys(s:cov))
182 return
183 endif
184  
185 if getftime(a:filename) > s:cov_time
186 echom "File is newer than coverage run of " . strftime("%c", s:cov_time)
187 call CoverageOld(a:filename)
188 else
189 call CoverageValid(a:filename)
190 endif
191  
192 if !exists("s:signs['" . a:filename . "']")
193 let s:signs[a:filename] = {}
194 endif
195 let s:s = s:signs[a:filename]
196  
197 for s:type in reverse(copy(s:types))
198 for s:line in s:cov[s:type]
199 if !exists("s:s['" . s:line . "']")
200 let s:id = s:sign_num
201 let s:sign_num += 1
202 let s:s[s:line] = s:id
203 exe ":sign place " . s:id . " line=" . s:line . " name=" . s:type . " file=" . a:filename
204 endif
205 endfor
206 endfor
207 endfunction
208  
209 function! s:del_coverage_signs(filename)
210 if exists("s:signs['" . a:filename . "']")
211 let s:s = s:signs[a:filename]
212 for s:line in keys(s:s)
213 exe ":sign unplace " . s:s[s:line]
214 endfor
215 let s:signs[a:filename] = {}
216 endif
217 endfunction
218  
219 let s:filename = expand("<sfile>")
220 function! s:uncover(sourced)
221 if a:sourced == s:filename
222 call s:del_coverage_signs(expand("%:p"))
223 endif
224 endfunction
225  
226 command! -nargs=0 Cov call s:add_coverage_signs(expand("%:p"))
227 command! -nargs=0 Uncov call s:del_coverage_signs(expand("%:p"))
228  
229 augroup devel-cover
230 au!
231 exe "au SourcePre " . expand("<sfile>:t") . " call s:uncover(expand('<afile>:p'))"
232  
233 " show signs automatically for all known files
234 for s:filename in keys(s:coverage)
235 exe "au BufReadPost " . s:filename . ' call s:add_coverage_signs(expand("%:p"))'
236 let s:f = substitute(s:filename, "^blib/", "", "")
237 if s:filename != s:f
238 exe "au BufReadPost " . s:f . ' call s:add_coverage_signs(expand("%:p"))'
239 endif
240 endfor
241 augroup end
242  
243 Cov
244 EOT
245  
246 1
247  
248 __END__