Diagnostic Providers¶
Diagnostic providers are invoked when text documents are updated and are responsible to send diagnostics (e.g. actual or potential problems with the code) to the client.
Example¶
Example of a diagnostic provider:
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 | <?php namespace Phpactor\LanguageServer\Example\Diagnostics; use Amp\CancellationToken; use Amp\Promise; use Phpactor\LanguageServerProtocol\Diagnostic; use Phpactor\LanguageServerProtocol\DiagnosticSeverity; use Phpactor\LanguageServerProtocol\Position; use Phpactor\LanguageServerProtocol\Range; use Phpactor\LanguageServerProtocol\TextDocumentItem; use Phpactor\LanguageServer\Core\Diagnostics\DiagnosticsProvider; use function Amp\call; class SayHelloDiagnosticsProvider implements DiagnosticsProvider { /** * {@inheritDoc} */ public function provideDiagnostics(TextDocumentItem $textDocument, CancellationToken $cancel): Promise { /** @phpstan-ignore-next-line */ return call(function () { return [ new Diagnostic( new Range( new Position(0, 0), new Position(1, 0) ), 'This is the first line, hello!', DiagnosticSeverity::INFORMATION ) ]; }); } public function name(): string { return 'say-hello'; } } |
$diagnosticsService = new DiagnosticsService(
new DiagnosticsEngine($clientApi, new AggregateDiagnosticsProvider(
$logger,
new SayHelloDiagnosticsProvider()
))
);
Integration¶
Diagnostics are facilitated through the “Diagnostics Service” which in turn
requires the DiagnosticsEngine
which accepts a DiagnosticProvider
-
below we use the AggregateDiagnosticsProvider
which allows you to provide
many diagnostic providers:
<?php
$diagnosticsService = new DiagnosticsService(
new DiagnosticsEngine($clientApi, new AggregateDiagnosticsProvider(
$logger,
new SayHelloDiagnosticsProvider()
))
);