blib/lib/YATT/Lite/LanguageServer.pm

filter: Statementstmt 20.0% 33 / 165 Branchbran 2.0% 1 / 50 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 28.5% 10 / 35 Podpod 5.8% 1 / 17 totaltotal 15.0% 45 / 299 CC 32 SCAR 28.1
CC
32
Cov
14%
lspcall__initialize
72.0
lspcall__textDocument__didSave
30.0
locate_symbol_at_file_position
20.0
CRAP 16.7
« blib/lib/YATT/Lite/Macro.pm ↑ Summary blib/lib/YATT/Lite/Util.pm »
1 #!/usr/bin/env perl
2 package YATT::Lite::LanguageServer;
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::AddInc;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 my $libDir = File::AddInc->libdir;
9  
10 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use YATT::Lite::LanguageServer::Generic -as_base
Subroutine BEGIN: called CC=1 SCAR=0.0
14 , [fields => qw/_initialized
15 _client_cap
16 _inspector
17 _last_error
18 current_workspace
19 /
20 2 ];
21  
22 2 ▶use MOP4Import::Util qw/terse_dump lexpand/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use YATT::Lite::LanguageServer::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use YATT::Lite::Inspector [as => 'Inspector']
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 , qw/Zipper AltNode LintResult/;
28  
29 0 ▶sub after_configure_default {
Subroutine after_configure_default: not called CC=2 SCAR=17.9
Pod documented
30 0 (my MY $self) = @_;
31 0 $self->next::method;
32 0 ▶ $self->{current_workspace} = $ENV{PWD} || getcwd;
Condition0%0/3
$ENV{'PWD'} || getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'PWD'} || getcwd()
$ENV{'PWD'} getcwd()
Truth table0%0/3
A || B
A= $ENV{'PWD'}B= getcwd()
ABresult
1X1
011
000
33 }
34  
35 0 ▶sub lspcall__initialize {
Subroutine lspcall__initialize: not called CC=8 SCAR=42.8
Pod undocumented
36 0 (my MY $self, my InitializeParams $params) = @_;
37 0 $self->{_client_cap} = $params->{capabilities};
38  
39 # Protocol.pm predates LSP 3.17: workspaceFolders, rootPath and
40 # positionEncoding(s) are accessed untyped.
41 0 my $untyped = $params;
42 0 my $rootUri = $params->{rootUri};
43 0 ▶ if (not defined $rootUri
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rootUri and ref $untyped->{"workspaceFolders"} eq "ARRAY" and @{$untyped->{"workspaceFolders"};}) 0 0 0
Condition0%0/6
not defined $rootUri and ref $untyped->{"workspaceFolders"} eq "ARRAY" and @{$untyped->{"workspaceFolders"};}
!ll&&!rl&&r
000
not defined $rootUri and ref $untyped->{"workspaceFolders"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $rootUri and ref $untyped->{"workspaceFolders"} eq "ARRAY" and @{$untyped->{"workspaceFolders"};}
not defined $rootUri ref $untyped->{"workspaceFolders"} eq "ARRAY" @{$untyped->{"workspaceFolders"};}
Truth table0%0/4
not(A) and B and C
A= not defined $rootUriB= ref $untyped->{"workspaceFolders"} eq "ARRAY"C= @{$untyped->{"workspaceFolders"};}
ABCresult
0XX0
10X0
1100
1111
44 and ref $untyped->{workspaceFolders} eq 'ARRAY'
45 0 and @{$untyped->{workspaceFolders}}) {
46 0 $rootUri = $untyped->{workspaceFolders}[0]{uri};
47 }
48 0 ▶ if (my $path = $self->uri2localpath($rootUri) // $untyped->{rootPath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $self->uri2localpath($rootUri) // $untyped->{"rootPath"}) 0 0 0
Condition0%0/3
$self->uri2localpath($rootUri) // $untyped->{"rootPath"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->uri2localpath($rootUri) // $untyped->{"rootPath"}
$self->uri2localpath($rootUri) $untyped->{"rootPath"}
Truth table0%0/3
A // B
A= $self->uri2localpath($rootUri)B= $untyped->{"rootPath"}
ABresult
1X1
011
000
49 0 $self->load_inspector($self->{current_workspace} = $path);
50 }
51  
52 0 my InitializeResult $res = {};
53 0 $res->{capabilities} = my ServerCapabilities $svcap = {};
54 0 $svcap->{definitionProvider} = JSON()->true;
55 0 $svcap->{implementationProvider} = JSON()->true;
56 0 $svcap->{hoverProvider} = JSON()->true;
57 0 $svcap->{documentSymbolProvider} = JSON()->true;
58 0 $svcap->{completionProvider} = my CompletionOptions $copts = {};
59 0 $copts->{triggerCharacters} = ['<', ':', "\$", '&', '=', '"', "'"];
60 0 $copts->{resolveProvider} = JSON()->false;
61 0 $svcap->{textDocumentSync} = my TextDocumentSyncOptions $sopts = +{};
62 0 $sopts->{openClose} = JSON()->true;
63 0 $sopts->{save} = JSON()->true;
64 0 $sopts->{change} = TextDocumentSyncKind__Incremental;
65  
66 # Positions are exchanged as perl character offsets, which equal UTF-32
67 # code units. Advertise that when the client can handle it (eglot does).
68 # Clients limited to utf-16 get the same offsets, so their columns drift
69 # after a non-BMP character on the same line. GH-275
70 0 my $encodings = $untyped->{capabilities}{general}{positionEncodings};
71 0 ▶ if ($encodings and grep {$_ eq 'utf-32'} lexpand($encodings)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encodings and grep {$_ eq "utf-32";} lexpand($encodings)) 0 0 0
Condition0%0/3
$encodings and grep {$_ eq "utf-32";} lexpand($encodings)
!ll&&!rl&&r
000
MC/DC0%0/2
$encodings and grep {$_ eq "utf-32";} lexpand($encodings)
$encodings grep {$_ eq "utf-32";} lexpand($encodings)
Truth table0%0/3
A and B
A= $encodingsB= grep {$_ eq "utf-32";} lexpand($encodings)
ABresult
0X0
100
111
72 0 $res->{capabilities}{positionEncoding} = 'utf-32';
73 }
74 0 $res;
75 }
76  
77 0 ▶sub lspcall__initialized {
Subroutine lspcall__initialized: not called CC=1 SCAR=6.9
Pod undocumented
78 0 (my MY $self, my $params) = @_;
79 0 $self->{_initialized} = 1;
80 0 undef;
81 }
82  
83 # '$/cancelRequest'. Requests are processed to completion anyway;
84 # accepted so that the client does not see "Not implemented".
85 0 ▶sub lspcall____ext__cancelRequest {
Subroutine lspcall____ext__cancelRequest: not called CC=1 SCAR=6.9
Pod undocumented
86 0 (my MY $self, my $params) = @_;
87 0 undef;
88 }
89  
90 0 ▶sub lspcall__textDocument__didOpen {
Subroutine lspcall__textDocument__didOpen: not called CC=2 SCAR=17.9
Pod undocumented
91 0 (my MY $self, my DidOpenTextDocumentParams $params) = @_;
92  
93 0 my TextDocumentItem $docItem = $params->{textDocument};
94 0 my $fn = $self->uri2localpath($docItem->{uri});
95  
96 my LintResult $error
97 0 = $self->inspector->load_string_into_file($fn, $docItem->{text});
98  
99 0 $self->{_last_error}{$docItem->{uri}} = $error;
100  
101 0 my PublishDiagnosticsParams $notif = {};
102 0 $notif->{uri} = $docItem->{uri};
103 0 ▶ $notif->{diagnostics} = [$error ? lexpand($error->{diagnostics}) : ()];
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
104  
105 0 $self->send_notification('textDocument/publishDiagnostics', $notif);
106 }
107  
108 0 ▶sub last_error {
Subroutine last_error: not called CC=1 SCAR=6.9
Pod undocumented
109 0 (my MY $self, my TextDocumentIdentifier $docId) = @_;
110 $self->{_last_error}{$docId->{uri}}
111 0 }
112  
113 0 ▶sub lspcall__textDocument__didChange {
Subroutine lspcall__textDocument__didChange: not called CC=3 SCAR=24.8
Pod undocumented
114 0 (my MY $self, my DidChangeTextDocumentParams $params) = @_;
115  
116 0 my TextDocumentIdentifier $docId = $params->{textDocument};
117 0 my $fn = $self->uri2localpath($docId->{uri});
118  
119 0 (my $updated, my LintResult $error) = $self->inspector->apply_changes($fn, @{$params->{contentChanges}});
120  
121 0 $self->{_last_error}{$docId->{uri}} = $error;
122  
123 $self->logmsg("updated ", ($error ? "with error " : ""), "as: "
124 0 ▶ , sub {$self->truncate_for_log(terse_dump($updated), 300)});
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
125  
126 0 my PublishDiagnosticsParams $notif = {};
127 0 $notif->{uri} = $docId->{uri};
128 0 ▶ $notif->{diagnostics} = [$error ? lexpand($error->{diagnostics}) : ()];
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
129  
130 0 $self->send_notification('textDocument/publishDiagnostics', $notif);
131 }
132  
133 0 ▶sub lspcall__textDocument__didSave {
Subroutine lspcall__textDocument__didSave: not called CC=5 SCAR=34.0
Pod undocumented
134 0 (my MY $self, my DidSaveTextDocumentParams $params) = @_;
135  
136 0 my TextDocumentIdentifier $docId = $params->{textDocument};
137 0 my $fn = $self->uri2localpath($docId->{uri});
138  
139 0 my LintResult $res = $self->inspector->lint($fn); # XXX: process isolation
140  
141 0 ▶ $self->{_last_error}{$docId->{uri}} = $res->{is_success} ? undef : $res;
Branch: 1 branch
BranchTrueFalseTotal
$res->{'is_success'} ? : 0 0 0
142  
143 0 ▶ $self->logmsg("lint result: ", sub {terse_dump($res)});
Subroutine __ANON__: not called CC=1 SCAR=6.9
144  
145 0 my PublishDiagnosticsParams $notif = {};
146 0 $notif->{uri} = $docId->{uri};
147  
148 0 ▶ if ($res->{is_success}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->{'is_success'}) { } 0 0 0
elsif ($res->{'diagnostics'}) { } 0 0 0
149 # ok.
150 0 $notif->{diagnostics} = [];
151 } elsif ($res->{diagnostics}) {
152  
153 0 $notif->{diagnostics} = [lexpand($res->{diagnostics})];
154 }
155  
156 0 ▶ if ($notif->{diagnostics}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notif->{"diagnostics"}) 0 0 0
157 0 $self->send_notification('textDocument/publishDiagnostics', $notif);
158 }
159 }
160  
161 0 ▶sub lspcall__textDocument__didClose {
Subroutine lspcall__textDocument__didClose: not called CC=2 SCAR=17.9
Pod undocumented
162 0 (my MY $self, my $params) = @_;
163  
164 0 my TextDocumentIdentifier $docId = $params->{textDocument};
165 0 delete $self->{_last_error}{$docId->{uri}};
166  
167 my $fn = $self->uri2localpath($docId->{uri})
168 0 ▶ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $self->uri2localpath($docId->{"uri"}) or return undef 0 0 0
169  
170 # The buffer is gone: drop its (possibly unsaved) text from the template
171 # cache so that lookups from other files see what is on disk.
172 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
173 0 ▶ $self->inspector->reload_file_from_disk($fn) if -r $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -r $fn 0 0 0
174 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 $self->logmsg("didClose: reload of $fn failed: $_");
176 0 };
177 0 undef;
178 }
179  
180 #
181 # WIP
182 #
183 0 ▶sub lspcall__textDocument__hover {
Subroutine lspcall__textDocument__hover: not called CC=3 SCAR=24.8
Pod undocumented
184 0 (my MY $self, my TextDocumentPositionParams $params) = @_;
185  
186 0 my Hover $result = {};
187  
188 0 my TextDocumentIdentifier $docId = $params->{textDocument};
189  
190 # Skip if the document has error
191 0 ▶ return undef if $self->last_error($docId);
Branch: 1 branch
BranchTrueFalseTotal
if $self->last_error($docId) 0 0 0
192  
193 my $fn = $self->uri2localpath($docId->{uri})
194 0 ▶ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $self->uri2localpath($docId->{"uri"}) or return undef 0 0 0
195 0 my Position $pos = $params->{position};
196  
197 0 my $found;
198 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
199 my ($symbol, $cursor) = $self->locate_symbol_at_file_position(
200 $fn, $pos->{line}, $pos->{character}
201 0 ▶ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->locate_symbol_at_file_position($fn, $pos->{"line"}, $pos->{"character"}) or return 0 0 0
202  
203 # Nothing to say (e.g. an attribute of a macro element): null, not a
204 # debug dump. GH-277
205 0 ▶ if (my $contents = $self->inspector->describe_symbol($symbol, $cursor)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $contents = $self->inspector->describe_symbol($symbol, $cursor)) 0 0 0
206 0 $result->{contents} = $contents;
207 0 $found = $result;
208 }
209 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 $self->logmsg("hover failed at $fn:$pos->{line}:$pos->{character}: $_");
211 0 };
212  
213 0 $found;
214 }
215  
216  
217 0 ▶sub lspcall__textDocument__definition {
Subroutine lspcall__textDocument__definition: not called CC=1 SCAR=6.9
Pod undocumented
218 0 goto &lspcall__textDocument__implementation;
219 }
220  
221 #
222 # No last_error guard here on purpose: parts before a syntax error still
223 # resolve. Any failure degrades to null (no definition) instead of a
224 # JSON-RPC error, which eglot would show as an error. GH-275
225 #
226 0 ▶sub lspcall__textDocument__implementation {
Subroutine lspcall__textDocument__implementation: not called CC=2 SCAR=17.9
Pod undocumented
227 0 (my MY $self, my TextDocumentPositionParams $params) = @_;
228  
229 0 my TextDocumentIdentifier $docId = $params->{textDocument};
230 my $fn = $self->uri2localpath($docId->{uri})
231 0 ▶ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fn = $self->uri2localpath($docId->{"uri"}) or return undef 0 0 0
232 0 my Position $pos = $params->{position};
233  
234 0 my $found;
235 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
236 my ($symbol, $cursor) = $self->locate_symbol_at_file_position(
237 $fn, $pos->{line}, $pos->{character}
238 0 ▶ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->locate_symbol_at_file_position($fn, $pos->{"line"}, $pos->{"character"}) or return 0 0 0
239  
240 0 $found = $self->inspector->lookup_symbol_definition($symbol, $cursor);
241 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 $self->logmsg("definition failed at $fn:$pos->{line}:$pos->{character}: $_");
243 0 };
244  
245 0 $found;
246 }
247  
248 #
249 # Extract a symbol at file/position.
250 # In list context, also returns $cursor for later tree walk.
251 #
252 0 ▶sub locate_symbol_at_file_position {
Subroutine locate_symbol_at_file_position: not called CC=4 SCAR=30.0
Pod undocumented
253 0 (my MY $self, my ($fn, $line, $character)) = @_;
254  
255 0 ▶ my ($symbol, $cursor) = $self->inspector->locate_symbol_at_file_position(
Branch: 1 branch
BranchTrueFalseTotal
() = $self->inspector->locate_symbol_at_file_position($fn, $line, $character // 0) or return 0 0 0
Condition0%0/2
$character // 0
l!l
00
MC/DC0%0/1
$character // 0
$character
Truth table0%0/2
A
A= $character
Aresult
11
00
256 $fn, $line, $character // 0
257 ) or return;
258  
259 0 ▶ wantarray ? ($symbol, $cursor) : $symbol;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
260 }
261  
262 0 ▶sub lspcall__textDocument__documentSymbol {
Subroutine lspcall__textDocument__documentSymbol: not called CC=3 SCAR=24.8
Pod undocumented
263 0 (my MY $self, my DocumentSymbolParams $params) = @_;
264  
265 0 my TextDocumentIdentifier $docId = $params->{textDocument};
266  
267 # Skip if the document has error
268 0 ▶ return undef if $self->last_error($docId);
Branch: 1 branch
BranchTrueFalseTotal
if $self->last_error($docId) 0 0 0
269  
270 0 my $fn = $self->uri2localpath($docId->{uri});
271  
272 0 ▶ if (my @result = $self->inspector->list_parts_in($fn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@result) = $self->inspector->list_parts_in($fn)) { } 0 0 0
273 \@result
274 0 } else {
275 0 undef;
276 }
277 }
278  
279 0 ▶sub lspcall__textDocument__completion {
Subroutine lspcall__textDocument__completion: not called CC=3 SCAR=24.8
Pod undocumented
280 0 (my MY $self, my CompletionParams $params) = @_;
281  
282 0 my TextDocumentIdentifier $docId = $params->{textDocument};
283
284 # Don't skip even if the document has error - completion is often triggered while typing
285 # and the document may be in an incomplete/error state
286  
287 0 my $fn = $self->uri2localpath($docId->{uri});
288 0 my Position $pos = $params->{position};
289 0 my CompletionContext $context = $params->{context};
290  
291 # Get completion items from inspector
292 my @items = $self->inspector->get_completion_items(
293 $fn, $pos->{line}, $pos->{character},
294 $context ? $context->{triggerCharacter} : undef
295 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$context ? : 0 0 0
296  
297 0 ▶ unless (@items) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@items) 0 0 0
298 0 return undef;
299 }
300  
301 0 my CompletionList $result = {};
302 0 $result->{isIncomplete} = JSON()->false;
303 0 $result->{items} = \@items;
304
305 0 $result;
306 }
307  
308 #----------------------------------------
309  
310 0 ▶sub inspector {
Subroutine inspector: not called CC=1 SCAR=6.9
Pod undocumented
311 0 (my MY $self) = @_;
312 0 $self->load_inspector($self->{current_workspace});
313 }
314  
315 0 ▶sub load_inspector {
Subroutine load_inspector: not called CC=2 SCAR=17.9
Pod undocumented
316 0 (my MY $self, my $rootPath) = @_;
317 0 ▶ $self->{_inspector}{$rootPath} //= do {
Condition0%0/3
$self->{"_inspector"}{$rootPath} //= do { $self->Inspector->new("dir", $rootPath) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_inspector"}{$rootPath} //= do { $self->Inspector->new("dir", $rootPath) }
$self->{"_inspector"}{$rootPath} do { $self->Inspector->new("dir", $rootPath) }
Truth table0%0/3
A //= B
A= $self->{"_inspector"}{$rootPath}B= do { $self->Inspector->new("dir", $rootPath) }
ABresult
1X1
011
000
318 0 $self->Inspector->new(dir => $rootPath);
319 };
320 }
321  
322  
323 #----------------------------------------
324  
325 2 ▶MY->run(\@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 2 2
326  
327 2 1;
« blib/lib/YATT/Lite/Macro.pm ↑ Summary blib/lib/YATT/Lite/Util.pm »