PLS/Server/Request/Diagnostics/PublishDiagnostics.pm

filter: Statementstmt 39.7% 37 / 93 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 3 totaltotal 31.6% 49 / 155 CC 21 SCAR 43.5
CC
21
Cov
28%
get_compilation_errors
182.0
get_perlcritic_errors
42.0
new
20.0
CRAP 77.4
« PLS/Parser/Element.pm ↑ Summary PLS/Server/Method/TextDocument.pm »
1 package PLS::Server::Request::Diagnostics::PublishDiagnostics;
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 parent 'PLS::Server::Request';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Perl::Critic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use PLS::Parser::Pod;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use PLS::Server::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 PLS::Server::Request::Diagnostics::PublishDiagnostics
22  
23 =head1 DESCRIPTION
24  
25 This is a message from the server to the client requesting that
26 diagnostics be published.
27  
28 These diagnostics currently include compilation errors and linting (using L<perlcritic>).
29  
30 =cut
31  
32 sub new
33 {
34 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
35  
36 0 my $uri = $args{uri};
37 0 my $path = URI->new($uri);
38  
39 0 ▶ return unless (ref $path eq 'URI::file');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "URI::file" 0 0 0
40 0 $path = $path->file;
41  
42 0 my @diagnostics;
43  
44 0 ▶ if (not $args{close})
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"close"}) 0 0 0
45 {
46 0 @diagnostics = (@{get_compilation_errors($path)});
47 0 ▶ push @diagnostics, @{get_perlcritic_errors($path)} if $PLS::Server::State::CONFIG->{perlcritic}{enabled};
Branch: 1 branch
BranchTrueFalseTotal
if $PLS::Server::State::CONFIG->{"perlcritic"}{"enabled"} 0 0 0
48 }
49  
50 0 my $self = {
51 method => 'textDocument/publishDiagnostics',
52 params => {
53 uri => $uri,
54 diagnostics => \@diagnostics
55 },
56 notification => 1 # indicates to the server that this should not be assigned an id, and that there will be no response
57 };
58  
59 0 return bless $self, $class;
60 } ## end sub new
61  
62 sub get_compilation_errors
63 {
64 0 ▶ my ($path) = @_;
Subroutine get_compilation_errors: not called CC=13 SCAR=52.0
Pod undocumented
65  
66 0 my $inc = PLS::Parser::Pod->get_clean_inc();
67 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
68  
69 0 my @line_lengths;
70 0 ▶ my $pid = open my $fh, '<', $path or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open(my $fh, "<", $path) or return [] 0 0 0
71  
72 0 ▶ while (my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
73 {
74 0 chomp $line;
75 0 $line_lengths[$.] = length $line;
76 }
77  
78 0 waitpid $pid, 0;
79  
80 0 ▶ $pid = open3 my $in, my $out, my $err = gensym, $^X, @inc, '-c', $path or return [];
Branch: 1 branch
BranchTrueFalseTotal
$pid = open3(my $in, my $out, my $err = &gensym(), $^X, @inc, "-c", $path) or return [] 0 0 0
81 0 close $in;
82 0 close $out;
83  
84 0 my @diagnostics;
85  
86 0 ▶ while (my $line = <$err>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $err)) 0 0 0
87 {
88 0 chomp $line;
89 0 ▶ next if $line =~ /syntax OK$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /syntax OK$/ 0 0 0
90  
91 # Hide warnings from circular references
92 0 ▶ next if $line =~ /Subroutine .+ redefined/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /Subroutine .+ redefined/ 0 0 0
93  
94 # Hide "BEGIN failed" and "Compilation failed" messages - these provide no useful info.
95 0 ▶ next if $line =~ /^BEGIN failed/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^BEGIN failed/ 0 0 0
96 0 ▶ next if $line =~ /^Compilation failed/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^Compilation failed/ 0 0 0
97 0 ▶ if (my ($error, $file, $line, $area) = $line =~ /^(.+) at (.+) line (\d+)(, .+)?/)
Branch: 1 branch
BranchTrueFalseTotal
if (() = $line =~ /^(.+) at (.+) line (\d+)(, .+)?/) 0 0 0
98 {
99 0 ▶ $error .= $area if (length $area);
Branch: 1 branch
BranchTrueFalseTotal
if length $area 0 0 0
100 0 $line = int $line;
101 0 ▶ next if $file ne $path;
Branch: 1 branch
BranchTrueFalseTotal
if $file ne $path 0 0 0
102  
103 0 push @diagnostics,
104 {
105 range => {
106 start => {line => $line - 1, character => 0},
107 end => {line => $line - 1, character => $line_lengths[$line]}
108 },
109 message => $error,
110 severity => 1,
111 source => 'perl',
112 };
113 } ## end if (my ($error, $file,...))
114 } ## end while (my $line = <$err>)
115  
116 0 waitpid $pid, 0;
117  
118 0 return \@diagnostics;
119 } ## end sub get_compilation_errors
120  
121 sub get_perlcritic_errors
122 {
123 0 ▶ my ($path) = @_;
Subroutine get_perlcritic_errors: not called CC=6 SCAR=37.4
Pod undocumented
124  
125 0 my ($profile) = glob $PLS::Server::State::CONFIG->{perlcritic}{perlcriticrc};
126 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
127 0 my $critic = Perl::Critic->new(-profile => $profile);
128 0 my @violations = $critic->critique($path);
129  
130 0 my @diagnostics;
131  
132 0 foreach my $violation (@violations)
133 {
134 0 my $severity = 5 - $violation->severity;
135 0 ▶ $severity = 1 unless ($severity);
Branch: 1 branch
BranchTrueFalseTotal
unless $severity 0 0 0
136  
137 0 my $doc = URI->new();
138 0 $doc->scheme('https');
139 0 $doc->authority('metacpan.org');
140 0 $doc->path('pod/' . $violation->policy);
141  
142 0 push @diagnostics,
143 {
144 range => {
145 start => {line => $violation->line_number - 1, character => $violation->column_number - 1},
146 end => {line => $violation->line_number - 1, character => $violation->column_number + length($violation->source) - 1}
147 },
148 message => $violation->description,
149 code => $violation->policy,
150 codeDescription => {href => $doc->as_string},
151 severity => $severity,
152 source => 'perlcritic'
153 };
154 } ## end foreach my $violation (@violations...)
155  
156 0 return \@diagnostics;
157 } ## end sub get_perlcritic_errors
158  
159 1 1;
« PLS/Parser/Element.pm ↑ Summary PLS/Server/Method/TextDocument.pm »