From 1106521b9ea2831454f032d3b75591db32d93dde Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Wed, 19 Oct 2016 11:22:47 +0200 Subject: [PATCH] Add more tests --- src/Server/TextDocument.php | 51 +++++++++--- tests/Server/TextDocument/HoverTest.php | 101 +++++++++++++++++++++++- 2 files changed, 142 insertions(+), 10 deletions(-) diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 0d3bb2f..e515e43 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -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 = []; - $defLine = clone $def; + + // 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', "getAttribute('docBlock'); - if ($docBlock !== null) { - $contents[] = $docBlock->getSummary(); + + // 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); } } diff --git a/tests/Server/TextDocument/HoverTest.php b/tests/Server/TextDocument/HoverTest.php index 848616c..ad7d3cf 100644 --- a/tests/Server/TextDocument/HoverTest.php +++ b/tests/Server/TextDocument/HoverTest.php @@ -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', "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', "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', "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', "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', "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', "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', "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', "