PLS/Parser/Pod.pm

filter: Statementstmt 25.7% 25 / 97 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 8 / 20 Podpod 91.6% 11 / 12 totaltotal 28.5% 44 / 154 CC 12 SCAR 21.9
CC
12
Cov
21%
get_clean_inc
30.0
get_perldoc_location
12.0
get_markdown_for_package
12.0
CRAP 9.0
« PLS/Server/Response/Location.pm ↑ Summary PLS/Trie.pm »
1 package PLS::Parser::Pod;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Pod::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use PLS::Server::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 PLS::Parser::Pod
17  
18 =head1 DESCRIPTION
19  
20 This class finds and parses POD for an element. It formats the POD into markdown suitable
21 for sending to the Language Server Protocol.
22  
23 =cut
24  
25 sub new
26 {
27 0 ▶ my ($class, @args) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28  
29 0 my %args = @args;
30  
31 my %self = (
32 index => $args{index},
33 element => $args{element}
34 0 );
35  
36 0 return bless \%self, $class;
37 } ## end sub new
38  
39 =head2 line_number
40  
41 The line number of the element
42  
43 =cut
44  
45 sub line_number
46 {
47 0 ▶ my ($self) = @_;
Subroutine line_number: not called CC=1 SCAR=6.9
Pod documented
48  
49 0 return $self->{element}->lsp_line_number;
50 }
51  
52 =head2 column_number
53  
54 The column number of the element
55  
56 =cut
57  
58 sub column_number
59 {
60 0 ▶ my ($self) = @_;
Subroutine column_number: not called CC=1 SCAR=6.9
Pod documented
61  
62 0 return $self->{element}->lsp_column_number;
63 }
64  
65 =head2 name
66  
67 The name of the element.
68  
69 =cut
70  
71 sub name
72 {
73 0 ▶ my ($self) = @_;
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
74  
75 0 return '';
76 }
77  
78 =head2 get_perldoc_location
79  
80 Tries to find the path to the perldoc utility.
81  
82 =cut
83  
84 sub get_perldoc_location
85 {
86 0 ▶ my (undef, $dir) = File::Spec->splitpath($^X);
Subroutine get_perldoc_location: not called CC=3 SCAR=24.8
Pod documented
87 0 my $perldoc = File::Spec->catfile($dir, 'perldoc');
88  
89 # try to use the perldoc matching this perl executable, falling back to the perldoc in the PATH
90 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
91 } ## end sub get_perldoc_location
92  
93 =head2 run_perldoc_command
94  
95 Runs a perldoc command and returns the text formatted into markdown.
96  
97 =cut
98  
99 sub run_perldoc_command
100 {
101 0 ▶ my ($class, @command) = @_;
Subroutine run_perldoc_command: not called CC=2 SCAR=17.9
Pod documented
102  
103 0 my $markdown = '';
104  
105 0 my $err = gensym;
106 0 my $pid = open3(my $in, my $out, $err, get_perldoc_location(), @command);
107  
108 0 close $in, () = <$err>; # need to read all of error file handle
109 0 my $pod = do { local $/; <$out> };
110 0 close $out;
111 0 waitpid $pid, 0;
112 0 my $exit_code = $? >> 8;
113 0 ▶ return 0 if $exit_code != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit_code != 0 0 0 0
114 0 return $class->get_markdown_from_text(\$pod);
115 } ## end sub run_perldoc_command
116  
117 =head2 get_markdown_for_package
118  
119 Finds the POD for a package and returns its POD, formatted into markdown.
120  
121 =cut
122  
123 sub get_markdown_for_package
124 {
125 0 ▶ my ($class, $package) = @_;
Subroutine get_markdown_for_package: not called CC=3 SCAR=24.8
Pod documented
126  
127 0 my $include = $class->get_clean_inc();
128 0 my $path = Pod::Find::pod_where({-dirs => $include}, $package);
129 0 ▶ return unless (length $path);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 0 0 0
130 0 ▶ open my $fh, '<', $path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return 0 0 0
131 0 my $text = do { local $/; <$fh> };
132 0 return $class->get_markdown_from_text(\$text);
133 } ## end sub get_markdown_for_package
134  
135 =head2 get_markdown_from_lines
136  
137 This formats POD from an array of lines into markdown and fixes up improperly formatted text.
138  
139 =cut
140  
141 sub get_markdown_from_lines
142 {
143 0 ▶ my ($class, $lines) = @_;
Subroutine get_markdown_from_lines: not called CC=2 SCAR=17.9
Pod documented
144  
145 0 my $markdown = '';
146 0 my $parser = Pod::Markdown->new();
147  
148 0 $parser->output_string(\$markdown);
149 0 $parser->no_whining(1);
150 0 $parser->parse_lines(@$lines, undef);
151  
152 0 $class->clean_markdown(\$markdown);
153  
154 0 my $ok = $parser->content_seen;
155 0 ▶ return 0 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
156 0 return $ok, \$markdown;
157 } ## end sub get_markdown_from_lines
158  
159  
160 =head2 get_markdown_from_text
161  
162 This formats POD from SCALAR ref to a string into markdown and fixes up improperly formatted text.
163  
164 =cut
165  
166 sub get_markdown_from_text
167 {
168 0 ▶ my ($class, $text) = @_;
Subroutine get_markdown_from_text: not called CC=2 SCAR=17.9
Pod documented
169  
170 0 my $markdown = '';
171 0 my $parser = Pod::Markdown->new();
172  
173 0 $parser->output_string(\$markdown);
174 0 $parser->no_whining(1);
175 0 $parser->parse_string_document($$text);
176  
177 0 $class->clean_markdown(\$markdown);
178  
179 0 my $ok = $parser->content_seen;
180 0 ▶ return 0 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
181 0 return $ok, \$markdown;
182 } ## end sub get_markdown_from_text
183  
184 =head2 clean_markdown
185  
186 This fixes markdown so that documentation isn't incorrectly displayed as code.
187  
188 =cut
189  
190 sub clean_markdown
191 {
192 0 ▶ my ($class, $markdown) = @_;
Subroutine clean_markdown: not called CC=1 SCAR=6.9
Pod documented
193  
194 # remove first extra space to avoid markdown from being displayed inappropriately as code
195 0 $$markdown =~ s/\n\n/\n/;
196 } ## end sub clean_markdown
197  
198 =head2 combine_markdown
199  
200 This combines multiple markdown sections into a single string.
201  
202 =cut
203  
204 sub combine_markdown
205 {
206 0 ▶ my ($class, @markdown_parts) = @_;
Subroutine combine_markdown: not called CC=1 SCAR=6.9
Pod documented
207  
208 0 return join "\n---\n", @markdown_parts;
209 }
210  
211 =head2 get_clean_inc
212  
213 Starts a new perl process and retrieves its @INC, so we do not use an @INC tainted
214 with things included in PLS.
215  
216 =cut
217  
218 sub get_clean_inc
219 {
220 # default to including everything except PLS code in search.
221 0 ▶ my @include = grep { not /\Q$FindBin::RealBin\E/ } @INC;
Subroutine get_clean_inc: not called CC=5 SCAR=34.0
Pod documented
222  
223 # try to get a clean @INC from the perl we're using
224 0 ▶ if (my $pid = open my $perl, '-|', $^X, '-e', q{$, = "\n"; print @INC; print "\n"})
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = open(my $perl, "-|", $^X, "-e", "\$, = \"\\n\"; print \@INC; print \"\\n\"")) 0 0 0
225 {
226 0 @include = ();
227  
228 0 ▶ while (my $line = <$perl>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $perl)) 0 0 0
229 {
230 0 chomp $line;
231 0 ▶ next unless (length $line);
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
232 0 push @include, $line;
233 } ## end while (my $line = <$perl>...)
234  
235 0 waitpid $pid, 0;
236 } ## end if (open my $perl, '-|'...)
237  
238 0 ▶ unshift @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
239 0 unshift @include, $PLS::Server::State::ROOT_PATH;
240 0 return \@include;
241 } ## end sub get_clean_inc
242  
243 1 1;
« PLS/Server/Response/Location.pm ↑ Summary PLS/Trie.pm »