Skip to content

Commit 34c0a63

Browse files
authored
Fixing the document symbol provider (phpactor#2678)
1 parent afc9cb3 commit 34c0a63

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

lib/Extension/LanguageServerSymbolProvider/Adapter/TolerantDocumentSymbolProvider.php

+33-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Microsoft\PhpParser\Node\DelimitedList\ExpressionList;
1212
use Microsoft\PhpParser\Node\EnumCaseDeclaration;
1313
use Microsoft\PhpParser\Node\EnumMembers;
14+
use Microsoft\PhpParser\Node\Expression;
15+
use Microsoft\PhpParser\Node\Expression\AssignmentExpression;
1416
use Microsoft\PhpParser\Node\Expression\Variable;
1517
use Microsoft\PhpParser\Node\InterfaceMembers;
1618
use Microsoft\PhpParser\Node\MethodDeclaration;
@@ -141,21 +143,13 @@ private function buildNode(Node $node, string $source): ?DocumentSymbol
141143
}
142144

143145
if ($node instanceof Variable) {
144-
if ($node->getFirstAncestor(PropertyDeclaration::class)) {
145-
return new DocumentSymbol(
146-
(string)$node->getName(),
147-
SymbolKind::PROPERTY,
148-
new Range(
149-
PositionConverter::intByteOffsetToPosition($node->parent->getStartPosition(), $source),
150-
PositionConverter::intByteOffsetToPosition($node->parent->getEndPosition(), $source)
151-
),
152-
new Range(
153-
PositionConverter::intByteOffsetToPosition($node->getStartPosition(), $source),
154-
PositionConverter::intByteOffsetToPosition($node->getEndPosition(), $source)
155-
),
156-
children: []
157-
);
158-
}
146+
return $this->resolvePropertyVariable($node, $source);
147+
}
148+
149+
if ($node instanceof AssignmentExpression) {
150+
/** @var Expression $left */
151+
$left = $node->leftOperand;
152+
return $this->resolvePropertyVariable($left, $source);
159153
}
160154

161155
if ($node instanceof ConstElement) {
@@ -209,6 +203,30 @@ private function buildNode(Node $node, string $source): ?DocumentSymbol
209203
return null;
210204
}
211205

206+
207+
private function resolvePropertyVariable(Node $node, string $source): ?DocumentSymbol
208+
{
209+
if (!$node instanceof Variable) {
210+
return null;
211+
}
212+
if (!$node->getFirstAncestor(PropertyDeclaration::class)) {
213+
return null;
214+
}
215+
return new DocumentSymbol(
216+
(string)$node->getName(),
217+
SymbolKind::PROPERTY,
218+
new Range(
219+
PositionConverter::intByteOffsetToPosition($node->parent->getStartPosition(), $source),
220+
PositionConverter::intByteOffsetToPosition($node->parent->getEndPosition(), $source)
221+
),
222+
new Range(
223+
PositionConverter::intByteOffsetToPosition($node->getStartPosition(), $source),
224+
PositionConverter::intByteOffsetToPosition($node->getEndPosition(), $source)
225+
),
226+
children: []
227+
);
228+
}
229+
212230
private function memberNodes(Node $node): Generator
213231
{
214232
return $node->getDescendantNodes(function (Node $node) {

lib/Extension/LanguageServerSymbolProvider/Tests/Unit/Adapter/TolerantDocumentSymbolProviderTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ public function provideClasses(): Generator
116116
]
117117
];
118118

119+
yield 'class property with value' => [
120+
'<?php class Foo { private $bar = "Hallo"; }',
121+
[
122+
DocumentSymbol::fromArray([
123+
'name' => 'Foo',
124+
'kind' => SymbolKind::CLASS_,
125+
'range' => $this->dummyRange(),
126+
'selectionRange' => $this->dummyRange(),
127+
'children' => [
128+
DocumentSymbol::fromArray([
129+
'name' => 'bar',
130+
'kind' => SymbolKind::PROPERTY,
131+
'range' => $this->dummyRange(),
132+
'selectionRange' => $this->dummyRange(),
133+
'children' => [],
134+
]),
135+
]
136+
])
137+
]
138+
];
139+
119140
yield 'class property' => [
120141
'<?php class Foo { private $bar; }',
121142
[

0 commit comments

Comments
 (0)