From e7b04f751869e0a4aae7c133f4152d113fbdf0d1 Mon Sep 17 00:00:00 2001 From: Sara Itani Date: Wed, 1 Mar 2017 16:14:13 -0800 Subject: [PATCH] extract symbol information --- src/Protocol/SymbolInformation.php | 2 +- src/Protocol/TolerantSymbolInformation.php | 73 ++++++++++++++++++++++ src/TolerantDefinitionResolver.php | 3 +- 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/Protocol/TolerantSymbolInformation.php diff --git a/src/Protocol/SymbolInformation.php b/src/Protocol/SymbolInformation.php index 5d442f0..a608ebd 100644 --- a/src/Protocol/SymbolInformation.php +++ b/src/Protocol/SymbolInformation.php @@ -46,7 +46,7 @@ class SymbolInformation * @param string $fqn If given, $containerName will be extracted from it * @return self|null */ - public static function fromNode(Node $node, string $fqn = null) + public static function fromNode($node, string $fqn = null) { $parent = $node->getAttribute('parentNode'); $symbol = new self; diff --git a/src/Protocol/TolerantSymbolInformation.php b/src/Protocol/TolerantSymbolInformation.php new file mode 100644 index 0000000..df86df1 --- /dev/null +++ b/src/Protocol/TolerantSymbolInformation.php @@ -0,0 +1,73 @@ +getAttribute('parentNode'); + $symbol = new self; + if ($node instanceof Node\Stmt\Class_) { + $symbol->kind = SymbolKind::CLASS_; + } else if ($node instanceof Node\Stmt\Trait_) { + $symbol->kind = SymbolKind::CLASS_; + } else if ($node instanceof Node\Stmt\Interface_) { + $symbol->kind = SymbolKind::INTERFACE; + } else if ($node instanceof Node\Name && $parent instanceof Node\Stmt\Namespace_) { + $symbol->kind = SymbolKind::NAMESPACE; + } else if ($node instanceof Node\Stmt\Function_) { + $symbol->kind = SymbolKind::FUNCTION; + } else if ($node instanceof Node\Stmt\ClassMethod) { + $symbol->kind = SymbolKind::METHOD; + } else if ($node instanceof Node\Stmt\PropertyProperty) { + $symbol->kind = SymbolKind::PROPERTY; + } else if ($node instanceof Node\Const_) { + $symbol->kind = SymbolKind::CONSTANT; + } else if ( + ( + ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) + && $node->var instanceof Node\Expr\Variable + ) + || $node instanceof Node\Expr\ClosureUse + || $node instanceof Node\Param + ) { + $symbol->kind = SymbolKind::VARIABLE; + } else { + return null; + } + if ($node instanceof Node\Name) { + $symbol->name = (string)$node; + } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { + $symbol->name = $node->var->name; + } else if ($node instanceof Node\Expr\ClosureUse) { + $symbol->name = $node->var; + } else if (isset($node->name)) { + $symbol->name = (string)$node->name; + } else { + return null; + } + $symbol->location = Location::fromNode($node); + if ($fqn !== null) { + $parts = preg_split('/(::|->|\\\\)/', $fqn); + array_pop($parts); + $symbol->containerName = implode('\\', $parts); + } + return $symbol; + } +} diff --git a/src/TolerantDefinitionResolver.php b/src/TolerantDefinitionResolver.php index e09a6ca..65f1f2f 100644 --- a/src/TolerantDefinitionResolver.php +++ b/src/TolerantDefinitionResolver.php @@ -3,6 +3,7 @@ declare(strict_types = 1); namespace LanguageServer; +use LanguageServer\Protocol\TolerantSymbolInformation; use PhpParser\Node; use PhpParser\PrettyPrinter\Standard as PrettyPrinter; use phpDocumentor\Reflection\{Types, Type, Fqsen, TypeResolver}; @@ -128,7 +129,7 @@ class TolerantDefinitionResolver implements DefinitionResolverInterface $def->extends[] = (string)$n; } } - $def->symbolInformation = SymbolInformation::fromNode($node, $fqn); + $def->symbolInformation = TolerantSymbolInformation::fromNode($node, $fqn); $def->type = $this->getTypeFromNode($node); $def->declarationLine = $this->getDeclarationLineFromNode($node); $def->documentation = $this->getDocumentationFromNode($node);