/
LanguageServer.php
573 lines (500 loc) · 19.2 KB
/
LanguageServer.php
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<?php
declare(strict_types=1);
namespace Psalm\Internal\LanguageServer;
use AdvancedJsonRpc\Dispatcher;
use AdvancedJsonRpc\Error;
use AdvancedJsonRpc\ErrorCode;
use AdvancedJsonRpc\ErrorResponse;
use AdvancedJsonRpc\Request;
use AdvancedJsonRpc\Response;
use AdvancedJsonRpc\SuccessResponse;
use Amp\Promise;
use Amp\Success;
use Generator;
use InvalidArgumentException;
use LanguageServerProtocol\ClientCapabilities;
use LanguageServerProtocol\CompletionOptions;
use LanguageServerProtocol\Diagnostic;
use LanguageServerProtocol\DiagnosticSeverity;
use LanguageServerProtocol\InitializeResult;
use LanguageServerProtocol\Position;
use LanguageServerProtocol\Range;
use LanguageServerProtocol\SaveOptions;
use LanguageServerProtocol\ServerCapabilities;
use LanguageServerProtocol\SignatureHelpOptions;
use LanguageServerProtocol\TextDocumentSyncKind;
use LanguageServerProtocol\TextDocumentSyncOptions;
use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\LanguageServer\Server\TextDocument as ServerTextDocument;
use Psalm\Internal\LanguageServer\Server\Workspace as ServerWorkspace;
use Psalm\IssueBuffer;
use Throwable;
use function Amp\asyncCoroutine;
use function Amp\call;
use function array_combine;
use function array_keys;
use function array_map;
use function array_shift;
use function array_unshift;
use function explode;
use function implode;
use function max;
use function parse_url;
use function rawurlencode;
use function realpath;
use function str_replace;
use function strpos;
use function substr;
use function trim;
use function urldecode;
/**
* @internal
*/
class LanguageServer extends Dispatcher
{
/**
* Handles textDocument/* method calls
*
* @var ?ServerTextDocument
*/
public $textDocument;
/**
* Handles workspace/* method calls
*
* @var ?ServerWorkspace
*/
public $workspace;
/**
* @var ProtocolReader
*/
protected $protocolReader;
/**
* @var ProtocolWriter
*/
protected $protocolWriter;
/**
* @var LanguageClient
*/
public $client;
/**
* @var ProjectAnalyzer
*/
protected $project_analyzer;
/**
* @var array<string, string>
*/
protected $onsave_paths_to_analyze = [];
/**
* @var array<string, string>
*/
protected $onchange_paths_to_analyze = [];
/**
* @var array<string, list<IssueData>>
*/
protected $current_issues = [];
public function __construct(
ProtocolReader $reader,
ProtocolWriter $writer,
ProjectAnalyzer $project_analyzer
) {
parent::__construct($this, '/');
$this->project_analyzer = $project_analyzer;
$this->protocolWriter = $writer;
$this->protocolReader = $reader;
$this->protocolReader->on(
'close',
function (): void {
$this->shutdown();
$this->exit();
}
);
$this->protocolReader->on(
'message',
asyncCoroutine(
/**
* @return Generator<int, Promise, mixed, void>
*/
function (Message $msg): Generator {
if (!$msg->body) {
return;
}
// Ignore responses, this is the handler for requests and notifications
if (Response::isResponse($msg->body)) {
return;
}
$result = null;
$error = null;
try {
// Invoke the method handler to get a result
/**
* @var Promise
*/
$dispatched = $this->dispatch($msg->body);
$result = yield $dispatched;
} catch (Error $e) {
// If a ResponseError is thrown, send it back in the Response
$error = $e;
} catch (Throwable $e) {
// If an unexpected error occurred, send back an INTERNAL_ERROR error response
$error = new Error(
(string) $e,
ErrorCode::INTERNAL_ERROR,
null,
$e
);
}
// Only send a Response for a Request
// Notifications do not send Responses
/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
*/
if (Request::isRequest($msg->body)) {
if ($error !== null) {
$responseBody = new ErrorResponse($msg->body->id, $error);
} else {
$responseBody = new SuccessResponse($msg->body->id, $result);
}
yield $this->protocolWriter->write(new Message($responseBody));
}
}
)
);
$this->protocolReader->on(
'readMessageGroup',
function (): void {
$this->doAnalysis();
}
);
$this->client = new LanguageClient($reader, $writer);
$this->verboseLog("Language server has started.");
}
/**
* The initialize request is sent as the first request from the client to the server.
*
* @param ClientCapabilities $capabilities The capabilities provided by the client (editor)
* @param string|null $rootPath The rootPath of the workspace. Is null if no folder is open.
* @param int|null $processId The process Id of the parent process that started the server.
* Is null if the process has not been started by another process. If the parent process is
* not alive then the server should exit (see exit notification) its process.
* @psalm-return Promise<InitializeResult>
* @psalm-suppress PossiblyUnusedMethod
*/
public function initialize(
ClientCapabilities $capabilities,
?string $rootPath = null,
?int $processId = null
): Promise {
return call(
/** @return Generator<int, true, mixed, InitializeResult> */
function (): Generator {
$this->verboseLog("Initializing...");
$this->clientStatus('initializing');
// Eventually, this might block on something. Leave it as a generator.
/** @psalm-suppress TypeDoesNotContainType */
if (false) {
yield true;
}
$this->verboseLog("Initializing: Getting code base...");
$this->clientStatus('initializing', 'getting code base');
$codebase = $this->project_analyzer->getCodebase();
$this->verboseLog("Initializing: Scanning files...");
$this->clientStatus('initializing', 'scanning files');
$codebase->scanFiles($this->project_analyzer->threads);
$this->verboseLog("Initializing: Registering stub files...");
$this->clientStatus('initializing', 'registering stub files');
$codebase->config->visitStubFiles($codebase);
if ($this->textDocument === null) {
$this->textDocument = new ServerTextDocument(
$this,
$codebase,
$this->project_analyzer
);
}
if ($this->workspace === null) {
$this->workspace = new ServerWorkspace(
$this,
$codebase,
$this->project_analyzer
);
}
$serverCapabilities = new ServerCapabilities();
$textDocumentSyncOptions = new TextDocumentSyncOptions();
$textDocumentSyncOptions->openClose = true;
$saveOptions = new SaveOptions();
$saveOptions->includeText = true;
$textDocumentSyncOptions->save = $saveOptions;
if ($this->project_analyzer->onchange_line_limit === 0) {
$textDocumentSyncOptions->change = TextDocumentSyncKind::NONE;
} else {
$textDocumentSyncOptions->change = TextDocumentSyncKind::FULL;
}
$serverCapabilities->textDocumentSync = $textDocumentSyncOptions;
// Support "Find all symbols"
$serverCapabilities->documentSymbolProvider = false;
// Support "Find all symbols in workspace"
$serverCapabilities->workspaceSymbolProvider = false;
// Support "Go to definition"
$serverCapabilities->definitionProvider = true;
// Support "Find all references"
$serverCapabilities->referencesProvider = false;
// Support "Hover"
$serverCapabilities->hoverProvider = true;
// Support "Completion"
$serverCapabilities->codeActionProvider = true;
// Support "Code Actions"
if ($this->project_analyzer->provide_completion) {
$serverCapabilities->completionProvider = new CompletionOptions();
$serverCapabilities->completionProvider->resolveProvider = false;
$serverCapabilities->completionProvider->triggerCharacters = ['$', '>', ':',"[", "(", ",", " "];
}
$serverCapabilities->signatureHelpProvider = new SignatureHelpOptions(['(', ',']);
// Support global references
$serverCapabilities->xworkspaceReferencesProvider = false;
$serverCapabilities->xdefinitionProvider = false;
$serverCapabilities->dependenciesProvider = false;
$this->verboseLog("Initializing: Complete.");
$this->clientStatus('initialized');
return new InitializeResult($serverCapabilities);
}
);
}
/**
* @psalm-suppress PossiblyUnusedMethod
*
*/
public function initialized(): void
{
$this->clientStatus('running');
}
public function queueTemporaryFileAnalysis(string $file_path, string $uri): void
{
$this->onchange_paths_to_analyze[$file_path] = $uri;
}
public function queueFileAnalysis(string $file_path, string $uri): void
{
$this->onsave_paths_to_analyze[$file_path] = $uri;
}
public function doAnalysis(): void
{
$this->clientStatus('analyzing');
try {
$codebase = $this->project_analyzer->getCodebase();
$all_files_to_analyze = $this->onchange_paths_to_analyze + $this->onsave_paths_to_analyze;
if (!$all_files_to_analyze) {
return;
}
if ($this->onsave_paths_to_analyze) {
$codebase->reloadFiles($this->project_analyzer, array_keys($this->onsave_paths_to_analyze));
}
if ($this->onchange_paths_to_analyze) {
$codebase->reloadFiles($this->project_analyzer, array_keys($this->onchange_paths_to_analyze));
}
$all_file_paths_to_analyze = array_keys($all_files_to_analyze);
$codebase->analyzer->addFilesToAnalyze(
array_combine($all_file_paths_to_analyze, $all_file_paths_to_analyze)
);
$codebase->analyzer->analyzeFiles($this->project_analyzer, 1, false);
$this->emitIssues($all_files_to_analyze);
$this->onchange_paths_to_analyze = [];
$this->onsave_paths_to_analyze = [];
} finally {
// we are done, so set the status back to running
$this->clientStatus('running');
}
}
/**
* @param array<string, string> $uris
*
*/
public function emitIssues(array $uris): void
{
$data = IssueBuffer::clear();
$this->current_issues = $data;
foreach ($uris as $file_path => $uri) {
$diagnostics = [];
foreach (($data[$file_path] ?? []) as $issue_data) {
//$check_name = $issue->check_name;
$description = $issue_data->message;
$severity = $issue_data->severity;
$start_line = max($issue_data->line_from, 1);
$end_line = $issue_data->line_to;
$start_column = $issue_data->column_from;
$end_column = $issue_data->column_to;
// Language server has 0 based lines and columns, phan has 1-based lines and columns.
$range = new Range(
new Position($start_line - 1, $start_column - 1),
new Position($end_line - 1, $end_column - 1)
);
switch ($severity) {
case Config::REPORT_INFO:
$diagnostic_severity = DiagnosticSeverity::WARNING;
break;
case Config::REPORT_ERROR:
default:
$diagnostic_severity = DiagnosticSeverity::ERROR;
break;
}
$diagnostic = new Diagnostic(
$description,
$range,
null,
$diagnostic_severity,
'Psalm'
);
//$code = 'PS' . \str_pad((string) $issue_data->shortcode, 3, "0", \STR_PAD_LEFT);
$code = $issue_data->link;
if ($this->project_analyzer->language_server_use_extended_diagnostic_codes) {
// Added in VSCode 1.43.0 and will be part of the LSP 3.16.0 standard.
// Since this new functionality is not backwards compatible, we use a
// configuration option so the end user must opt in to it using the cli argument.
// https://github.com/microsoft/vscode/blob/1.43.0/src/vs/vscode.d.ts#L4688-L4699
/** @psalm-suppress InvalidPropertyAssignmentValue */
$diagnostic->code = [
"value" => $code,
"target" => $issue_data->link,
];
} else {
// the Diagnostic constructor only takes `int` for the code, but the property can be
// `int` or `string`, so we set the property directly because we want to use a `string`
/** @psalm-suppress InvalidPropertyAssignmentValue */
$diagnostic->code = $code;
}
$diagnostics[] = $diagnostic;
}
$this->client->textDocument->publishDiagnostics($uri, $diagnostics);
}
}
/**
* The shutdown request is sent from the client to the server. It asks the server to shut down,
* but to not exit (otherwise the response might not be delivered correctly to the client).
* There is a separate exit notification that asks the server to exit.
* @psalm-suppress PossiblyUnusedReturnValue
*/
public function shutdown(): Promise
{
$this->clientStatus('closing');
$this->verboseLog("Shutting down...");
$codebase = $this->project_analyzer->getCodebase();
$scanned_files = $codebase->scanner->getScannedFiles();
$codebase->file_reference_provider->updateReferenceCache(
$codebase,
$scanned_files
);
$this->clientStatus('closed');
return new Success(null);
}
/**
* A notification to ask the server to exit its process.
*
*/
public function exit(): void
{
exit(0);
}
/**
* Send log message to the client
*
* @param string $message The log message to send to the client.
* @psalm-param 1|2|3|4 $type
* @param int $type The log type:
* - 1 = Error
* - 2 = Warning
* - 3 = Info
* - 4 = Log
*/
public function verboseLog(string $message, int $type = 4): void
{
if ($this->project_analyzer->language_server_verbose) {
try {
$this->client->logMessage(
'[Psalm ' .PSALM_VERSION. ' - PHP Language Server] ' . $message,
$type
);
} catch (Throwable $err) {
// do nothing
}
}
new Success(null);
}
/**
* Send status message to client. This is the same as sending a log message,
* except this is meant for parsing by the client to present status updates in a UI.
*
* @param string $status The log message to send to the client. Should not contain colons `:`.
* @param string|null $additional_info This is additional info that the client
* can use as part of the display message.
*/
private function clientStatus(string $status, ?string $additional_info = null): void
{
try {
// here we send a notification to the client using the telemetry notification method
$this->client->logMessage(
$status . (!empty($additional_info) ? ': ' . $additional_info : ''),
3,
'telemetry/event'
);
} catch (Throwable $err) {
// do nothing
}
new Success(null);
}
/**
* Transforms an absolute file path into a URI as used by the language server protocol.
*
* @psalm-pure
*/
public static function pathToUri(string $filepath): string
{
$filepath = trim(str_replace('\\', '/', $filepath), '/');
$parts = explode('/', $filepath);
// Don't %-encode the colon after a Windows drive letter
$first = array_shift($parts);
if (substr($first, -1) !== ':') {
$first = rawurlencode($first);
}
$parts = array_map('rawurlencode', $parts);
array_unshift($parts, $first);
$filepath = implode('/', $parts);
return 'file:///' . $filepath;
}
/**
* Transforms URI into file path
*
*
*/
public static function uriToPath(string $uri): string
{
$fragments = parse_url($uri);
if ($fragments === false
|| !isset($fragments['scheme'])
|| $fragments['scheme'] !== 'file'
|| !isset($fragments['path'])
) {
throw new InvalidArgumentException("Not a valid file URI: $uri");
}
$filepath = urldecode($fragments['path']);
if (strpos($filepath, ':') !== false) {
if ($filepath[0] === '/') {
$filepath = substr($filepath, 1);
}
$filepath = str_replace('/', '\\', $filepath);
}
$realpath = realpath($filepath);
if ($realpath !== false) {
return $realpath;
}
return $filepath;
}
/**
* Get the value of current_issues
*
* @return array<string, list<IssueData>>
*/
public function getCurrentIssues(): array
{
return $this->current_issues;
}
}