This repository was archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHighlighter.php
298 lines (270 loc) · 12.6 KB
/
Highlighter.php
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace Phpactor\Extension\LanguageServerReferenceFinder\Model;
use Generator;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\ConstElement;
use Microsoft\PhpParser\Node\Expression\AssignmentExpression;
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\Expression\MemberAccessExpression;
use Microsoft\PhpParser\Node\Expression\ScopedPropertyAccessExpression;
use Microsoft\PhpParser\Node\Expression\Variable;
use Microsoft\PhpParser\Node\MethodDeclaration;
use Microsoft\PhpParser\Node\NamespaceUseClause;
use Microsoft\PhpParser\Node\Parameter;
use Microsoft\PhpParser\Node\PropertyDeclaration;
use Microsoft\PhpParser\Node\QualifiedName;
use Microsoft\PhpParser\Node\SourceFileNode;
use Microsoft\PhpParser\Node\Statement\ClassDeclaration;
use Microsoft\PhpParser\Parser;
use Microsoft\PhpParser\Token;
use Phpactor\Extension\LanguageServerBridge\Converter\PositionConverter;
use Phpactor\LanguageServerProtocol\DocumentHighlight;
use Phpactor\LanguageServerProtocol\DocumentHighlightKind;
use Phpactor\LanguageServerProtocol\Range;
use Phpactor\TextDocument\ByteOffset;
class Highlighter
{
/**
* @var Parser
*/
private $parser;
public function __construct(Parser $parser)
{
$this->parser = $parser;
}
public function highlightsFor(string $source, ByteOffset $offset): Highlights
{
$rootNode = $this->parser->parseSourceFile($source);
$node = $rootNode->getDescendantNodeAtPosition($offset->toInt());
if ($node instanceof Variable && $node->getFirstAncestor(PropertyDeclaration::class)) {
return Highlights::fromIterator($this->properties($rootNode, $node->getName()), );
}
if ($node instanceof Parameter) {
return Highlights::fromIterator($this->variables($rootNode, (string)$node->getName()));
}
if ($node instanceof Variable) {
return Highlights::fromIterator($this->variables($rootNode, (string)$node->getName()));
}
if ($node instanceof MethodDeclaration) {
return Highlights::fromIterator($this->methods($rootNode, $node->getName()));
}
if ($node instanceof ClassDeclaration) {
return Highlights::fromIterator($this->namespacedNames($rootNode, (string)$node->getNamespacedName()));
}
if ($node instanceof ConstElement) {
return Highlights::fromIterator($this->constants($rootNode, (string)$node->getNamespacedName()));
}
if ($node instanceof QualifiedName) {
return Highlights::fromIterator($this->namespacedNames($rootNode, (string)$node->getResolvedName() ?: (string)$node->getNamespacedName()));
}
if ($node instanceof ScopedPropertyAccessExpression) {
$memberName = $node->memberName;
if (!$memberName instanceof Token) {
return Highlights::empty();
}
return Highlights::fromIterator($this->memberAccess($rootNode, $node, (string)$memberName->getText($rootNode->getFileContents())));
}
if ($node instanceof MemberAccessExpression) {
return Highlights::fromIterator($this->memberAccess($rootNode, $node, (string)$node->memberName->getText($rootNode->getFileContents())));
}
return Highlights::empty();
}
/**
* @return Generator<DocumentHighlight>
*/
private function variables(SourceFileNode $rootNode, string $name): Generator
{
$name = $this->normalizeVarName($name);
foreach ($rootNode->getDescendantNodes() as $childNode) {
if ($childNode instanceof Variable && $childNode->getName() === $name) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($childNode->getStart(), $childNode->getFileContents()),
PositionConverter::intByteOffsetToPosition($childNode->getEndPosition(), $childNode->getFileContents())
),
$this->variableKind($childNode)
);
}
if ($childNode instanceof Parameter && $this->normalizeVarName((string)$childNode->variableName->getText($childNode->getFileContents())) === $name) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($childNode->variableName->getStartPosition(), $childNode->getFileContents()),
PositionConverter::intByteOffsetToPosition($childNode->variableName->getEndPosition(), $childNode->getFileContents()),
),
DocumentHighlightKind::READ
);
}
}
}
/**
* @return DocumentHighlightKind::*
* @phpstan-ignore-next-line
*/
private function variableKind(Node $node): int
{
$expression = $node->parent;
if ($expression instanceof AssignmentExpression) {
if ($expression->leftOperand === $node) {
return DocumentHighlightKind::WRITE;
}
}
return DocumentHighlightKind::READ;
}
/**
* @return Generator<DocumentHighlight>
*/
private function properties(Node $rootNode, string $name): Generator
{
foreach ($rootNode->getDescendantNodes() as $node) {
if ($node instanceof Variable && $node->getFirstAncestor(PropertyDeclaration::class) && (string)$node->getName() === $name) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->getStart(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->getEndPosition(), $node->getFileContents())
),
DocumentHighlightKind::TEXT
);
}
if ($node instanceof MemberAccessExpression) {
if ($name === $node->memberName->getText($rootNode->getFileContents())) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->memberName->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->memberName->getEndPosition(), $node->getFileContents())
),
$this->variableKind($node)
);
}
}
}
}
/**
* @return Generator<DocumentHighlight>
*/
private function memberAccess(SourceFileNode $rootNode, Node $node, string $memberName): Generator
{
if ($node->parent instanceof CallExpression) {
return yield from $this->methods($rootNode, $memberName);
}
if (false !== strpos($node->getText(), '$')) {
return yield from $this->properties($rootNode, $memberName);
}
return yield from $this->constants($rootNode, $memberName);
}
/**
* @return Generator<DocumentHighlight>
*/
private function methods(SourceFileNode $rootNode, string $name): Generator
{
foreach ($rootNode->getDescendantNodes() as $node) {
if ($node instanceof MethodDeclaration && $node->getName() === $name) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->name->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->name->getEndPosition(), $node->getFileContents())
),
DocumentHighlightKind::TEXT
);
}
if ($node instanceof MemberAccessExpression) {
if ($name === $node->memberName->getText($rootNode->getFileContents())) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->memberName->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->memberName->getEndPosition(), $node->getFileContents())
),
$this->variableKind($node)
);
}
}
if ($node instanceof ScopedPropertyAccessExpression) {
$memberName = $node->memberName;
if (!$memberName instanceof Token) {
return;
}
if ($name === $memberName->getText($rootNode->getFileContents())) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($memberName->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($memberName->getEndPosition(), $node->getFileContents())
),
$this->variableKind($node)
);
}
}
}
}
/**
* @return Generator<DocumentHighlight>
*/
private function constants(SourceFileNode $rootNode, string $name): Generator
{
foreach ($rootNode->getDescendantNodes() as $node) {
if ($node instanceof ConstElement && (string)$node->getNamespacedName() === $name) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->name->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->name->getEndPosition(), $node->getFileContents())
),
DocumentHighlightKind::TEXT
);
}
if ($node instanceof ScopedPropertyAccessExpression) {
$memberName = $node->memberName;
if (!$memberName instanceof Token) {
return;
}
if ($name === $memberName->getText($rootNode->getFileContents())) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($memberName->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($memberName->getEndPosition(), $node->getFileContents())
),
$this->variableKind($node)
);
}
}
}
}
/**
* @return Generator<DocumentHighlight>
*/
private function namespacedNames(Node $rootNode, string $fullyQualfiiedName): Generator
{
foreach ($rootNode->getDescendantNodes() as $node) {
if ($node instanceof NamespaceUseClause && $node->namespaceName instanceof QualifiedName && (string)$node->namespaceName === $fullyQualfiiedName) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->namespaceName->getStart(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->namespaceName->getEndPosition(), $node->getFileContents())
),
DocumentHighlightKind::TEXT
);
}
if ($node instanceof ClassDeclaration && (string)$node->getNamespacedName() === $fullyQualfiiedName) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->name->getStartPosition(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->name->getEndPosition(), $node->getFileContents())
),
DocumentHighlightKind::TEXT
);
}
if ($node instanceof QualifiedName) {
if ($fullyQualfiiedName === (string)$node->getResolvedName()) {
yield new DocumentHighlight(
new Range(
PositionConverter::intByteOffsetToPosition($node->getStart(), $node->getFileContents()),
PositionConverter::intByteOffsetToPosition($node->getEndPosition(), $node->getFileContents())
),
$this->variableKind($node)
);
}
}
}
}
private function normalizeVarName(string $varName): string
{
return ltrim($varName, '$');
}
}