forked from phpactor/language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormattingHandler.php
More file actions
57 lines (50 loc) · 1.93 KB
/
FormattingHandler.php
File metadata and controls
57 lines (50 loc) · 1.93 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
<?php
namespace Phpactor\LanguageServer\Handler\TextDocument;
use Amp\Promise;
use Phpactor\LanguageServerProtocol\FormattingOptions;
use Phpactor\LanguageServerProtocol\ServerCapabilities;
use Phpactor\LanguageServerProtocol\TextDocumentIdentifier;
use Phpactor\LanguageServerProtocol\TextEdit;
use Phpactor\LanguageServer\Core\Formatting\Formatter;
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 FormattingHandler implements Handler, CanRegisterCapabilities
{
public function __construct(
private Workspace $workspace,
private Formatter $formatter,
private ProgressNotifier $notifier = new SilentWorkDoneProgressNotifier(),
) {
}
public function methods(): array
{
return ['textDocument/formatting' => 'formatting'];
}
/**
* @return Promise<array<int,TextEdit[]>|null>
*/
public function formatting(TextDocumentIdentifier $textDocument, FormattingOptions $options): Promise
{
return call(function () use ($textDocument) {
$token = WorkDoneToken::generate();
yield $this->notifier->create($token);
$document = $this->workspace->get($textDocument->uri);
$this->notifier->begin($token, 'Formatting document');
try {
$formatted = yield $this->formatter->format($document);
} finally {
$this->notifier->end($token);
}
return $formatted;
});
}
public function registerCapabiltiies(ServerCapabilities $capabilities): void
{
$capabilities->documentFormattingProvider = true;
}
}