From 5c9b155004946f6af3315650354f35651169151d Mon Sep 17 00:00:00 2001 From: jens1o Date: Wed, 15 Nov 2017 18:09:19 +0100 Subject: [PATCH 1/2] Begin with Foreach variables recognition --- src/DefinitionResolver.php | 9 +++++++++ src/Server/TextDocument.php | 2 ++ tests/DefinitionResolverTest.php | 15 +++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 620527e..66bfe60 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -534,6 +534,7 @@ class DefinitionResolver if ($n instanceof Node\Statement\ExpressionStatement) { $n = $n->expression; } + if ( // TODO - clean this up ($n instanceof Node\Expression\AssignmentExpression && $n->operator->kind === PhpParser\TokenKind::EqualsToken) @@ -541,6 +542,14 @@ class DefinitionResolver ) { return $n; } + + // get variables from foreach statements + if ( + ($n instanceof Node\ForeachValue || $n instanceof Node\ForeachKey) && + $n->expression->getName() === $name + ) { + return $n; + } } } while (isset($n) && $n = $n->parent); // Return null if nothing was found diff --git a/src/Server/TextDocument.php b/src/Server/TextDocument.php index 58e7074..1b1b479 100644 --- a/src/Server/TextDocument.php +++ b/src/Server/TextDocument.php @@ -288,6 +288,8 @@ class TextDocument public function hover(TextDocumentIdentifier $textDocument, Position $position): Promise { return coroutine(function () use ($textDocument, $position) { + $contents = []; + $document = yield $this->documentLoader->getOrLoad($textDocument->uri); // Find the node under the cursor $node = $document->getNodeAtPosition($position); diff --git a/tests/DefinitionResolverTest.php b/tests/DefinitionResolverTest.php index 0397e71..0ccb42e 100644 --- a/tests/DefinitionResolverTest.php +++ b/tests/DefinitionResolverTest.php @@ -62,4 +62,19 @@ class DefinitionResolverTest extends TestCase $this->assertEquals('TEST_DEFINE', $fqn); } + + public function testGetVaribleFromForeachValue() + { + $parser = new PhpParser\Parser; + $doc = new MockPhpDocument; + $sourceFileNode = $parser->parseSourceFile("getUri()); + + $index = new Index; + + $definitionResolver = new DefinitionResolver($index); + $testValueVariable = $sourceFileNode->statementList[1]->statements->statements[0]->expression->expressions->children[0]; + $fqn = $definitionResolver->resolveVariableToNode($testValueVariable); + + $this->assertInstanceOf(PhpParser\Node\ForeachValue::class, $fqn); + } } From f970833b0a6d65a38e85ddbedcb9333df6ce5d66 Mon Sep 17 00:00:00 2001 From: jens1o Date: Wed, 15 Nov 2017 18:37:16 +0100 Subject: [PATCH 2/2] add more things https://github.com/felixfbecker/php-language-server/issues/200 --- src/DefinitionResolver.php | 5 +++++ src/Protocol/SymbolInformation.php | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 66bfe60..1d08a78 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -979,6 +979,11 @@ class DefinitionResolver return $this->resolveExpressionNodeToType($node->argumentExpressionList->children[2]->expression); } + // FOREACH KEY VARIABLE + if ($node instanceof Node\ForeachKey) { + return new Types\Integer; + } + // PARAMETERS // Get the type of the parameter: // 1. Doc block diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 6b4d39e..0ac8973 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -83,6 +83,8 @@ class SymbolInformation ) || $node instanceof Node\UseVariableName || $node instanceof Node\Parameter + || $node instanceof Node\ForeachValue + || $node instanceof Node\ForeachKey ) { $symbol->kind = SymbolKind::VARIABLE; } else { @@ -103,6 +105,8 @@ class SymbolInformation } else { $symbol->name = ltrim((string)$node->name->getText($node->getFileContents()), "$"); } + } else if ($node instanceof Node\ForeachValue || $node instanceof Node\ForeachKey) { + $symbol->name = $node->expression->getName(); } else if (isset($node->variableName)) { $symbol->name = $node->variableName->getText($node); } else if (!isset($symbol->name)) {