Begin with Foreach variables recognition
							parent
							
								
									1ec8d8d8e2
								
							
						
					
					
						commit
						5c9b155004
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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("<?php\nforeach([1, 2] as \$testValue) { echo \$testValue; }", $doc->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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue