Extract static FQN methods to FqnUtilities
parent
df315df04b
commit
591ecbd7d3
|
@ -142,7 +142,7 @@ class CompletionProvider
|
|||
// If the name is an Error node, just filter by the class
|
||||
if ($node instanceof Node\Expr\MethodCall || $node instanceof Node\Expr\PropertyFetch) {
|
||||
// For instances, resolve the variable type
|
||||
$prefixes = DefinitionResolver::getFqnsFromType(
|
||||
$prefixes = FqnUtilities::getFqnsFromType(
|
||||
$this->definitionResolver->resolveExpressionNodeToType($node->var)
|
||||
);
|
||||
} else {
|
||||
|
|
|
@ -170,31 +170,6 @@ class DefinitionResolver implements DefinitionResolverInterface
|
|||
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
|
||||
* 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
|
||||
* @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;
|
||||
// When a use is passed, start outside the closure to not return immediatly
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ namespace LanguageServer\NodeVisitor;
|
|||
|
||||
use PhpParser\{NodeVisitorAbstract, Node};
|
||||
use LanguageServer\{
|
||||
Definition, DefinitionResolver, DefinitionResolverInterface
|
||||
Definition, DefinitionResolver, DefinitionResolverInterface, FqnUtilities
|
||||
};
|
||||
use LanguageServer\Protocol\SymbolInformation;
|
||||
|
||||
|
@ -38,7 +38,7 @@ class DefinitionCollector extends NodeVisitorAbstract
|
|||
|
||||
public function enterNode(Node $node)
|
||||
{
|
||||
$fqn = DefinitionResolver::getDefinedFqn($node);
|
||||
$fqn = FqnUtilities::getDefinedFqn($node);
|
||||
// Only index definitions with an FQN (no variables)
|
||||
if ($fqn === null) {
|
||||
return;
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace LanguageServer\Server;
|
|||
|
||||
use PhpParser\{Node, NodeTraverser};
|
||||
use LanguageServer\{
|
||||
DefinitionResolverInterface, LanguageClient, PhpDocumentLoader, PhpDocument, DefinitionResolver, CompletionProvider
|
||||
DefinitionResolverInterface, FqnUtilities, LanguageClient, PhpDocumentLoader, PhpDocument, DefinitionResolver, CompletionProvider
|
||||
};
|
||||
use LanguageServer\NodeVisitor\VariableReferencesCollector;
|
||||
use LanguageServer\Protocol\{
|
||||
|
@ -221,7 +221,7 @@ class TextDocument
|
|||
}
|
||||
} else {
|
||||
// Definition with a global FQN
|
||||
$fqn = DefinitionResolver::getDefinedFqn($node);
|
||||
$fqn = FqnUtilities::getDefinedFqn($node);
|
||||
// Wait until indexing finished
|
||||
if (!$this->index->isComplete()) {
|
||||
yield waitForEvent($this->index, 'complete');
|
||||
|
@ -266,7 +266,7 @@ class TextDocument
|
|||
return [];
|
||||
}
|
||||
// Handle definition nodes
|
||||
$fqn = DefinitionResolver::getDefinedFqn($node);
|
||||
$fqn = FqnUtilities::getDefinedFqn($node);
|
||||
while (true) {
|
||||
if ($fqn) {
|
||||
$def = $this->index->getDefinition($fqn);
|
||||
|
@ -307,7 +307,7 @@ class TextDocument
|
|||
if ($node === null) {
|
||||
return new Hover([]);
|
||||
}
|
||||
$definedFqn = DefinitionResolver::getDefinedFqn($node);
|
||||
$definedFqn = FqnUtilities::getDefinedFqn($node);
|
||||
while (true) {
|
||||
if ($definedFqn) {
|
||||
// Support hover for definitions
|
||||
|
|
|
@ -170,31 +170,6 @@ class TolerantDefinitionResolver implements DefinitionResolverInterface
|
|||
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
|
||||
* Returns null if the FQN could not be resolved or the reference node references a variable
|
||||
|
|
Loading…
Reference in New Issue