LanguageServer.pm

filter: Statementstmt 25.7% 82 / 318 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 46.5% 20 / 43 Podpod 12.5% 1 / 8 totaltotal 18.0% 103 / 571 CC 93 SCAR 51.4
CC
93
Cov
16%
mainloop
457.6
__ANON__
377.2
call_method
180.2
CRAP 170.4
« LanguageServer/Parser.pm ↑ Summary LanguageServer/Methods/workspace.pm »
1 package Perl::LanguageServer;
2
3 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Util qw( apply_all_roles );
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 ▶use Coro ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Coro::AIO ;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Coro::Handle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use AnyEvent::Socket ;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON ;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dump qw{dump pp} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::Select ;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 1 ▶use Perl::LanguageServer::Req ;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Perl::LanguageServer::Workspace ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20
21 1 with 'Perl::LanguageServer::Methods' ;
22 1 with 'Perl::LanguageServer::IO' ;
23
24 1 ▶no warnings 'uninitialized' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 =head1 NAME
27
28 Perl::LanguageServer - Language Server and Debug Protocol Adapter for Perl
29
30 =head1 VERSION
31
32 Version 2.5.0
33
34 =cut
35
36 1 our $VERSION = '2.6.2';
37
38
39 =head1 SYNOPSIS
40
41 This is a Language Server and Debug Protocol Adapter for Perl
42
43 It implements the Language Server Protocol which provides
44 syntax-checking, symbol search, etc. Perl to various editors, for
45 example Visual Studio Code or Atom.
46
47 L<https://microsoft.github.io/language-server-protocol/specification>
48
49 It also implements the Debug Adapter Protocol, which allow debugging
50 with various editors/includes
51
52 L<https://microsoft.github.io/debug-adapter-protocol/overview>
53
54 Should work with any Editor/IDE that support the Language-Server-Protocol.
55
56 To use both with Visual Studio Code, install the extension "perl"
57
58 Any comments and patches are welcome.
59
60 =cut
61
62 1 our $json = JSON -> new -> utf8(1) -> ascii(1) ;
63 1 our $jsonpretty = JSON -> new -> utf8(1) -> ascii(1) -> pretty (1) ;
64
65 1 our %running_reqs ;
66 1 our %running_coros ;
67 1 our $exit ;
68 1 our $workspace ;
69 1 our $dev_tool ;
70 1 our $debug1 = 0 ;
71 1 our $debug2 = 0 ;
72 1 our $log_file ;
73 1 our $client_version ;
74 1 our $reqseq = 1_000_000_000 ;
75
76
77 has 'channel' =>
78 (
79 is => 'ro',
80 isa => 'Coro::Channel',
81 0 ▶ default => sub { Coro::Channel -> new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 1 ) ;
83
84 1 has 'debug' =>
85 (
86 is => 'rw',
87 isa => 'Int',
88 default => 1,
89 ) ;
90
91 1 has 'listen_port' =>
92 (
93 is => 'rw',
94 isa => 'Maybe[Int]',
95 ) ;
96
97 has 'roles' =>
98 (
99 is => 'rw',
100 isa => 'HashRef',
101 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 1 ) ;
103
104 has 'out_semaphore' =>
105 (
106 is => 'ro',
107 isa => 'Coro::Semaphore',
108 0 ▶ default => sub { Coro::Semaphore -> new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 1 ) ;
110
111 1 has 'log_prefix' =>
112 (
113 is => 'rw',
114 isa => 'Str',
115 default => 'LS',
116 ) ;
117
118 1 has 'log_req_txt' =>
119 (
120 is => 'rw',
121 isa => 'Str',
122 default => '---> Request: ',
123 ) ;
124
125 # ---------------------------------------------------------------------------
126
127 sub logger
128 0 ▶ {
Subroutine logger: not called CC=8 SCAR=42.8
Pod undocumented
129 0 my $self = shift ;
130 0 my $src ;
131 0 ▶ if (!defined ($_[0]) || ref ($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0] or ref $_[0]) 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
132 {
133 0 $src = shift ;
134 }
135 0 ▶ $src = $self if (!$src) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 0 0
136
137 0 ▶ if ($log_file)
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::LanguageServer::log_file) { } 0 0 0
138 {
139 0 ▶ open my $fh, '>>', $log_file or warn "$log_file : $!" ;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $Perl::LanguageServer::log_file or warn "$Perl::LanguageServer::log_file : $!" 0 0 0
140 0 ▶ print $fh $src?$src -> log_prefix . ': ':'', @_ ;
Branch: 1 branch
BranchTrueFalseTotal
$src ? : 0 0 0
141 0 close $fh ;
142 }
143 else
144 {
145 0 ▶ print STDERR $src?$src -> log_prefix . ': ':'', @_ ;
Branch: 1 branch
BranchTrueFalseTotal
$src ? : 0 0 0
146 }
147 }
148
149
150 # ---------------------------------------------------------------------------
151
152 sub send_notification
153 0 ▶ {
Subroutine send_notification: not called CC=4 SCAR=26.3
Pod undocumented
154 0 my ($self, $notification, $src, $txt) = @_ ;
155
156 0 ▶ $txt ||= "<--- Notification: " ;
Condition0%0/2
$txt ||= "<--- Notification: "
l!l
00
MC/DC0%0/1
$txt ||= "<--- Notification: "
$txt
Truth table0%0/2
A
A= $txt
Aresult
11
00
157 0 $notification -> {jsonrpc} = '2.0' ;
158 0 my $outdata = $json -> encode ($notification) ;
159 0 my $guard = $self -> out_semaphore -> guard ;
160 1 ▶ use bytes ;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 0 my $len = length($outdata) ;
162 0 my $wrdata = "Content-Length: $len\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n$outdata" ;
163 0 $self -> _write ($wrdata) ;
164 0 ▶ if ($debug1)
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::LanguageServer::debug1) 0 0 0
165 {
166 0 $wrdata =~ s/\r//g ;
167 0 ▶ $self -> logger ($src, $txt, $jsonpretty -> encode ($notification), "\n") if ($debug1) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug1 0 0 0
168 }
169 }
170
171 # ---------------------------------------------------------------------------
172
173 sub call_method
174 0 ▶ {
Subroutine call_method: not called CC=14 SCAR=51.9
Pod undocumented
175 0 my ($self, $reqdata, $req, $id) = @_ ;
176
177 0 ▶ my $method = $req -> is_dap?$reqdata -> {command}:$reqdata -> {method} ;
Branch: 1 branch
BranchTrueFalseTotal
$req->is_dap ? : 0 0 0
178 0 my $module ;
179 my $name ;
180
181 0 ▶ if ($method =~ /^(\w+)\/(\w+)$/)
Branch: 3 branches
BranchTrueFalseTotal
if ($method =~ m[^(\w+)/(\w+)$]u) { } 0 0 0
elsif ($method =~ /^(\w+)$/u) { } 0 0 0
elsif ($method =~ m[^\$/(\w+)$]u) { } 0 0 0
182 {
183 0 $module = $1 ;
184 0 $name = $2 ;
185 }
186 elsif ($method =~ /^(\w+)$/)
187 {
188 0 $name = $1 ;
189 }
190 elsif ($method =~ /^\$\/(\w+)$/)
191 {
192 0 $name = $1 ;
193 }
194 else
195 {
196 0 die "Unknown method $method" ;
197 }
198 0 ▶ $module = $req -> type eq 'dbgint'?'DebugAdapterInterface':'DebugAdapter' if ($req -> is_dap) ;
Branch: 2 branches
BranchTrueFalseTotal
if $req->is_dap 0 0 0
$req->type eq 'dbgint' ? : 0 0 0
199
200 0 my $base_package = __PACKAGE__ . '::Methods' ;
201 0 my $package = $base_package ;
202 0 ▶ $package .= '::' . $module if ($module) ;
Branch: 1 branch
BranchTrueFalseTotal
if $module 0 0 0
203
204 0 my $fn = $package . '.pm' ;
205 0 $fn =~ s/::/\//g ;
206 0 ▶ if (!exists $INC{$fn} || !exists $self -> roles -> {$fn})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$fn} and exists $self->roles->{$fn}) 0 0 0
Condition0%0/3
exists $INC{$fn} and exists $self->roles->{$fn}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $INC{$fn} and exists $self->roles->{$fn}
exists $INC{$fn} exists $self->roles->{$fn}
Truth table0%0/3
A and B
A= exists $INC{$fn}B= exists $self->roles->{$fn}
ABresult
0X0
100
111
207 {
208 #$self -> logger (dump (\%INC), "\n") ;
209 0 $self -> logger ("apply_all_roles ($self, $package, $fn)\n") ;
210 0 apply_all_roles ($self, $package) ;
211 0 $self -> roles -> {$fn} = 1 ;
212 }
213
214 0 my $perlmethod ;
215 0 ▶ if ($req -> is_dap)
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_dap) { } 0 0 0
216 {
217 0 $perlmethod = '_dapreq_' . $name ;
218 }
219 else
220 {
221 0 ▶ $perlmethod = (defined($id)?'_rpcreq_':'_rpcnot_') . $name ;
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 0 0 0
222 }
223 0 ▶ $self -> logger ("method=$perlmethod\n") if ($debug1) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug1 0 0 0
224 0 ▶ die "Unknown perlmethod $perlmethod" if (!$self -> can ($perlmethod)) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($perlmethod) 0 0 0
225
226 1 ▶no strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 0 return $self -> $perlmethod ($workspace, $req) ;
228 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 }
230
231 # ---------------------------------------------------------------------------
232
233 sub process_req
234 0 ▶ {
Subroutine process_req: not called CC=2 SCAR=17.2
Pod undocumented
235 0 my ($self, $id, $reqdata) = @_ ;
236
237 0 my $xid = $id ;
238 0 ▶ $xid ||= $reqseq++ ;
Condition0%0/3
$xid ||= $Perl::LanguageServer::reqseq++
l!l&&r!l&&!r
000
MC/DC0%0/2
$xid ||= $Perl::LanguageServer::reqseq++
$xid $Perl::LanguageServer::reqseq++
Truth table0%0/3
A ||= B
A= $xidB= $Perl::LanguageServer::reqseq++
ABresult
1X1
011
000
239 $running_coros{$xid} = async
240 0 ▶ {
Subroutine __ANON__: not called CC=20 SCAR=59.3
241 my $req_guard = Guard::guard
242 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
243 0 ▶ $self -> logger ("done handle_req id=$xid\n") if ($debug1) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug1 0 0 0
244 0 delete $running_reqs{$xid} ;
245 0 delete $running_coros{$xid} ;
246 0 };
247
248 0 my $type = $reqdata -> {type} ;
249 0 ▶ my $is_dap = $type?1:0 ;
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
250 0 ▶ $type = defined ($id)?'request':'notification' if (!$type) ;
Branch: 2 branches
BranchTrueFalseTotal
unless $type 0 0 0
defined $id ? : 0 0 0
251 0 ▶ $self -> logger ("handle_req id=$id\n") if ($debug1) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug1 0 0 0
252 0 ▶ my $req = Perl::LanguageServer::Req -> new ({ id => $id, is_dap => $is_dap, type => $type, params => $is_dap?$reqdata -> {arguments} || {}:$reqdata -> {params} || {}}) ;
Branch: 1 branch
BranchTrueFalseTotal
$is_dap ? : 0 0 0
Condition0%0/4
$reqdata->{"arguments"} or {}
l!l
00
$reqdata->{"params"} or {}
l!l
00
MC/DC0%0/1
$reqdata->{"arguments"} or {}
$reqdata->{"arguments"}
MC/DC0%0/1
$reqdata->{"params"} or {}
$reqdata->{"params"}
Truth table0%0/2
A
A= $reqdata->{"arguments"}
Aresult
11
00
Truth table0%0/2
A
A= $reqdata->{"params"}
Aresult
11
00
253 0 $running_reqs{$xid} = $req ;
254
255 0 my $rsp ;
256 my $outdata ;
257 0 my $outjson ;
258 eval
259 0 {
260 0 $rsp = $self -> call_method ($reqdata, $req, $id) ;
261 0 ▶ $id = undef if (!$rsp) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $rsp 0 0 0
262 0 ▶ if ($req -> is_dap)
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_dap) { } 0 0 0
263 {
264 0 ▶ $outjson = { request_seq => -$id, seq => -$id, command => $reqdata -> {command}, success => JSON::true, type => 'response', $rsp?(body => $rsp):()} ;
Branch: 1 branch
BranchTrueFalseTotal
$rsp ? : 0 0 0
265 }
266 else
267 {
268 0 ▶ $outjson = { id => $id, jsonrpc => '2.0', result => $rsp} if ($rsp) ;
Branch: 1 branch
BranchTrueFalseTotal
if $rsp 0 0 0
269 }
270 0 ▶ $outdata = $json -> encode ($outjson) if ($outjson) ;
Branch: 1 branch
BranchTrueFalseTotal
if $outjson 0 0 0
271 } ;
272 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
273 {
274 0 $self -> logger ("ERROR: $@\n") ;
275 0 ▶ if ($req -> is_dap)
Branch: 1 branch
BranchTrueFalseTotal
if ($req->is_dap) { } 0 0 0
276 {
277 0 $outjson = { request_seq => -$id, command => $reqdata -> {command}, success => JSON::false, message => "$@", , type => 'response'} ;
278 }
279 else
280 {
281 0 $outjson = { id => $id, jsonrpc => '2.0', error => { code => -32001, message => "$@" }} ;
282 }
283 0 ▶ $outdata = $json -> encode ($outjson) if ($outjson) ;
Branch: 1 branch
BranchTrueFalseTotal
if $outjson 0 0 0
284 }
285
286 0 ▶ if (defined($id))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 0 0
287 {
288 0 my $guard = $self -> out_semaphore -> guard ;
289 1 ▶ use bytes ;
Subroutine BEGIN: called CC=1 SCAR=0.0
290 0 my $len = length ($outdata) ;
291 0 my $wrdata = "Content-Length: $len\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n$outdata" ;
292 0 my $sum = 0 ;
293 0 my $cnt ;
294 0 ▶ while ($sum < length ($wrdata))
Branch: 1 branch
BranchTrueFalseTotal
if ($sum < length $wrdata) 0 0 0
295 {
296 0 $cnt = $self -> _write ($wrdata, undef, $sum) ;
297 0 ▶ die "write_error ($!)" if ($cnt <= 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt <= 0 0 0 0
298 0 $sum += $cnt ;
299 }
300
301 0 ▶ if ($debug1)
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::LanguageServer::debug1) 0 0 0
302 {
303 0 $wrdata =~ s/\r//g ;
304 0 $self -> logger ("<--- Response: ", $jsonpretty -> encode ($outjson), "\n") ;
305 }
306 }
307 0 } ;
308 }
309
310 # ---------------------------------------------------------------------------
311
312 sub mainloop
313 0 ▶ {
Subroutine mainloop: not called CC=22 SCAR=61.3
Pod undocumented
314 0 my ($self) = @_ ;
315
316 0 my $buffer = '' ;
317 0 ▶ while (!$exit)
Branch: 1 branch
BranchTrueFalseTotal
unless ($Perl::LanguageServer::exit) 0 0 0
318 {
319 1 ▶ use bytes ;
Subroutine BEGIN: called CC=1 SCAR=0.0
320 0 my %header ;
321 my $line ;
322 0 my $cnt ;
323 0 my $loop ;
324 header:
325 0 while (1)
326 {
327 0 ▶ $self -> logger ("start aio read, buffer len = " . length ($buffer) . "\n") if ($debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
328 0 ▶ if ($loop)
Branch: 1 branch
BranchTrueFalseTotal
if ($loop) 0 0 0
329 {
330 0 $cnt = $self -> _read (\$buffer, 8192, length ($buffer), undef, 1) ;
331 0 ▶ $self -> logger ("end aio read cnt=$cnt, buffer len = " . length ($buffer) . "\n") if ($debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
332 0 ▶ die "read_error reading headers ($!)" if ($cnt < 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt < 0 0 0 0
333 0 ▶ return if ($cnt == 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 0 0 0
334 }
335
336 0 ▶ while ($buffer =~ s/^(.*?)\R//)
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ s/^(.*?)\R//d) 0 0 0
337 {
338 0 $line = $1 ;
339 0 ▶ $self -> logger ("line=<$line>\n") if ($debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
340 0 ▶ last header if ($line eq '') ;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 0 0
341 0 ▶ $header{$1} = $2 if ($line =~ /(.+?):\s*(.+)/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /(.+?):\s*(.+)/d 0 0 0
342 }
343 0 $loop = 1 ;
344 }
345
346 0 my $len = $header{'Content-Length'} ;
347 0 ▶ return 1 if ($len == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 0 0 0
348 0 my $data ;
349 #$self -> logger ("len=$len len buffer=", length ($buffer), "\n") if ($debug2) ;
350 0 ▶ while ($len > length ($buffer))
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length $buffer) 0 0 0
351 {
352 0 $cnt = $self -> _read (\$buffer, $len - length ($buffer), length ($buffer)) ;
353
354 #$self -> logger ("cnt=$cnt len=$len len buffer=", length ($buffer), "\n") if ($debug2) ;
355 0 ▶ die "read_error reading data ($!)" if ($cnt < 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt < 0 0 0 0
356 0 ▶ return if ($cnt == 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 0 0 0
357 }
358 0 ▶ if ($len == length ($buffer))
Branch: 2 branches
BranchTrueFalseTotal
if ($len == length $buffer) { } 0 0 0
elsif ($len < length $buffer) { } 0 0 0
359 {
360 0 $data = $buffer ;
361 0 $buffer = '' ;
362 }
363 elsif ($len < length ($buffer))
364 {
365 0 $data = substr ($buffer, 0, $len) ;
366 0 $buffer = substr ($buffer, $len) ;
367 }
368 else
369 {
370 0 die "to few data bytes" ;
371 }
372 0 ▶ $self -> logger ("read data=", $data, "\n") if ($debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
373 0 ▶ $self -> logger ("read header=", dump (\%header), "\n") if ($debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
374
375 0 my $reqdata ;
376 0 ▶ $reqdata = $json -> decode ($data) if ($data) ;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
377 0 ▶ if ($debug1)
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::LanguageServer::debug1) 0 0 0
378 {
379 0 $self -> logger ($self -> log_req_txt, $jsonpretty -> encode ($reqdata), "\n") ;
380 }
381 0 ▶ my $id = $reqdata -> {type}?-$reqdata -> {seq}:$reqdata -> {id};
Branch: 1 branch
BranchTrueFalseTotal
$reqdata->{'type'} ? : 0 0 0
382
383 0 $self -> process_req ($id, $reqdata) ;
384 0 cede () ;
385 }
386
387 0 return 1 ;
388 }
389
390 # ---------------------------------------------------------------------------
391
392 sub _run_tcp_server
393 0 ▶ {
Subroutine _run_tcp_server: not called CC=7 SCAR=40.3
394 0 my ($listen_port) = @_ ;
395
396 0 ▶ if ($listen_port)
Branch: 1 branch
BranchTrueFalseTotal
if ($listen_port) 0 0 0
397 {
398 0 my $quit ;
399 0 ▶ while (!$quit && !$exit)
Branch: 1 branch
BranchTrueFalseTotal
if (not $quit || $Perl::LanguageServer::exit) 0 0 0
Condition0%0/3
$quit || $Perl::LanguageServer::exit
l!l&&r!l&&!r
000
MC/DC0%0/2
$quit || $Perl::LanguageServer::exit
$quit $Perl::LanguageServer::exit
Truth table0%0/3
A || B
A= $quitB= $Perl::LanguageServer::exit
ABresult
1X1
011
000
400 {
401 0 logger (undef, "tcp server start listen on port $listen_port\n") ;
402 0 my $tcpcv = AnyEvent::CondVar -> new ;
403 0 my $guard ;
404 eval
405 0 {
406 $guard = tcp_server '127.0.0.1', $listen_port, sub
407 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
408 0 my ($fh, $host, $port) = @_ ;
409
410 async
411 0 ▶ {
Subroutine __ANON__: not called CC=6 SCAR=37.4
412 eval
413 0 {
414 0 $fh = Coro::Handle::unblock ($fh) ;
415 0 my $self = Perl::LanguageServer -> new ({out_fh => $fh, in_fh => $fh, log_prefix => 'DAx'});
416 0 $self -> logger ("connect from $host:$port\n") ;
417 0 $self -> listen_port ($listen_port) ;
418
419 0 $quit = $self -> mainloop () ;
420 0 ▶ $self -> logger ("got quit signal\n") if ($quit) ;
Branch: 1 branch
BranchTrueFalseTotal
if $quit 0 0 0
421 } ;
422 0 ▶ logger (undef, $@) if ($@) ;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
423 0 ▶ if ($fh)
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 0 0
424 {
425 0 close ($fh) ;
426 0 $fh = undef ;
427 }
428 0 ▶ if ($quit || $exit)
Branch: 1 branch
BranchTrueFalseTotal
if ($quit or $Perl::LanguageServer::exit) 0 0 0
Condition0%0/3
$quit or $Perl::LanguageServer::exit
l!l&&r!l&&!r
000
MC/DC0%0/2
$quit or $Perl::LanguageServer::exit
$quit $Perl::LanguageServer::exit
Truth table0%0/3
A or B
A= $quitB= $Perl::LanguageServer::exit
ABresult
1X1
011
000
429 {
430 0 $tcpcv -> send ;
431 0 IO::AIO::reinit () ; # stop AIO requests
432 0 exit (1) ;
433 }
434 0 } ;
435 0 } ;
436 } ;
437 0 ▶ if (!$@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 0 0
438 {
439 0 $tcpcv -> recv ;
440 }
441 else
442 {
443 0 $guard = undef ;
444 0 logger (undef, $@) ;
445 #$quit = 1 ;
446 0 ▶ if (!$guard && ($@ =~ /Address already in use/))
Branch: 1 branch
BranchTrueFalseTotal
if (not $guard and $@ =~ /Address already in use/u) 0 0 0
Condition0%0/3
not $guard and $@ =~ /Address already in use/u
!ll&&!rl&&r
000
MC/DC0%0/2
not $guard and $@ =~ /Address already in use/u
not $guard $@ =~ /Address already in use/u
Truth table0%0/3
not(A) and B
A= not $guardB= $@ =~ /Address already in use/u
ABresult
0X0
100
111
447 {
448 # stop other server
449 tcp_connect '127.0.0.1', $listen_port, sub
450 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
451 0 my ($fh) = @_ ;
452 0 ▶ syswrite ($fh, "Content-Length: 0\r\n\r\n") if ($fh) ;
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
453 0 } ;
454 }
455 0 $@ = undef ;
456 0 Coro::AnyEvent::sleep (2) ;
457 0 IO::AIO::reinit () ; # stop AIO requests
458 0 exit (1) ; # stop LS, vscode will restart it
459 }
460 }
461 }
462 }
463
464 # ---------------------------------------------------------------------------
465
466 sub run
467 0 ▶ {
Subroutine run: not called CC=11 SCAR=48.8
Pod documented
468 0 my $listen_port ;
469 my $no_stdio ;
470 0 my $heartbeat ;
471
472 0 ▶ while (my $opt = shift @ARGV)
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = shift @ARGV) 0 0 0
473 {
474 0 ▶ if ($opt eq '--debug')
Branch: 7 branches
BranchTrueFalseTotal
if ($opt eq '--debug') { } 0 0 0
elsif ($opt eq '--log-level') { } 0 0 0
elsif ($opt eq '--log-file') { } 0 0 0
elsif ($opt eq '--port') { } 0 0 0
elsif ($opt eq '--nostdio') { } 0 0 0
elsif ($opt eq '--heartbeat') { } 0 0 0
elsif ($opt eq '--version') { } 0 0 0
475 {
476 0 $debug1 = $debug2 = 1 ;
477 }
478 elsif ($opt eq '--log-level')
479 {
480 0 $debug1 = shift @ARGV ;
481 0 ▶ $debug2 = $debug1 > 1?1:0 ;
Branch: 1 branch
BranchTrueFalseTotal
$Perl::LanguageServer::debug1 > 1 ? : 0 0 0
482 }
483 elsif ($opt eq '--log-file')
484 {
485 0 $log_file = shift @ARGV ;
486 }
487 elsif ($opt eq '--port')
488 {
489 0 $listen_port = shift @ARGV ;
490 }
491 elsif ($opt eq '--nostdio')
492 {
493 0 $no_stdio = 1 ;
494 }
495 elsif ($opt eq '--heartbeat')
496 {
497 0 $heartbeat = 1 ;
498 }
499 elsif ($opt eq '--version')
500 {
501 0 $client_version = shift @ARGV ;
502 }
503 }
504
505 0 $|= 1 ;
506
507 0 my $cv = AnyEvent::CondVar -> new ;
508
509 async
510 0 ▶ {
Subroutine __ANON__: not called CC=3 SCAR=24.8
511 0 my $i = 0 ;
512 0 while (1)
513 {
514 0 ▶ if ($heartbeat || $debug2)
Branch: 1 branch
BranchTrueFalseTotal
if ($heartbeat or $Perl::LanguageServer::debug2) 0 0 0
Condition0%0/3
$heartbeat or $Perl::LanguageServer::debug2
l!l&&r!l&&!r
000
MC/DC0%0/2
$heartbeat or $Perl::LanguageServer::debug2
$heartbeat $Perl::LanguageServer::debug2
Truth table0%0/3
A or B
A= $heartbeatB= $Perl::LanguageServer::debug2
ABresult
1X1
011
000
515 {
516 0 logger (undef, "##### $i #####\n running: " . dump (\%running_reqs) . " coros: " . dump (\%running_coros), "\n") ;
517 0 $i++ ;
518 }
519
520 0 Coro::AnyEvent::sleep (10) ;
521 }
522 0 } ;
523
524 0 ▶ if (!$no_stdio)
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_stdio) 0 0 0
525 {
526 async
527 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
528 0 my $self = Perl::LanguageServer -> new ({out_fh => 1, in_fh => 0});
529 0 $self -> listen_port ($listen_port) ;
530
531 0 $self -> mainloop () ;
532
533 0 $cv -> send ;
534 0 } ;
535 }
536
537 async
538 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
539 0 _run_tcp_server ($listen_port) ;
540 0 } ;
541
542 0 $cv -> recv ;
543 0 $exit = 1 ;
544 }
545
546 # ---------------------------------------------------------------------------
547
548 sub parsews
549 0 ▶ {
Subroutine parsews: not called CC=1 SCAR=6.9
Pod undocumented
550 0 my $class = shift ;
551 0 my @args = @_ ;
552
553 0 $|= 1 ;
554
555 0 my $cv = AnyEvent::CondVar -> new ;
556
557 async
558 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
559 0 my $self = Perl::LanguageServer -> new ;
560 0 $workspace = Perl::LanguageServer::Workspace -> new ({ config => {} }) ;
561 0 my %folders ;
562 0 foreach my $path (@args)
563 {
564 0 $folders{$path} = $path ;
565 }
566 0 $workspace -> folders (\%folders) ;
567 0 $workspace -> background_parser ($self) ;
568
569 0 $cv -> send ;
570 0 } ;
571
572 0 $cv -> recv ;
573 }
574
575 # ---------------------------------------------------------------------------
576
577 sub check_file
578 0 ▶ {
Subroutine check_file: not called CC=1 SCAR=6.9
Pod undocumented
579 0 my $class = shift ;
580 0 my @args = @_ ;
581
582 0 $|= 1 ;
583
584 0 my $cv = AnyEvent::CondVar -> new ;
585
586 0 my $self = Perl::LanguageServer -> new ;
587 0 $workspace = Perl::LanguageServer::Workspace -> new ({ config => {} }) ;
588 async
589 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
590 0 my %folders ;
591 0 foreach my $path (@args)
592 {
593 0 $folders{$path} = $path ;
594 }
595 0 $workspace -> folders (\%folders) ;
596 0 $workspace -> background_checker ($self) ;
597
598 0 $cv -> send ;
599 0 } ;
600
601 async
602 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
603 0 foreach my $path (@args)
604 {
605 0 my $text ;
606 0 aio_load ($path, $text) ;
607
608 0 $workspace -> check_perl_syntax ($workspace, $path, $text) ;
609 }
610
611 0 } ;
612
613 0 $cv -> recv ;
614 }
615
616 1 1 ;
617
618 __END__
« LanguageServer/Parser.pm ↑ Summary LanguageServer/Methods/workspace.pm »