Skip to content

Commit 1106521

Browse files
committed
Add more tests
1 parent 49f16f5 commit 1106521

File tree

2 files changed

+142
-10
lines changed

2 files changed

+142
-10
lines changed

src/Server/TextDocument.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use LanguageServer\{LanguageClient, Project};
77
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
8+
use PhpParser\Node;
89
use LanguageServer\Protocol\{
910
TextDocumentItem,
1011
TextDocumentIdentifier,
@@ -164,21 +165,36 @@ public function definition(TextDocumentIdentifier $textDocument, Position $posit
164165
*
165166
* @param TextDocumentIdentifier $textDocument The text document
166167
* @param Position $position The position inside the text document
167-
* @return Hover|null
168+
* @return Hover
168169
*/
169-
public function hover(TextDocumentIdentifier $textDocument, Position $position)
170+
public function hover(TextDocumentIdentifier $textDocument, Position $position): Hover
170171
{
171172
$document = $this->project->getDocument($textDocument->uri);
173+
// Find the node under the cursor
172174
$node = $document->getNodeAtPosition($position);
173175
if ($node === null) {
174-
return null;
176+
return new Hover([]);
175177
}
178+
$range = Range::fromNode($node);
179+
// Get the definition node for whatever node is under the cursor
176180
$def = $document->getDefinitionByNode($node);
177181
if ($def === null) {
178-
return null;
182+
return new Hover([], $range);
179183
}
180184
$contents = [];
181-
$defLine = clone $def;
185+
186+
// Build a declaration string
187+
if ($def instanceof Node\Stmt\PropertyProperty || $def instanceof Node\Const_) {
188+
// Properties and constants can have multiple declarations
189+
// Use the parent node (that includes the modifiers), but only render the requested declaration
190+
$child = $def;
191+
$def = $def->getAttribute('parentNode');
192+
$defLine = clone $def;
193+
$defLine->props = [$child];
194+
} else {
195+
$defLine = clone $def;
196+
}
197+
// Don't include the docblock in the declaration string
182198
$defLine->setAttribute('comments', []);
183199
if (isset($defLine->stmts)) {
184200
$defLine->stmts = [];
@@ -188,10 +204,27 @@ public function hover(TextDocumentIdentifier $textDocument, Position $position)
188204
if (isset($lines[0])) {
189205
$contents[] = new MarkedString('php', "<?php\n" . $lines[0]);
190206
}
191-
$docBlock = $def->getAttribute('docBlock');
192-
if ($docBlock !== null) {
193-
$contents[] = $docBlock->getSummary();
207+
208+
// Get the documentation string
209+
if ($def instanceof Node\Param) {
210+
$fn = $def->getAttribute('parentNode');
211+
$docBlock = $fn->getAttribute('docBlock');
212+
if ($docBlock !== null) {
213+
$tags = $docBlock->getTagsByName('param');
214+
foreach ($tags as $tag) {
215+
if ($tag->getVariableName() === $def->name) {
216+
$contents[] = $tag->getDescription()->render();
217+
break;
218+
}
219+
}
220+
}
221+
} else {
222+
$docBlock = $def->getAttribute('docBlock');
223+
if ($docBlock !== null) {
224+
$contents[] = $docBlock->getSummary();
225+
}
194226
}
195-
return new Hover($contents, Range::fromNode($node));
227+
228+
return new Hover($contents, $range);
196229
}
197230
}

tests/Server/TextDocument/HoverTest.php

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,91 @@ public function testHoverForClassLike()
2323
], $reference->range), $result);
2424
}
2525

