From 8a354ba1afb79c9272798131149e9e5dedea7ad2 Mon Sep 17 00:00:00 2001 From: Michal Niewrzal Date: Wed, 26 Oct 2016 11:47:02 +0200 Subject: [PATCH] Definition for use function (#116) --- fixtures/global_references.php | 2 ++ fixtures/references.php | 2 ++ src/Fqn.php | 2 ++ tests/Server/ServerTestCase.php | 6 ++++-- tests/Server/TextDocument/Definition/GlobalTest.php | 9 +++++++++ tests/Server/TextDocument/References/GlobalTest.php | 5 ++++- 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/fixtures/global_references.php b/fixtures/global_references.php index 8f88247..e44955a 100644 --- a/fixtures/global_references.php +++ b/fixtures/global_references.php @@ -28,3 +28,5 @@ $fn = function() use ($var) { }; echo TEST_CONST; + +use function test_function; diff --git a/fixtures/references.php b/fixtures/references.php index 3f58d05..11fcf7f 100644 --- a/fixtures/references.php +++ b/fixtures/references.php @@ -28,3 +28,5 @@ $fn = function() use ($var) { }; echo TEST_CONST; + +use function TestNamespace\test_function; diff --git a/src/Fqn.php b/src/Fqn.php index a361a7d..dbf670b 100644 --- a/src/Fqn.php +++ b/src/Fqn.php @@ -45,6 +45,8 @@ function getReferencedFqn(Node $node) $grandParent = $parent->getAttribute('parentNode'); if ($grandParent instanceof Node\Stmt\GroupUse) { $name = $grandParent->prefix . '\\' . $name; + } else if ($grandParent instanceof Node\Stmt\Use_ && $grandParent->type === Node\Stmt\Use_::TYPE_FUNCTION) { + $name .= '()'; } // Only the name node should be considered a reference, not the New_ node itself } else if ($parent instanceof Node\Expr\New_) { diff --git a/tests/Server/ServerTestCase.php b/tests/Server/ServerTestCase.php index 0886655..81196b0 100644 --- a/tests/Server/ServerTestCase.php +++ b/tests/Server/ServerTestCase.php @@ -126,7 +126,8 @@ abstract class ServerTestCase extends TestCase 0 => new Location($referencesUri, new Range(new Position( 5, 0), new Position( 5, 18))) ], 'TestNamespace\\test_function()' => [ - 0 => new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13))) + 0 => new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13))), + 1 => new Location($referencesUri, new Range(new Position(31, 13), new Position(31, 40))) ], // Global @@ -164,7 +165,8 @@ abstract class ServerTestCase extends TestCase 0 => new Location($globalReferencesUri, new Range(new Position( 5, 0), new Position( 5, 18))) ], 'test_function()' => [ - 0 => new Location($globalReferencesUri, new Range(new Position(10, 0), new Position(10, 13))) + 0 => new Location($globalReferencesUri, new Range(new Position(10, 0), new Position(10, 13))), + 1 => new Location($globalReferencesUri, new Range(new Position(31, 13), new Position(31, 40))) ] ]; // @codingStandardsIgnoreEnd diff --git a/tests/Server/TextDocument/Definition/GlobalTest.php b/tests/Server/TextDocument/Definition/GlobalTest.php index f4a854d..527200e 100644 --- a/tests/Server/TextDocument/Definition/GlobalTest.php +++ b/tests/Server/TextDocument/Definition/GlobalTest.php @@ -202,4 +202,13 @@ class GlobalTest extends ServerTestCase $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); $this->assertEquals($this->getDefinitionLocation('test_function()'), $result); } + + public function testDefinitionForUseFunctions() + { + // use function test_function; + // Get definition for test_function + $reference = $this->getReferenceLocations('test_function()')[1]; + $result = $this->textDocument->definition(new TextDocumentIdentifier($reference->uri), $reference->range->start); + $this->assertEquals($this->getDefinitionLocation('test_function()'), $result); + } } diff --git a/tests/Server/TextDocument/References/GlobalTest.php b/tests/Server/TextDocument/References/GlobalTest.php index 284c32b..1ecf1f9 100644 --- a/tests/Server/TextDocument/References/GlobalTest.php +++ b/tests/Server/TextDocument/References/GlobalTest.php @@ -101,6 +101,9 @@ class GlobalTest extends ServerTestCase $referencesUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/references.php')); $symbolsUri = pathToUri(realpath(__DIR__ . '/../../../../fixtures/symbols.php')); $result = $this->textDocument->references(new ReferenceContext, new TextDocumentIdentifier($symbolsUri), new Position(78, 16)); - $this->assertEquals([new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13)))], $result); + $this->assertEquals([ + new Location($referencesUri, new Range(new Position(10, 0), new Position(10, 13))), + new Location($referencesUri, new Range(new Position(31, 13), new Position(31, 40))) + ], $result); } }