PLS/Parser/Index.pm

filter: Statementstmt 21.0% 80 / 381 Branchbran 5.6% 7 / 124 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 39.2% 20 / 51 Podpod 0.0% 0 / 25 totaltotal 17.4% 107 / 614 CC 82 SCAR 38.1
CC
82
Cov
17%
__ANON__
132.0
get_subroutines
132.0
cleanup_file
90.0
CRAP 45.2
« PLS/Parser/Pod.pm ↑ Summary PLS/Server/Response/WorkspaceSymbols.pm »
1 package PLS::Parser::Index;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use IO::Async::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use PPR;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 PLS::Parser::Index
24  
25 =head1 DESCRIPTION
26  
27 This class caches and stores indexed data about the workspace.
28 It is used for quick searching of subroutines and packages by name.
29  
30 =cut
31  
32 sub new
33 {
34 53 ▶ my ($class, @args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
35  
36 53 state $self;
37 53 ▶ return $self if (ref $self eq 'PLS::Parser::Index');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "PLS::Parser::Index" 46 7 53
38  
39 7 my %args = @args;
40 $self = bless {
41 workspace_folders => $args{workspace_folders},
42 7 subs => {},
43 packages => {},
44 subs_by_package => {},
45 files => {},
46 ignored_files => {},
47 ignore_files_mtimes => {}
48 }, $class;
49  
50 7 return $self;
51 } ## end sub new
52  
53 sub workspace_folders
54 {
55 16 ▶ my ($self) = @_;
Subroutine workspace_folders: called CC=1 SCAR=0.0
Pod undocumented
56  
57 16 return $self->{workspace_folders};
58 }
59  
60 sub subs
61 {
62 0 ▶ my ($self) = @_;
Subroutine subs: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 0 return $self->{subs};
65 }
66  
67 sub packages
68 {
69 0 ▶ my ($self) = @_;
Subroutine packages: not called CC=1 SCAR=6.9
Pod undocumented
70  
71 0 return $self->{packages};
72 }
73  
74 sub subs_by_package
75 {
76 0 ▶ my ($self) = @_;
Subroutine subs_by_package: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 return $self->{subs_by_package};
79 }
80  
81 sub files
82 {
83 0 ▶ my ($self) = @_;
Subroutine files: not called CC=1 SCAR=6.9
Pod undocumented
84  
85 0 return $self->{files};
86 }
87  
88 sub _index_file
89 {
90 0 ▶ my ($uri, $text) = @_;
Subroutine _index_file: not called CC=2 SCAR=17.9
91  
92 0 ▶ $text = PLS::Parser::Document->text_from_uri($uri) if (ref $text ne 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $text ne "SCALAR" 0 0 0
93 0 my $line_offsets = PLS::Parser::Index->get_line_offsets($text);
94 0 my $packages = PLS::Parser::Index->get_packages($text, $uri, $line_offsets);
95 0 my $subroutines = PLS::Parser::Index->get_subroutines($text, $uri, $line_offsets);
96  
97 0 return $packages, $subroutines;
98 } ## end sub _index_file
99  
100 sub index_files
101 {
102 5 ▶ my ($self, @uris) = @_;
Subroutine index_files: called CC=3 SCAR=18.7
Pod undocumented
103  
104 5 state $function;
105  
106 5 ▶ if (ref $function ne 'IO::Async::Function')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $function ne "IO::Async::Function") 5 0 5
107 {
108 5 $function = IO::Async::Function->new(code => \&_index_file);
109 5 IO::Async::Loop->new->add($function);
110 }
111  
112 5 my $get_files_future;
113  
114 5 ▶ if (scalar @uris)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @uris) { } 0 5 5
115 {
116 0 $get_files_future = Future->done(\@uris);
117 }
118 else
119 {
120 5 $get_files_future = $self->get_all_perl_files_async();
121 }
122  
123 return $get_files_future->then(
124 sub {
125 5 ▶ my ($uris) = @_;
Subroutine __ANON__: called CC=3 SCAR=21.6
126  
127 5 my @futures;
128  
129 5 my %open_files = map { $_ => 1 } PLS::Parser::Document->open_files();
130 5 require PLS::Parser::Document;
131  
132 5 foreach my $uri (@{$uris})
133 {
134 # If the file is open, pass the current text to the function, since the other
135 # process will not have the most up-to-date document text.
136 # If the file is not open, allow the function to open the file and get the text.
137 0 my $version = PLS::Parser::Document::uri_version($uri);
138 0 ▶ my $text = length $version ? PLS::Parser::Document->text_from_uri($uri) : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $version ? : 0 0 0
139  
140 push @futures, $function->call(args => [$uri, $text])->then(
141 sub {
142 0 ▶ my ($packages, $subs) = @_;
Subroutine __ANON__: not called CC=11 SCAR=48.8
143  
144 0 my $current_version = PLS::Parser::Document::uri_version($uri);
145 0 ▶ return if (length $version and length $current_version and $current_version < $version);
Branch: 1 branch
BranchTrueFalseTotal
if length $version and length $current_version and $current_version < $version 0 0 0
Condition0%0/6
length $version and length $current_version and $current_version < $version
!ll&&!rl&&r
000
length $version and length $current_version
!ll&&!rl&&r
000
MC/DC0%0/3
length $version and length $current_version and $current_version < $version
length $version length $current_version $current_version < $version
Truth table0%0/4
A and B and C
A= length $versionB= length $current_versionC= $current_version < $version
ABCresult
0XX0
10X0
1100
1111
146  
147 0 my $file = URI->new($uri)->file;
148 0 ▶ return if $self->is_ignored($file);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ignored($file) 0 0 0
149  
150 0 ▶ $file = readlink $file if (-l $file);
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 0 0
151 0 ▶ return if $self->is_ignored($file);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ignored($file) 0 0 0
152  
153 0 $self->cleanup_file($file);
154  
155 0 foreach my $ref (keys %{$packages})
156 {
157 0 push @{$self->packages->{$ref}}, @{$packages->{$ref}};
158 0 push @{$self->files->{$file}{packages}}, $ref;
159 }
160  
161 0 foreach my $ref (keys %{$subs})
162 {
163 0 push @{$self->subs->{$ref}}, @{$subs->{$ref}};
164 0 push @{$self->files->{$file}{subs}}, $ref;
165  
166 0 foreach my $sub (@{$subs->{$ref}})
167 {
168 0 ▶ push @{$self->subs_by_package->{$sub->{package}}}, $ref if (length $sub->{package});
Branch: 1 branch
BranchTrueFalseTotal
if length $sub->{"package"} 0 0 0
169 }
170  
171 } ## end foreach my $ref (keys %{$subs...})
172  
173 0 return Future->done($file);
174 }
175 0 );
176 } ## end foreach my $uri (@{$uris})
177  
178 5 return Future->done(@futures);
179 }
180 5 )->retain();
181 } ## end sub index_files
182  
183 sub get_all_perl_files_async
184 {
185 5 ▶ my ($self, @folders) = @_;
Subroutine get_all_perl_files_async: called CC=4 SCAR=14.2
Pod undocumented
186  
187 5 ▶ @folders = @{$self->workspace_folders} unless (scalar @folders);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @folders 5 0 5
188 5 ▶ return Future->done([]) unless (scalar @folders);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @folders 0 5 5
189  
190 5 state $function;
191  
192 5 ▶ if (ref $function ne 'IO::Async::Function')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $function ne "IO::Async::Function") 5 0 5
193 {
194 5 $function = IO::Async::Function->new(code => \&get_all_perl_files);
195  
196 5 IO::Async::Loop->new->add($function);
197 } ## end if (ref $function ne 'IO::Async::Function'...)
198  
199 5 return $function->call(args => [$self, @folders]);
200 } ## end sub get_all_perl_files_async
201  
202 sub deindex_workspace
203 {
204 0 ▶ my ($self, $path) = @_;
Subroutine deindex_workspace: not called CC=3 SCAR=24.8
Pod undocumented
205  
206 0 @{$self->workspace_folders} = grep { $_ ne $path } @{$self->workspace_folders};
207  
208 0 foreach my $file (keys %{$self->files})
209 {
210 0 ▶ next unless path($path)->subsumes($file);
Branch: 1 branch
BranchTrueFalseTotal
unless path($path)->subsumes($file) 0 0 0
211  
212 0 $self->cleanup_file($file);
213 } ## end foreach my $file (keys %{$self...})
214  
215 0 return;
216 } ## end sub deindex_workspace
217  
218 sub index_workspace
219 {
220 0 ▶ my ($self, $path) = @_;
Subroutine index_workspace: not called CC=1 SCAR=6.9
Pod undocumented
221  
222 0 push @{$self->workspace_folders}, $path;
223  
224 $self->get_all_perl_files_async($path)->then(
225 sub {
226 0 ▶ my ($workspace_uris) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
227  
228 0 return $self->index_files(@{$workspace_uris});
229 }
230 0 ▶ )->then(sub { Future->wait_all(@_) })->retain();
Subroutine __ANON__: not called CC=1 SCAR=6.9
231  
232 0 return;
233 } ## end sub index_workspace
234  
235 sub cleanup_file
236 {
237 0 ▶ my ($self, $file) = @_;
Subroutine cleanup_file: not called CC=9 SCAR=45.0
Pod undocumented
238  
239 0 ▶ $self->files->{$file}{subs} = [] if (ref $self->files->{$file}{subs} ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->files->{$file}{"subs"} ne "ARRAY" 0 0 0
240 0 ▶ $self->files->{$file}{packages} = [] if (ref $self->files->{$file}{packages} ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->files->{$file}{"packages"} ne "ARRAY" 0 0 0
241  
242 0 foreach my $ref (@{$self->files->{$file}{subs}})
243 {
244 0 @{$self->subs->{$ref}} = grep { $_->{uri} ne URI::file->new($file)->as_string() } @{$self->subs->{$ref}};
245 0 ▶ delete $self->subs->{$ref} unless (scalar @{$self->subs->{$ref}});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->subs->{$ref};} 0 0 0
246 }
247  
248 0 foreach my $ref (@{$self->files->{$file}{packages}})
249 {
250 0 my @old_packages = @{$self->packages->{$ref}};
251 0 @{$self->packages->{$ref}} = ();
252  
253 0 foreach my $package (@old_packages)
254 {
255 0 ▶ if ($package->{uri} eq URI::file->new($file)->as_string())
Branch: 1 branch
BranchTrueFalseTotal
if ($package->{'uri'} eq 'URI::file'->new($file)->as_string) { } 0 0 0
256 {
257 0 delete $self->subs_by_package->{$ref};
258 }
259 else
260 {
261 0 push @{$self->packages->{$ref}}, $package;
262 }
263 } ## end foreach my $package (@old_packages...)
264  
265 0 ▶ delete $self->packages->{$ref} unless (scalar @{$self->packages->{$ref}});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->packages->{$ref};} 0 0 0
266 } ## end foreach my $ref (@{$self->files...})
267  
268 0 delete $self->files->{$file};
269  
270 0 return;
271 } ## end sub cleanup_file
272  
273 sub find_package_subroutine
274 {
275 0 ▶ my ($self, $package, $subroutine) = @_;
Subroutine find_package_subroutine: not called CC=4 SCAR=30.0
Pod undocumented
276  
277 0 my $locations = $self->packages->{$package};
278  
279 0 ▶ if (ref $locations ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $locations ne "ARRAY") 0 0 0
280 {
281 0 require PLS::Parser::Document;
282 0 my $external = PLS::Parser::Document->find_external_subroutine($package, $subroutine);
283 0 ▶ return [$external] if (ref $external eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $external eq "HASH" 0 0 0
284 0 return [];
285 } ## end if (ref $locations ne ...)
286  
287 0 my @subroutines;
288  
289 0 foreach my $file (@{$locations})
290 {
291 0 push @subroutines, @{$self->find_subroutine($subroutine, $file->{uri})};
292 }
293  
294 0 return \@subroutines;
295 } ## end sub find_package_subroutine
296  
297 sub find_subroutine
298 {
299 0 ▶ my ($self, $subroutine, @uris) = @_;
Subroutine find_subroutine: not called CC=3 SCAR=24.8
Pod undocumented
300  
301 0 my $found = $self->subs->{$subroutine};
302 0 ▶ return [] unless (ref $found eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $found eq "ARRAY" 0 0 0
303  
304 0 $found = Storable::dclone($found);
305 0 my %uris = map { $_ => 1 } @uris;
306 0 ▶ @{$found} = grep { $uris{$_->{uri}} } @{$found} if (scalar @uris);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @uris 0 0 0
307  
308 0 return $found;
309 } ## end sub find_subroutine
310  
311 sub find_package
312 {
313 0 ▶ my ($self, $package) = @_;
Subroutine find_package: not called CC=3 SCAR=24.8
Pod undocumented
314  
315 0 my $found = $self->packages->{$package};
316  
317 0 ▶ if (ref $found ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $found ne "ARRAY") 0 0 0
318 {
319 0 require PLS::Parser::Document;
320 0 my $external = PLS::Parser::Document->find_external_package($package);
321 0 ▶ return [$external] if (ref $external eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $external eq "HASH" 0 0 0
322 0 return [];
323 } ## end if (ref $found ne 'ARRAY'...)
324  
325 0 return Storable::dclone($found);
326 } ## end sub find_package
327  
328 sub get_ignored_files
329 {
330 0 ▶ my ($self) = @_;
Subroutine get_ignored_files: not called CC=8 SCAR=42.8
Pod undocumented
331  
332 0 foreach my $workspace_folder (@{$self->workspace_folders})
333 {
334 0 my $plsignore = File::Spec->catfile($workspace_folder, '.plsignore');
335 0 ▶ next if (not -f $plsignore or not -r $plsignore);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $plsignore and -r $plsignore 0 0 0
Condition0%0/3
-f $plsignore and -r $plsignore
!ll&&!rl&&r
000
MC/DC0%0/2
-f $plsignore and -r $plsignore
-f $plsignore -r $plsignore
Truth table0%0/3
A and B
A= -f $plsignoreB= -r $plsignore
ABresult
0X0
100
111
336  
337 0 my $mtime = stat($plsignore)->mtime;
338 0 ▶ next if (length $self->{ignore_files_mtimes}{$plsignore} and $self->{ignore_file_mtimes}{$plsignore} >= $mtime);
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"ignore_files_mtimes"}{$plsignore} and $self->{"ignore_file_mtimes"}{$plsignore} >= $mtime 0 0 0
Condition0%0/3
length $self->{"ignore_files_mtimes"}{$plsignore} and $self->{"ignore_file_mtimes"}{$plsignore} >= $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->{"ignore_files_mtimes"}{$plsignore} and $self->{"ignore_file_mtimes"}{$plsignore} >= $mtime
length $self->{"ignore_files_mtimes"}{$plsignore} $self->{"ignore_file_mtimes"}{$plsignore} >= $mtime
Truth table0%0/3
A and B
A= length $self->{"ignore_files_mtimes"}{$plsignore}B= $self->{"ignore_file_mtimes"}{$plsignore} >= $mtime
ABresult
0X0
100
111
339  
340 0 ▶ open my $fh, '<', $plsignore or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $plsignore or next 0 0 0
341  
342 0 $self->{ignored_files}{$plsignore} = [];
343 0 $self->{ignore_file_mtimes}{$plsignore} = $mtime;
344  
345 0 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
346 {
347 0 chomp $line;
348 0 push @{$self->{ignored_files}{$plsignore}}, glob File::Spec->catfile($workspace_folder, $line);
349 }
350  
351 0 @{$self->{ignored_files}{$plsignore}} = map { path($_)->realpath } @{$self->{ignored_files}{$plsignore}};
352 } ## end foreach my $workspace_folder...
353  
354 0 return [map { @{$self->{ignored_files}{$_}} } keys %{$self->{ignored_files}}];
355 } ## end sub get_ignored_files
356  
357 sub get_all_packages
358 {
359 0 ▶ my ($self) = @_;
Subroutine get_all_packages: not called CC=2 SCAR=17.9
Pod undocumented
360  
361 0 ▶ return [] if (ref $self->packages ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->packages ne "HASH" 0 0 0
362 0 return [keys %{$self->packages}];
363 } ## end sub get_all_packages
364  
365 sub get_all_fully_qualified_subroutines
366 {
367 0 ▶ my ($self) = @_;
Subroutine get_all_fully_qualified_subroutines: not called CC=4 SCAR=30.0
Pod undocumented
368  
369 0 ▶ return [] if (ref $self->subs_by_package ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->subs_by_package ne "HASH" 0 0 0
370  
371 0 my @subroutines;
372  
373 0 foreach my $package (keys %{$self->subs_by_package})
374 {
375 0 foreach my $subroutine (@{$self->subs_by_package->{$package}})
376 {
377 0 push @subroutines, "${package}::${subroutine}";
378 }
379 } ## end foreach my $package (keys %...)
380  
381 0 return \@subroutines;
382 } ## end sub get_all_fully_qualified_subroutines
383  
384 sub is_ignored
385 {
386 0 ▶ my ($self, $file) = @_;
Subroutine is_ignored: not called CC=4 SCAR=30.0
Pod undocumented
387  
388 0 my @ignore_files = @{$self->get_ignored_files()};
389 0 ▶ return unless (scalar @ignore_files);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @ignore_files 0 0 0
390  
391 0 my $real_path = path($file)->realpath;
392  
393 0 ▶ return 1 if any { $_ eq $real_path } @ignore_files;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $real_path; } , @ignore_files) 0 0 0
394 0 ▶ return 1 if any { $_->subsumes($real_path) } @ignore_files;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_->subsumes($real_path); } , @ignore_files) 0 0 0
395  
396 0 return;
397 } ## end sub is_ignored
398  
399 sub get_all_perl_files
400 {
401 0 ▶ my ($self, @folders) = @_;
Subroutine get_all_perl_files: not called CC=3 SCAR=24.8
Pod undocumented
402  
403 0 ▶ @folders = @{$self->workspace_folders} unless (scalar @folders);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @folders 0 0 0
404 0 ▶ return [] unless (scalar @folders);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @folders 0 0 0
405  
406 0 my @perl_files;
407  
408 File::Find::find(
409 {
410 preprocess => sub {
411 0 ▶ return () if $self->is_ignored($File::Find::dir);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ignored($File::Find::dir) 0 0 0
412 0 return grep { not $self->is_ignored($_) } @_;
413 },
414 wanted => sub {
415 0 ▶ return unless $self->is_perl_file($File::Find::name);
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_perl_file($File::Find::name) 0 0 0
416 0 my @pieces = File::Spec->splitdir($File::Find::name);
417  
418 # exclude hidden files and files in hidden directories
419 0 ▶ return if any { /^\./ } @pieces;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { /^\./; } , @pieces) 0 0 0
420  
421 0 push @perl_files, $File::Find::name;
422 }
423 },
424 @folders
425 0 );
426  
427 0 return [map { URI::file->new($_)->as_string } @perl_files];
428 } ## end sub get_all_perl_files
429  
430 sub is_perl_file
431 {
432 0 ▶ my ($class, $file) = @_;
Subroutine is_perl_file: not called CC=9 SCAR=45.0
Pod undocumented
433  
434 0 ▶ return if -l $file;
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 0 0
435 0 ▶ return unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
436 0 ▶ return if any { /^\.pls-tmp/ } grep { length } File::Spec->splitdir($file);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { /^\.pls-tmp/; } , grep({length $_;} "File::Spec"->splitdir($file))) 0 0 0
437 0 ▶ return if $file =~ /\.t$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.t$/ 0 0 0
438  
439 0 ▶ return 1 if $file =~ /\.p[lm]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /\.p[lm]$/ 0 0 0
440 0 ▶ open my $fh, '<', $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 0 0
441 0 my $first_line = <$fh>;
442 0 close $fh;
443 0 ▶ return 1 if (length $first_line and $first_line =~ /^\s*#!.*perl$/);
Branch: 1 branch
BranchTrueFalseTotal
if length $first_line and $first_line =~ /^\s*#!.*perl$/ 0 0 0
Condition0%0/3
length $first_line and $first_line =~ /^\s*#!.*perl$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $first_line and $first_line =~ /^\s*#!.*perl$/
length $first_line $first_line =~ /^\s*#!.*perl$/
Truth table0%0/3
A and B
A= length $first_lineB= $first_line =~ /^\s*#!.*perl$/
ABresult
0X0
100
111
444 0 return;
445 } ## end sub is_perl_file
446  
447 sub log
448 {
449 0 ▶ my (undef, $message) = @_;
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
450  
451 0 my $time = Time::Piece->new;
452 0 $time = $time->ymd . ' ' . $time->hms;
453 0 print {\*STDERR} "[$time] $message\n";
454  
455 0 return;
456 } ## end sub log
457  
458 sub get_line_offsets
459 {
460 0 ▶ my ($class, $text) = @_;
Subroutine get_line_offsets: not called CC=2 SCAR=17.9
Pod undocumented
461  
462 0 my @line_offsets = (0);
463  
464 0 ▶ while ($$text =~ /\r?\n/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\r?\n/g) 0 0 0
465 {
466 0 push @line_offsets, pos($$text);
467 }
468  
469 0 return \@line_offsets;
470 } ## end sub get_line_offsets
471  
472 sub get_line_by_offset
473 {
474 0 ▶ my ($class, $line_offsets, $offset) = @_;
Subroutine get_line_by_offset: not called CC=6 SCAR=37.4
Pod undocumented
475  
476 0 ▶ for (my $i = 0 ; $i <= $#{$line_offsets} ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$line_offsets;}) 0 0 0
477 {
478 0 my $current_offset = $line_offsets->[$i];
479 0 ▶ my $next_offset = $i + 1 <= $#{$line_offsets} ? $line_offsets->[$i + 1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$i + 1 <= $#{$line_offsets;} ? : 0 0 0
480  
481 0 ▶ if ($current_offset <= $offset and (not defined $next_offset or $next_offset > $offset))
Branch: 1 branch
BranchTrueFalseTotal
if ($current_offset <= $offset and !defined($next_offset) || $next_offset > $offset) 0 0 0
Condition0%0/6
$current_offset <= $offset and !defined($next_offset) || $next_offset > $offset
!ll&&!rl&&r
000
not defined $next_offset or $next_offset > $offset
l!l&&r!l&&!r
000
MC/DC0%0/3
$current_offset <= $offset and !defined($next_offset) || $next_offset > $offset
$current_offset <= $offset not defined $next_offset $next_offset > $offset
Truth table0%0/4
A and not(B) or C
A= $current_offset <= $offsetB= not defined $next_offsetC= $next_offset > $offset
ABCresult
0XX0
1000
11X1
1011
482 {
483 0 return $i;
484 }
485 } ## end for (my $i = 0 ; $i <= ...)
486  
487 0 return $#{$line_offsets};
488 } ## end sub get_line_by_offset
489  
490 sub get_packages
491 {
492 0 ▶ my ($class, $text, $uri, $line_offsets) = @_;
Subroutine get_packages: not called CC=3 SCAR=24.8
Pod undocumented
493  
494 0 state $rx = qr/((?&PerlPackageDeclaration))$PPR::GRAMMAR/x;
495 0 my %packages;
496  
497 0 my $file = URI->new($uri)->file;
498 0 ▶ $file = readlink $file if (-l $file);
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 0 0
499 0 $uri = URI::file->new($file)->as_string();
500  
501 0 ▶ while ($$text =~ /$rx/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$rx/g) 0 0 0
502 {
503 0 my $name = $1;
504  
505 0 my $end = pos($$text);
506 0 my $start = $end - length $name;
507 0 my $start_line = $class->get_line_by_offset($line_offsets, $start);
508 0 $start -= $line_offsets->[$start_line];
509 0 my $end_line = $class->get_line_by_offset($line_offsets, $end);
510 0 $end -= $line_offsets->[$end_line];
511  
512 0 $name =~ s/package//;
513 0 $name =~ s/;\s*$//g;
514 0 $name =~ s/^\s+|\s+$//g;
515  
516 0 push @{$packages{$name}},
517 {
518 uri => $uri,
519 range => {
520 start => {
521 line => $start_line,
522 character => $start
523 },
524 end => {
525 line => $end_line,
526 character => $end
527 }
528 }
529 };
530 } ## end while ($$text =~ /$rx/g)
531  
532 0 return \%packages;
533 } ## end sub get_packages
534  
535 sub get_subroutines
536 {
537 0 ▶ my ($class, $text, $uri, $line_offsets) = @_;
Subroutine get_subroutines: not called CC=11 SCAR=48.8
Pod undocumented
538  
539 0 my $file = URI->new($uri)->file;
540 0 ▶ $file = readlink $file if (-l $file);
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 0 0
541 0 $uri = URI::file->new($file)->as_string();
542  
543 # Stolen mostly from PPR definition for PerlSubroutineDeclaration
544 0 state $sub_rx = qr/
545 (?<full>
546 (?<declaration>(?>
547 (?: (?> my | our | state ) \b (?>(?&PerlOWS)) )?+
548 sub \b (?>(?&PerlOWS))
549 (?<name>(?>(?&PerlOldQualifiedIdentifier))) (?&PerlOWS)
550 |
551 (?<name>AUTOLOAD) (?&PerlOWS)
552 |
553 (?<name>DESTROY) (?&PerlOWS)
554 ))
555 (?:
556 # Perl pre 5.028
557 (?:
558 (?>
559 (?<params>(?<label>(?&PerlParenthesesList))) # Parameter list
560 |
561 \( [^)]*+ \) # Prototype (
562 )
563 (?&PerlOWS)
564 )?+
565 (?: (?>(?&PerlAttributes)) (?&PerlOWS) )?+
566 |
567 # Perl post 5.028
568 (?: (?>(?&PerlAttributes)) (?&PerlOWS) )?+
569 (?<params>(?<label>(?: (?>(?&PerlParenthesesList)) (?&PerlOWS) )?+)) # Parameter list
570 )
571 (?> ; | \{
572 (?&PerlOWS)
573 (?<label>(?<params>(?&PerlVariableDeclaration))(?&PerlOWS)=(?&PerlOWS)\@_;?)?
574 (?&PerlOWS)
575 (?>(?&PerlStatementSequence))
576 \} )
577 )
578 $PPR::GRAMMAR/x;
579  
580 0 state $var_rx = qr/((?&PerlVariable)|undef)$PPR::GRAMMAR/;
581 0 state $package_rx = qr/((?&PerlPackageDeclaration))$PPR::GRAMMAR/;
582  
583 0 my %subroutines;
584  
585 0 ▶ while ($$text =~ /$sub_rx/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$sub_rx/g) 0 0 0
586 {
587 0 my $end = pos($$text);
588 0 my $start = $end - length $+{full};
589 0 $end = $start + length $+{declaration};
590  
591 0 my $start_line = $class->get_line_by_offset($line_offsets, $start);
592 0 $start -= $line_offsets->[$start_line];
593 0 my $end_line = $class->get_line_by_offset($line_offsets, $end);
594 0 $end -= $line_offsets->[$end_line];
595  
596 0 my $signature = $+{label};
597 0 my @parameters;
598  
599 0 ▶ if (length $+{params})
Branch: 1 branch
BranchTrueFalseTotal
if (length $+{"params"}) 0 0 0
600 {
601 0 my $parameters = $+{params};
602 0 ▶ while ($parameters =~ /$var_rx/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($parameters =~ /$var_rx/g) 0 0 0
603 {
604 0 push @parameters, {label => $1};
605 }
606 } ## end if (length $+{params})
607  
608 0 my $name = $+{name};
609  
610 # Look for package declaration anywhere from the start of the document
611 # to the subroutine declaration.
612 0 my $package;
613  
614 0 ▶ if (substr($$text, 0, pos($$text)) =~ /$package_rx/)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$text, 0, pos $$text) =~ /$package_rx/) 0 0 0
615 {
616 0 ($package) = $1 =~ /^package\s+(.+)\s*;\s*$/;
617 }
618  
619 0 push @{$subroutines{$name}},
620 {
621 uri => $uri,
622 range => {
623 start => {
624 line => $start_line,
625 character => $start
626 },
627 end => {
628 line => $end_line,
629 character => $end
630 }
631 },
632 signature => {label => $signature, parameters => \@parameters},
633 'package' => $package,
634 kind => 3
635 };
636 } ## end while ($$text =~ /$sub_rx/g...)
637  
638 0 state $block_rx = qr/use\h+constant(?&PerlOWS)((?&PerlBlock))$PPR::GRAMMAR/;
639 0 state $bareword_rx = qr/((?&PerlBareword))(?&PerlOWS)(?&PerlComma)$PPR::GRAMMAR/;
640 0 state $one_constant_rx = qr/use\h+constant\h+((?&PerlBareword))(?&PerlOWS)(?&PerlComma)$PPR::GRAMMAR/;
641  
642 0 ▶ while ($$text =~ /$block_rx/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$block_rx/g) 0 0 0
643 {
644 0 my $block = $1;
645 0 my $block_end = $+[1];
646 0 my $block_start = $-[1];
647  
648 # Look for package declaration anywhere from the start of the document
649 # to the constant declaration
650 0 my $package;
651  
652 0 ▶ if (substr($$text, 0, pos($$text)) =~ /$package_rx/)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$text, 0, pos $$text) =~ /$package_rx/) 0 0 0
653 {
654 0 ($package) = $1 =~ /^package\s+(.+)\s*;\s*$/;
655 }
656  
657 0 ▶ while ($block =~ /$bareword_rx/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($block =~ /$bareword_rx/g) 0 0 0
658 {
659 0 my $bareword = $1;
660 0 my $bareword_end = $+[1];
661 0 my $bareword_start = $-[1];
662  
663 0 $bareword_start += $block_start;
664 0 $bareword_end += $block_start;
665  
666 0 my $start_line = $class->get_line_by_offset($line_offsets, $bareword_start);
667 0 $bareword_start -= $line_offsets->[$start_line];
668 0 my $end_line = $class->get_line_by_offset($line_offsets, $bareword_end);
669 0 $bareword_end -= $line_offsets->[$end_line];
670  
671 0 push @{$subroutines{$bareword}}, {
672 uri => $uri,
673 range => {
674 start => {
675 line => $start_line,
676 character => $bareword_start
677 },
678 end => {
679 line => $end_line,
680 character => $bareword_end
681 }
682 },
683 'package' => $package,
684 kind => 21 # constant kind
685 };
686 } ## end while ($block =~ /$bareword_rx/g...)
687 } ## end while ($$text =~ /$block_rx/g...)
688  
689 0 ▶ while ($$text =~ /$one_constant_rx/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /$one_constant_rx/g) 0 0 0
690 {
691 0 my $bareword = $1;
692 0 my $end = $+[1];
693 0 my $start = $-[1];
694  
695 0 my $start_line = $class->get_line_by_offset($line_offsets, $start);
696 0 $start -= $line_offsets->[$start_line];
697 0 my $end_line = $class->get_line_by_offset($line_offsets, $end);
698 0 $end -= $line_offsets->[$end_line];
699  
700 # Look for package declaration anywhere from the start of the document
701 # to the constant declaration
702 0 my $package;
703  
704 0 ▶ if (substr($$text, 0, pos($$text)) =~ /$package_rx/)
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$text, 0, pos $$text) =~ /$package_rx/) 0 0 0
705 {
706 0 ($package) = $1 =~ /^package\s+(.+)\s*;\s*$/;
707 }
708  
709 0 push @{$subroutines{$bareword}}, {
710 uri => $uri,
711 range => {
712 start => {
713 line => $start_line,
714 character => $start
715 },
716 end => {
717 line => $end_line,
718 character => $end
719 }
720 },
721 'package' => $package,
722 kind => 21 # constant kind
723 };
724 } ## end while ($$text =~ /$one_constant_rx/g...)
725  
726 0 return \%subroutines;
727 } ## end sub get_subroutines
728  
729 11 1;
« PLS/Parser/Pod.pm ↑ Summary PLS/Server/Response/WorkspaceSymbols.pm »