26-
public function testHoverWithoutDocBlock()
26+
public function testHoverForMethod()
27+
{
28+
// $obj->testMethod();
29+
// Get hover for testMethod
30+
$reference = $this->getReferenceLocations('TestClass::testMethod()')[0];
31+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
32+
$this->assertEquals(new Hover([
33+
new MarkedString('php', "<?php\npublic function testMethod(\$testParameter)"),
34+
'Non culpa nostrud mollit esse sunt laboris in irure ullamco cupidatat amet.'
35+
], $reference->range), $result);
36+
}
37+
38+
public function testHoverForProperty()
39+
{
40+
// echo $obj->testProperty;
41+
// Get hover for testProperty
42+
$reference = $this->getReferenceLocations('TestClass::testProperty')[0];
43+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
44+
$this->assertEquals(new Hover([
45+
new MarkedString('php', "<?php\npublic \$testProperty;"),
46+
'Reprehenderit magna velit mollit ipsum do.'
47+
], $reference->range), $result);
48+
}
49+
50+
public function testHoverForStaticMethod()
51+
{
52+
// TestClass::staticTestMethod();
53+
// Get hover for staticTestMethod
54+
$reference = $this->getReferenceLocations('TestClass::staticTestMethod()')[0];
55+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
56+
$this->assertEquals(new Hover([
57+
new MarkedString('php', "<?php\npublic static function staticTestMethod()"),
58+
'Do magna consequat veniam minim proident eiusmod incididunt aute proident.'
59+
], $reference->range), $result);
60+
}
61+
62+
public function testHoverForStaticProperty()
63+
{
64+
// echo TestClass::staticTestProperty;
65+
// Get hover for staticTestProperty
66+
$reference = $this->getReferenceLocations('TestClass::staticTestProperty')[0];
67+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
68+
$this->assertEquals(new Hover([
69+
new MarkedString('php', "<?php\npublic static \$staticTestProperty;"),
70+
'Lorem excepteur officia sit anim velit veniam enim.'
71+
], $reference->range), $result);
72+
}
73+
74+
public function testHoverForClassConstant()
75+
{
76+
// echo TestClass::TEST_CLASS_CONST;
77+
// Get hover for TEST_CLASS_CONST
78+
$reference = $this->getReferenceLocations('TestClass::TEST_CLASS_CONST')[0];
79+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
80+
$this->assertEquals(new Hover([
81+
new MarkedString('php', "<?php\nconst TEST_CLASS_CONST = 123;"),
82+
'Anim labore veniam consectetur laboris minim quis aute aute esse nulla ad.'
83+
], $reference->range), $result);
84+
}
85+
86+
public function testHoverForFunction()
87+
{
88+
// test_function();
89+
// Get hover for test_function
90+
$reference = $this->getReferenceLocations('test_function()')[0];
91+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
92+
$this->assertEquals(new Hover([
93+
new MarkedString('php', "<?php\nfunction test_function()"),
94+
'Officia aliquip adipisicing et nulla et laboris dolore labore.'
95+
], $reference->range), $result);
96+
}
97+
98+
public function testHoverForConstant()
99+
{
100+
// echo TEST_CONST;
101+
// Get hover for TEST_CONST
102+
$reference = $this->getReferenceLocations('TEST_CONST')[0];
103+
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
104+
$this->assertEquals(new Hover([
105+
new MarkedString('php', "<?php\nconst TEST_CONST = 123;"),
106+
'Esse commodo excepteur pariatur Lorem est aute incididunt reprehenderit.'
107+
], $reference->range), $result);
108+
}
109+
110+
public function testHoverForVariable()
27111
{
28112
// echo $var;
29113
// Get hover for $var
@@ -34,4 +118,19 @@ public function testHoverWithoutDocBlock()
34118
new Range(new Position(13, 5), new Position(13, 9))
35119
), $result);
36120
}
121+
122+
public function testHoverForParam()
123+
{
124+
// echo $param;
125+
// Get hover for $param
126+
$uri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php'));
127+
$result = $this->textDocument->hover(new TextDocumentIdentifier($uri), new Position(22, 11));
128+
$this->assertEquals(new Hover(
129+
[
130+
new MarkedString('php', "<?php\n\TestNamespace\TestClass \$param"),
131+
'Adipisicing non non cillum sint incididunt cillum enim mollit.'
132+
],
133+
new Range(new Position(22, 9), new Position(22, 15))
134+
), $result);
135+
}
37136
}

0 commit comments

Comments
 (0)