-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTextDocumentHandler.php
More file actions
98 lines (84 loc) · 3.37 KB
/
TextDocumentHandler.php
File metadata and controls
98 lines (84 loc) · 3.37 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
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
<?php
namespace Phpactor\LanguageServer\Handler\TextDocument;
use Phpactor\LanguageServerProtocol\DidChangeTextDocumentParams;
use Phpactor\LanguageServerProtocol\DidCloseTextDocumentParams;
use Phpactor\LanguageServerProtocol\DidOpenTextDocumentParams;
use Phpactor\LanguageServerProtocol\DidSaveTextDocumentParams;
use Phpactor\LanguageServerProtocol\ServerCapabilities;
use Phpactor\LanguageServerProtocol\TextDocumentContentChangeFullEvent;
use Phpactor\LanguageServerProtocol\TextDocumentContentChangeIncrementalEvent;
use Phpactor\LanguageServerProtocol\TextDocumentSyncKind;
use Phpactor\LanguageServerProtocol\WillSaveTextDocumentParams;
use Phpactor\LanguageServer\Core\Handler\CanRegisterCapabilities;
use Phpactor\LanguageServer\Core\Handler\Handler;
use Phpactor\LanguageServer\Event\TextDocumentClosed;
use Phpactor\LanguageServer\Event\TextDocumentOpened;
use Phpactor\LanguageServer\Event\TextDocumentIncrementallyUpdated;
use Phpactor\LanguageServer\Event\TextDocumentSaved;
use Phpactor\LanguageServer\Event\TextDocumentUpdated;
use Psr\EventDispatcher\EventDispatcherInterface;
final class TextDocumentHandler implements Handler, CanRegisterCapabilities
{
/**
* @param TextDocumentSyncKind::* $syncKind
*/
public function __construct(
private EventDispatcherInterface $dispatcher,
private int $syncKind = TextDocumentSyncKind::FULL,
) {
}
public function methods(): array
{
return [
'textDocument/didOpen' => 'didOpen',
'textDocument/didChange' => 'didChange',
'textDocument/didClose' => 'didClose',
'textDocument/didSave' => 'didSave',
'textDocument/willSave' => 'willSave',
'textDocument/willSaveWaitUntil' => 'willSaveWaitUntil',
];
}
public function didOpen(DidOpenTextDocumentParams $params): void
{
$this->dispatcher->dispatch(new TextDocumentOpened($params->textDocument));
}
public function didChange(DidChangeTextDocumentParams $params): void
{
$increments = [];
foreach ($params->contentChanges as $contentChange) {
if ($contentChange instanceof TextDocumentContentChangeIncrementalEvent) {
$increments[] = $contentChange;
continue;
}
if ($contentChange instanceof TextDocumentContentChangeFullEvent) {
$this->dispatcher->dispatch(new TextDocumentUpdated($params->textDocument, $contentChange->text));
continue;
}
}
if ($increments === []) {
return;
}
$this->dispatcher->dispatch(new TextDocumentIncrementallyUpdated(
$params->textDocument,
$increments,
));
}
public function didClose(DidCloseTextDocumentParams $params): void
{
$this->dispatcher->dispatch(new TextDocumentClosed($params->textDocument));
}
public function didSave(DidSaveTextDocumentParams $params): void
{
$this->dispatcher->dispatch(new TextDocumentSaved($params->textDocument, $params->text));
}
public function willSave(WillSaveTextDocumentParams $params): void
{
}
public function willSaveWaitUntil(WillSaveTextDocumentParams $params): void
{
}
public function registerCapabiltiies(ServerCapabilities $capabilities): void
{
$capabilities->textDocumentSync = $this->syncKind;
}
}