|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phpactor\LanguageServer\Core\Diagnostics; |
| 4 | + |
| 5 | +use Amp\CancelledException; |
| 6 | +use Phpactor\LanguageServer\Core\Server\ClientApi; |
| 7 | +use Phpactor\LanguageServer\Event\TextDocumentSaved; |
| 8 | +use Phpactor\LanguageServer\Event\TextDocumentUpdated; |
| 9 | +use Phpactor\LanguageServer\Core\Rpc\NotificationMessage; |
| 10 | +use Amp\Promise; |
| 11 | +use Amp\CancellationToken; |
| 12 | +use Amp\Deferred; |
| 13 | +use Phpactor\LanguageServerProtocol\TextDocumentItem; |
| 14 | + |
| 15 | +class DiagnosticsEngine |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var int |
| 19 | + */ |
| 20 | + private $pollTime; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Deferred<TextDocumentItem> |
| 24 | + */ |
| 25 | + private $deferred; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var bool |
| 29 | + */ |
| 30 | + private $running = false; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ?TextDocumentItem |
| 34 | + */ |
| 35 | + private $next; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var DiagnosticsProvider |
| 39 | + */ |
| 40 | + private $provider; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ClientApi |
| 44 | + */ |
| 45 | + private $clientApi; |
| 46 | + |
| 47 | + public function __construct(ClientApi $clientApi, DiagnosticsProvider $provider, int $pollTime = 100) |
| 48 | + { |
| 49 | + $this->pollTime = $pollTime; |
| 50 | + $this->deferred = new Deferred(); |
| 51 | + $this->provider = $provider; |
| 52 | + $this->clientApi = $clientApi; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return Promise<bool> |
| 57 | + */ |
| 58 | + public function run(CancellationToken $token): Promise |
| 59 | + { |
| 60 | + return \Amp\call(function () use ($token) { |
| 61 | + while (true) { |
| 62 | + try { |
| 63 | + $token->throwIfRequested(); |
| 64 | + } catch (CancelledException $cancelled) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // if another update came in while doing the previous lint use |
| 69 | + // use that. |
| 70 | + if ($this->next) { |
| 71 | + $textDocument = $this->next; |
| 72 | + $this->next = null; |
| 73 | + } else { |
| 74 | + $textDocument = yield $this->deferred->promise(); |
| 75 | + } |
| 76 | + |
| 77 | + $this->deferred = new Deferred(); |
| 78 | + |
| 79 | + // after we have reset deferred, we can safely set linting to |
| 80 | + // `false` and let another resolve happen |
| 81 | + $this->running = false; |
| 82 | + |
| 83 | + assert($textDocument instanceof TextDocumentItem); |
| 84 | + |
| 85 | + $this->clientApi->diagnostics()->publishDiagnostics( |
| 86 | + $textDocument->uri, |
| 87 | + $textDocument->version, |
| 88 | + yield $this->provider->provideDiagnostics($textDocument) |
| 89 | + ); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + public function enqueue(TextDocumentItem $textDocument): void |
| 95 | + { |
| 96 | + // if we are already linting then store whatever comes afterwards in |
| 97 | + // next, overwriting the redundant update |
| 98 | + if ($this->running === true) { |
| 99 | + $this->next = $textDocument; |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // resolving the promise will start PHPStan |
| 104 | + $this->running = true; |
| 105 | + $this->deferred->resolve($textDocument); |
| 106 | + } |
| 107 | +} |
0 commit comments