1
0
Fork 0

Extract static FQN methods to FqnUtilities

pull/357/head
Sara Itani 2017-03-01 15:55:29 -08:00
parent df315df04b
commit 591ecbd7d3
6 changed files with 61 additions and 58 deletions

View File

@ -142,7 +142,7 @@ class CompletionProvider
// If the name is an Error node, just filter by the class // If the name is an Error node, just filter by the class
if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) { if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) {
// For instances, resolve the variable type // For instances, resolve the variable type
$prefixes = DefinitionResolver::getFqnsFromType( $prefixes = FqnUtilities::getFqnsFromType(
$this->definitionResolver->resolveExpressionNodeToType($node->var) $this->definitionResolver->resolveExpressionNodeToType($node->var)
); );
} else { } else {

View File

@ -170,31 +170,6 @@ class DefinitionResolver implements DefinitionResolverInterface
return $this->index->getDefinition($fqn, $globalFallback); return $this->index->getDefinition($fqn, $globalFallback);
} }
/**
* Returns all possible FQNs in a type
*
* @param Type $type
* @return string[]
*/
public static function getFqnsFromType(Type $type): array
{
$fqns = [];
if ($type instanceof Types\Object_) {
$fqsen = $type->getFqsen();
if ($fqsen !== null) {
$fqns[] = substr((string)$fqsen, 1);
}
}
if ($type instanceof Types\Compound) {
for ($i = 0; $t = $type->get($i); $i++) {
foreach (self::getFqnsFromType($type) as $fqn) {
$fqns[] = $fqn;
}
}
}
return $fqns;
}
/** /**
* Given any node, returns the FQN of the symbol that is referenced * Given any node, returns the FQN of the symbol that is referenced
* Returns null if the FQN could not be resolved or the reference node references a variable * Returns null if the FQN could not be resolved or the reference node references a variable
@ -366,7 +341,7 @@ class DefinitionResolver implements DefinitionResolverInterface
* @param Node\Expr\Variable|Node\Expr\ClosureUse $var The variable access * @param Node\Expr\Variable|Node\Expr\ClosureUse $var The variable access
* @return Node\Expr\Assign|Node\Expr\AssignOp|Node\Param|Node\Expr\ClosureUse|null * @return Node\Expr\Assign|Node\Expr\AssignOp|Node\Param|Node\Expr\ClosureUse|null
*/ */
public static function resolveVariableToNode(Node\Expr $var) private static function resolveVariableToNode(Node\Expr $var)
{ {
$n = $var; $n = $var;
// When a use is passed, start outside the closure to not return immediatly // When a use is passed, start outside the closure to not return immediatly

53
src/FqnUtilities.php Normal file
View File

@ -0,0 +1,53 @@
<?php
namespace LanguageServer;
use phpDocumentor\Reflection\{Type, Types};
use PhpParser\Node;
use Microsoft\PhpParser as Tolerant;
class FqnUtilities
{
/**
* Returns the fully qualified name (FQN) that is defined by a node
* Returns null if the node does not declare any symbol that can be referenced by an FQN
*
* @param Node | Tolerant\Node $node
* @return string|null
*/
public static function getDefinedFqn($node)
{
if ($node instanceof Node) {
return DefinitionResolver::getDefinedFqn($node);
} elseif ($node instanceof Tolerant\Node) {
return TolerantDefinitionResolver::getDefinedFqn($node);
}
throw new \TypeError("Unspported Node class");
}
/**
* Returns all possible FQNs in a type
*
* @param Type $type
* @return string[]
*/
public static function getFqnsFromType(Type $type): array
{
$fqns = [];
if ($type instanceof Types\Object_) {
$fqsen = $type->getFqsen();
if ($fqsen !== null) {
$fqns[] = substr((string)$fqsen, 1);
}
}
if ($type instanceof Types\Compound) {
for ($i = 0; $t = $type->get($i); $i++) {
foreach (self::getFqnsFromType($type) as $fqn) {
$fqns[] = $fqn;
}
}
}
return $fqns;
}
}

View File

@ -5,7 +5,7 @@ namespace LanguageServer\NodeVisitor;
use PhpParser\{NodeVisitorAbstract, Node}; use PhpParser\{NodeVisitorAbstract, Node};
use LanguageServer\{ use LanguageServer\{
Definition, DefinitionResolver, DefinitionResolverInterface Definition, DefinitionResolver, DefinitionResolverInterface, FqnUtilities
}; };
use LanguageServer\Protocol\SymbolInformation; use LanguageServer\Protocol\SymbolInformation;
@ -38,7 +38,7 @@ class DefinitionCollector extends NodeVisitorAbstract
public function enterNode(Node $node) public function enterNode(Node $node)
{ {
$fqn = DefinitionResolver::getDefinedFqn($node); $fqn = FqnUtilities::getDefinedFqn($node);
// Only index definitions with an FQN (no variables) // Only index definitions with an FQN (no variables)
if ($fqn === null) { if ($fqn === null) {
return; return;

View File

@ -5,7 +5,7 @@ namespace LanguageServer\Server;
use PhpParser\{Node, NodeTraverser}; use PhpParser\{Node, NodeTraverser};
use LanguageServer\{ use LanguageServer\{
DefinitionResolverInterface, LanguageClient, PhpDocumentLoader, PhpDocument, DefinitionResolver, CompletionProvider DefinitionResolverInterface, FqnUtilities, LanguageClient, PhpDocumentLoader, PhpDocument, DefinitionResolver, CompletionProvider
}; };
use LanguageServer\NodeVisitor\VariableReferencesCollector; use LanguageServer\NodeVisitor\VariableReferencesCollector;
use LanguageServer\Protocol\{ use LanguageServer\Protocol\{
@ -221,7 +221,7 @@ class TextDocument
} }
} else { } else {
// Definition with a global FQN // Definition with a global FQN
$fqn = DefinitionResolver::getDefinedFqn($node); $fqn = FqnUtilities::getDefinedFqn($node);
// Wait until indexing finished // Wait until indexing finished
if (!$this->index->isComplete()) { if (!$this->index->isComplete()) {
yield waitForEvent($this->index, 'complete'); yield waitForEvent($this->index, 'complete');
@ -266,7 +266,7 @@ class TextDocument
return []; return [];
} }
// Handle definition nodes // Handle definition nodes
$fqn = DefinitionResolver::getDefinedFqn($node); $fqn = FqnUtilities::getDefinedFqn($node);
while (true) { while (true) {
if ($fqn) { if ($fqn) {
$def = $this->index->getDefinition($fqn); $def = $this->index->getDefinition($fqn);
@ -307,7 +307,7 @@ class TextDocument
if ($node === null) { if ($node === null) {
return new Hover([]); return new Hover([]);
} }
$definedFqn = DefinitionResolver::getDefinedFqn($node); $definedFqn = FqnUtilities::getDefinedFqn($node);
while (true) { while (true) {
if ($definedFqn) { if ($definedFqn) {
// Support hover for definitions // Support hover for definitions

View File

@ -170,31 +170,6 @@ class TolerantDefinitionResolver implements DefinitionResolverInterface
return $this->index->getDefinition($fqn, $globalFallback); return $this->index->getDefinition($fqn, $globalFallback);
} }
/**
* Returns all possible FQNs in a type
*
* @param Type $type
* @return string[]
*/
public static function getFqnsFromType(Type $type): array
{
$fqns = [];
if ($type instanceof Types\Object_) {
$fqsen = $type->getFqsen();
if ($fqsen !== null) {
$fqns[] = substr((string)$fqsen, 1);
}
}
if ($type instanceof Types\Compound) {
for ($i = 0; $t = $type->get($i); $i++) {
foreach (self::getFqnsFromType($type) as $fqn) {
$fqns[] = $fqn;
}
}
}
return $fqns;
}
/** /**
* Given any node, returns the FQN of the symbol that is referenced * Given any node, returns the FQN of the symbol that is referenced
* Returns null if the FQN could not be resolved or the reference node references a variable * Returns null if the FQN could not be resolved or the reference node references a variable