2016-09-30 09:30:08 +00:00
|
|
|
<?php
|
2016-09-30 09:54:49 +00:00
|
|
|
declare(strict_types = 1);
|
2016-09-30 09:30:08 +00:00
|
|
|
|
|
|
|
namespace LanguageServer;
|
|
|
|
|
2016-10-11 13:28:53 +00:00
|
|
|
use LanguageServer\Protocol\{Diagnostic, DiagnosticSeverity, Range, Position, TextEdit};
|
2016-10-11 23:45:15 +00:00
|
|
|
use LanguageServer\NodeVisitor\{
|
|
|
|
NodeAtPositionFinder,
|
|
|
|
ReferencesAdder,
|
2016-10-19 10:31:32 +00:00
|
|
|
DocBlockParser,
|
2016-10-11 23:45:15 +00:00
|
|
|
DefinitionCollector,
|
|
|
|
ColumnCalculator,
|
2016-12-13 00:51:02 +00:00
|
|
|
ReferencesCollector
|
2016-10-11 23:45:15 +00:00
|
|
|
};
|
2016-12-13 00:51:02 +00:00
|
|
|
use LanguageServer\Index\Index;
|
2016-10-26 20:25:24 +00:00
|
|
|
use PhpParser\{Error, ErrorHandler, Node, NodeTraverser};
|
2016-09-30 09:30:08 +00:00
|
|
|
use PhpParser\NodeVisitor\NameResolver;
|
2016-10-19 10:31:32 +00:00
|
|
|
use phpDocumentor\Reflection\DocBlockFactory;
|
2016-11-14 19:00:10 +00:00
|
|
|
use Sabre\Uri;
|
2016-09-30 09:30:08 +00:00
|
|
|
|
|
|
|
class PhpDocument
|
|
|
|
{
|
2016-10-08 10:51:55 +00:00
|
|
|
/**
|
|
|
|
* The PHPParser instance
|
|
|
|
*
|
|
|
|
* @var Parser
|
|
|
|
*/
|
2016-09-30 09:30:08 +00:00
|
|
|
private $parser;
|
|
|
|
|
2016-10-19 10:31:32 +00:00
|
|
|
/**
|
|
|
|
* The DocBlockFactory instance to parse docblocks
|
|
|
|
*
|
|
|
|
* @var DocBlockFactory
|
|
|
|
*/
|
|
|
|
private $docBlockFactory;
|
|
|
|
|
2016-11-18 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* The DefinitionResolver instance to resolve reference nodes to definitions
|
|
|
|
*
|
|
|
|
* @var DefinitionResolver
|
|
|
|
*/
|
|
|
|
private $definitionResolver;
|
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
/**
|
|
|
|
* @var Index
|
|
|
|
*/
|
|
|
|
private $index;
|
|
|
|
|
2016-10-08 10:51:55 +00:00
|
|
|
/**
|
|
|
|
* The URI of the document
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-30 09:30:08 +00:00
|
|
|
private $uri;
|
2016-10-08 10:51:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The content of the document
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-30 09:30:08 +00:00
|
|
|
private $content;
|
2016-10-08 10:51:55 +00:00
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
/**
|
|
|
|
* The AST of the document
|
|
|
|
*
|
|
|
|
* @var Node[]
|
|
|
|
*/
|
2016-10-11 23:45:15 +00:00
|
|
|
private $stmts;
|
2016-10-08 12:59:08 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-18 14:22:24 +00:00
|
|
|
* Map from fully qualified name (FQN) to Definition
|
2016-10-08 12:59:08 +00:00
|
|
|
*
|
2016-11-18 14:22:24 +00:00
|
|
|
* @var Definition[]
|
2016-10-08 12:59:08 +00:00
|
|
|
*/
|
2016-10-11 12:42:56 +00:00
|
|
|
private $definitions;
|
2016-10-08 12:59:08 +00:00
|
|
|
|
2016-10-09 08:09:09 +00:00
|
|
|
/**
|
2016-11-18 14:22:24 +00:00
|
|
|
* Map from fully qualified name (FQN) to Node
|
2016-10-09 08:09:09 +00:00
|
|
|
*
|
2016-11-18 14:22:24 +00:00
|
|
|
* @var Node[]
|
2016-10-09 08:09:09 +00:00
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
private $definitionNodes;
|
2016-10-09 08:09:09 +00:00
|
|
|
|
2016-10-20 00:08:23 +00:00
|
|
|
/**
|
2016-11-18 14:22:24 +00:00
|
|
|
* Map from fully qualified name (FQN) to array of nodes that reference the symbol
|
2016-10-20 00:08:23 +00:00
|
|
|
*
|
2016-11-18 14:22:24 +00:00
|
|
|
* @var Node[][]
|
2016-10-20 00:08:23 +00:00
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
private $referenceNodes;
|
2016-10-20 00:08:23 +00:00
|
|
|
|
2016-10-08 10:51:55 +00:00
|
|
|
/**
|
2016-12-13 00:51:02 +00:00
|
|
|
* Diagnostics for this document that were collected while parsing
|
|
|
|
*
|
|
|
|
* @var Diagnostic[]
|
|
|
|
*/
|
|
|
|
private $diagnostics;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uri The URI of the document
|
|
|
|
* @param string $content The content of the document
|
|
|
|
* @param Index $index The Index to register definitions and references to
|
|
|
|
* @param Parser $parser The PHPParser instance
|
|
|
|
* @param DocBlockFactory $docBlockFactory The DocBlockFactory instance to parse docblocks
|
|
|
|
* @param DefinitionResolver $definitionResolver The DefinitionResolver to resolve definitions to symbols in the workspace
|
2016-10-08 10:51:55 +00:00
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
public function __construct(
|
|
|
|
string $uri,
|
|
|
|
string $content,
|
2016-12-13 00:51:02 +00:00
|
|
|
Index $index,
|
2016-11-18 14:22:24 +00:00
|
|
|
Parser $parser,
|
|
|
|
DocBlockFactory $docBlockFactory,
|
|
|
|
DefinitionResolver $definitionResolver
|
|
|
|
) {
|
2016-09-30 09:30:08 +00:00
|
|
|
$this->uri = $uri;
|
2016-12-13 00:51:02 +00:00
|
|
|
$this->index = $index;
|
2016-09-30 09:30:08 +00:00
|
|
|
$this->parser = $parser;
|
2016-10-19 10:31:32 +00:00
|
|
|
$this->docBlockFactory = $docBlockFactory;
|
2016-11-18 14:22:24 +00:00
|
|
|
$this->definitionResolver = $definitionResolver;
|
2016-10-11 12:42:56 +00:00
|
|
|
$this->updateContent($content);
|
2016-09-30 09:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-11 23:45:15 +00:00
|
|
|
* Get all references of a fully qualified name
|
|
|
|
*
|
|
|
|
* @param string $fqn The fully qualified name of the symbol
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
public function getReferenceNodesByFqn(string $fqn)
|
2016-10-11 23:45:15 +00:00
|
|
|
{
|
2016-11-18 14:22:24 +00:00
|
|
|
return isset($this->referenceNodes) && isset($this->referenceNodes[$fqn]) ? $this->referenceNodes[$fqn] : null;
|
2016-10-11 23:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the content on this document.
|
2016-10-11 12:42:56 +00:00
|
|
|
* Re-parses a source file, updates symbols and reports parsing errors
|
|
|
|
* that may have occured as diagnostics.
|
2016-09-30 09:30:08 +00:00
|
|
|
*
|
|
|
|
* @param string $content
|
2016-10-08 10:51:55 +00:00
|
|
|
* @return void
|
2016-09-30 09:30:08 +00:00
|
|
|
*/
|
|
|
|
public function updateContent(string $content)
|
|
|
|
{
|
|
|
|
$this->content = $content;
|
2016-12-16 22:33:55 +00:00
|
|
|
|
|
|
|
// Unregister old definitions
|
|
|
|
if (isset($this->definitions)) {
|
|
|
|
foreach ($this->definitions as $fqn => $definition) {
|
|
|
|
$this->index->removeDefinition($fqn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unregister old references
|
|
|
|
if (isset($this->referenceNodes)) {
|
|
|
|
foreach ($this->referenceNodes as $fqn => $node) {
|
|
|
|
$this->index->removeReferenceUri($fqn, $this->uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->referenceNodes = null;
|
|
|
|
$this->definitions = null;
|
|
|
|
$this->definitionNodes = null;
|
2016-09-30 09:30:08 +00:00
|
|
|
|
2016-10-26 20:25:24 +00:00
|
|
|
$errorHandler = new ErrorHandler\Collecting;
|
|
|
|
$stmts = $this->parser->parse($content, $errorHandler);
|
2016-09-30 09:30:08 +00:00
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
$this->diagnostics = [];
|
2016-10-26 20:25:24 +00:00
|
|
|
foreach ($errorHandler->getErrors() as $error) {
|
2016-12-13 00:51:02 +00:00
|
|
|
$this->diagnostics[] = Diagnostic::fromError($error, $this->content, DiagnosticSeverity::ERROR, 'php');
|
2016-09-30 09:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// $stmts can be null in case of a fatal parsing error
|
|
|
|
if ($stmts) {
|
|
|
|
$traverser = new NodeTraverser;
|
2016-10-08 11:22:34 +00:00
|
|
|
|
|
|
|
// Resolve aliased names to FQNs
|
2016-10-26 20:25:24 +00:00
|
|
|
$traverser->addVisitor(new NameResolver($errorHandler));
|
2016-10-08 11:22:34 +00:00
|
|
|
|
|
|
|
// Add parentNode, previousSibling, nextSibling attributes
|
2016-10-08 12:53:13 +00:00
|
|
|
$traverser->addVisitor(new ReferencesAdder($this));
|
2016-10-08 11:22:34 +00:00
|
|
|
|
|
|
|
// Add column attributes to nodes
|
2016-10-11 12:42:56 +00:00
|
|
|
$traverser->addVisitor(new ColumnCalculator($content));
|
2016-10-08 11:22:34 +00:00
|
|
|
|
2016-10-19 10:31:32 +00:00
|
|
|
// Parse docblocks and add docBlock attributes to nodes
|
|
|
|
$docBlockParser = new DocBlockParser($this->docBlockFactory);
|
|
|
|
$traverser->addVisitor($docBlockParser);
|
|
|
|
|
2016-10-11 23:45:15 +00:00
|
|
|
$traverser->traverse($stmts);
|
2016-10-19 10:31:32 +00:00
|
|
|
|
|
|
|
// Report errors from parsing docblocks
|
|
|
|
foreach ($docBlockParser->errors as $error) {
|
2016-12-13 00:51:02 +00:00
|
|
|
$this->diagnostics[] = Diagnostic::fromError($error, $this->content, DiagnosticSeverity::WARNING, 'php');
|
2016-10-19 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 23:45:15 +00:00
|
|
|
$traverser = new NodeTraverser;
|
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
// Collect all definitions
|
2016-11-18 14:22:24 +00:00
|
|
|
$definitionCollector = new DefinitionCollector($this->definitionResolver);
|
2016-10-08 12:59:08 +00:00
|
|
|
$traverser->addVisitor($definitionCollector);
|
|
|
|
|
2016-10-11 23:45:15 +00:00
|
|
|
// Collect all references
|
2016-11-18 14:22:24 +00:00
|
|
|
$referencesCollector = new ReferencesCollector($this->definitionResolver);
|
2016-10-11 23:45:15 +00:00
|
|
|
$traverser->addVisitor($referencesCollector);
|
|
|
|
|
2016-09-30 09:30:08 +00:00
|
|
|
$traverser->traverse($stmts);
|
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
// Register this document on the project for all the symbols defined in it
|
2016-10-11 23:45:15 +00:00
|
|
|
$this->definitions = $definitionCollector->definitions;
|
2016-11-18 14:22:24 +00:00
|
|
|
$this->definitionNodes = $definitionCollector->nodes;
|
|
|
|
foreach ($definitionCollector->definitions as $fqn => $definition) {
|
2016-12-13 00:51:02 +00:00
|
|
|
$this->index->setDefinition($fqn, $definition);
|
2016-10-08 12:59:08 +00:00
|
|
|
}
|
2016-10-11 23:45:15 +00:00
|
|
|
// Register this document on the project for references
|
2016-11-18 14:22:24 +00:00
|
|
|
$this->referenceNodes = $referencesCollector->nodes;
|
|
|
|
foreach ($referencesCollector->nodes as $fqn => $nodes) {
|
2016-12-13 00:51:02 +00:00
|
|
|
$this->index->addReferenceUri($fqn, $this->uri);
|
2016-10-11 23:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->stmts = $stmts;
|
2016-10-08 11:34:49 +00:00
|
|
|
}
|
2016-11-14 19:00:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 09:30:08 +00:00
|
|
|
/**
|
2016-10-10 13:06:02 +00:00
|
|
|
* Returns array of TextEdit changes to format this document.
|
2016-09-30 09:30:08 +00:00
|
|
|
*
|
2016-10-10 13:06:02 +00:00
|
|
|
* @return \LanguageServer\Protocol\TextEdit[]
|
2016-09-30 09:30:08 +00:00
|
|
|
*/
|
|
|
|
public function getFormattedText()
|
|
|
|
{
|
2016-10-11 12:42:56 +00:00
|
|
|
if (empty($this->content)) {
|
2016-09-30 09:30:08 +00:00
|
|
|
return [];
|
|
|
|
}
|
2016-10-10 13:06:02 +00:00
|
|
|
return Formatter::format($this->content, $this->uri);
|
2016-09-30 09:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this document's text content.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getContent()
|
|
|
|
{
|
|
|
|
return $this->content;
|
|
|
|
}
|
2016-10-08 11:34:49 +00:00
|
|
|
|
2016-12-13 00:51:02 +00:00
|
|
|
/**
|
|
|
|
* Returns this document's diagnostics
|
|
|
|
*
|
|
|
|
* @return Diagnostic[]
|
|
|
|
*/
|
|
|
|
public function getDiagnostics()
|
|
|
|
{
|
|
|
|
return $this->diagnostics;
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the URI of the document
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getUri(): string
|
|
|
|
{
|
|
|
|
return $this->uri;
|
|
|
|
}
|
|
|
|
|
2016-10-11 23:45:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the AST of the document
|
|
|
|
*
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
|
|
|
public function getStmts(): array
|
|
|
|
{
|
|
|
|
return $this->stmts;
|
|
|
|
}
|
|
|
|
|
2016-10-08 11:34:49 +00:00
|
|
|
/**
|
|
|
|
* Returns the node at a specified position
|
|
|
|
*
|
|
|
|
* @param Position $position
|
|
|
|
* @return Node|null
|
|
|
|
*/
|
|
|
|
public function getNodeAtPosition(Position $position)
|
|
|
|
{
|
2016-11-30 21:23:51 +00:00
|
|
|
if ($this->stmts === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-08 11:34:49 +00:00
|
|
|
$traverser = new NodeTraverser;
|
|
|
|
$finder = new NodeAtPositionFinder($position);
|
|
|
|
$traverser->addVisitor($finder);
|
2016-10-11 23:45:15 +00:00
|
|
|
$traverser->traverse($this->stmts);
|
2016-10-08 11:34:49 +00:00
|
|
|
return $finder->node;
|
|
|
|
}
|
2016-10-08 12:59:08 +00:00
|
|
|
|
2016-11-30 21:23:51 +00:00
|
|
|
/**
|
|
|
|
* Returns a range of the content
|
|
|
|
*
|
|
|
|
* @param Range $range
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getRange(Range $range)
|
|
|
|
{
|
|
|
|
if ($this->content === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$start = $range->start->toOffset($this->content);
|
|
|
|
$length = $range->end->toOffset($this->content) - $start;
|
|
|
|
return substr($this->content, $start, $length);
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the definition node for a fully qualified name
|
|
|
|
*
|
|
|
|
* @param string $fqn
|
|
|
|
* @return Node|null
|
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
public function getDefinitionNodeByFqn(string $fqn)
|
2016-10-08 12:59:08 +00:00
|
|
|
{
|
2016-11-18 14:22:24 +00:00
|
|
|
return $this->definitionNodes[$fqn] ?? null;
|
2016-10-08 12:59:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 12:42:56 +00:00
|
|
|
/**
|
|
|
|
* Returns a map from fully qualified name (FQN) to Nodes defined in this document
|
|
|
|
*
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
public function getDefinitionNodes()
|
2016-10-11 12:42:56 +00:00
|
|
|
{
|
2016-11-18 14:22:24 +00:00
|
|
|
return $this->definitionNodes;
|
2016-10-11 12:42:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 00:08:23 +00:00
|
|
|
/**
|
2016-11-18 14:22:24 +00:00
|
|
|
* Returns a map from fully qualified name (FQN) to Definition defined in this document
|
2016-10-20 00:08:23 +00:00
|
|
|
*
|
2016-11-18 14:22:24 +00:00
|
|
|
* @return Definition[]
|
2016-10-20 00:08:23 +00:00
|
|
|
*/
|
2016-11-18 14:22:24 +00:00
|
|
|
public function getDefinitions()
|
2016-10-20 00:08:23 +00:00
|
|
|
{
|
2016-11-29 12:10:02 +00:00
|
|
|
return $this->definitions ?? [];
|
2016-10-20 00:08:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
/**
|
2016-10-09 08:09:09 +00:00
|
|
|
* Returns true if the given FQN is defined in this document
|
|
|
|
*
|
|
|
|
* @param string $fqn The fully qualified name of the symbol
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isDefined(string $fqn): bool
|
|
|
|
{
|
|
|
|
return isset($this->definitions[$fqn]);
|
|
|
|
}
|
2016-09-30 09:30:08 +00:00
|
|
|
}
|