PLS/Server/Request/TextDocument/PublishDiagnostics.pm

filter: Statementstmt 34.3% 55 / 160 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 62.9% 17 / 27 Podpod 0.0% 0 / 4 totaltotal 25.9% 72 / 277 CC 34 SCAR 39.7
CC
34
Cov
24%
new
132.0
get_compilation_errors
110.0
__ANON__
56.0
CRAP 52.8
« PLS/Server/Method/TextDocument.pm ↑ Summary PLS/Parser/Pod/Package.pm »
1 package PLS::Server::Request::TextDocument::PublishDiagnostics;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use parent 'PLS::Server::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use IO::Async::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use IO::Async::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Perl::Critic;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 ▶use PLS::Parser::Pod;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use PLS::Server::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 PLS::Server::Request::TextDocument::PublishDiagnostics
27  
28 =head1 DESCRIPTION
29  
30 This is a message from the server to the client requesting that
31 diagnostics be published.
32  
33 These diagnostics currently include compilation errors and linting (using L<perlcritic>).
34  
35 =cut
36  
37 9 my $function = IO::Async::Function->new(code => \&run_perlcritic);
38  
39 9 my $loop = IO::Async::Loop->new();
40 9 $loop->add($function);
41  
42 sub new
43 {
44 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=11 SCAR=48.8
Pod undocumented
45  
46 0 ▶ return if (ref $PLS::Server::State::CONFIG ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $PLS::Server::State::CONFIG ne "HASH" 0 0 0
47  
48 0 my $uri = URI->new($args{uri});
49 0 ▶ return if (ref $uri ne 'URI::file');
Branch: 1 branch
BranchTrueFalseTotal
if ref $uri ne "URI::file" 0 0 0
50  
51 0 my $self = bless {
52 method => 'textDocument/publishDiagnostics',
53 params => {
54 uri => $uri->as_string,
55 diagnostics => []
56 },
57 notification => 1
58 },
59 $class;
60  
61 0 my (undef, $dir) = File::Basename::fileparse($uri->file);
62  
63 0 my $source = $uri->file;
64 0 my $text = PLS::Parser::Document->text_from_uri($uri->as_string);
65 0 ▶ $source = $text if (ref $text eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $text eq "SCALAR" 0 0 0
66 0 my $version = PLS::Parser::Document::uri_version($uri->as_string);
67 0 my $client_has_version_support = $PLS::Server::State::CLIENT_CAPABILITIES->{textDocument}{publishDiagnostics}{versionSupport};
68 0 ▶ $self->{params}{version} = $version if (length $version and $client_has_version_support);
Branch: 1 branch
BranchTrueFalseTotal
if length $version and $client_has_version_support 0 0 0
Condition0%0/3
length $version and $client_has_version_support
!ll&&!rl&&r
000
MC/DC0%0/2
length $version and $client_has_version_support
length $version $client_has_version_support
Truth table0%0/3
A and B
A= length $versionB= $client_has_version_support
ABresult
0X0
100
111
69  
70 # If closing, return empty list of diagnostics.
71 0 ▶ return Future->done($self) if $args{close};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"close"} 0 0 0
72  
73 0 my @futures;
74  
75 0 ▶ push @futures, get_compilation_errors($source, $dir) if (defined $PLS::Server::State::CONFIG->{syntax}{enabled} and $PLS::Server::State::CONFIG->{syntax}{enabled});
Branch: 1 branch
BranchTrueFalseTotal
if defined $PLS::Server::State::CONFIG->{"syntax"}{"enabled"} and $PLS::Server::State::CONFIG->{"syntax"}{"enabled"} 0 0 0
Condition0%0/3
defined $PLS::Server::State::CONFIG->{"syntax"}{"enabled"} and $PLS::Server::State::CONFIG->{"syntax"}{"enabled"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $PLS::Server::State::CONFIG->{"syntax"}{"enabled"} and $PLS::Server::State::CONFIG->{"syntax"}{"enabled"}
defined $PLS::Server::State::CONFIG->{"syntax"}{"enabled"} $PLS::Server::State::CONFIG->{"syntax"}{"enabled"}
Truth table0%0/3
A and B
A= defined $PLS::Server::State::CONFIG->{"syntax"}{"enabled"}B= $PLS::Server::State::CONFIG->{"syntax"}{"enabled"}
ABresult
0X0
100
111
76 push @futures, get_perlcritic_errors($source, $uri->file)
77 0 ▶ if (defined $PLS::Server::State::CONFIG->{perlcritic}{enabled} and $PLS::Server::State::CONFIG->{perlcritic}{enabled});
Branch: 1 branch
BranchTrueFalseTotal
if defined $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"} and $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"} 0 0 0
Condition0%0/3
defined $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"} and $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"} and $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"}
defined $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"} $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"}
Truth table0%0/3
A and B
A= defined $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"}B= $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"}
ABresult
0X0
100
111
78  
79 return Future->wait_all(@futures)->then(
80 sub {
81 0 ▶ my $current_version = PLS::Parser::Document::uri_version($uri->as_string);
Subroutine __ANON__: not called CC=4 SCAR=30.0
82  
83 # No version will be returned if the document has been closed.
84 # Since the only way we got here is if the document is open, we
85 # should return nothing, since any diagnostics we return will be from
86 # when the document was still open.
87 0 ▶ return Future->done(undef) unless (length $current_version);
Branch: 1 branch
BranchTrueFalseTotal
unless length $current_version 0 0 0
88  
89 # If the document has been updated since the diagnostics were created,
90 # send nothing back. The next update will re-trigger the diagnostics.
91 0 ▶ return Future->done(undef) if (length $version and $current_version > $version);
Branch: 1 branch
BranchTrueFalseTotal
if length $version and $current_version > $version 0 0 0
Condition0%0/3
length $version and $current_version > $version
!ll&&!rl&&r
000
MC/DC0%0/2
length $version and $current_version > $version
length $version $current_version > $version
Truth table0%0/3
A and B
A= length $versionB= $current_version > $version
ABresult
0X0
100
111
92  
93 0 @{$self->{params}{diagnostics}} = map { $_->result } @_;
94  
95 0 return Future->done($self);
96 }
97 0 );
98 } ## end sub new
99  
100 sub get_compilation_errors
101 {
102 0 ▶ my ($source, $dir) = @_;
Subroutine get_compilation_errors: not called CC=10 SCAR=47.0
Pod undocumented
103  
104 0 my $temp;
105 0 my $future = $loop->new_future();
106  
107 0 my $fh;
108 my $path;
109  
110 0 ▶ if (ref $source eq 'SCALAR')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq 'SCALAR') { } 0 0 0
111 {
112 0 $temp = eval { File::Temp->new(CLEANUP => 0, TEMPLATE => '.pls-tmp-XXXXXXXXXX', DIR => $dir) };
113 0 ▶ $temp = eval { File::Temp->new(CLEANUP => 0) } if (ref $temp ne 'File::Temp');
Branch: 1 branch
BranchTrueFalseTotal
if ref $temp ne "File::Temp" 0 0 0
114 0 $path = $temp->filename;
115  
116 0 ▶ $future->on_done(sub { unlink $temp });
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 my $source_text = Encode::encode_utf8($$source);
118  
119 0 print {$temp} $source_text;
120 0 close $temp;
121  
122 0 open $fh, '<', \$source_text;
123 } ## end if (ref $source eq 'SCALAR'...)
124 else
125 {
126 0 $path = $source;
127 0 ▶ open $fh, '<', $path or return [];
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $path or return [] 0 0 0
128 }
129  
130 0 my @line_lengths;
131  
132 0 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
133 {
134 0 chomp $line;
135 0 $line_lengths[$.] = length $line;
136 }
137  
138 0 close $fh;
139  
140 0 my $perl = PLS::Parser::Pod->get_perl_exe();
141 0 my $inc = PLS::Parser::Pod->get_clean_inc();
142 0 my $args = PLS::Parser::Pod->get_perl_args();
143 0 ▶ my @inc = map { "-I$_" } @{$inc // []};
Condition0%0/2
$inc // []
l!l
00
MC/DC0%0/1
$inc // []
$inc
Truth table0%0/2
A
A= $inc
Aresult
11
00
144 0 my $index = PLS::Parser::Index->new();
145 0 ▶ my $workspace_folder = List::Util::first { path($_)->subsumes($path) } @{$index->workspace_folders};
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 ▶ ($workspace_folder) = @{$index->workspace_folders} unless (length $workspace_folder);
Branch: 1 branch
BranchTrueFalseTotal
unless length $workspace_folder 0 0 0
147 0 ▶ my $new_cwd = $PLS::Server::State::CONFIG->{cwd} // '';
Condition0%0/2
$PLS::Server::State::CONFIG->{"cwd"} // ""
l!l
00
MC/DC0%0/1
$PLS::Server::State::CONFIG->{"cwd"} // ""
$PLS::Server::State::CONFIG->{"cwd"}
Truth table0%0/2
A
A= $PLS::Server::State::CONFIG->{"cwd"}
Aresult
11
00
148 0 $new_cwd =~ s/\$ROOT_PATH/$workspace_folder/;
149  
150 0 my @setup;
151 0 ▶ push @setup, (chdir => $new_cwd) if (length $new_cwd and -d $new_cwd);
Branch: 1 branch
BranchTrueFalseTotal
if length $new_cwd and -d $new_cwd 0 0 0
Condition0%0/3
length $new_cwd and -d $new_cwd
!ll&&!rl&&r
000
MC/DC0%0/2
length $new_cwd and -d $new_cwd
length $new_cwd -d $new_cwd
Truth table0%0/3
A and B
A= length $new_cwdB= -d $new_cwd
ABresult
0X0
100
111
152  
153 0 my @diagnostics;
154  
155 my $proc = IO::Async::Process->new(
156 0 command => [$perl, @inc, '-c', $path, @{$args}],
157 setup => \@setup,
158 stderr => {
159 on_read => sub {
160 0 ▶ my ($stream, $buffref, $eof) = @_;
Subroutine __ANON__: not called CC=7 SCAR=40.3
161  
162 0 ▶ while ($$buffref =~ s/^(.*)\n//)
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffref =~ s/^(.*)\n//) 0 0 0
163 {
164 0 my $line = $1;
165 0 ▶ next if $line =~ /syntax OK$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /syntax OK$/ 0 0 0
166  
167 # Hide warnings from circular references
168 0 ▶ next if $line =~ /Subroutine .+ redefined/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /Subroutine .+ redefined/ 0 0 0
169  
170 # Hide "BEGIN failed" and "Compilation failed" messages - these provide no useful info.
171 #next if $line =~ /^BEGIN failed/;
172 #next if $line =~ /^Compilation failed/;
173 0 ▶ if (my ($error, $file, $line, $area) = $line =~ /^(.+) at (.+?) line (\d+)(, .+)?/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /^(.+) at (.+?) line (\d+)(, .+)?/) 0 0 0
174 {
175 0 ▶ $error .= $area if (length $area);
Branch: 1 branch
BranchTrueFalseTotal
if length $area 0 0 0
176 0 $line = int $line;
177 0 ▶ next if $file ne $path;
Branch: 1 branch
BranchTrueFalseTotal
if $file ne $path 0 0 0
178  
179 0 push @diagnostics,
180 {
181 range => {
182 start => {line => $line - 1, character => 0},
183 end => {line => $line - 1, character => $line_lengths[$line]}
184 },
185 message => $error,
186 severity => 1,
187 source => 'perl',
188 };
189 } ## end if (my ($error, $file,...))
190  
191 } ## end while ($$buffref =~ s/^(.*)\n//...)
192  
193 0 return 0;
194 }
195 },
196 stdout => {
197 on_read => sub {
198 0 ▶ my ($stream, $buffref) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
199  
200 # Discard STDOUT, otherwise it might interfere with the server execution.
201 # This can happen if there is a BEGIN block that prints to STDOUT.
202 0 $$buffref = '';
203 0 return 0;
204 }
205 },
206 on_finish => sub {
207 0 ▶ $future->done(@diagnostics);
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 }
209 0 );
210  
211 0 $loop->add($proc);
212  
213 0 return $future;
214 } ## end sub get_compilation_errors
215  
216 sub get_perlcritic_errors
217 {
218 0 ▶ my ($source, $path) = @_;
Subroutine get_perlcritic_errors: not called CC=4 SCAR=30.0
Pod undocumented
219  
220 0 my ($profile) = glob $PLS::Server::State::CONFIG->{perlcritic}{perlcriticrc};
221 0 ▶ undef $profile if (not length $profile or not -f $profile or not -r $profile);
Branch: 1 branch
BranchTrueFalseTotal
unless length $profile and -f $profile and -r $profile 0 0 0
Condition0%0/6
length $profile and -f $profile and -r $profile
!ll&&!rl&&r
000
length $profile and -f $profile
!ll&&!rl&&r
000
MC/DC0%0/3
length $profile and -f $profile and -r $profile
length $profile -f $profile -r $profile
Truth table0%0/4
A and B and C
A= length $profileB= -f $profileC= -r $profile
ABCresult
0XX0
10X0
1100
1111
222  
223 0 return $function->call(args => [$profile, $source, $path]);
224 } ## end sub get_perlcritic_errors
225  
226 sub run_perlcritic
227 {
228 0 ▶ my ($profile, $source, $path) = @_;
Subroutine run_perlcritic: not called CC=3 SCAR=24.8
Pod undocumented
229  
230 0 my $critic = Perl::Critic->new(-profile => $profile);
231 0 my %args;
232 0 ▶ $args{filename} = $path if (ref $source eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $source eq "SCALAR" 0 0 0
233 0 my $doc = PPI::Document->new($source, %args);
234 0 my @violations = eval { $critic->critique($doc) };
235  
236 0 my @diagnostics;
237  
238 # Mapping from perlcritic severity to LSP severity
239 0 my %severity_map = (
240 5 => 1,
241 4 => 1,
242 3 => 2,
243 2 => 3,
244 1 => 3
245 );
246  
247 0 foreach my $violation (@violations)
248 {
249 0 my $severity = $severity_map{$violation->severity};
250  
251 0 my $doc = URI->new();
252 0 $doc->scheme('https');
253 0 $doc->authority('metacpan.org');
254 0 $doc->path('pod/' . $violation->policy);
255  
256 0 push @diagnostics,
257 {
258 range => {
259 start => {line => $violation->line_number - 1, character => $violation->column_number - 1},
260 end => {line => $violation->line_number - 1, character => $violation->column_number + length($violation->source) - 1}
261 },
262 message => $violation->description,
263 code => $violation->policy,
264 codeDescription => {href => $doc->as_string},
265 severity => $severity,
266 source => 'perlcritic'
267 };
268 } ## end foreach my $violation (@violations...)
269  
270 0 return @diagnostics;
271 } ## end sub run_perlcritic
272  
273 9 1;
« PLS/Server/Method/TextDocument.pm ↑ Summary PLS/Parser/Pod/Package.pm »