/
Perl6LangServer.pm6
468 lines (396 loc) · 11.8 KB
/
Perl6LangServer.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use v6;
unit class App::Perl6LangServer;
# Necessary evil to evaluate p6ddoc index data (Perl 6 data structure)
use MONKEY-SEE-NO-EVAL;
use File::Temp;
use JSON::Tiny;
# Cache p6doc index.data (Hash of help topics strings)
my %help-index;
# Text documents
my %text-documents;
method run {
# No standard input/output buffering to prevent unwanted hangs/failures/waits
$*OUT.out-buffer = False;
$*ERR.out-buffer = False;
debug-log("🙂: Starting perl6-langserver... Reading/writing stdin/stdout.");
my $initialized = False;
loop {
my %headers;
for $*IN.lines -> $line {
# we're done here
last if $line eq '';
# Parse HTTP-style header
my ($name, $value) = $line.split(': ');
if $name eq 'Content-Length' {
$value = +$value;
}
%headers{$name} = $value;
}
# Read JSON::RPC request
my $content-length = 0 + %headers<Content-Length>;
if $content-length > 0 {
my $json = $*IN.read($content-length).decode;
my $request = from-json($json);
my $id = $request<id>;
my $method = $request<method>;
#TODO throw an exception if a method is called before $initialized = True
given $method {
when 'initialize' {
my $result = initialize($request<params>);
send-json-response($id, $result);
}
when 'initialized' {
# Initialization done
$initialized = True;
}
when 'textDocument/didOpen' {
text-document-did-open($request<params>);
}
when 'textDocument/didSave' {
text-document-did-save($request<params>);
}
when 'textDocument/didChange' {
text-document-did-change($request<params>);
}
when 'textDocument/didClose' {
text-document-did-close($request<params>);
}
when 'textDocument/documentSymbol' {
# When outline tree view is shown, it asks for symbols
my $result = on-text-document-symbol($request<params>);
send-json-response($id, $result);
}
when 'textDocument/hover' {
my $result = on-text-document-hover($request<params>);
send-json-response($id, $result);
}
when 'shutdown' {
# Client requested to shutdown...
send-json-response($id, Any);
}
when 'exit' {
exit 0;
}
}
}
}
}
sub debug-log($text) {
$*ERR.say($text);
}
sub send-json-response($id, $result) {
my %response = %(
jsonrpc => "2.0",
id => $id,
result => $result,
);
my $json-response = to-json(%response);
my $content-length = $json-response.chars;
my $response = "Content-Length: $content-length\r\n\r\n" ~ $json-response;
print($response);
}
sub send-json-request($method, %params) {
my %request = %(
jsonrpc => "2.0",
'method' => $method,
params => %params,
);
my $json-request = to-json(%request);
my $content-length = $json-request.chars;
my $request = "Content-Length: $content-length\r\n\r\n" ~ $json-request;
print($request);
}
sub initialize(%params) {
#TODO handle windows
my $index-file = qx{p6doc path-to-index}.chomp;
unless $index-file.path ~~ :f {
# run p6doc-index build to build the index.data file
debug-log("Building index.data... Please wait");
my $dummy = qqx{p6doc build};
debug-log("Built index.data... Thanks for waiting");
}
if $index-file.path ~~ :f {
debug-log("Loading index.data... Please wait");
%help-index = EVAL $index-file.IO.slurp;
debug-log("Loaded index.data... Thanks for waiting");
}
else {
debug-log("Cannot find $index-file");
}
%(
capabilities => {
# TextDocumentSyncKind.Full
# Documents are synced by always sending the full content of the document.
textDocumentSync => 1,
# Provide outline view support
documentSymbolProvider => True,
# Provide hover support
hoverProvider => True
}
)
}
sub text-document-did-open(%params) {
my %text-document = %params<textDocument>;
%text-documents{%text-document<uri>} = %text-document;
return;
}
sub publish-diagnostics($uri) {
# Create a temporary file for Perl 6 source code buffer
my ($file-name,$file-handle) = tempfile(:!unlink);
# Remove temporary file when leaving lexical scope
LEAVE unlink $file-handle;
# Write source code and flush
my $source = %text-documents{$uri}<text>;
$file-handle.print($source);
$file-handle.flush;
# Invoke perl -c temp-filder
#TODO handle windows platform
my Str $output = qqx{$*EXECUTABLE -c $file-name 2>&1};
my @problems;
if $output !~~ /^'Syntax OK'/ &&
$output ~~ m/\n(.+?)at\s.+?\:(\d+)/ {
# A syntax error occurred
my $message = ~$/[0];
my $line-number = +$/[1];
@problems.push: {
range => {
start => {
line => $line-number,
character => 0
},
end => {
line => $line-number,
character => 0
},
},
severity => 1,
source => 'perl6 -c',
message => $message
}
}
my %parameters = %(
uri => $uri,
diagnostics => @problems
);
send-json-request('textDocument/publishDiagnostics', %parameters);
}
sub text-document-did-save(%params) {
my %text-document = %params<textDocument>;
return;
}
sub text-document-did-change(%params) {
my %text-document = %params<textDocument>;
my $uri = %text-document<uri>;
%text-documents{$uri}<text> = %params<contentChanges>[0]<text>;
publish-diagnostics($uri);
return;
}
sub text-document-did-close(%params) {
my %text-document = %params<textDocument>;
%text-documents{%text-document<uri>}:delete;
return;
}
constant symbol-kind-file = 1;
constant symbol-kind-module = 2;
constant symbol-kind-namespace = 3;
constant symbol-kind-package = 4;
constant symbol-kind-class = 5;
constant symbol-kind-method = 6;
constant symbol-kind-property = 7;
constant symbol-kind-field = 8;
constant symbol-kind-constructor = 9;
constant symbol-kind-enum = 10;
constant symbol-kind-interface = 11;
constant symbol-kind-function = 12;
constant symbol-kind-variable = 13;
constant symbol-kind-constant = 14;
constant symbol-kind-string = 15;
constant symbol-kind-number = 16;
constant symbol-kind-boolean = 17;
constant symbol-kind-array = 18;
constant symbol-kind-object = 19;
constant symbol-kind-key = 20;
constant symbol-kind-null = 21;
constant symbol-kind-enummember = 22;
constant symbol-kind-struct = 23;
constant symbol-kind-event = 24;
constant symbol-kind-operator = 25;
constant symbol-kind-typeparameter = 26;
# Called when the client sends a textDocument/documentSymbol request
sub on-text-document-symbol(%params) {
my %text-document = %params<textDocument>;
my $uri = %text-document<uri>;
#TODO handle heredoc comments
# Remove line comments
my $source-code = %text-documents{$uri}<text> or return [];
$source-code = $source-code.lines.map({
$_.subst(/ '#' (.+?) $ /, { '#' ~ (" " x $0.chars) })
}).join("\n");
my $to = 0;
my $line-number = 0;
my @line-ranges;
for $source-code.lines -> $line {
my $length = $line.chars;
my $from = $to;
$to += $length;
@line-ranges.push: {
line-number => $line-number++,
from => $from,
to => $to
};
}
sub to-line-number(Int $position) returns Int {
for @line-ranges -> $line-range {
if $position >= $line-range<from> && $position <= $line-range<to> {
return $line-range<line-number>;
}
}
return -1;
}
my %to-symbol-kind = %(
'class' => symbol-kind-class,
'grammar' => symbol-kind-class,
'module' => symbol-kind-module,
'package' => symbol-kind-namespace,
'class' => symbol-kind-class,
'sub' => symbol-kind-function,
'method' => symbol-kind-method,
'my' => symbol-kind-variable,
'state' => symbol-kind-variable,
);
# Symbol information
my @results;
sub add-results(@declarations) {
for @declarations -> $decl {
my $type = ~$decl[0];
my Int $line-number = to-line-number($decl[0].from);
my Int $kind = %to-symbol-kind{$type};
my %record = %(
name => ~$decl[1],
kind => $kind,
location => {
uri => $uri,
range => {
start => {
line => $line-number,
character => 0, # $decl[0].from
},
end => {
line => $line-number,
character => 0, # $decl[0].from
},
},
},
);
@results.push(%record);
}
}
# Find all package declarations
my @package-declarations = $source-code ~~ m:global/
# Declaration
('class'| 'grammar'| 'module'| 'package'| 'role')
# Whitespace
\s+
# Identifier
(\w+ ('::' \w+))
/;
add-results(@package-declarations);
my @routine-declarations = $source-code ~~ m:global/
# Declaration
('sub'| 'method')
# Whitespace
\s+
# Identifier
(\w (\w | '-')*)
/;
add-results(@routine-declarations);
# SymbolInformation[]
return @results;
}
# Called when client gets a 'textDocument/hover' request.
sub on-text-document-hover(%params) {
my %text-document = %params<textDocument>;
my $uri = %text-document<uri>;
my %position = %params<position>;
my $source-code = %text-documents{$uri}<text> or return [];
my $hover-text = '';
my $line-number = 0;
my $start-pos = 0;
for $source-code.lines -> $line {
if $line-number == %position<line> {
my @chars = $line.comb;
my $buffer = '';
my $pos = %position<character>;
for 0..@chars.elems - 1 -> $i {
my $ch = @chars[$i];
if $ch eq any(' ', '.', '(', ')', '<', '>', ';', ',' , '{', '}', '[', ']', '+', ':') {
last if $i >= $pos;
$start-pos = $i;
$buffer = '';
} else {
$start-pos = $i if $buffer eq '';
$buffer ~= $ch;
}
}
$hover-text = $buffer.trim;
last
}
$line-number++;
}
my $hover-contents = help-search($hover-text);
$hover-contents = "Unable to find help for '$hover-text'" if $hover-contents eq '';
#TODO add definition for variables hover support
{
# The hover content
contents => $hover-contents,
range => {
start => {
line => %position<line>,
character => $start-pos
},
end => {
line => %position<line>,
character => $start-pos + $hover-text.chars
}
}
};
}
sub help-search(Str $pattern is copy) {
debug-log("help-search('$pattern')");
return '' unless %help-index;
# Trim the pattern and make sure we dont fail on undefined
$pattern = $pattern // '';
$pattern = $pattern.trim;
my @results;
constant MAX-SIZE = 5;
for %help-index.keys -> $topic {
if $topic ~~ m:i/"$pattern"/ {
@results.push({
"name" => $topic,
"matches" => %help-index{$topic}.unique(:as(&lc))
});
last if @results.elems >= MAX-SIZE;
}
}
@results = @results.sort(-> $a, $b { uc($a) leg uc($b) });
my @contents;
LOOP: for @results -> $result {
for @($result<matches>) -> $match {
# Compose help keyword from matches
my $name = $match[1].subst(/^ ( 'sub'| 'routine' | 'method' ) /, "").trim;
my $keyword = $match[0] ~ $name;
my $proc = run 'p6doc', '-n', $keyword, :out;
my $content = $proc.out.slurp(:close).chomp;
my $exit-code = $proc.exitcode;
if $exit-code == 0 {
# On success, add p6doc output
@contents.push($content);
#TODO how many p6doc choices to show the user
last LOOP
} else {
debug-log("p6doc failed with exit code: {$exit-code}")
}
}
}
@contents.join("\n---\n");
}