PLS/Parser/PackageSymbols.pm

filter: Statementstmt 73.0% 57 / 78 Branchbran 21.4% 3 / 14 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 60.0% 15 / 25 Podpod 0.0% 0 / 6 totaltotal 56.2% 77 / 137 CC 12 SCAR 13.9
CC
12
Cov
61%
get_package_symbols
12.0
get_imported_package_symbols
12.0
__ANON__
6.0
CRAP 4.0
« PLS/Server/Request/Workspace/ExecuteCommand.pm ↑ Summary PLS/Server/Request/TextDocument/DidChange.pm »
1 package PLS::Parser::PackageSymbols;
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 11 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use IO::Async::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 ▶use PLS::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use PLS::Parser::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use PLS::Parser::Pod;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 PLS::Parser::PackageSymbols
18  
19 =head1 DESCRIPTION
20  
21 This package executes a Perl process to import a package and interrogate
22 its symbol table to find all of the symbols in the package.
23  
24 =cut
25  
26 11 my $package_symbols_process;
27 my $imported_symbols_process;
28  
29 sub get_package_symbols
30 {
31 0 ▶ my ($config, @packages) = @_;
Subroutine get_package_symbols: not called CC=3 SCAR=24.8
Pod undocumented
32  
33 0 ▶ return Future->done({}) unless (scalar @packages);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @packages 0 0 0
34  
35 0 ▶ start_package_symbols_process($config) if (ref $package_symbols_process ne 'IO::Async::Process');
Branch: 1 branch
BranchTrueFalseTotal
if ref $package_symbols_process ne "IO::Async::Process" 0 0 0
36  
37 0 ▶ return $package_symbols_process->stdin->write(encode_json(\@packages) . "\n")->then(sub { $package_symbols_process->stdout->read_until("\n") })->then(
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 sub {
39 0 ▶ my ($json) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
40  
41 0 ▶ return Future->done(eval { decode_json $json } // {});
Condition0%0/2
eval { // {}
l!l
00
MC/DC0%0/1
eval { do { &decode_json($json) } } // {}
eval { do { &decode_json($json) } }
Truth table0%0/2
A
A= eval { do { &decode_json($json) } }
Aresult
11
00
42 },
43 0 ▶ sub { Future->done({}) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 );
45 } ## end sub get_package_symbols
46  
47 sub get_imported_package_symbols
48 {
49 0 ▶ my ($config, @imports) = @_;
Subroutine get_imported_package_symbols: not called CC=3 SCAR=24.8
Pod undocumented
50  
51 0 ▶ return Future->done({}) unless (scalar @imports);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @imports 0 0 0
52  
53 0 ▶ start_imported_package_symbols_process($config) if (ref $imported_symbols_process ne 'IO::Async::Process');
Branch: 1 branch
BranchTrueFalseTotal
if ref $imported_symbols_process ne "IO::Async::Process" 0 0 0
54  
55 0 ▶ return $imported_symbols_process->stdin->write(encode_json(\@imports) . "\n")->then(sub { $imported_symbols_process->stdout->read_until("\n") })->then(
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 sub {
57 0 ▶ my ($json) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
58  
59 0 ▶ return Future->done(eval { decode_json $json } // {});
Condition0%0/2
eval { // {}
l!l
00
MC/DC0%0/1
eval { do { &decode_json($json) } } // {}
eval { do { &decode_json($json) } }
Truth table0%0/2
A
A= eval { do { &decode_json($json) } }
Aresult
11
00
60 },
61 0 ▶ sub { Future->done({}) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 );
63 } ## end sub get_imported_package_symbols
64  
65 sub _start_process
66 {
67 6 ▶ my ($config, $code) = @_;
Subroutine _start_process: called CC=1 SCAR=0.1
68  
69 6 my $perl = PLS::Parser::Pod->get_perl_exe();
70 6 my @inc = map { "-I$_" } @{$config->{inc}};
71 6 my $args = PLS::Parser::Pod->get_perl_args();
72 my $process = IO::Async::Process->new(
73 6 command => [$perl, @inc, '-e', $code, @{$args}],
74 setup => _get_setup($config),
75 stdin => {via => 'pipe_write'},
76 stdout => {
77 0 ▶ on_read => sub { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 },
79 ▶ on_finish => sub { }
Subroutine __ANON__: not called CC=1 SCAR=0.0
80 6 );
81  
82 6 IO::Async::Loop->new->add($process);
83  
84 6 return $process;
85 } ## end sub _start_process
86  
87 sub start_package_symbols_process
88 {
89 3 ▶ my ($config) = @_;
Subroutine start_package_symbols_process: called CC=2 SCAR=7.6
Pod undocumented
90  
91 3 ▶ eval { $package_symbols_process->kill('TERM') } if (ref $package_symbols_process eq 'IO::Async::Process');
Branch: 1 branch
BranchTrueFalseTotal
if ref $package_symbols_process eq "IO::Async::Process" 0 3 3
92 3 $package_symbols_process = _start_process($config, get_package_symbols_code());
93 } ## end sub start_package_symbols_process
94  
95 sub start_imported_package_symbols_process
96 {
97 3 ▶ my ($config) = @_;
Subroutine start_imported_package_symbols_process: called CC=2 SCAR=7.0
Pod undocumented
98  
99 3 ▶ eval { $imported_symbols_process->kill('TERM') } if (ref $package_symbols_process eq 'IO::Async::Process');
Branch: 1 branch
BranchTrueFalseTotal
if ref $package_symbols_process eq "IO::Async::Process" 3 0 3
100 3 $imported_symbols_process = _start_process($config, get_imported_package_symbols_code());
101 } ## end sub start_imported_package_symbols_process
102  
103 sub _get_setup
104 {
105 6 ▶ my ($config) = @_;
Subroutine _get_setup: called CC=4 SCAR=14.6
106  
107 # Just use the first workspace folder as ROOT_PATH - we don't know
108 # which folder the code will ultimately be in, and it doesn't really matter
109 # for anyone except me.
110 6 my ($workspace_folder) = @{PLS::Parser::Index->new->workspace_folders};
111 6 ▶ my $cwd = $config->{cwd} // '';
Condition50%1/2
$config->{"cwd"} // ""
l!l
60
MC/DC0%0/1
$config->{"cwd"} // ""
$config->{"cwd"}
Truth table50%1/2
A
A= $config->{"cwd"}
Aresult
11
00
112 6 $cwd =~ s/\$ROOT_PATH/$workspace_folder/;
113 6 my @setup;
114 6 ▶ push @setup, (chdir => $cwd) if (length $cwd and -d $cwd);
Branch: 1 branch
BranchTrueFalseTotal
if length $cwd and -d $cwd 0 6 6
Condition33%1/3
length $cwd and -d $cwd
!ll&&!rl&&r
600
MC/DC0%0/2
length $cwd and -d $cwd
length $cwd -d $cwd
Truth table33%1/3
A and B
A= length $cwdB= -d $cwd
ABresult
0X0
100
111
115  
116 6 return \@setup;
117 } ## end sub _get_setup
118  
119 sub get_package_symbols_code
120 {
121 3 ▶ my $code = <<'EOF';
Subroutine get_package_symbols_code: called CC=1 SCAR=0.0
Pod undocumented
122 close STDERR;
123  
124 use IO::Handle;
125 use JSON::PP;
126 use B;
127  
128 STDOUT->autoflush();
129  
130 my $json = JSON::PP->new->utf8;
131  
132 package PackageSymbols;
133  
134 while (my $line = <STDIN>)
135 {
136 my $packages_to_find = $json->decode($line);
137 my %functions;
138  
139 foreach my $find_package (@{$packages_to_find})
140 {
141 my @module_parts = split /::/, $find_package;
142 my @parent_module_parts = @module_parts;
143 pop @parent_module_parts;
144  
145 my @packages;
146  
147 foreach my $parts (\@parent_module_parts, \@module_parts)
148 {
149 my $package = join '::', @{$parts};
150 next unless (length $package);
151  
152 eval "require $package";
153 next if (length $@);
154  
155 push @packages, $package;
156  
157 my @isa = add_parent_classes($package);
158  
159 foreach my $isa (@isa)
160 {
161 eval "require $isa";
162 next if (length $@);
163 push @packages, $isa;
164 } ## end foreach my $isa (@isa)
165 } ## end foreach my $parts (\@parent_module_parts...)
166  
167 foreach my $package (@packages)
168 {
169 my @parts = split /::/, $package;
170 my $ref = \%{"${package}::"};
171  
172 foreach my $name (keys %{$ref})
173 {
174 next if $name =~ /^BEGIN|UNITCHECK|INIT|CHECK|END|VERSION|import|unimport$/;
175  
176 my $code_ref = $package->can($name);
177 next if (ref $code_ref ne 'CODE');
178 my $defined_in = eval { B::svref_2object($code_ref)->GV->STASH->NAME };
179 next if ($defined_in ne $package and not $package->isa($defined_in));
180  
181 if ($find_package->isa($package))
182 {
183 push @{$functions{$find_package}}, $name;
184 }
185 else
186 {
187 push @{$functions{$package}}, $name;
188 }
189 } ## end foreach my $name (keys %{$ref...})
190  
191 # Unrequire packages
192 my $package_path = $package =~ s/::/\//gr;
193 $package_path .= '.pm';
194 delete $INC{$package_path};
195 } ## end foreach my $package (@packages...)
196 } ## end foreach my $find_package (@...)
197  
198 print $json->encode(\%functions);
199 print "\n";
200 } ## end while (my $packages_to_find...)
201  
202 sub add_parent_classes
203 {
204 my ($package) = @_;
205  
206 my @isa = eval "\@${package}::ISA";
207 return unless (scalar @isa);
208  
209 foreach my $isa (@isa)
210 {
211 push @isa, add_parent_classes($isa);
212 }
213  
214 return @isa;
215 } ## end sub add_parent_classes
216 EOF
217  
218 3 return $code;
219 } ## end sub get_package_symbols_code
220  
221 sub get_imported_package_symbols_code
222 {
223 3 ▶ my $code = <<'EOF';
Subroutine get_imported_package_symbols_code: called CC=1 SCAR=0.0
Pod undocumented
224 close STDERR;
225  
226 use IO::Handle;
227 use JSON::PP;
228  
229 STDOUT->autoflush();
230  
231 my $json = JSON::PP->new->utf8;
232  
233 package ImportedPackageSymbols;
234  
235 my %mtimes;
236 my %inc;
237 my %symbol_cache;
238  
239 while (my $line = <STDIN>)
240 {
241 my $imports = $json->decode($line);
242  
243 my %functions;
244  
245 foreach my $import (@{$imports})
246 {
247 if (-f $inc{$import->{module}} and $mtimes{$import->{use}} and (stat $inc{$import->{module}})[9] == $mtimes{$import->{use}} and ref $symbol_cache{$import->{use}} eq 'HASH')
248 {
249 foreach my $subroutine (keys %{$symbol_cache{$import->{use}}})
250 {
251 $functions{$import->{module}}{$subroutine} = 1;
252 }
253 next;
254 } ## end if (-f $inc{$import->{...}})
255  
256 my %symbol_table_before = %ImportedPackageSymbols::;
257 eval $import->{use};
258 my %symbol_table_after = %ImportedPackageSymbols::;
259 delete @symbol_table_after{keys %symbol_table_before};
260  
261 $functions{$import->{module}} = {};
262  
263 foreach my $subroutine (keys %symbol_table_after)
264 {
265 # Constants are created as scalar refs in the symbol table
266 next if (ref $symbol_table_after{$subroutine} ne 'SCALAR' and ref $symbol_table_after{$subroutine} ne 'GLOB' and ref \($symbol_table_after{$subroutine}) ne 'GLOB');
267 next if ((ref $symbol_table_after{$subroutine} eq 'GLOB' or ref \($symbol_table_after{$subroutine}) eq 'GLOB') and ref *{$symbol_table_after{$subroutine}}{CODE} ne 'CODE');
268 $functions{$import->{module}}{$subroutine} = 1;
269 } ## end foreach my $subroutine (keys...)
270  
271 # Reset symbol table and %INC
272 %ImportedPackageSymbols:: = %symbol_table_before;
273 my $module_path = $import->{module} =~ s/::/\//gr;
274 $module_path .= '.pm';
275  
276 $mtimes{$import->{use}} = (stat $INC{$module_path})[9];
277 $inc{$import->{module}} = $INC{$module_path};
278 $symbol_cache{$import->{use}} = $functions{$import->{module}};
279  
280 delete $INC{$module_path};
281 } ## end foreach my $import (@{$imports...})
282  
283 foreach my $module (keys %functions)
284 {
285 $functions{$module} = [keys %{$functions{$module}}];
286 }
287  
288 print $json->encode(\%functions);
289 print "\n";
290 } ## end while (my $line = <STDIN>...)
291 EOF
292  
293 3 return $code;
294 } ## end sub get_imported_package_symbols_code
295  
296 11 1;
« PLS/Server/Request/Workspace/ExecuteCommand.pm ↑ Summary PLS/Server/Request/TextDocument/DidChange.pm »