2016-08-22 20:40:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LanguageServer\Protocol;
|
|
|
|
|
2016-10-09 12:34:30 +00:00
|
|
|
use PhpParser\Node;
|
2016-10-11 12:42:56 +00:00
|
|
|
use Exception;
|
2016-10-09 12:34:30 +00:00
|
|
|
|
2016-08-22 20:40:16 +00:00
|
|
|
/**
|
|
|
|
* Represents information about programming constructs like variables, classes,
|
|
|
|
* interfaces etc.
|
|
|
|
*/
|
|
|
|
class SymbolInformation
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name of this symbol.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The kind of this symbol.
|
|
|
|
*
|
2016-10-20 01:48:30 +00:00
|
|
|
* @var int
|
2016-08-22 20:40:16 +00:00
|
|
|
*/
|
|
|
|
public $kind;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The location of this symbol.
|
|
|
|
*
|
|
|
|
* @var Location
|
|
|
|
*/
|
|
|
|
public $location;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the symbol containing this symbol.
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $containerName;
|
2016-10-11 12:42:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a Node to a SymbolInformation
|
|
|
|
*
|
|
|
|
* @param Node $node
|
|
|
|
* @param string $fqn If given, $containerName will be extracted from it
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function fromNode(Node $node, string $fqn = null)
|
|
|
|
{
|
|
|
|
$nodeSymbolKindMap = [
|
|
|
|
Node\Stmt\Class_::class => SymbolKind::CLASS_,
|
|
|
|
Node\Stmt\Trait_::class => SymbolKind::CLASS_,
|
|
|
|
Node\Stmt\Interface_::class => SymbolKind::INTERFACE,
|
|
|
|
Node\Stmt\Namespace_::class => SymbolKind::NAMESPACE,
|
|
|
|
Node\Stmt\Function_::class => SymbolKind::FUNCTION,
|
|
|
|
Node\Stmt\ClassMethod::class => SymbolKind::METHOD,
|
2016-10-19 23:53:07 +00:00
|
|
|
Node\Stmt\PropertyProperty::class => SymbolKind::PROPERTY,
|
2016-10-11 12:42:56 +00:00
|
|
|
Node\Const_::class => SymbolKind::CONSTANT
|
|
|
|
];
|
|
|
|
$class = get_class($node);
|
|
|
|
if (!isset($nodeSymbolKindMap[$class])) {
|
|
|
|
throw new Exception("Not a declaration node: $class");
|
|
|
|
}
|
|
|
|
$symbol = new self;
|
|
|
|
$symbol->kind = $nodeSymbolKindMap[$class];
|
|
|
|
$symbol->name = (string)$node->name;
|
|
|
|
$symbol->location = Location::fromNode($node);
|
|
|
|
if ($fqn !== null) {
|
|
|
|
$parts = preg_split('/(::|\\\\)/', $fqn);
|
|
|
|
array_pop($parts);
|
|
|
|
$symbol->containerName = implode('\\', $parts);
|
|
|
|
}
|
|
|
|
return $symbol;
|
|
|
|
}
|
2016-10-18 21:09:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param int $kind
|
|
|
|
* @param Location $location
|
|
|
|
* @param string $containerName
|
|
|
|
*/
|
|
|
|
public function __construct($name = null, $kind = null, $location = null, $containerName = null)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->kind = $kind;
|
|
|
|
$this->location = $location;
|
|
|
|
$this->containerName = $containerName;
|
|
|
|
}
|
2016-08-22 20:40:16 +00:00
|
|
|
}
|