1
0
Fork 0

Add more tests

pull/50/head
Felix Becker 2016-10-19 11:22:47 +02:00
parent 49f16f5c3e
commit 1106521b9e
2 changed files with 142 additions and 10 deletions

View File

@ -5,6 +5,7 @@ namespace LanguageServer\Server;
use LanguageServer\{LanguageClient, Project};
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
use PhpParser\Node;
use LanguageServer\Protocol\{
TextDocumentItem,
TextDocumentIdentifier,
@ -164,21 +165,36 @@ class TextDocument
*
* @param TextDocumentIdentifier $textDocument The text document
* @param Position $position The position inside the text document
* @return Hover|null
* @return Hover
*/
public function hover(TextDocumentIdentifier $textDocument, Position $position)
public function hover(TextDocumentIdentifier $textDocument, Position $position): Hover
{
$document = $this->project->getDocument($textDocument->uri);
// Find the node under the cursor
$node = $document->getNodeAtPosition($position);
if ($node === null) {
return null;
return new Hover([]);
}
$range = Range::fromNode($node);
// Get the definition node for whatever node is under the cursor
$def = $document->getDefinitionByNode($node);
if ($def === null) {
return null;
return new Hover([], $range);
}
$contents = [];
// Build a declaration string
if ($def instanceof Node\Stmt\PropertyProperty || $def instanceof Node\Const_) {
// Properties and constants can have multiple declarations
// Use the parent node (that includes the modifiers), but only render the requested declaration
$child = $def;
$def = $def->getAttribute('parentNode');
$defLine = clone $def;
$defLine->props = [$child];
} else {
$defLine = clone $def;
}
// Don't include the docblock in the declaration string
$defLine->setAttribute('comments', []);
if (isset($defLine->stmts)) {
$defLine->stmts = [];
@ -188,10 +204,27 @@ class TextDocument
if (isset($lines[0])) {
$contents[] = new MarkedString('php', "<?php\n" . $lines[0]);
}
// Get the documentation string
if ($def instanceof Node\Param) {
$fn = $def->getAttribute('parentNode');
$docBlock = $fn->getAttribute('docBlock');
if ($docBlock !== null) {
$tags = $docBlock->getTagsByName('param');
foreach ($tags as $tag) {
if ($tag->getVariableName() === $def->name) {
$contents[] = $tag->getDescription()->render();
break;
}
}
}
} else {
$docBlock = $def->getAttribute('docBlock');
if ($docBlock !== null) {
$contents[] = $docBlock->getSummary();
}
return new Hover($contents, Range::fromNode($node));
}
return new Hover($contents, $range);
}
}

View File

@ -23,7 +23,91 @@ class HoverTest extends ServerTestCase
], $reference->range), $result);
}
public function testHoverWithoutDocBlock()
public function testHoverForMethod()
{
// $obj->testMethod();
// Get hover for testMethod
$reference = $this->getReferenceLocations('TestClass::testMethod()')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\npublic function testMethod(\$testParameter)"),
'Non culpa nostrud mollit esse sunt laboris in irure ullamco cupidatat amet.'
], $reference->range), $result);
}
public function testHoverForProperty()
{
// echo $obj->testProperty;
// Get hover for testProperty
$reference = $this->getReferenceLocations('TestClass::testProperty')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\npublic \$testProperty;"),
'Reprehenderit magna velit mollit ipsum do.'
], $reference->range), $result);
}
public function testHoverForStaticMethod()
{
// TestClass::staticTestMethod();
// Get hover for staticTestMethod
$reference = $this->getReferenceLocations('TestClass::staticTestMethod()')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\npublic static function staticTestMethod()"),
'Do magna consequat veniam minim proident eiusmod incididunt aute proident.'
], $reference->range), $result);
}
public function testHoverForStaticProperty()
{
// echo TestClass::staticTestProperty;
// Get hover for staticTestProperty
$reference = $this->getReferenceLocations('TestClass::staticTestProperty')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\npublic static \$staticTestProperty;"),
'Lorem excepteur officia sit anim velit veniam enim.'
], $reference->range), $result);
}
public function testHoverForClassConstant()
{
// echo TestClass::TEST_CLASS_CONST;
// Get hover for TEST_CLASS_CONST
$reference = $this->getReferenceLocations('TestClass::TEST_CLASS_CONST')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\nconst TEST_CLASS_CONST = 123;"),
'Anim labore veniam consectetur laboris minim quis aute aute esse nulla ad.'
], $reference->range), $result);
}
public function testHoverForFunction()
{
// test_function();
// Get hover for test_function
$reference = $this->getReferenceLocations('test_function()')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\nfunction test_function()"),
'Officia aliquip adipisicing et nulla et laboris dolore labore.'
], $reference->range), $result);
}
public function testHoverForConstant()
{
// echo TEST_CONST;
// Get hover for TEST_CONST
$reference = $this->getReferenceLocations('TEST_CONST')[0];
$result = $this->textDocument->hover(new TextDocumentIdentifier($reference->uri), $reference->range->end);
$this->assertEquals(new Hover([
new MarkedString('php', "<?php\nconst TEST_CONST = 123;"),
'Esse commodo excepteur pariatur Lorem est aute incididunt reprehenderit.'
], $reference->range), $result);
}
public function testHoverForVariable()
{
// echo $var;
// Get hover for $var
@ -34,4 +118,19 @@ class HoverTest extends ServerTestCase
new Range(new Position(13, 5), new Position(13, 9))
), $result);
}
public function testHoverForParam()
{
// echo $param;
// Get hover for $param
$uri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php'));
$result = $this->textDocument->hover(new TextDocumentIdentifier($uri), new Position(22, 11));
$this->assertEquals(new Hover(
[
new MarkedString('php', "<?php\n\TestNamespace\TestClass \$param"),
'Adipisicing non non cillum sint incididunt cillum enim mollit.'
],
new Range(new Position(22, 9), new Position(22, 15))
), $result);
}
}