From 43a91b0d0907542dd27991e19cae633192474a36 Mon Sep 17 00:00:00 2001 From: Ivan Bozhanov Date: Thu, 19 Jan 2017 16:47:11 +0200 Subject: [PATCH] Handle hover for $this (#249) --- src/DefinitionResolver.php | 4 ++++ tests/Server/TextDocument/HoverTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index d1f756e..8cbddf7 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -144,6 +144,10 @@ class DefinitionResolver { // Variables are not indexed globally, as they stay in the file scope anyway if ($node instanceof Node\Expr\Variable) { + // Resolve $this + if ($node->name === 'this' && $fqn = $this->getContainingClassFqn($node)) { + return $this->index->getDefinition($fqn, false); + } // Resolve the variable to a definition node (assignment, param or closure use) $defNode = self::resolveVariableToNode($node); if ($defNode === null) { diff --git a/tests/Server/TextDocument/HoverTest.php b/tests/Server/TextDocument/HoverTest.php index 9f33268..7d61354 100644 --- a/tests/Server/TextDocument/HoverTest.php +++ b/tests/Server/TextDocument/HoverTest.php @@ -172,4 +172,16 @@ class HoverTest extends ServerTestCase new Range(new Position(22, 9), new Position(22, 15)) ), $result); } + + public function testHoverForThis() + { + // $this; + // Get hover for $this + $uri = pathToUri(realpath(__DIR__ . '/../../../fixtures/global_symbols.php')); + $result = $this->textDocument->hover(new TextDocumentIdentifier($uri), new Position(59, 11))->wait(); + $this->assertEquals(new Hover([ + new MarkedString('php', "