PLS/Parser/Document.pm

filter: Statementstmt 13.3% 83 / 620 Branchbran 0.0% 0 / 322 Conditioncond 0.0% 0 / 179 MC/DCmcdc 0.0% 0 / 97 Subroutinesub 33.7% 27 / 80 Podpod 96.7% 30 / 31 totaltotal 10.5% 140 / 1329 CC 235 SCAR 58.6
CC
235
Cov
7%
go_to_variable_definition
1056.0
find_word_under_cursor
992.0
search_elements_for_definition
506.0
CRAP 349.6
« PLS/Server/Response/Completion.pm ↑ Summary PLS/Server/Response/Resolve.pm »
1 package PLS::Parser::Document;
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 feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use ExtUtils::Installed;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util qw(first any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use PPI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use PPR;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Perl::Tidy;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Time::Seconds;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use PLS::Parser::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use PLS::Parser::Element::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use PLS::Parser::Element::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use PLS::Parser::Element::Subroutine;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use PLS::Parser::Element::VariableStatement;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use PLS::Parser::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use PLS::Parser::Pod::ClassMethod;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use PLS::Parser::Pod::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use PLS::Parser::Pod::Package;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use PLS::Parser::Pod::Subroutine;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use PLS::Parser::Pod::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 my %FILES;
35 my $INDEX;
36  
37 =head1 NAME
38  
39 PLS::Parser::Document
40  
41 =head1 DESCRIPTION
42  
43 This is a class that represents a text document. It has methods
44 for parsing and manipulating the document using L<PPI> and L<PPR>.
45  
46 =head1 METHODS
47  
48 =head2 new
49  
50 This creates a new L<PLS::Parser::Document> object.
51 It takes named parameters.
52  
53 Either C<uri> or C<path> must be passed.
54  
55 C<line> with a line number may be passed, which indicates that only one line
56 of the document should be parsed. This greatly enhances performance for completion items.
57  
58 =cut
59  
60 sub new
61 {
62 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
63  
64 0 my ($path, $uri);
65  
66 0 ▶ if (length $args{uri})
Branch: 2 branches
BranchTrueFalseTotal
if (length $args{'uri'}) { } 0 0 0
elsif (length $args{'path'}) { } 0 0 0
67 {
68 0 $path = URI->new($args{uri})->file;
69 0 $args{path} = $path;
70 0 $uri = $args{uri};
71 } ## end if (length $args{uri})
72 elsif (length $args{path})
73 {
74 0 $path = $args{path};
75 0 $uri = URI::file->new($path)->as_string;
76 0 $args{uri} = $uri;
77 } ## end elsif (length $args{path}...)
78 0 ▶ return unless (length $path and length $uri);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path and length $uri 0 0 0
Condition0%0/3
length $path and length $uri
!ll&&!rl&&r
000
MC/DC0%0/2
length $path and length $uri
length $path length $uri
Truth table0%0/3
A and B
A= length $pathB= length $uri
ABresult
0X0
100
111
79  
80 0 my $self = bless {
81 path => $path,
82 uri => $uri,
83 index => $INDEX
84 }, $class;
85  
86 0 $self->get_index();
87 0 my $document = $self->_get_ppi_document(%args);
88 0 ▶ return unless (ref $document eq 'PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $document eq "PPI::Document" 0 0 0
89 0 $self->{document} = $document;
90  
91 0 return $self;
92 } ## end sub new
93  
94 =head2 set_index
95  
96 This sets the L<PLS::Parser::Index> object to be used by all L<PLS::Parser::Document>
97 objects.
98  
99 =cut
100  
101 sub set_index
102 {
103 0 ▶ my ($class, $index) = @_;
Subroutine set_index: not called CC=1 SCAR=6.9
Pod documented
104  
105 0 $INDEX = $index;
106 }
107  
108 =head2 get_index
109  
110 This gets the L<PLS::Parser::Index> object to use.
111 It will set it for other objects to use if it does not already exist.
112  
113 =cut
114  
115 sub get_index
116 {
117 0 ▶ my ($class) = @_;
Subroutine get_index: not called CC=2 SCAR=17.9
Pod documented
118  
119 0 ▶ $INDEX = PLS::Parser::Index->new(root => $PLS::Server::State::ROOT_PATH) unless (ref $INDEX eq 'PLS::Parser::Index');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $INDEX eq "PLS::Parser::Index" 0 0 0
120 0 return $INDEX;
121 } ## end sub get_index
122  
123 =head2 go_to_definition
124  
125 This finds the definition of a symbol located at a given line and column number.
126  
127 =cut
128  
129 sub go_to_definition
130 {
131 0 ▶ my ($self, $line_number, $column_number) = @_;
Subroutine go_to_definition: not called CC=1 SCAR=6.9
Pod documented
132  
133 0 my @matches = $self->find_elements_at_location($line_number, $column_number);
134  
135 0 return $self->search_elements_for_definition($line_number, $column_number, @matches);
136 } ## end sub go_to_definition
137  
138 =head2 find_current_list
139  
140 This finds the nearest list structure that surrounds the current column on the current line.
141 This is useful for finding which parameter the cursor is on when calling a function.
142  
143 =cut
144  
145 sub find_current_list
146 {
147 0 ▶ my ($self, $line_number, $column_number) = @_;
Subroutine find_current_list: not called CC=1 SCAR=6.9
Pod documented
148  
149 0 my @elements = $self->find_elements_at_location($line_number, $column_number);
150 0 ▶ my $find = PPI::Find->new(sub { $_[0]->isa('PPI::Structure::List') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
151  
152 # Find the nearest list structure that completely surrounds the column.
153 0 ▶ return first { $_->lsp_column_number < $column_number < $_->lsp_column_number + length($_->content) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 sort { abs($column_number - $a->lsp_column_number) - abs($column_number - $b->lsp_column_number) }
155 0 map { PLS::Parser::Element->new(element => $_, document => $self->{document}, file => $self->{path}) }
156 0 map { $find->in($_->element) } @elements;
157 } ## end sub find_current_list
158  
159 =head2 go_to_definition_of_closest_subroutine
160  
161 Given a list of elements, this finds the closest subroutine call to the current line and column.
162  
163 =cut
164  
165 sub go_to_definition_of_closest_subroutine
166 {
167 0 ▶ my ($self, $list, $line_number, $column_number) = @_;
Subroutine go_to_definition_of_closest_subroutine: not called CC=10 SCAR=47.0
Pod documented
168  
169 0 ▶ return if (not blessed($list) or not $list->isa('PLS::Parser::Element') and $list->type eq 'PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
if not builtin::blessed($list) or not $list->isa("PLS::Parser::Element") and $list->type eq "PPI::Structure::List" 0 0 0
Condition0%0/6
not builtin::blessed($list) or not $list->isa("PLS::Parser::Element") and $list->type eq "PPI::Structure::List"
l!l&&r!l&&!r
000
not $list->isa("PLS::Parser::Element") and $list->type eq "PPI::Structure::List"
!ll&&!rl&&r
000
MC/DC0%0/3
not builtin::blessed($list) or not $list->isa("PLS::Parser::Element") and $list->type eq "PPI::Structure::List"
not builtin::blessed($list) not $list->isa("PLS::Parser::Element") $list->type eq "PPI::Structure::List"
Truth table0%0/4
not(A) or not(B) and C
A= not builtin::blessed($list)B= not $list->isa("PLS::Parser::Element")C= $list->type eq "PPI::Structure::List"
ABCresult
1XX1
0111
00X0
0100
170  
171 # Try to find the closest word before the list - this is the function name.
172 0 my $word = $list;
173  
174 0 ▶ while (blessed($word) and $word->isa('PLS::Parser::Element') and not $word->element->isa('PPI::Token::Word'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($word) and $word->isa("PLS::Parser::Element") and not $word->element->isa("PPI::Token::Word")) 0 0 0
Condition0%0/6
builtin::blessed($word) and $word->isa("PLS::Parser::Element") and not $word->element->isa("PPI::Token::Word")
!ll&&!rl&&r
000
builtin::blessed($word) and $word->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($word) and $word->isa("PLS::Parser::Element") and not $word->element->isa("PPI::Token::Word")
builtin::blessed($word) $word->isa("PLS::Parser::Element") not $word->element->isa("PPI::Token::Word")
Truth table0%0/4
A and B and not(C)
A= builtin::blessed($word)B= $word->isa("PLS::Parser::Element")C= not $word->element->isa("PPI::Token::Word")
ABCresult
0XX0
10X0
1100
1111
175 {
176 0 $word = $word->previous_sibling;
177 }
178  
179 0 ▶ return if (not blessed($word) or not $word->isa('PLS::Parser::Element') or not $word->element->isa('PPI::Token::Word'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($word) and $word->isa("PLS::Parser::Element") and $word->element->isa("PPI::Token::Word") 0 0 0
Condition0%0/6
builtin::blessed($word) and $word->isa("PLS::Parser::Element") and $word->element->isa("PPI::Token::Word")
!ll&&!rl&&r
000
builtin::blessed($word) and $word->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($word) and $word->isa("PLS::Parser::Element") and $word->element->isa("PPI::Token::Word")
builtin::blessed($word) $word->isa("PLS::Parser::Element") $word->element->isa("PPI::Token::Word")
Truth table0%0/4
A and B and C
A= builtin::blessed($word)B= $word->isa("PLS::Parser::Element")C= $word->element->isa("PPI::Token::Word")
ABCresult
0XX0
10X0
1100
1111
180 0 return $self->search_elements_for_definition($line_number, $column_number, $word);
181 } ## end sub go_to_definition_of_closest_subroutine
182  
183 =head2 search_elements_for_definition
184  
185 This tries to find the definition in a list of elements, and returns the first definition found.
186  
187 =cut
188  
189 sub search_elements_for_definition
190 {
191 0 ▶ my ($self, $line_number, $column_number, @matches) = @_;
Subroutine search_elements_for_definition: not called CC=22 SCAR=62.3
Pod documented
192  
193 0 foreach my $match (@matches)
194 {
195 0 ▶ if (my ($package, $subroutine) = $match->subroutine_package_and_name())
Branch: 1 branch
BranchTrueFalseTotal
if (() = $match->subroutine_package_and_name) 0 0 0
196 {
197 0 ▶ if ($match->cursor_on_package($column_number))
Branch: 1 branch
BranchTrueFalseTotal
if ($match->cursor_on_package($column_number)) 0 0 0
198 {
199 0 return $self->{index}->find_package($package);
200 }
201  
202 0 ▶ if (length $package)
Branch: 1 branch
BranchTrueFalseTotal
if (length $package) 0 0 0
203 {
204 0 my $results = $self->{index}->find_package_subroutine($package, $subroutine);
205 0 ▶ return $results if (ref $results eq 'ARRAY' and scalar @{$results});
Branch: 1 branch
BranchTrueFalseTotal
if ref $results eq "ARRAY" and scalar @{$results;} 0 0 0
Condition0%0/2
ref $results eq "ARRAY" and scalar @{$results;}
!ll
00
MC/DC0%0/1
ref $results eq "ARRAY" and scalar @{$results;}
ref $results eq "ARRAY"
Truth table0%0/2
A
A= ref $results eq "ARRAY"
Aresult
00
11
206  
207 0 my $external = $self->find_external_subroutine($package, $subroutine);
208 0 ▶ return [$external] if (ref $external eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $external eq "HASH" 0 0 0
209 } ## end if (length $package)
210  
211 0 my $results = $self->{index}->find_subroutine($subroutine);
212 0 ▶ return $results if (ref $results eq 'ARRAY' and scalar @{$results});
Branch: 1 branch
BranchTrueFalseTotal
if ref $results eq "ARRAY" and scalar @{$results;} 0 0 0
Condition0%0/2
ref $results eq "ARRAY" and scalar @{$results;}
!ll
00
MC/DC0%0/1
ref $results eq "ARRAY" and scalar @{$results;}
ref $results eq "ARRAY"
Truth table0%0/2
A
A= ref $results eq "ARRAY"
Aresult
00
11
213 } ## end if (my ($package, $subroutine...))
214 0 ▶ if (my ($class, $method) = $match->class_method_package_and_name())
Branch: 1 branch
BranchTrueFalseTotal
if (() = $match->class_method_package_and_name) 0 0 0
215 {
216 0 my $results = $self->{index}->find_package_subroutine($class, $method);
217  
218 # fall back to treating as a method instead of class method
219 0 ▶ return $results if (ref $results eq 'ARRAY' and scalar @$results);
Branch: 1 branch
BranchTrueFalseTotal
if ref $results eq "ARRAY" and scalar @$results 0 0 0
Condition0%0/2
ref $results eq "ARRAY" and scalar @$results
!ll
00
MC/DC0%0/1
ref $results eq "ARRAY" and scalar @$results
ref $results eq "ARRAY"
Truth table0%0/2
A
A= ref $results eq "ARRAY"
Aresult
00
11
220  
221 0 my $external = $self->find_external_subroutine($class, $method);
222 0 ▶ return [$external] if (ref $external eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $external eq "HASH" 0 0 0
223 } ## end if (my ($class, $method...))
224 0 ▶ if (my $method = $match->method_name())
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $match->method_name) 0 0 0
225 {
226 0 $method =~ s/SUPER:://;
227 0 return $self->{index}->find_subroutine($method);
228 }
229 0 ▶ if (my ($package, $import) = $match->package_name($column_number))
Branch: 1 branch
BranchTrueFalseTotal
if (() = $match->package_name($column_number)) 0 0 0
230 {
231 0 ▶ if (length $import)
Branch: 1 branch
BranchTrueFalseTotal
if (length $import) { } 0 0 0
232 {
233 0 return $self->{index}->find_package_subroutine($package, $import);
234 }
235 else
236 {
237 0 return $self->{index}->find_package($package);
238 }
239 } ## end if (my ($package, $import...))
240 0 ▶ if (my $variable = $match->variable_name())
Branch: 1 branch
BranchTrueFalseTotal
if (my $variable = $match->variable_name) 0 0 0
241 {
242 0 return $self->go_to_variable_definition($variable, $match, $line_number, $column_number);
243 }
244 } ## end foreach my $match (@matches...)
245  
246 # If all else fails, see if we're on a POD link.
247 0 ▶ if (my $link = $self->pod_link($line_number, $column_number))
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $self->pod_link($line_number, $column_number)) 0 0 0
248 {
249 0 my $package = $self->{index}->find_package($link);
250 0 ▶ return $package if (ref $package eq 'ARRAY' and scalar @{$package});
Branch: 1 branch
BranchTrueFalseTotal
if ref $package eq "ARRAY" and scalar @{$package;} 0 0 0
Condition0%0/2
ref $package eq "ARRAY" and scalar @{$package;}
!ll
00
MC/DC0%0/1
ref $package eq "ARRAY" and scalar @{$package;}
ref $package eq "ARRAY"
Truth table0%0/2
A
A= ref $package eq "ARRAY"
Aresult
00
11
251  
252 0 my @pieces = split /::/, $link;
253 0 my $subroutine_name = pop @pieces;
254 0 my $package_name = join '::', @pieces;
255 0 ▶ return $self->{index}->find_package_subroutine($package_name, $subroutine_name) if (length $package_name);
Branch: 1 branch
BranchTrueFalseTotal
if length $package_name 0 0 0
256  
257 0 return $self->{index}->find_subroutine($subroutine_name);
258 } ## end if (my $link = $self->...)
259  
260 0 return;
261 } ## end sub search_elements_for_definition
262  
263 =head2 pod_link
264  
265 This determines if the line and column are within a POD LE<lt>E<gt> code,
266 and returns the contents of the link if so.
267  
268 =cut
269  
270 sub pod_link
271 {
272 0 ▶ my ($self, $line_number, $column_number) = @_;
Subroutine pod_link: not called CC=7 SCAR=40.3
Pod documented
273  
274 0 $line_number++;
275  
276 my $find = PPI::Find->new(
277 sub {
278 0 ▶ my ($element) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
279 0 ▶ return 0 unless $element->isa('PPI::Token::Pod');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Token::Pod") 0 0 0
280 0 ▶ return 0 if $element->line_number > $line_number;
Branch: 1 branch
BranchTrueFalseTotal
if $element->line_number > $line_number 0 0 0
281 0 ▶ return 0 if $element->line_number + scalar($element->lines) < $line_number;
Branch: 1 branch
BranchTrueFalseTotal
if $element->line_number + scalar($element->lines) < $line_number 0 0 0
282 0 return 1;
283 }
284 0 );
285  
286 0 ▶ return unless (scalar $find->in($self->{document}));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar $find->in($self->{"document"}) 0 0 0
287  
288 0 ▶ open my $fh, '<', $self->get_full_text() or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->get_full_text or return 0 0 0
289  
290 0 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
291 {
292 0 ▶ next unless $. == $line_number;
Branch: 1 branch
BranchTrueFalseTotal
unless $. == $line_number 0 0 0
293 0 chomp $line;
294  
295 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / L< # starting L< (?: <+ # optional additional < \s+ # spaces required if any additional < )? (.+?) # the actual link content (?: \s+ # spaces required if any additional > +>+ # optional additional > )? > # final closing > /gx) 0 0 0
296 $line =~ m{
297 L< # starting L<
298 (?:
299 <+ # optional additional <
300 \s+ # spaces required if any additional <
301 )?
302 (.+?) # the actual link content
303 (?:
304 \s+ # spaces required if any additional >
305 +>+ # optional additional >
306 )?
307 > # final closing >
308 }gx
309 )
310 {
311 0 my $start = $-[1];
312 0 my $end = $+[1];
313 0 my $link = $1;
314  
315 0 ▶ next unless ($start <= $column_number <= $end);
Branch: 1 branch
BranchTrueFalseTotal
unless $start <= $column_number <= $end 0 0 0
316  
317 # Get just the name - remove the text and section parts
318 0 $link =~ s/^[^<]*\|//;
319 0 $link =~ s/\/[^>]*$//;
320 0 return $link;
321 } ## end while ($line =~ m{ ) (})
322  
323 0 last;
324 } ## end while (my $line = <$fh>)
325  
326 0 return;
327 } ## end sub pod_link
328  
329 =head2 find_pod
330  
331 This attempts to find POD for the symbol at the given location.
332  
333 =cut
334  
335 sub find_pod
336 {
337 0 ▶ my ($self, $line_number, $column_number) = @_;
Subroutine find_pod: not called CC=14 SCAR=53.5
Pod documented
338  
339 0 my @elements = $self->find_elements_at_location($line_number, $column_number);
340  
341 0 foreach my $element (@elements)
342 {
343 0 my ($package, $subroutine, $variable, $import);
344  
345 0 ▶ if (($package, $import) = $element->package_name($column_number))
Branch: 1 branch
BranchTrueFalseTotal
if (($package, $import) = $element->package_name($column_number)) 0 0 0
346 {
347 0 my %args = (index => $self->{index}, element => $element, package => $package);
348 0 my $class_name = 'PLS::Parser::Pod::Package';
349  
350 0 ▶ if (length $import)
Branch: 1 branch
BranchTrueFalseTotal
if (length $import) 0 0 0
351 {
352 0 ▶ if ($import =~ /^[\$\@\%]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($import =~ /^[\$\@\%]/) { } 0 0 0
353 {
354 0 $args{variable} = $import;
355 0 $class_name = 'PLS::Parser::Pod::Variable';
356 }
357 else
358 {
359 0 $args{subroutine} = $import;
360 0 $class_name = 'PLS::Parser::Pod::Subroutine';
361 }
362 } ## end if (length $import)
363  
364 0 my $pod = $class_name->new(%args);
365 0 my $ok = $pod->find();
366 0 ▶ return (1, $pod) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
367 } ## end if (($package, $import...))
368 0 ▶ if (($package, $subroutine) = $element->class_method_package_and_name())
Branch: 1 branch
BranchTrueFalseTotal
if (() = $element->class_method_package_and_name) 0 0 0
369 {
370 my $pod =
371 PLS::Parser::Pod::ClassMethod->new(
372 index => $self->{index},
373 0 element => $element,
374 package => $package,
375 subroutine => $subroutine
376 );
377 0 my $ok = $pod->find();
378 0 ▶ return (1, $pod) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
379 } ## end if (($package, $subroutine...))
380 0 ▶ if ($subroutine = $element->method_name())
Branch: 1 branch
BranchTrueFalseTotal
if ($subroutine = $element->method_name) 0 0 0
381 {
382 my $pod =
383 PLS::Parser::Pod::Method->new(
384 index => $self->{index},
385 0 element => $element,
386 subroutine => $subroutine
387 );
388 0 my $ok = $pod->find();
389 0 ▶ return (1, $pod) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
390 } ## end if ($subroutine = $element...)
391 0 ▶ if (($package, $subroutine) = $element->subroutine_package_and_name())
Branch: 1 branch
BranchTrueFalseTotal
if (() = $element->subroutine_package_and_name) 0 0 0
392 {
393 my $pod =
394 PLS::Parser::Pod::Subroutine->new(
395 index => $self->{index},
396 0 element => $element,
397 package => $package,
398 subroutine => $subroutine,
399 include_builtins => 1
400 );
401 0 my $ok = $pod->find();
402 0 ▶ return (1, $pod) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
403 } ## end if (($package, $subroutine...))
404 0 ▶ if ($variable = $element->variable_name())
Branch: 1 branch
BranchTrueFalseTotal
if ($variable = $element->variable_name) 0 0 0
405 {
406 my $pod =
407 PLS::Parser::Pod::Variable->new(
408 index => $self->{index},
409 0 element => $element,
410 variable => $variable
411 );
412 0 my $ok = $pod->find();
413 0 ▶ return (1, $pod) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
414 } ## end if ($variable = $element...)
415 } ## end foreach my $element (@elements...)
416  
417 0 return 0;
418 } ## end sub find_pod
419  
420 sub find_elements_at_location
421 {
422 0 ▶ my ($self, $line_number, $column_number) = @_;
Subroutine find_elements_at_location: not called CC=2 SCAR=17.9
Pod undocumented
423  
424 0 ($line_number, $column_number) = _ppi_location($line_number, $column_number);
425 0 ▶ $line_number = 1 if $self->{one_line};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"one_line"} 0 0 0
426  
427 my $find = PPI::Find->new(
428 sub {
429 0 ▶ my ($element) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
430  
431 0 ▶ return 0 unless $element->line_number == $line_number;
Branch: 1 branch
BranchTrueFalseTotal
unless $element->line_number == $line_number 0 0 0
432 0 ▶ return 0 if $element->column_number > $column_number;
Branch: 1 branch
BranchTrueFalseTotal
if $element->column_number > $column_number 0 0 0
433 0 ▶ return 0 if $element->column_number + (length $element->content) < $column_number;
Branch: 1 branch
BranchTrueFalseTotal
if $element->column_number + length($element->content) < $column_number 0 0 0
434 0 return 1;
435 }
436 0 );
437  
438 0 my @matches = $find->in($self->{document});
439 @matches =
440 0 sort { (abs $column_number - $a->column_number) <=> (abs $column_number - $b->column_number) } @matches;
441 0 @matches = map { PLS::Parser::Element->new(document => $self->{document}, element => $_, file => $self->{path}) } @matches;
442 0 return @matches;
443 } ## end sub find_elements_at_location
444  
445 =head2 find_external_subroutine
446  
447 This attempts to find the location of a subroutine inside an external module,
448 by name.
449  
450 =cut
451  
452 sub find_external_subroutine
453 {
454 0 ▶ my ($self, $package_name, $subroutine_name) = @_;
Subroutine find_external_subroutine: not called CC=5 SCAR=34.0
Pod documented
455  
456 0 my $include = PLS::Parser::Pod->get_clean_inc();
457 0 my $package = Module::Metadata->new_from_module($package_name, inc => $include);
458 0 ▶ return unless (ref $package eq 'Module::Metadata');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $package eq "Module::Metadata" 0 0 0
459  
460 0 my $doc = PLS::Parser::Document->new(path => $package->filename);
461 0 ▶ return unless (ref $doc eq 'PLS::Parser::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc eq "PLS::Parser::Document" 0 0 0
462  
463 0 foreach my $subroutine (@{$doc->get_subroutines()})
464 {
465 0 ▶ next unless ($subroutine->name eq $subroutine_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $subroutine->name eq $subroutine_name 0 0 0
466  
467 return {
468 uri => URI::file->new($package->filename)->as_string,
469 range => $subroutine->range(),
470 signature => $subroutine->location_info->{signature}
471 0 };
472 } ## end foreach my $subroutine (@{$doc...})
473  
474 0 return;
475 } ## end sub find_external_subroutine
476  
477 =head2 find_external_package
478  
479 This attempts to find the location of an external package by name.
480  
481 =cut
482  
483 sub find_external_package
484 {
485 0 ▶ my ($self, $package_name) = @_;
Subroutine find_external_package: not called CC=6 SCAR=37.4
Pod documented
486  
487 0 ▶ return unless (length $package_name);
Branch: 1 branch
BranchTrueFalseTotal
unless length $package_name 0 0 0
488  
489 0 my $include = PLS::Parser::Pod->get_clean_inc();
490 0 my $metadata = Module::Metadata->new_from_module($package_name, inc => $include);
491  
492 0 ▶ return unless (ref $metadata eq 'Module::Metadata');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $metadata eq "Module::Metadata" 0 0 0
493  
494 0 my $document = PLS::Parser::Document->new(path => $metadata->filename);
495 0 ▶ return unless (ref $document eq 'PLS::Parser::Document');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $document eq "PLS::Parser::Document" 0 0 0
496  
497 0 foreach my $package (@{$document->get_packages()})
498 {
499 0 ▶ next unless ($package->name eq $package_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $package->name eq $package_name 0 0 0
500  
501 return {
502 0 uri => URI::file->new($metadata->filename)->as_string,
503 range => $package->range()
504 };
505 } ## end foreach my $package (@{$document...})
506  
507 0 return;
508 } ## end sub find_external_package
509  
510 =head2 go_to_variable_definition
511  
512 This finds the definition of a variable.
513  
514 This B<probably> only works correctly for C<my>, C<local>, and C<state> variables,
515 but may also work for C<our> variables as long as they are in the same file.
516  
517 =cut
518  
519 sub go_to_variable_definition
520 {
521 0 ▶ my ($self, $variable, $element, $line_number, $column_number) = @_;
Subroutine go_to_variable_definition: not called CC=32 SCAR=69.6
Pod documented
522  
523 0 my $cursor = $element->element;
524 0 my $prev_cursor;
525 0 my $document = $cursor->top;
526  
527 0 my $declaration;
528  
529 0 OUTER: while (1)
530 {
531 0 $prev_cursor = $cursor;
532 0 $cursor = $cursor->parent;
533  
534 0 ▶ next unless blessed($cursor);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($cursor) 0 0 0
535  
536 0 ▶ if ($cursor->isa('PPI::Structure::Block') or $cursor->isa('PPI::Document'))
Branch: 2 branches
BranchTrueFalseTotal
if ($cursor->isa('PPI::Structure::Block') or $cursor->isa('PPI::Document')) { } 0 0 0
elsif ($cursor->isa('PPI::Statement::Compound')) { } 0 0 0
Condition0%0/3
$cursor->isa("PPI::Structure::Block") or $cursor->isa("PPI::Document")
l!l&&r!l&&!r
000
MC/DC0%0/2
$cursor->isa("PPI::Structure::Block") or $cursor->isa("PPI::Document")
$cursor->isa("PPI::Structure::Block") $cursor->isa("PPI::Document")
Truth table0%0/3
A or B
A= $cursor->isa("PPI::Structure::Block")B= $cursor->isa("PPI::Document")
ABresult
1X1
011
000
537 {
538 0 CHILDREN: foreach my $child ($cursor->children)
539 {
540 0 ▶ last CHILDREN if $child == $prev_cursor;
Branch: 1 branch
BranchTrueFalseTotal
if $child == $prev_cursor 0 0 0
541 0 ▶ next unless blessed($child);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($child) 0 0 0
542  
543 0 ▶ if ($child->isa('PPI::Statement::Variable') and any { $_ eq $variable } $child->variables)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Statement::Variable") and &any(sub { $_ eq $variable; } , $child->variables)) 0 0 0
Condition0%0/3
$child->isa("PPI::Statement::Variable") and &any(sub {
!ll&&!rl&&r
000
MC/DC0%0/2
$child->isa("PPI::Statement::Variable") and &any(sub { $_ eq $variable; } , $child->variables)
$child->isa("PPI::Statement::Variable") &any(sub { $_ eq $variable; } , $child->variables)
Truth table0%0/3
A and B
A= $child->isa("PPI::Statement::Variable")B= &any(sub { $_ eq $variable; } , $child->variables)
ABresult
0X0
100
111
544 {
545 0 $declaration = $child;
546 0 last OUTER;
547 }
548 0 ▶ if ($child->isa('PPI::Statement::Include') and $child->type eq 'use' and $child->pragma eq 'vars')
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Statement::Include") and $child->type eq "use" and $child->pragma eq "vars") 0 0 0
Condition0%0/6
$child->isa("PPI::Statement::Include") and $child->type eq "use" and $child->pragma eq "vars"
!ll&&!rl&&r
000
$child->isa("PPI::Statement::Include") and $child->type eq "use"
!ll&&!rl&&r
000
MC/DC0%0/3
$child->isa("PPI::Statement::Include") and $child->type eq "use" and $child->pragma eq "vars"
$child->isa("PPI::Statement::Include") $child->type eq "use" $child->pragma eq "vars"
Truth table0%0/4
A and B and C
A= $child->isa("PPI::Statement::Include")B= $child->type eq "use"C= $child->pragma eq "vars"
ABCresult
0XX0
10X0
1100
1111
549 {
550 0 my @variables = grep { defined } $child =~ /((?&PerlVariable))$PPR::GRAMMAR/gx;
551  
552 0 ▶ if (any { $_ eq $variable } @variables)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $variable; } , @variables)) 0 0 0
553 {
554 0 $declaration = $child;
555 0 last OUTER;
556 }
557 } ## end if ($child->isa('PPI::Statement::Include'...))
558 } ## end foreach my $child ($cursor->...)
559 } ## end if ($cursor->isa('PPI::Structure::Block'...))
560 elsif ($cursor->isa('PPI::Statement::Compound'))
561 {
562 0 ▶ if ($cursor->type eq 'foreach')
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor->type eq 'foreach') { } 0 0 0
563 {
564 0 CHILDREN: foreach my $child ($cursor->children)
565 {
566 0 ▶ last CHILDREN if $child == $prev_cursor;
Branch: 1 branch
BranchTrueFalseTotal
if $child == $prev_cursor 0 0 0
567 0 ▶ next unless blessed($child);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($child) 0 0 0
568  
569 0 ▶ if ($child->isa('PPI::Token::Word') and $child =~ /^my|our|local|state$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Word") and $child =~ /^my|our|local|state$/) 0 0 0
Condition0%0/3
$child->isa("PPI::Token::Word") and $child =~ /^my|our|local|state$/
!ll&&!rl&&r
000
MC/DC0%0/2
$child->isa("PPI::Token::Word") and $child =~ /^my|our|local|state$/
$child->isa("PPI::Token::Word") $child =~ /^my|our|local|state$/
Truth table0%0/3
A and B
A= $child->isa("PPI::Token::Word")B= $child =~ /^my|our|local|state$/
ABresult
0X0
100
111
570 {
571 0 ▶ if (blessed($child->snext_sibling) and $child->snext_sibling->isa('PPI::Token::Symbol') and $child->snext_sibling->symbol eq $variable)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($child->snext_sibling) and $child->snext_sibling->isa("PPI::Token::Symbol") and $child->snext_sibling->symbol eq $variable) 0 0 0
Condition0%0/6
builtin::blessed($child->snext_sibling) and $child->snext_sibling->isa("PPI::Token::Symbol") and $child->snext_sibling->symbol eq $variable
!ll&&!rl&&r
000
builtin::blessed($child->snext_sibling) and $child->snext_sibling->isa("PPI::Token::Symbol")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($child->snext_sibling) and $child->snext_sibling->isa("PPI::Token::Symbol") and $child->snext_sibling->symbol eq $variable
builtin::blessed($child->snext_sibling) $child->snext_sibling->isa("PPI::Token::Symbol") $child->snext_sibling->symbol eq $variable
Truth table0%0/4
A and B and C
A= builtin::blessed($child->snext_sibling)B= $child->snext_sibling->isa("PPI::Token::Symbol")C= $child->snext_sibling->symbol eq $variable
ABCresult
0XX0
10X0
1100
1111
572 {
573 0 $declaration = $cursor;
574 0 last OUTER;
575 }
576 } ## end if ($child->isa('PPI::Token::Word'...))
577 } ## end foreach my $child ($cursor->...)
578 } ## end if ($cursor->type eq 'foreach'...)
579 else
580 {
581 0 ▶ my $condition = first { $_->isa('PPI::Structure::Condition') } grep { blessed($_) } $cursor->children;
Subroutine __ANON__: not called CC=1 SCAR=6.9
582 0 ▶ next OUTER if (not blessed($condition) or not $condition->isa('PPI::Structure::Condition'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($condition) and $condition->isa("PPI::Structure::Condition") 0 0 0
Condition0%0/3
builtin::blessed($condition) and $condition->isa("PPI::Structure::Condition")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($condition) and $condition->isa("PPI::Structure::Condition")
builtin::blessed($condition) $condition->isa("PPI::Structure::Condition")
Truth table0%0/3
A and B
A= builtin::blessed($condition)B= $condition->isa("PPI::Structure::Condition")
ABresult
0X0
100
111
583  
584 0 CHILDREN: foreach my $child ($condition->children)
585 {
586 0 ▶ last CHILDREN if $child == $prev_cursor;
Branch: 1 branch
BranchTrueFalseTotal
if $child == $prev_cursor 0 0 0
587 0 ▶ next unless blessed($child);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($child) 0 0 0
588  
589 0 ▶ if ($child->isa('PPI::Statement::Variable'))
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Statement::Variable")) 0 0 0
590 {
591 0 $declaration = $child;
592 0 last OUTER;
593 }
594 } ## end foreach my $child ($condition...)
595 } ## end else [ if ($cursor->type eq 'foreach'...)]
596 } ## end elsif ($cursor->isa('PPI::Statement::Compound'...))
597  
598 0 ▶ last if $cursor == $document;
Branch: 1 branch
BranchTrueFalseTotal
if $cursor == $document 0 0 0
599 } ## end while (1)
600  
601 0 ▶ return if (not blessed($declaration) or not $declaration->isa('PPI::Element'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($declaration) and $declaration->isa("PPI::Element") 0 0 0
Condition0%0/3
builtin::blessed($declaration) and $declaration->isa("PPI::Element")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($declaration) and $declaration->isa("PPI::Element")
builtin::blessed($declaration) $declaration->isa("PPI::Element")
Truth table0%0/3
A and B
A= builtin::blessed($declaration)B= $declaration->isa("PPI::Element")
ABresult
0X0
100
111
602  
603 0 $element = PLS::Parser::Element->new(file => $self->{path}, document => $self->{document}, element => $declaration);
604  
605 return [
606 {
607 uri => $self->{uri},
608 0 range => $element->range()
609 }
610 ];
611 } ## end sub go_to_variable_definition
612  
613 =head2 open_file
614  
615 This adds a file and its text to a list of open files.
616  
617 =cut
618  
619 sub open_file
620 {
621 0 ▶ my ($class, %args) = @_;
Subroutine open_file: not called CC=2 SCAR=17.9
Pod documented
622  
623 0 ▶ return unless $args{languageId} eq 'perl';
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"languageId"} eq "perl" 0 0 0
624  
625 0 $FILES{$args{uri}} = {text => $args{text}};
626  
627 0 return;
628 } ## end sub open_file
629  
630 =head2 open_files
631  
632 This provides a list of names of files that are currently open.
633  
634 =cut
635  
636 sub open_files
637 {
638 0 ▶ return [keys %FILES];
Subroutine open_files: not called CC=1 SCAR=6.9
Pod documented
639 }
640  
641 =head2 update_file
642  
643 This patches an open file in memory to keep it synched with
644 the actual file in the editor.
645  
646 =cut
647  
648 sub update_file
649 {
650 0 ▶ my ($class, @args) = @_;
Subroutine update_file: not called CC=10 SCAR=47.0
Pod documented
651  
652 0 my %args = @args;
653  
654 0 my $file = $FILES{$args{uri}};
655 0 ▶ return unless (ref $file eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file eq "HASH" 0 0 0
656  
657 0 foreach my $change (@{$args{changes}})
658 {
659 0 ▶ if (ref $change->{range} eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $change->{'range'} eq 'HASH') { } 0 0 0
660 {
661 0 my @lines = _split_lines($file->{text});
662 0 my @replacement = _split_lines($change->{text});
663  
664 0 my ($starting_text, $ending_text);
665  
666 # get the text that we're not replacing at the start and end of each selection
667 $starting_text = substr $lines[$change->{range}{start}{line}], 0, $change->{range}{start}{character}
668 0 ▶ if ($#lines >= $change->{range}{start}{line});
Branch: 1 branch
BranchTrueFalseTotal
if $#lines >= $change->{"range"}{"start"}{"line"} 0 0 0
669 0 ▶ $ending_text = substr $lines[$change->{range}{end}{line}], $change->{range}{end}{character} if ($#lines >= $change->{range}{end}{line});
Branch: 1 branch
BranchTrueFalseTotal
if $#lines >= $change->{"range"}{"end"}{"line"} 0 0 0
670  
671 # append the existing text to the replacement
672 0 ▶ if (length $starting_text)
Branch: 1 branch
BranchTrueFalseTotal
if (length $starting_text) 0 0 0
673 {
674 0 ▶ $replacement[0] = length $replacement[0] ? $starting_text . $replacement[0] : $starting_text;
Branch: 1 branch
BranchTrueFalseTotal
length $replacement[0] ? : 0 0 0
675 }
676 0 ▶ if (length $ending_text)
Branch: 1 branch
BranchTrueFalseTotal
if (length $ending_text) 0 0 0
677 {
678 0 ▶ if (scalar @replacement)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @replacement) { } 0 0 0
679 {
680 0 $replacement[-1] .= $ending_text;
681 }
682 else
683 {
684 0 $replacement[0] = $ending_text;
685 }
686 } ## end if (length $ending_text...)
687  
688 # replace the lines in the range (which may not match the number of lines in the replacement)
689 # with the replacement, including the existing text that is not changing, that we appended above
690 0 my $lines_replacing = $change->{range}{end}{line} - $change->{range}{start}{line} + 1;
691 0 splice @lines, $change->{range}{start}{line}, $lines_replacing, @replacement;
692 0 $file->{text} = join '', @lines;
693 } ## end if (ref $change->{range...})
694 else
695 {
696 # no range means we're updating the entire document
697 0 $file->{text} = $change->{text};
698 }
699 } ## end foreach my $change (@{$args...})
700  
701 0 return;
702 } ## end sub update_file
703  
704 =head2 close_file
705  
706 This removes a file from the list of open files.
707  
708 =cut
709  
710 sub close_file
711 {
712 0 ▶ my ($class, @args) = @_;
Subroutine close_file: not called CC=1 SCAR=6.9
Pod documented
713  
714 0 my %args = @args;
715  
716 0 delete $FILES{$args{uri}};
717 } ## end sub close_file
718  
719 =head2 get_subroutines
720  
721 This gets a list of all subroutines in a document.
722  
723 =cut
724  
725 sub get_subroutines
726 {
727 0 ▶ my ($self) = @_;
Subroutine get_subroutines: not called CC=1 SCAR=6.9
Pod documented
728  
729 my $find = PPI::Find->new(
730 sub {
731 0 ▶ $_[0]->isa('PPI::Statement::Sub') and not $_[0]->isa('PPI::Statement::Scheduled') and ref $_[0]->block eq 'PPI::Structure::Block';
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa("PPI::Statement::Sub") and not $_[0]->isa("PPI::Statement::Scheduled") and ref $_[0]->block eq "PPI::Structure::Block" 0 0 0
Condition0%0/3
$_[0]->isa("PPI::Statement::Sub") and not $_[0]->isa("PPI::Statement::Scheduled")
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0]->isa("PPI::Statement::Sub") and not $_[0]->isa("PPI::Statement::Scheduled")
$_[0]->isa("PPI::Statement::Sub") not $_[0]->isa("PPI::Statement::Scheduled")
Truth table0%0/3
A and not(B)
A= $_[0]->isa("PPI::Statement::Sub")B= not $_[0]->isa("PPI::Statement::Scheduled")
ABresult
0X0
100
111
732 }
733 0 );
734 0 return [map { PLS::Parser::Element::Subroutine->new(document => $self->{document}, element => $_, file => $self->{path}) } $find->in($self->{document})];
735 } ## end sub get_subroutines
736  
737 =head2 get_constants
738  
739 This gets a list of all constants in a document.
740  
741 Only constants declared with C<use constant> are found.
742  
743 =cut
744  
745 sub get_constants
746 {
747 0 ▶ my ($self, $element) = @_;
Subroutine get_constants: not called CC=4 SCAR=30.0
Pod documented
748  
749 0 my @matches;
750  
751 0 ▶ if (ref $element eq 'PPI::Statement::Include')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $element eq 'PPI::Statement::Include') { } 0 0 0
752 {
753 0 @matches = ($element);
754 }
755 else
756 {
757 my $find = PPI::Find->new(
758 sub {
759 0 ▶ my ($element) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
760  
761 0 ▶ return 0 unless $element->isa('PPI::Statement::Include');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement::Include") 0 0 0
762 0 ▶ return unless $element->type eq 'use';
Branch: 1 branch
BranchTrueFalseTotal
unless $element->type eq "use" 0 0 0
763 0 ▶ return (length $element->module and $element->module eq 'constant');
Condition0%0/3
length $element->module && $element->module eq 'constant'
!ll&&!rl&&r
000
MC/DC0%0/2
length $element->module && $element->module eq 'constant'
length $element->module $element->module eq 'constant'
Truth table0%0/3
A && B
A= length $element->moduleB= $element->module eq 'constant'
ABresult
0X0
100
111
764 }
765 0 );
766  
767 0 @matches = $find->in($self->{document});
768 } ## end else [ if (ref $element eq 'PPI::Statement::Include'...)]
769  
770 0 my @constants;
771  
772 0 foreach my $match (@matches)
773 {
774 0 my ($constructor) = grep { $_->isa('PPI::Structure::Constructor') } $match->children;
775  
776 0 ▶ if (ref $constructor eq 'PPI::Structure::Constructor')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $constructor eq 'PPI::Structure::Constructor') { } 0 0 0
777 {
778 0 push @constants, grep { _is_constant($_) }
779 0 map { $_->children }
780 0 grep { $_->isa('PPI::Statement::Expression') } $constructor->children;
781 } ## end if (ref $constructor eq...)
782 else
783 {
784 0 push @constants, grep { _is_constant($_) } $match->children;
785 }
786 } ## end foreach my $match (@matches...)
787  
788 0 return [map { PLS::Parser::Element::Constant->new(document => $self->{document}, element => $_, file => $self->{path}) } @constants];
789 } ## end sub get_constants
790  
791 =head2 get_packages
792  
793 This gets a list of all packages in a document.
794  
795 =cut
796  
797 sub get_packages
798 {
799 0 ▶ my ($self) = @_;
Subroutine get_packages: not called CC=1 SCAR=6.9
Pod documented
800  
801 0 ▶ my $find = PPI::Find->new(sub { $_[0]->isa('PPI::Statement::Package') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
802 0 return [map { PLS::Parser::Element::Package->new(document => $self->{document}, element => $_, file => $self->{path}) } $find->in($self->{document})];
803 } ## end sub get_packages
804  
805 =head2 get_variable_statements
806  
807 This gets a list of all variable statements in a document.
808 A variable statement is a statement which declares one or more variables.
809  
810 =cut
811  
812 sub get_variable_statements
813 {
814 0 ▶ my ($self, $element) = @_;
Subroutine get_variable_statements: not called CC=3 SCAR=24.8
Pod documented
815  
816 0 my @elements;
817  
818 0 ▶ if (blessed($element) and $element->isa('PPI::Statement::Variable'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($element) and $element->isa('PPI::Statement::Variable')) { } 0 0 0
Condition0%0/3
builtin::blessed($element) and $element->isa("PPI::Statement::Variable")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($element) and $element->isa("PPI::Statement::Variable")
builtin::blessed($element) $element->isa("PPI::Statement::Variable")
Truth table0%0/3
A and B
A= builtin::blessed($element)B= $element->isa("PPI::Statement::Variable")
ABresult
0X0
100
111
819 {
820 0 @elements = ($element);
821 }
822 else
823 {
824 0 ▶ my $find = PPI::Find->new(sub { $_[0]->isa('PPI::Statement::Variable') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
825 0 @elements = $find->in($self->{document});
826 }
827  
828 0 return [map { PLS::Parser::Element::VariableStatement->new(document => $self->{document}, element => $_, file => $self->{path}) } @elements];
829 } ## end sub get_variable_statements
830  
831 =head2 get_full_text
832  
833 This returns a SCALAR reference of the in-memory text of the current document.
834  
835 =cut
836  
837 sub get_full_text
838 {
839 0 ▶ my ($self) = @_;
Subroutine get_full_text: not called CC=1 SCAR=6.9
Pod documented
840  
841 0 return _text_from_uri($self->{uri});
842 }
843  
844 =head2 get_variables_fast
845  
846 This gets a list of all variables in the current document.
847 It uses L<PPR> to do so, which is faster than L<PPI>, but only provides a list of strings.
848  
849 =cut
850  
851 sub get_variables_fast
852 {
853 0 ▶ my ($self, $text) = @_;
Subroutine get_variables_fast: not called CC=3 SCAR=24.8
Pod documented
854  
855 0 ▶ $text = $self->get_full_text() unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
856 0 ▶ return [] unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
857  
858 0 my @variable_declarations = $$text =~ /((?&PerlVariableDeclaration))$PPR::GRAMMAR/gx;
859 0 @variable_declarations = grep { defined } @variable_declarations;
860  
861 # Precompile regex used multiple times
862 0 my $re = qr/((?&PerlVariable))$PPR::GRAMMAR/x;
863  
864 return [
865 0 map { s/^\s+|\s+$//r }
866 0 grep { defined } map { /$re/g } @variable_declarations
867 ];
868 } ## end sub get_variables_fast
869  
870 =head2 get_packages_fast
871  
872 This gets a list of all packages in the current document.
873 It uses L<PPR> to do so, which is faster than L<PPI>, but only provides a list of strings.
874  
875 =cut
876  
877 sub get_packages_fast
878 {
879 0 ▶ my ($self, $text) = @_;
Subroutine get_packages_fast: not called CC=3 SCAR=24.8
Pod documented
880  
881 0 ▶ $text = $self->get_full_text() unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
882 0 ▶ return [] unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
883  
884 0 my @package_declarations = $$text =~ /((?&PerlPackageDeclaration))$PPR::GRAMMAR/gx;
885 0 @package_declarations = grep { defined } @package_declarations;
886  
887 # Precompile regex used multiple times
888 0 my $re = qr/((?&PerlQualifiedIdentifier))$PPR::GRAMMAR/x;
889  
890 return [
891 0 map { s/^\s+|\s+$//r }
892 0 grep { defined } map { /$re/g } @package_declarations
893 ];
894 } ## end sub get_packages_fast
895  
896 =head2 get_subroutines_fast
897  
898 This gets a list of all subroutines in the current document.
899 It uses L<PPR> to do so, which is faster than L<PPI>, but only provides a list of strings.
900  
901 =cut
902  
903 sub get_subroutines_fast
904 {
905 0 ▶ my ($self, $text) = @_;
Subroutine get_subroutines_fast: not called CC=3 SCAR=24.8
Pod documented
906  
907 0 ▶ $text = $self->get_full_text() unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
908 0 ▶ return [] unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
909  
910 0 my @subroutine_declarations = $$text =~ /sub\b(?&PerlOWS)((?&PerlOldQualifiedIdentifier))$PPR::GRAMMAR/gx;
911  
912 return [
913 0 map { s/^\s+|\s+$//r }
914 0 grep { defined } @subroutine_declarations
915 ];
916 } ## end sub get_subroutines_fast
917  
918 =head2 get_constants_fast
919  
920 This gets a list of all constants in the current document.
921 It uses L<PPR> to do so, which is faster than L<PPI>, but only provides a list of strings.
922  
923 This only finds constants declared with C<use constant>.
924  
925 =cut
926  
927 sub get_constants_fast
928 {
929 0 ▶ my ($self, $text) = @_;
Subroutine get_constants_fast: not called CC=3 SCAR=24.8
Pod documented
930  
931 0 ▶ $text = $self->get_full_text() unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
932 0 ▶ return [] unless (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $text eq "SCALAR" 0 0 0
933  
934 0 my @use_statements = $$text =~ /((?&PerlUseStatement)) $PPR::GRAMMAR/gx;
935 0 @use_statements = grep { defined } @use_statements;
936  
937 # Precompile regex used multiple times
938 0 my $block_re = qr/constant (?&PerlOWS) ((?&PerlBlock)) $PPR::GRAMMAR/x;
939 0 my $bareword_re = qr/((?&PerlBareword)) (?&PerlOWS) (?&PerlComma) $PPR::GRAMMAR/x;
940  
941 return [
942 0 map { s/^\s+|\s+$//r }
943 0 grep { defined } map { /$bareword_re/g }
944 0 grep { defined } map { /$block_re/g } @use_statements
945 ];
946 } ## end sub get_constants_fast
947  
948 =head2 format_range
949  
950 This formats a range of text in the document using perltidy.
951  
952 =cut
953  
954 sub format_range
955 {
956 0 ▶ my ($class, %args) = @_;
Subroutine format_range: not called CC=18 SCAR=58.3
Pod documented
957  
958 0 ▶ $args{formatting_options} = {} unless (ref $args{formatting_options} eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"formatting_options"} eq "HASH" 0 0 0
959 0 my $range = $args{range};
960  
961 0 my $text = _text_from_uri($args{uri});
962  
963 0 ▶ if (ref $text ne 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text ne "SCALAR") 0 0 0
964 {
965 0 return (0, {code => -32700, message => 'Could not get document text.'});
966 }
967  
968 0 my $selection = '';
969 0 my $whole_file = 0;
970  
971 0 ▶ if (ref $range eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $range eq 'HASH') { } 0 0 0
972 {
973 # if we've selected up until the first character of the next line,
974 # just format up to the line before that
975 0 ▶ $range->{end}{line}-- if ($range->{end}{character} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $range->{"end"}{"character"} == 0 0 0 0
976  
977 0 my @lines = _split_lines($$text);
978 0 @lines = @lines[$range->{start}{line} .. $range->{end}{line}];
979  
980 # ignore the column, and just format the entire line.
981 # the text will likely get messed up if you don't include the entire line, anyway.
982 0 $range->{start}{character} = 0;
983 0 $range->{end}{character} = 0;
984 0 $range->{end}{line}++;
985 0 $selection = join '', @lines;
986 } ## end if (ref $range eq 'HASH'...)
987 else
988 {
989 0 $whole_file = 1;
990 0 $selection = $$text;
991 0 my $lines = () = $selection =~ m{($/)}g;
992 0 $lines++;
993  
994 0 $range = {
995 start => {
996 line => 0,
997 character => 0
998 },
999 end => {
1000 line => $lines,
1001 character => 0
1002 }
1003 };
1004 } ## end else [ if (ref $range eq 'HASH'...)]
1005  
1006 0 my $formatted = '';
1007 0 my $stderr = '';
1008 0 my $argv = '-se';
1009 0 ▶ if (length $args{formatting_options}{tabSize})
Branch: 1 branch
BranchTrueFalseTotal
if (length $args{"formatting_options"}{"tabSize"}) 0 0 0
1010 {
1011 0 ▶ $argv .= $args{formatting_options}{insertSpaces} ? ' -i=' : ' -et=';
Branch: 1 branch
BranchTrueFalseTotal
$args{'formatting_options'}{'insertSpaces'} ? : 0 0 0
1012 0 $argv .= $args{formatting_options}{tabSize};
1013 }
1014 0 my ($perltidyrc) = glob $PLS::Server::State::CONFIG->{perltidyrc};
1015 0 ▶ undef $perltidyrc if (not length $perltidyrc or not -f $perltidyrc or not -r $perltidyrc);
Branch: 1 branch
BranchTrueFalseTotal
unless length $perltidyrc and -f $perltidyrc and -r $perltidyrc 0 0 0
Condition0%0/6
length $perltidyrc and -f $perltidyrc and -r $perltidyrc
!ll&&!rl&&r
000
length $perltidyrc and -f $perltidyrc
!ll&&!rl&&r
000
MC/DC0%0/3
length $perltidyrc and -f $perltidyrc and -r $perltidyrc
length $perltidyrc -f $perltidyrc -r $perltidyrc
Truth table0%0/4
A and B and C
A= length $perltidyrcB= -f $perltidyrcC= -r $perltidyrc
ABCresult
0XX0
10X0
1100
1111
1016 0 my $error = Perl::Tidy::perltidy(source => \$selection, destination => \$formatted, stderr => \$stderr, perltidyrc => $perltidyrc, argv => $argv);
1017  
1018 # get the number of lines in the formatted result - we need to modify the range if
1019 # any lines were added
1020 0 my $lines = () = $formatted =~ m{($/)}g;
1021 0 $lines++;
1022  
1023 # if the selection length has increased due to formatting, update the end.
1024 0 ▶ $range->{end}{line} = $lines if ($whole_file and $lines > $range->{end}{line});
Branch: 1 branch
BranchTrueFalseTotal
if $whole_file and $lines > $range->{"end"}{"line"} 0 0 0
Condition0%0/3
$whole_file and $lines > $range->{"end"}{"line"}
!ll&&!rl&&r
000
MC/DC0%0/2
$whole_file and $lines > $range->{"end"}{"line"}
$whole_file $lines > $range->{"end"}{"line"}
Truth table0%0/3
A and B
A= $whole_fileB= $lines > $range->{"end"}{"line"}
ABresult
0X0
100
111
1025  
1026 0 ▶ $formatted =~ s/\h+$//gm if ($args{formatting_options}{trimTrailingWhitespace});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"formatting_options"}{"trimTrailingWhitespace"} 0 0 0
1027  
1028 0 ▶ if ($args{formatting_options}{insertFinalNewline})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"formatting_options"}{"insertFinalNewline"}) 0 0 0
1029 {
1030 0 ▶ $formatted .= "\n" unless ($formatted =~ /\n$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $formatted =~ /\n$/ 0 0 0
1031 }
1032 0 ▶ if ($args{formatting_options}{trimFinalNewlines})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"formatting_options"}{"trimFinalNewlines"}) 0 0 0
1033 {
1034 0 $formatted =~ s/\n+$/\n/;
1035 }
1036  
1037 0 $stderr =~ s/^<source_stream>:\s*//gm;
1038 0 $stderr =~ s/^Begin Error Output Stream.*$//m;
1039 0 $stderr =~ s/^.*To save a full \.LOG file.*$//m;
1040 0 $stderr =~ s/^\s*$//gm;
1041  
1042 0 ▶ if ($error == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($error == 1) 0 0 0
1043 {
1044 0 return (0, {code => -32700, message => 'Perltidy failed to format the text.', data => $stderr});
1045 }
1046 0 ▶ if (length $stderr)
Branch: 1 branch
BranchTrueFalseTotal
if (length $stderr) 0 0 0
1047 {
1048 return (
1049 0 0,
1050 {
1051 code => -32700,
1052 message => 'There were warnings or errors when running Perltidy. Formatting aborted.',
1053 data => $stderr
1054 }
1055 );
1056 } ## end if (length $stderr)
1057  
1058 return (
1059 0 1,
1060 [
1061 {
1062 range => $range,
1063 newText => $formatted
1064 }
1065 ]
1066 );
1067 } ## end sub format_range
1068  
1069 =head2 format
1070  
1071 This formats the entire document using perltidy.
1072  
1073 =cut
1074  
1075 sub format
1076 {
1077 0 ▶ my ($class, %args) = @_;
Subroutine format: not called CC=1 SCAR=6.9
Pod documented
1078  
1079 0 return $class->format_range(formatting_options => $args{formatting_options}, uri => $args{uri});
1080 }
1081  
1082 =head2 _ppi_location
1083  
1084 This converts an LSP 0-indexed location to a PPI 1-indexed location.
1085  
1086 =cut
1087  
1088 sub _ppi_location
1089 {
1090 0 ▶ my ($line_number, $column_number) = @_;
Subroutine _ppi_location: not called CC=1 SCAR=6.9
1091  
1092 0 return ++$line_number, ++$column_number;
1093 }
1094  
1095 =head2 _text_from_uri
1096  
1097 This returns a SCALAR reference to the text of a particular URI.
1098  
1099 =cut
1100  
1101 sub _text_from_uri
1102 {
1103 0 ▶ my ($uri) = @_;
Subroutine _text_from_uri: not called CC=3 SCAR=24.8
1104  
1105 0 ▶ if (ref $FILES{$uri} eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $FILES{$uri} eq 'HASH') { } 0 0 0
1106 {
1107 0 return \($FILES{$uri}{text});
1108 }
1109 else
1110 {
1111 0 my $file = URI->new($uri);
1112 0 ▶ open my $fh, '<', $file->file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file->file or return 0 0 0
1113 0 my $text = do { local $/; <$fh> };
1114 0 return \$text;
1115 } ## end else [ if (ref $FILES{$uri} eq...)]
1116 } ## end sub _text_from_uri
1117  
1118 =head2 _get_ppi_document
1119  
1120 This creates a L<PPI::Document> object for a document. It will
1121 return an L<PPI::Document> from memory if the file has not changed since it was last parsed.
1122  
1123 =cut
1124  
1125 sub _get_ppi_document
1126 {
1127 0 ▶ my ($self, %args) = @_;
Subroutine _get_ppi_document: not called CC=20 SCAR=60.4
1128  
1129 0 my $file;
1130 0 my $sha = Digest::SHA->new(256);
1131  
1132 0 ▶ if (length $args{uri})
Branch: 2 branches
BranchTrueFalseTotal
if (length $args{'uri'}) { } 0 0 0
elsif ($args{'text'}) { } 0 0 0
1133 {
1134 0 ▶ if (ref $FILES{$args{uri}} eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $FILES{$args{'uri'}} eq 'HASH') { } 0 0 0
1135 {
1136 0 $file = \($FILES{$args{uri}}{text});
1137 }
1138 else
1139 {
1140 0 $file = URI->new($args{uri})->file;
1141 }
1142 } ## end if (length $args{uri})
1143 elsif ($args{text})
1144 {
1145 0 $file = $args{text};
1146 }
1147  
1148 0 ▶ if (length $args{line})
Branch: 1 branch
BranchTrueFalseTotal
if (length $args{"line"}) 0 0 0
1149 {
1150 0 my $fh;
1151 0 ▶ if (ref $file eq 'SCALAR')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 0 0 0
elsif (open $fh, '<', $file) { } 0 0 0
1152 {
1153 0 my $line = $args{line};
1154 0 my $new_line = $/;
1155  
1156 0 my ($text) = $$file =~ /(?:[^$new_line]*$new_line){$line}([^$new_line]*)$new_line?/m;
1157  
1158 0 ▶ if (length $text)
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 0 0 0
1159 {
1160 0 $file = \$text;
1161 0 $self->{one_line} = 1;
1162 }
1163 } ## end if (ref $file eq 'SCALAR'...)
1164 elsif (open $fh, '<', $file)
1165 {
1166 0 my @text = <$fh>;
1167  
1168 0 ▶ if (length $text[$args{line}])
Branch: 1 branch
BranchTrueFalseTotal
if (length $text[$args{"line"}]) 0 0 0
1169 {
1170 0 $file = \($text[$args{line}]);
1171 0 $self->{one_line} = 1;
1172 }
1173 } ## end elsif (open $fh, '<', $file...)
1174 } ## end if (length $args{line}...)
1175  
1176 0 state %documents;
1177  
1178 0 ▶ if (ref $file eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 0 0 0
1179 {
1180 0 $sha->add(Encode::encode_utf8($$file));
1181 }
1182 else
1183 {
1184 0 $sha->addfile($file);
1185 }
1186  
1187 0 my $digest = $sha->hexdigest();
1188  
1189 0 ▶ if (exists $documents{$digest} and blessed($documents{$digest}{document}) and $documents{$digest}{document}->isa('PPI::Document') and not $args{no_cache})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $documents{$digest} and builtin::blessed($documents{$digest}{"document"}) and $documents{$digest}{"document"}->isa("PPI::Document") and not $args{"no_cache"}) 0 0 0
Condition0%0/9
exists $documents{$digest} and builtin::blessed($documents{$digest}{"document"}) and $documents{$digest}{"document"}->isa("PPI::Document") and not $args{"no_cache"}
!ll&&!rl&&r
000
exists $documents{$digest} and builtin::blessed($documents{$digest}{"document"}) and $documents{$digest}{"document"}->isa("PPI::Document")
!ll&&!rl&&r
000
exists $documents{$digest} and builtin::blessed($documents{$digest}{"document"})
!ll&&!rl&&r
000
MC/DC0%0/4
exists $documents{$digest} and builtin::blessed($documents{$digest}{"document"}) and $documents{$digest}{"document"}->isa("PPI::Document") and not $args{"no_cache"}
exists $documents{$digest} builtin::blessed($documents{$digest}{"document"}) $documents{$digest}{"document"}->isa("PPI::Document") not $args{"no_cache"}
Truth table0%0/5
A and B and C and not(D)
A= exists $documents{$digest}B= builtin::blessed($documents{$digest}{"document"})C= $documents{$digest}{"document"}->isa("PPI::Document")D= not $args{"no_cache"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1190 {
1191 0 return $documents{$digest}{document};
1192 }
1193  
1194 0 my $document = PPI::Document->new($file, readonly => 1);
1195 0 ▶ return if (not blessed($document) or not $document->isa('PPI::Document'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($document) and $document->isa("PPI::Document") 0 0 0
Condition0%0/3
builtin::blessed($document) and $document->isa("PPI::Document")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($document) and $document->isa("PPI::Document")
builtin::blessed($document) $document->isa("PPI::Document")
Truth table0%0/3
A and B
A= builtin::blessed($document)B= $document->isa("PPI::Document")
ABresult
0X0
100
111
1196 0 $document->index_locations();
1197 0 ▶ $documents{$digest} = {document => $document, time => time} if (length $digest and not $args{no_cache});
Branch: 1 branch
BranchTrueFalseTotal
if length $digest and not $args{"no_cache"} 0 0 0
Condition0%0/3
length $digest and not $args{"no_cache"}
!ll&&!rl&&r
000
MC/DC0%0/2
length $digest and not $args{"no_cache"}
length $digest not $args{"no_cache"}
Truth table0%0/3
A and not(B)
A= length $digestB= not $args{"no_cache"}
ABresult
0X0
100
111
1198  
1199 # Clear cache after one minute
1200 0 foreach my $digest (keys %documents)
1201 {
1202 0 ▶ delete $documents{$digest} if (time - $documents{$digest}{time} >= Time::Seconds::ONE_MINUTE);
Branch: 1 branch
BranchTrueFalseTotal
if time - $documents{$digest}{"time"} >= 60 0 0 0
1203 }
1204  
1205 0 return $document;
1206 } ## end sub _get_ppi_document
1207  
1208 =head2 _is_constant
1209  
1210 Determines if a PPI element is a constant.
1211  
1212 =cut
1213  
1214 sub _is_constant
1215 {
1216 0 ▶ my ($element) = @_;
Subroutine _is_constant: not called CC=3 SCAR=24.8
1217  
1218 0 ▶ return unless $element->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Token::Word") 0 0 0
1219 0 ▶ return unless ref $_->snext_sibling eq 'PPI::Token::Operator';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_->snext_sibling eq "PPI::Token::Operator" 0 0 0
1220 0 return $_->snext_sibling->content eq '=>';
1221 } ## end sub _is_constant
1222  
1223 =head2 find_word_under_cursor
1224  
1225 Gets information about the current word under the cursor.
1226 Returns a four-element list:
1227  
1228 =over
1229  
1230 =item The range where the word is located
1231  
1232 =item A boolean indicating whether the word is before an arrow (->) or not.
1233  
1234 =item The name of the package where the word is located
1235  
1236 =item The word under the cursor to be used as a filter for searching
1237  
1238 =back
1239  
1240 =cut
1241  
1242 sub find_word_under_cursor
1243 {
1244 0 ▶ my ($self, $line, $character) = @_;
Subroutine find_word_under_cursor: not called CC=31 SCAR=69.0
Pod documented
1245  
1246 0 my @elements = $self->find_elements_at_location($line, $character);
1247 0 @elements = map { $_->tokens } @elements;
1248 0 @elements = grep { $_->lsp_column_number < $character } @elements;
1249 0 ▶ my $element = first { $_->type eq 'PPI::Token::Word' or $_->type eq 'PPI::Token::Label' or $_->type eq 'PPI::Token::Symbol' } @elements;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq "PPI::Token::Word" or $_->type eq "PPI::Token::Label" or $_->type eq "PPI::Token::Symbol" 0 0 0
Condition0%0/3
$_->type eq "PPI::Token::Word" or $_->type eq "PPI::Token::Label"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->type eq "PPI::Token::Word" or $_->type eq "PPI::Token::Label"
$_->type eq "PPI::Token::Word" $_->type eq "PPI::Token::Label"
Truth table0%0/3
A or B
A= $_->type eq "PPI::Token::Word"B= $_->type eq "PPI::Token::Label"
ABresult
1X1
011
000
1250 0 ▶ my $closest_operator = first { $_->type eq 'PPI::Token::Operator' } @elements;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1251 0 ▶ return if (not blessed($element) or not $element->isa('PLS::Parser::Element'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($element) and $element->isa("PLS::Parser::Element") 0 0 0
Condition0%0/3
builtin::blessed($element) and $element->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($element) and $element->isa("PLS::Parser::Element")
builtin::blessed($element) $element->isa("PLS::Parser::Element")
Truth table0%0/3
A and B
A= builtin::blessed($element)B= $element->isa("PLS::Parser::Element")
ABresult
0X0
100
111
1252  
1253 # Short-circuit if this is a HASH reference subscript.
1254 0 my $parent = $element->parent;
1255 0 ▶ $parent = $parent->parent if (blessed($parent) and ref $parent eq 'PLS::Parser::Element');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($parent) and ref $parent eq "PLS::Parser::Element" 0 0 0
Condition0%0/3
builtin::blessed($parent) and ref $parent eq "PLS::Parser::Element"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($parent) and ref $parent eq "PLS::Parser::Element"
builtin::blessed($parent) ref $parent eq "PLS::Parser::Element"
Truth table0%0/3
A and B
A= builtin::blessed($parent)B= ref $parent eq "PLS::Parser::Element"
ABresult
0X0
100
111
1256 0 ▶ return if ($element->type eq 'PPI::Token::Word' and blessed($parent) and $parent->isa('PLS::Parser::Element') and $parent->type eq 'PPI::Structure::Subscript');
Branch: 1 branch
BranchTrueFalseTotal
if $element->type eq "PPI::Token::Word" and builtin::blessed($parent) and $parent->isa("PLS::Parser::Element") and $parent->type eq "PPI::Structure::Subscript" 0 0 0
Condition0%0/9
$element->type eq "PPI::Token::Word" and builtin::blessed($parent) and $parent->isa("PLS::Parser::Element") and $parent->type eq "PPI::Structure::Subscript"
!ll&&!rl&&r
000
$element->type eq "PPI::Token::Word" and builtin::blessed($parent) and $parent->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
$element->type eq "PPI::Token::Word" and builtin::blessed($parent)
!ll&&!rl&&r
000
MC/DC0%0/4
$element->type eq "PPI::Token::Word" and builtin::blessed($parent) and $parent->isa("PLS::Parser::Element") and $parent->type eq "PPI::Structure::Subscript"
$element->type eq "PPI::Token::Word" builtin::blessed($parent) $parent->isa("PLS::Parser::Element") $parent->type eq "PPI::Structure::Subscript"
Truth table0%0/5
A and B and C and D
A= $element->type eq "PPI::Token::Word"B= builtin::blessed($parent)C= $parent->isa("PLS::Parser::Element")D= $parent->type eq "PPI::Structure::Subscript"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1257  
1258 # if the cursor is on the word after an arrow, back up to the arrow so we can use any package information before it.
1259 0 ▶ if ( $element->type eq 'PPI::Token::Word'
Branch: 1 branch
BranchTrueFalseTotal
if ($element->type eq "PPI::Token::Word" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element") and $element->previous_sibling->name eq "->") 0 0 0
Condition0%0/9
$element->type eq "PPI::Token::Word" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element") and $element->previous_sibling->name eq "->"
!ll&&!rl&&r
000
$element->type eq "PPI::Token::Word" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
$element->type eq "PPI::Token::Word" and builtin::blessed($element->previous_sibling)
!ll&&!rl&&r
000
MC/DC0%0/4
$element->type eq "PPI::Token::Word" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element") and $element->previous_sibling->name eq "->"
$element->type eq "PPI::Token::Word" builtin::blessed($element->previous_sibling) $element->previous_sibling->isa("PLS::Parser::Element") $element->previous_sibling->name eq "->"
Truth table0%0/5
A and B and C and D
A= $element->type eq "PPI::Token::Word"B= builtin::blessed($element->previous_sibling)C= $element->previous_sibling->isa("PLS::Parser::Element")D= $element->previous_sibling->name eq "->"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1260 and blessed($element->previous_sibling)
1261 and $element->previous_sibling->isa('PLS::Parser::Element')
1262 and $element->previous_sibling->name eq '->')
1263 {
1264 0 $closest_operator = $element->previous_sibling;
1265 } ## end if ($element->type eq ...)
1266  
1267 0 ▶ if (blessed($closest_operator) and $closest_operator->isa('PLS::Parser::Element') and $closest_operator->name eq '->' and $element->type eq 'PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($closest_operator) and $closest_operator->isa("PLS::Parser::Element") and $closest_operator->name eq "->" and $element->type eq "PPI::Token::Word") 0 0 0
Condition0%0/9
builtin::blessed($closest_operator) and $closest_operator->isa("PLS::Parser::Element") and $closest_operator->name eq "->" and $element->type eq "PPI::Token::Word"
!ll&&!rl&&r
000
builtin::blessed($closest_operator) and $closest_operator->isa("PLS::Parser::Element") and $closest_operator->name eq "->"
!ll&&!rl&&r
000
builtin::blessed($closest_operator) and $closest_operator->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($closest_operator) and $closest_operator->isa("PLS::Parser::Element") and $closest_operator->name eq "->" and $element->type eq "PPI::Token::Word"
builtin::blessed($closest_operator) $closest_operator->isa("PLS::Parser::Element") $closest_operator->name eq "->" $element->type eq "PPI::Token::Word"
Truth table0%0/5
A and B and C and D
A= builtin::blessed($closest_operator)B= $closest_operator->isa("PLS::Parser::Element")C= $closest_operator->name eq "->"D= $element->type eq "PPI::Token::Word"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1268 {
1269 # default to inserting after the arrow
1270 0 my $arrow_range = $element->range;
1271 my $range = {
1272 start => $arrow_range->{end},
1273 end => $arrow_range->{end}
1274 0 };
1275  
1276 0 my $filter = '';
1277  
1278 # if the next element is a word, it is likely the start of a method name,
1279 # so we want to return it as a filter. we also want the range to be that
1280 # of the next element so that we replace the word when it is selected.
1281 0 ▶ if ( blessed($closest_operator->next_sibling)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($closest_operator->next_sibling) and $closest_operator->next_sibling->isa("PLS::Parser::Element") and $closest_operator->next_sibling->type eq "PPI::Token::Word" and $closest_operator->ppi_line_number == $closest_operator->next_sibling->ppi_line_number) 0 0 0
Condition0%0/9
builtin::blessed($closest_operator->next_sibling) and $closest_operator->next_sibling->isa("PLS::Parser::Element") and $closest_operator->next_sibling->type eq "PPI::Token::Word" and $closest_operator->ppi_line_number == $closest_operator->next_sibling->ppi_line_number
!ll&&!rl&&r
000
builtin::blessed($closest_operator->next_sibling) and $closest_operator->next_sibling->isa("PLS::Parser::Element") and $closest_operator->next_sibling->type eq "PPI::Token::Word"
!ll&&!rl&&r
000
builtin::blessed($closest_operator->next_sibling) and $closest_operator->next_sibling->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($closest_operator->next_sibling) and $closest_operator->next_sibling->isa("PLS::Parser::Element") and $closest_operator->next_sibling->type eq "PPI::Token::Word" and $closest_operator->ppi_line_number == $closest_operator->next_sibling->ppi_line_number
builtin::blessed($closest_operator->next_sibling) $closest_operator->next_sibling->isa("PLS::Parser::Element") $closest_operator->next_sibling->type eq "PPI::Token::Word" $closest_operator->ppi_line_number == $closest_operator->next_sibling->ppi_line_number
Truth table0%0/5
A and B and C and D
A= builtin::blessed($closest_operator->next_sibling)B= $closest_operator->next_sibling->isa("PLS::Parser::Element")C= $closest_operator->next_sibling->type eq "PPI::Token::Word"D= $closest_operator->ppi_line_number == $closest_operator->next_sibling->ppi_line_number
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1282 and $closest_operator->next_sibling->isa('PLS::Parser::Element')
1283 and $closest_operator->next_sibling->type eq 'PPI::Token::Word'
1284 and $closest_operator->ppi_line_number == $closest_operator->next_sibling->ppi_line_number)
1285 {
1286 0 $filter = $closest_operator->next_sibling->name;
1287 0 $range = $closest_operator->next_sibling->range;
1288 } ## end if (blessed($closest_operator...))
1289  
1290 # if the previous element is a word, it's possibly a class name,
1291 # so we return that to use for searching for that class's methods.
1292 0 my $package = '';
1293 0 ▶ if ( blessed($closest_operator->previous_sibling)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($closest_operator->previous_sibling) and $closest_operator->previous_sibling->isa("PLS::Parser::Element") and $closest_operator->previous_sibling->type eq "PPI::Token::Word") 0 0 0
Condition0%0/6
builtin::blessed($closest_operator->previous_sibling) and $closest_operator->previous_sibling->isa("PLS::Parser::Element") and $closest_operator->previous_sibling->type eq "PPI::Token::Word"
!ll&&!rl&&r
000
builtin::blessed($closest_operator->previous_sibling) and $closest_operator->previous_sibling->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($closest_operator->previous_sibling) and $closest_operator->previous_sibling->isa("PLS::Parser::Element") and $closest_operator->previous_sibling->type eq "PPI::Token::Word"
builtin::blessed($closest_operator->previous_sibling) $closest_operator->previous_sibling->isa("PLS::Parser::Element") $closest_operator->previous_sibling->type eq "PPI::Token::Word"
Truth table0%0/4
A and B and C
A= builtin::blessed($closest_operator->previous_sibling)B= $closest_operator->previous_sibling->isa("PLS::Parser::Element")C= $closest_operator->previous_sibling->type eq "PPI::Token::Word"
ABCresult
0XX0
10X0
1100
1111
1294 and $closest_operator->previous_sibling->isa('PLS::Parser::Element')
1295 and $closest_operator->previous_sibling->type eq 'PPI::Token::Word')
1296 {
1297 0 $package = $closest_operator->previous_sibling->name;
1298 } ## end if (blessed($closest_operator...))
1299  
1300 # the 1 indicates that the current token is an arrow, due to the special logic needed.
1301 0 return $range, 1, $package, $filter;
1302 } ## end if (blessed($closest_operator...))
1303  
1304 # something like "Package::Name:", we just want Package::Name.
1305 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($element->name eq ":" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element") and $element->previous_sibling->type eq "PPI::Token::Word" || $element->previous_sibling->type eq "PPI::Token::Label") 0 0 0
Condition0%0/12
$element->name eq ":" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element") and $element->previous_sibling->type eq "PPI::Token::Word" || $element->previous_sibling->type eq "PPI::Token::Label"
!ll&&!rl&&r
000
$element->name eq ":" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element")
!ll&&!rl&&r
000
$element->name eq ":" and builtin::blessed($element->previous_sibling)
!ll&&!rl&&r
000
$element->previous_sibling->type eq "PPI::Token::Word" or $element->previous_sibling->type eq "PPI::Token::Label"
l!l&&r!l&&!r
000
MC/DC0%0/5
$element->name eq ":" and builtin::blessed($element->previous_sibling) and $element->previous_sibling->isa("PLS::Parser::Element") and $element->previous_sibling->type eq "PPI::Token::Word" || $element->previous_sibling->type eq "PPI::Token::Label"
$element->name eq ":" builtin::blessed($element->previous_sibling) $element->previous_sibling->isa("PLS::Parser::Element") $element->previous_sibling->type eq "PPI::Token::Word" $element->previous_sibling->type eq "PPI::Token::Label"
Truth table0%0/6
A and B and C and D or E
A= $element->name eq ":"B= builtin::blessed($element->previous_sibling)C= $element->previous_sibling->isa("PLS::Parser::Element")D= $element->previous_sibling->type eq "PPI::Token::Word"E= $element->previous_sibling->type eq "PPI::Token::Label"
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
1306 $element->name eq ':'
1307 and blessed($element->previous_sibling)
1308 and $element->previous_sibling->isa('PLS::Parser::Element')
1309 and ( $element->previous_sibling->type eq 'PPI::Token::Word'
1310 or $element->previous_sibling->type eq 'PPI::Token::Label')
1311 )
1312 {
1313 0 $element = $element->previous_sibling;
1314 } ## end if ($element->name eq ...)
1315  
1316 # modify the range so we don't overwrite anything after the cursor.
1317 0 my $range = $element->range;
1318 0 $range->{end}{character} = $character;
1319  
1320 # look at labels as well, because a label looks like a package name before the second colon.
1321 0 my $package = '';
1322  
1323 0 ▶ if ( $element->type eq 'PPI::Token::Word'
Branch: 1 branch
BranchTrueFalseTotal
if ($element->type eq "PPI::Token::Word" or $element->type eq "PPI::Token::Label") 0 0 0
Condition0%0/3
$element->type eq "PPI::Token::Word" or $element->type eq "PPI::Token::Label"
l!l&&r!l&&!r
000
MC/DC0%0/2
$element->type eq "PPI::Token::Word" or $element->type eq "PPI::Token::Label"
$element->type eq "PPI::Token::Word" $element->type eq "PPI::Token::Label"
Truth table0%0/3
A or B
A= $element->type eq "PPI::Token::Word"B= $element->type eq "PPI::Token::Label"
ABresult
1X1
011
000
1324 or $element->type eq 'PPI::Token::Label')
1325 {
1326 0 $package = $element->name;
1327 }
1328  
1329 0 my $name = $element->name;
1330 0 $name =~ s/:?:$//;
1331  
1332 0 return $range, 0, $package, $name;
1333 } ## end sub find_word_under_cursor
1334  
1335 =head2 get_list_index
1336  
1337 Gets the index within a list where a cursor is.
1338  
1339 This is useful for determining which function parameter the cursor is on
1340 within a function call.
1341  
1342 =cut
1343  
1344 sub get_list_index
1345 {
1346 0 ▶ my ($self, $list, $line, $character) = @_;
Subroutine get_list_index: not called CC=11 SCAR=48.8
Pod documented
1347  
1348 0 ▶ return 0 if (not blessed($list) or not $list->isa('PLS::Parser::Element') or $list->type ne 'PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
if not builtin::blessed($list) && $list->isa("PLS::Parser::Element") or $list->type ne "PPI::Structure::List" 0 0 0
Condition0%0/6
not builtin::blessed($list) && $list->isa("PLS::Parser::Element") or $list->type ne "PPI::Structure::List"
l!l&&r!l&&!r
000
builtin::blessed($list) && $list->isa('PLS::Parser::Element')
!ll&&!rl&&r
000
MC/DC0%0/3
not builtin::blessed($list) && $list->isa("PLS::Parser::Element") or $list->type ne "PPI::Structure::List"
builtin::blessed($list) $list->isa('PLS::Parser::Element') $list->type ne "PPI::Structure::List"
Truth table0%0/4
not(A && B) or C
A= builtin::blessed($list)B= $list->isa('PLS::Parser::Element')C= $list->type ne "PPI::Structure::List"
ABCresult
0XX1
10X1
1111
1100
1349  
1350 0 ▶ my $find = PPI::Find->new(sub { $_[0]->isa('PPI::Statement::Expression') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1351 0 my $expr;
1352 0 ▶ $expr = $find->match() if $find->start($list->element);
Branch: 1 branch
BranchTrueFalseTotal
if $find->start($list->element) 0 0 0
1353  
1354 0 ▶ return 0 if (not blessed($expr) or not $expr->isa('PPI::Statement::Expression'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($expr) and $expr->isa("PPI::Statement::Expression") 0 0 0
Condition0%0/3
builtin::blessed($expr) and $expr->isa("PPI::Statement::Expression")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($expr) and $expr->isa("PPI::Statement::Expression")
builtin::blessed($expr) $expr->isa("PPI::Statement::Expression")
Truth table0%0/3
A and B
A= builtin::blessed($expr)B= $expr->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
1355  
1356 0 ▶ my @commas = grep { $_->isa('PPI::Token::Operator') and $_ eq ',' } $expr->schildren;
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("PPI::Token::Operator") and $_ eq "," 0 0 0
1357  
1358 0 ▶ return 0 unless (scalar @commas);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @commas 0 0 0
1359  
1360 0 my $param_index = -1;
1361  
1362 0 foreach my $index (reverse 0 .. $#commas)
1363 {
1364 0 my $param = $commas[$index];
1365  
1366 0 ▶ if ($param->column_number <= $character)
Branch: 1 branch
BranchTrueFalseTotal
if ($param->column_number <= $character) 0 0 0
1367 {
1368 0 $param_index = $index;
1369 0 last;
1370 }
1371 } ## end foreach my $index (reverse ...)
1372  
1373 0 return $param_index + 1;
1374 } ## end sub get_list_index
1375  
1376 =head2 sort_imports
1377  
1378 This sorts the imports within a file. The order is:
1379  
1380 =over
1381  
1382 =item C<use strict> and C<use warnings>
1383  
1384 =item C<use parent> and C<use base>
1385  
1386 =item Other pragmas (excluding C<use constant>)
1387  
1388 =item Core and external imports
1389  
1390 =item Internal imports (from the current project)
1391  
1392 =item Constants (C<use constant>)
1393  
1394 =back
1395  
1396 =cut
1397  
1398 sub sort_imports
1399 {
1400 0 ▶ my ($self) = @_;
Subroutine sort_imports: not called CC=22 SCAR=62.3
Pod documented
1401  
1402 0 my $doc = $self->{document}->clone();
1403 0 my @installed = ExtUtils::Installed->new->modules;
1404  
1405 # Just strict and warnings - I like them to be first and in their own group
1406 0 my @special_pragmas;
1407  
1408 # parent and base - I like them to be after strict and warnings and in their own group.
1409 my @isa_pragmas;
1410  
1411 # The rest of the pragmas
1412 0 my @pragmas;
1413  
1414 # Group of any modules that are installed (either core or external)
1415 0 my @installed_modules;
1416  
1417 # Group of modules that are part of this project,
1418 # though it gets tricky if this project is also installed
1419 0 my @internal_modules;
1420  
1421 # Put constant pragmas at the very end of all imports
1422 0 my @constant_pragmas;
1423  
1424 0 my $insert_after;
1425  
1426 0 foreach my $child ($doc->children)
1427 {
1428 0 my $seqno;
1429 0 ▶ next unless ($seqno = ($child->isa('PPI::Statement::Include') .. (not $child->isa('PPI::Statement::Include') and not $child->isa('PPI::Token::Whitespace'))));
Branch: 1 branch
BranchTrueFalseTotal
unless $seqno = $child->isa("PPI::Statement::Include") .. !($child->isa("PPI::Statement::Include") || $child->isa("PPI::Token::Whitespace")) 0 0 0
Condition0%0/3
$child->isa('PPI::Statement::Include') || $child->isa('PPI::Token::Whitespace')
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->isa('PPI::Statement::Include') || $child->isa('PPI::Token::Whitespace')
$child->isa('PPI::Statement::Include') $child->isa('PPI::Token::Whitespace')
Truth table0%0/3
A || B
A= $child->isa('PPI::Statement::Include')B= $child->isa('PPI::Token::Whitespace')
ABresult
1X1
011
000
1430 0 ▶ last if ($seqno =~ /E0/);
Branch: 1 branch
BranchTrueFalseTotal
if $seqno =~ /E0/ 0 0 0
1431 0 ▶ $insert_after = $child->previous_sibling if ($seqno eq '1');
Branch: 1 branch
BranchTrueFalseTotal
if $seqno eq 1 0 0 0
1432  
1433 0 ▶ if ($child->isa('PPI::Token::Whitespace'))
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa("PPI::Token::Whitespace")) 0 0 0
1434 {
1435 0 $child->delete;
1436 0 next;
1437 }
1438  
1439 0 ▶ if ($child->pragma eq 'strict' or $child->pragma eq 'warnings')
Branch: 4 branches
BranchTrueFalseTotal
if ($child->pragma eq 'strict' or $child->pragma eq 'warnings') { } 0 0 0
elsif ($child->pragma eq 'parent' or $child->pragma eq 'base') { } 0 0 0
elsif ($child->pragma eq 'constant') { } 0 0 0
elsif (length $child->pragma) { } 0 0 0
Condition0%0/3
$child->pragma eq "strict" or $child->pragma eq "warnings"
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->pragma eq "strict" or $child->pragma eq "warnings"
$child->pragma eq "strict" $child->pragma eq "warnings"
Truth table0%0/3
A or B
A= $child->pragma eq "strict"B= $child->pragma eq "warnings"
ABresult
1X1
011
000
1440 {
1441 0 push @special_pragmas, $child;
1442 }
1443 ▶ elsif ($child->pragma eq 'parent' or $child->pragma eq 'base')
Condition0%0/3
$child->pragma eq "parent" or $child->pragma eq "base"
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->pragma eq "parent" or $child->pragma eq "base"
$child->pragma eq "parent" $child->pragma eq "base"
Truth table0%0/3
A or B
A= $child->pragma eq "parent"B= $child->pragma eq "base"
ABresult
1X1
011
000
1444 {
1445 0 push @isa_pragmas, $child;
1446 }
1447 elsif ($child->pragma eq 'constant')
1448 {
1449 0 push @constant_pragmas, $child;
1450 }
1451 elsif (length $child->pragma)
1452 {
1453 0 push @pragmas, $child;
1454 }
1455 else
1456 {
1457 0 ▶ if (Module::CoreList::is_core($child->module) or any { $child->module =~ /^\Q$_\E/ } @installed)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (Module::CoreList::is_core($child->module) or &any(sub { $child->module =~ /^\Q$_\E/; } , @installed)) { } 0 0 0
Condition0%0/3
Module::CoreList::is_core($child->module) or &any(sub {
l!l&&r!l&&!r
000
MC/DC0%0/2
Module::CoreList::is_core($child->module) or &any(sub { $child->module =~ /^\Q$_\E/; } , @installed)
Module::CoreList::is_core($child->module) &any(sub { $child->module =~ /^\Q$_\E/; } , @installed)
Truth table0%0/3
A or B
A= Module::CoreList::is_core($child->module)B= &any(sub { $child->module =~ /^\Q$_\E/; } , @installed)
ABresult
1X1
011
000
1458 {
1459 0 push @installed_modules, $child;
1460 }
1461 else
1462 {
1463 0 push @internal_modules, $child;
1464 }
1465 } ## end else [ if ($child->pragma eq ...)]
1466  
1467 0 $child->remove;
1468 } ## end foreach my $child ($doc->children...)
1469  
1470 0 ▶ @special_pragmas = _pad_imports(sort _sort_imports @special_pragmas) if (scalar @special_pragmas);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @special_pragmas 0 0 0
1471 0 ▶ @isa_pragmas = _pad_imports(sort _sort_imports @isa_pragmas) if (scalar @isa_pragmas);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @isa_pragmas 0 0 0
1472 0 ▶ @pragmas = _pad_imports(sort _sort_imports @pragmas) if (scalar @pragmas);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @pragmas 0 0 0
1473 0 ▶ @installed_modules = _pad_imports(sort _sort_imports @installed_modules) if (scalar @installed_modules);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @installed_modules 0 0 0
1474 0 ▶ @internal_modules = _pad_imports(sort _sort_imports @internal_modules) if (scalar @internal_modules);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @internal_modules 0 0 0
1475 0 ▶ @constant_pragmas = _pad_imports(sort _sort_imports @constant_pragmas) if (scalar @constant_pragmas);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @constant_pragmas 0 0 0
1476  
1477 # There doesn't seem to be a better way to do this other than to use this private method.
1478 0 $insert_after->__insert_after(@special_pragmas, @isa_pragmas, @pragmas, @installed_modules, @internal_modules, @constant_pragmas);
1479  
1480 0 open my $fh, '<', $self->get_full_text();
1481  
1482 0 my $lines;
1483  
1484 0 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
1485 {
1486 0 $lines = $.;
1487 }
1488  
1489 0 return \($doc->serialize), $lines;
1490 } ## end sub sort_imports
1491  
1492 =head2 _sort_imports
1493  
1494 Determines the sorting of two imports within a block of imports.
1495  
1496 =cut
1497  
1498 sub _sort_imports
1499 {
1500 0 ▶ return $b->type cmp $a->type || $a->module cmp $b->module;
Subroutine _sort_imports: not called CC=2 SCAR=17.9
Condition0%0/3
$b->type cmp $a->type || $a->module cmp $b->module
l!l&&r!l&&!r
000
MC/DC0%0/2
$b->type cmp $a->type || $a->module cmp $b->module
$b->type cmp $a->type $a->module cmp $b->module
Truth table0%0/3
A || B
A= $b->type cmp $a->typeB= $a->module cmp $b->module
ABresult
1X1
011
000
1501 }
1502  
1503 =head2 _pad_imports
1504  
1505 Adds newlines to pad the various import sections from each other and from
1506 the rest of the document.
1507  
1508 =cut
1509  
1510 sub _pad_imports
1511 {
1512 0 ▶ my @imports = @_;
Subroutine _pad_imports: not called CC=1 SCAR=6.9
1513  
1514 # Newlines between the imports
1515 0 @imports = map { $_, PPI::Token::Whitespace->new("\n") } @imports;
1516  
1517 # An extra newline at the end of the section
1518 0 push @imports, PPI::Token::Whitespace->new("\n");
1519  
1520 0 return @imports;
1521 } ## end sub _pad_imports
1522  
1523 =head2 _split_lines
1524  
1525 Splits a document into lines using C<$/> as the separator.
1526  
1527 =cut
1528  
1529 sub _split_lines
1530 {
1531 0 ▶ my ($text) = @_;
Subroutine _split_lines: not called CC=1 SCAR=6.9
1532  
1533 0 my $sep = $/;
1534 0 return split /(?<=$sep)/, $text;
1535 } ## end sub _split_lines
1536  
1537 1 1;
« PLS/Server/Response/Completion.pm ↑ Summary PLS/Server/Response/Resolve.pm »