forked from phpactor/language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeActionHandler.php
More file actions
64 lines (58 loc) · 2.17 KB
/
CodeActionHandler.php
File metadata and controls
64 lines (58 loc) · 2.17 KB
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
<?php
namespace Phpactor\LanguageServer\Handler\TextDocument;
use Amp\CancellationToken;
use Amp\Promise;
use Phpactor\LanguageServerProtocol\CodeAction;
use Phpactor\LanguageServerProtocol\CodeActionOptions;
use Phpactor\LanguageServerProtocol\CodeActionParams;
use Phpactor\LanguageServerProtocol\CodeActionRequest;
use Phpactor\LanguageServerProtocol\ServerCapabilities;
use Phpactor\LanguageServer\Core\CodeAction\CodeActionProvider;
use Phpactor\LanguageServer\Core\Handler\CanRegisterCapabilities;
use Phpactor\LanguageServer\Core\Handler\Handler;
use Phpactor\LanguageServer\Core\Workspace\Workspace;
use Phpactor\LanguageServer\WorkDoneProgress\ProgressNotifier;
use Phpactor\LanguageServer\WorkDoneProgress\SilentWorkDoneProgressNotifier;
use Phpactor\LanguageServer\WorkDoneProgress\WorkDoneToken;
use function Amp\call;
class CodeActionHandler implements Handler, CanRegisterCapabilities
{
public function __construct(
private CodeActionProvider $provider,
private Workspace $workspace,
private ProgressNotifier $notifier = new SilentWorkDoneProgressNotifier(),
) {
}
/**
* {@inheritDoc}
*/
public function methods(): array
{
return [
CodeActionRequest::METHOD => 'codeAction'
];
}
public function registerCapabiltiies(ServerCapabilities $capabilities): void
{
$options = new CodeActionOptions($this->provider->kinds());
$capabilities->codeActionProvider = $options;
}
/**
* @return Promise<array<CodeAction>>
*/
public function codeAction(CodeActionParams $params, CancellationToken $cancel): Promise
{
return call(function () use ($params, $cancel) {
$token = WorkDoneToken::generate();
yield $this->notifier->create($token);
$document = $this->workspace->get($params->textDocument->uri);
$this->notifier->begin($token, title: 'Resolving code actions');
try {
$actions = yield $this->provider->provideActionsFor($document, $params->range, $cancel);
} finally {
$this->notifier->end($token);
}
return $actions;
});
}
}