-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDidChangeTextDocumentParams.php
More file actions
124 lines (107 loc) · 4.19 KB
/
DidChangeTextDocumentParams.php
File metadata and controls
124 lines (107 loc) · 4.19 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php // Auto-generated from vscode-languageserver-protocol (typescript)
namespace Phpactor\LanguageServerProtocol;
use DTL\Invoke\Invoke;
use Exception;
use RuntimeException;
/**
* The change text document notification's parameters.
*/
class DidChangeTextDocumentParams
{
/**
* The document that did change. The version number points
* to the version after all provided content changes have
* been applied.
*
* @var VersionedTextDocumentIdentifier
*/
public $textDocument;
/**
* The actual content changes. The content changes describe single state changes
* to the document. So if there are two content changes c1 (at array index 0) and
* c2 (at array index 1) for a document in state S then c1 moves the document from
* S to S' and c2 from S' to S''. So c1 is computed on the state S and c2 is computed
* on the state S'.
*
* To mirror the content of a document using change events use the following approach:
* - start with the same initial content
* - apply the 'textDocument/didChange' notifications in the order you receive them.
* - apply the `TextDocumentContentChangeEvent`s in a single notification in the order
* you receive them.
*
* @var array<TextDocumentContentChangeIncrementalEvent|TextDocumentContentChangeFullEvent>
*/
public $contentChanges;
/**
* @param VersionedTextDocumentIdentifier $textDocument
* @param array<TextDocumentContentChangeIncrementalEvent|TextDocumentContentChangeFullEvent> $contentChanges
*/
public function __construct(VersionedTextDocumentIdentifier $textDocument, array $contentChanges)
{
$this->textDocument = $textDocument;
$this->contentChanges = $contentChanges;
}
/**
* @param array<string,mixed> $array
* @return self
*/
public static function fromArray(array $array, bool $allowUnknownKeys = false)
{
$map = [
'textDocument' => ['names' => [VersionedTextDocumentIdentifier::class], 'iterable' => false],
'contentChanges' => ['names' => [TextDocumentContentChangeIncrementalEvent::class, TextDocumentContentChangeFullEvent::class], 'iterable' => true],
];
foreach ($array as $key => &$value) {
if (!isset($map[$key])) {
if ($allowUnknownKeys) {
unset($array[$key]);
continue;
}
throw new RuntimeException(sprintf(
'Parameter "%s" on class "%s" not known, known parameters: "%s"',
$key,
self::class,
implode('", "', array_keys($map))
));
}
// from here we only care about arrays that can be transformed into
// objects
if (!is_array($value)) {
continue;
}
if (empty($map[$key]['names'])) {
continue;
}
if ($map[$key]['iterable']) {
$value = array_map(function ($object) use ($map, $key, $allowUnknownKeys) {
if (!is_array($object)) {
return $object;
}
return self::invokeFromNames($map[$key]['names'], $object, $allowUnknownKeys) ?: $object;
}, $value);
continue;
}
$names = $map[$key]['names'];
$value = self::invokeFromNames($names, $value, $allowUnknownKeys) ?: $value;
}
return Invoke::new(self::class, $array);
}
/**
* @param array<string> $classNames
* @param array<string,mixed> $object
*/
private static function invokeFromNames(array $classNames, array $object, bool $allowUnknownKeys): ?object
{
$lastException = null;
foreach ($classNames as $className) {
try {
// @phpstan-ignore-next-line
return call_user_func_array($className . '::fromArray', [$object, $allowUnknownKeys]);
} catch (Exception $exception) {
$lastException = $exception;
continue;
}
}
throw $lastException;
}
}