PLS/Parser/Pod.pm

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