blib/lib/PLS/Parser/Pod.pm

filter: Statementstmt 45.0% 68 / 151 Branchbran 17.6% 6 / 34 Conditioncond 20.0% 5 / 25 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 62.0% 18 / 29 Podpod 85.7% 12 / 14 totaltotal 41.3% 110 / 266 CC 27 SCAR 36.2
CC
27
Cov
38%
find_pod_in_file
156.0
set_perl_exe
12.0
get_markdown_for_package
12.0
CRAP 37.4
« blib/lib/PLS/Server/Response/Location.pm ↑ Summary blib/lib/PLS/Server/Response/WorkspaceSymbols.pm »
1 package PLS::Parser::Pod;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use IO::Async::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Pod::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Pod::Simple::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 ▶use PLS::Parser::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use PLS::Server::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 PLS::Parser::Pod
21  
22 =head1 DESCRIPTION
23  
24 This class finds and parses POD for an element. It formats the POD into markdown suitable
25 for sending to the Language Server Protocol.
26  
27 =cut
28  
29 15 my $PERL_EXE = $^X;
30 15 my $PERL_ARGS = [];
31  
32 sub new
33 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my ($class, @args) = @_;
35  
36 0 my %args = @args;
37  
38 my %self = (
39 index => $args{index},
40 element => $args{element}
41 0 );
42  
43 0 return bless \%self, $class;
44 } ## end sub new
45  
46 =head2 set_perl_exe
47  
48 Store the perl executable path.
49  
50 =cut
51  
52 sub set_perl_exe
53 0 ▶{
Subroutine set_perl_exe: not called CC=3 SCAR=24.8
Pod documented
54 0 my (undef, $perl_exe) = @_;
55  
56 0 ▶ $PERL_EXE = $perl_exe if (length $perl_exe and -x $perl_exe);
Branch: 1 branch
BranchTrueFalseTotal
if length $perl_exe and -x $perl_exe 0 0 0
Condition0%0/3
length $perl_exe and -x $perl_exe
!ll&&!rl&&r
000
MC/DC0%0/2
length $perl_exe and -x $perl_exe
length $perl_exe -x $perl_exe
Truth table0%0/3
A and B
A= length $perl_exeB= -x $perl_exe
ABresult
0X0
100
111
57  
58 0 return;
59 } ## end sub set_perl_exe
60  
61 =head2 get_perl_exe
62  
63 Get the perl executable path.
64  
65 =cut
66  
67 sub get_perl_exe
68 10 ▶{
Subroutine get_perl_exe: called CC=1 SCAR=0.0
Pod documented
69 10 return $PERL_EXE;
70 }
71  
72 =head2 set_perl_args
73  
74 Set the arguments to be used when using the perl binary.
75  
76 =cut
77  
78 sub set_perl_args
79 0 ▶{
Subroutine set_perl_args: not called CC=1 SCAR=6.9
Pod documented
80 0 my (undef, $args) = @_;
81  
82 0 $PERL_ARGS = $args;
83  
84 0 return;
85 } ## end sub set_perl_args
86  
87 =head2 get_perl_args
88  
89 Get the arguments to be used when using the perl binary.
90  
91 =cut
92  
93 sub get_perl_args
94 10 ▶{
Subroutine get_perl_args: called CC=1 SCAR=0.0
Pod documented
95 10 return $PERL_ARGS;
96 }
97  
98 =head2 get_perldoc_location
99  
100 Tries to find the path to the perldoc utility.
101  
102 =cut
103  
104 sub get_perldoc_location
105 4 ▶{
Subroutine get_perldoc_location: called CC=3 SCAR=12.5
Pod documented
106 4 my (undef, $dir) = File::Spec->splitpath($^X);
107 4 my $perldoc = File::Spec->catfile($dir, 'perldoc');
108  
109 # try to use the perldoc matching this perl executable, falling back to the perldoc in the PATH
110 4 ▶ return (-f $perldoc and -x $perldoc) ? $perldoc : 'perldoc';
Branch: 1 branch
BranchTrueFalseTotal
-f $perldoc && -x $perldoc ? : 4 0 4
Condition33%1/3
-f $perldoc and -x $perldoc
!ll&&!rl&&r
004
MC/DC0%0/2
-f $perldoc and -x $perldoc
-f $perldoc -x $perldoc
Truth table33%1/3
A and B
A= -f $perldocB= -x $perldoc
ABresult
0X0
100
111
111 } ## end sub get_perldoc_location
112  
113 =head2 run_perldoc_command
114  
115 Runs a perldoc command and returns the text formatted into markdown.
116  
117 =cut
118  
119 sub run_perldoc_command
120 0 ▶{
Subroutine run_perldoc_command: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($class, @command) = @_;
122  
123 0 my $markdown = '';
124  
125 my $proc = IO::Async::Process->new(
126 command => [get_perldoc_location(), @command],
127 stderr => {into => \my $stderr},
128 stdout => {into => \my $stdout},
129 0 ▶ on_finish => sub { }
Subroutine __ANON__: not called CC=1 SCAR=0.0
130 0 );
131 0 IO::Async::Loop->new->add($proc);
132  
133 0 my $exit_code = $proc->finish_future->get();
134 0 ▶ return 0 if ($exit_code != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code != 0 0 0 0
135 0 return $class->get_markdown_from_text(\$stdout);
136 } ## end sub run_perldoc_command
137  
138 =head2 get_markdown_for_package
139  
140 Finds the POD for a package and returns its POD, formatted into markdown.
141  
142 =cut
143  
144 sub get_markdown_for_package
145 0 ▶{
Subroutine get_markdown_for_package: not called CC=3 SCAR=24.8
Pod documented
146 0 my ($class, $package) = @_;
147  
148 0 my $include = $class->get_clean_inc();
149 0 my $search = Pod::Simple::Search->new();
150 0 $search->inc(0);
151 0 my $path = $search->find($package, @{$include});
152 0 ▶ return unless (length $path);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 0 0 0
153 0 ▶ open my $fh, '<', $path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return 0 0 0
154 0 my $text = do { local $/; <$fh> };
155 0 return $class->get_markdown_from_text(\$text);
156 } ## end sub get_markdown_for_package
157  
158 =head2 get_markdown_from_lines
159  
160 This formats POD from an array of lines into markdown and fixes up improperly formatted text.
161  
162 =cut
163  
164 sub get_markdown_from_lines
165 0 ▶{
Subroutine get_markdown_from_lines: not called CC=2 SCAR=17.9
Pod documented
166 0 my ($class, $lines) = @_;
167  
168 0 my $markdown = '';
169 0 my $parser = Pod::Markdown->new();
170  
171 0 $parser->output_string(\$markdown);
172 0 $parser->no_whining(1);
173 0 $parser->parse_lines(@{$lines}, undef);
174  
175 0 $class->clean_markdown(\$markdown);
176  
177 0 my $ok = $parser->content_seen;
178 0 ▶ return 0 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
179 0 return $ok, \$markdown;
180 } ## end sub get_markdown_from_lines
181  
182 =head2 get_markdown_from_text
183  
184 This formats POD from SCALAR ref to a string into markdown and fixes up improperly formatted text.
185  
186 =cut
187  
188 sub get_markdown_from_text
189 0 ▶{
Subroutine get_markdown_from_text: not called CC=2 SCAR=17.9
Pod documented
190 0 my ($class, $text) = @_;
191  
192 0 my $markdown = '';
193 0 my $parser = Pod::Markdown->new();
194  
195 0 $parser->output_string(\$markdown);
196 0 $parser->no_whining(1);
197 0 $parser->parse_string_document(${$text});
198  
199 0 $class->clean_markdown(\$markdown);
200  
201 0 my $ok = $parser->content_seen;
202 0 ▶ return 0 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
203 0 return $ok, \$markdown;
204 } ## end sub get_markdown_from_text
205  
206 sub find_pod_in_file
207 0 ▶{
Subroutine find_pod_in_file: not called CC=12 SCAR=50.5
Pod undocumented
208 0 my ($self, $path, $name) = @_;
209  
210 0 ▶ open my $fh, '<', $path or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return 0 0 0 0
211  
212 0 my @lines;
213 0 my $start = '';
214  
215 0 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
216 {
217 0 ▶ if ($line =~ /^=(head\d|item).*\b\Q$name\E\b.*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^=(head\d|item).*\b\Q$name\E\b.*$/) 0 0 0
218 {
219 0 $start = $1;
220 0 push @lines, $line;
221 0 next;
222 } ## end if ($line =~ /^=(head\d|item).*\b\Q$name\E\b.*$/...)
223  
224 0 ▶ if (length $start)
Branch: 1 branch
BranchTrueFalseTotal
if (length $start) 0 0 0
225 {
226 0 push @lines, $line;
227  
228 0 ▶ if ( $start eq 'item' and $line =~ /^=item/
Branch: 1 branch
BranchTrueFalseTotal
if ($start eq "item" and $line =~ /^=item/ or $start =~ /head/ and $line =~ /^=$start/ or $line =~ /^=cut/) 0 0 0
Condition0%0/12
$start eq "item" and $line =~ /^=item/ or $start =~ /head/ and $line =~ /^=$start/ or $line =~ /^=cut/
l!l&&r!l&&!r
000
$start eq "item" and $line =~ /^=item/ or $start =~ /head/ and $line =~ /^=$start/
l!l&&r!l&&!r
000
$start eq "item" and $line =~ /^=item/
!ll&&!rl&&r
000
$start =~ /head/ and $line =~ /^=$start/
!ll&&!rl&&r
000
MC/DC0%0/5
$start eq "item" and $line =~ /^=item/ or $start =~ /head/ and $line =~ /^=$start/ or $line =~ /^=cut/
$start eq "item" $line =~ /^=item/ $start =~ /head/ $line =~ /^=$start/ $line =~ /^=cut/
Truth table0%0/11
A and B or C and D or E
A= $start eq "item"B= $line =~ /^=item/C= $start =~ /head/D= $line =~ /^=$start/E= $line =~ /^=cut/
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
229 or $start =~ /head/ and $line =~ /^=$start/
230 or $line =~ /^=cut/)
231 {
232 0 last;
233 } ## end if ($start eq 'item' and...)
234 } ## end if (length $start)
235 } ## end while (my $line = <$fh>)
236  
237 0 close $fh;
238  
239 # we don't want the last line - it's a start of a new section.
240 0 pop @lines;
241  
242 0 my $markdown = '';
243  
244 0 ▶ if (scalar @lines)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @lines) 0 0 0
245 {
246 0 my $parser = Pod::Markdown->new();
247  
248 0 $parser->output_string(\$markdown);
249 0 $parser->no_whining(1);
250 0 $parser->parse_lines(@lines, undef);
251  
252 # remove first extra space to avoid markdown from being displayed inappropriately as code
253 0 $markdown =~ s/\n\n/\n/;
254 0 my $ok = $parser->content_seen;
255 0 ▶ return 0 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
256 0 return $ok, \$markdown;
257 } ## end if (scalar @lines)
258  
259 0 return 0;
260 } ## end sub find_pod_in_file
261  
262 =head2 clean_markdown
263  
264 This fixes markdown so that documentation isn't incorrectly displayed as code.
265  
266 =cut
267  
268 sub clean_markdown
269 0 ▶{
Subroutine clean_markdown: not called CC=1 SCAR=6.9
Pod documented
270 0 my ($class, $markdown) = @_;
271  
272 # remove first extra space to avoid markdown from being displayed inappropriately as code
273 0 ${$markdown} =~ s/\n\n/\n/;
274  
275 0 return;
276 } ## end sub clean_markdown
277  
278 =head2 combine_markdown
279  
280 This combines multiple markdown sections into a single string.
281  
282 =cut
283  
284 sub combine_markdown
285 0 ▶{
Subroutine combine_markdown: not called CC=1 SCAR=6.9
Pod documented
286 0 my ($class, @markdown_parts) = @_;
287  
288 0 return join "\n---\n", @markdown_parts;
289 }
290  
291 =head2 get_clean_inc
292  
293 Starts a new perl process and retrieves its @INC, so we do not use an @INC tainted
294 with things included in PLS.
295  
296 =cut
297  
298 sub get_clean_inc
299 6 ▶{
Subroutine get_clean_inc: called CC=6 SCAR=18.0
Pod documented
300 6 state @include;
301 6 state $last_perl;
302  
303 6 ▶ if (not scalar @include or $last_perl ne $PERL_EXE)
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar @include or $last_perl ne $PERL_EXE) 4 2 6
Condition66%2/3
not scalar @include or $last_perl ne $PERL_EXE
l!l&&r!l&&!r
402
MC/DC50%1/2
not scalar @include or $last_perl ne $PERL_EXE
not scalar @include $last_perl ne $PERL_EXE
Truth table66%2/3
not(A) or B
A= not scalar @includeB= $last_perl ne $PERL_EXE
ABresult
1X1
011
000
304 {
305 4 $last_perl = $PERL_EXE;
306 4 local $ENV{PERL5LIB};
307  
308 # default to including everything except PLS code in search.
309 4 @include = grep { not /\Q$FindBin::RealBin\E/ } @INC;
310  
311 # try to get a clean @INC from the perl we're using
312 4 my @clean_inc;
313  
314 my $proc = IO::Async::Process->new(
315 command => [$PERL_EXE, '-e', q{print join "\n", @INC, ''}], ## no critic (RequireInterpolationOfMetachars)
316 stdout => {
317 8 ▶ on_read => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
318 8 my ($stream, $buffref) = @_;
319  
320 8 ▶ while (${$buffref} =~ s/^(.*)\n//)
Branch: 1 branch
BranchTrueFalseTotal
if (${$buffref;} =~ s/^(.*)\n//) 28 8 36
321 {
322 28 push @clean_inc, $1;
323 }
324 } ## end sub
325 },
326 4 ▶ on_finish => sub { }
Subroutine __ANON__: called CC=1 SCAR=0.0
327 4 );
328 4 IO::Async::Loop->new->add($proc);
329 4 $proc->finish_future->get();
330  
331 4 ▶ if (scalar @clean_inc)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @clean_inc) 4 0 4
332 {
333 4 @include = @clean_inc;
334 }
335 } ## end if (not scalar @include...)
336  
337 6 my @temp_include = @include;
338 6 ▶ push @temp_include, @{$PLS::Server::State::CONFIG->{inc} // []};
Condition50%1/2
$PLS::Server::State::CONFIG->{"inc"} // []
l!l
60
MC/DC0%0/1
$PLS::Server::State::CONFIG->{"inc"} // []
$PLS::Server::State::CONFIG->{"inc"}
Truth table50%1/2
A
A= $PLS::Server::State::CONFIG->{"inc"}
Aresult
11
00
339 6 my $index = PLS::Parser::Index->new();
340 6 ▶ push @temp_include, @{PLS::Parser::Index->new->workspace_folders // []};
Condition50%1/2
"PLS::Parser::Index"->new->workspace_folders // []
l!l
60
MC/DC0%0/1
"PLS::Parser::Index"->new->workspace_folders // []
"PLS::Parser::Index"->new->workspace_folders
Truth table50%1/2
A
A= "PLS::Parser::Index"->new->workspace_folders
Aresult
11
00
341  
342 6 return \@temp_include;
343 } ## end sub get_clean_inc
344  
345 15 1;
« blib/lib/PLS/Server/Response/Location.pm ↑ Summary blib/lib/PLS/Server/Response/WorkspaceSymbols.pm »