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;
|
|
|
|
|
|
|
|
use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer};
|
|
|
|
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
|
|
|
|
use PhpParser\NodeVisitor\NameResolver;
|
|
|
|
|
|
|
|
class Project
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* An associative array [string => PhpDocument]
|
|
|
|
* that maps URIs to loaded PhpDocuments
|
|
|
|
*
|
2016-10-08 10:51:55 +00:00
|
|
|
* @var PhpDocument[]
|
2016-09-30 09:30:08 +00:00
|
|
|
*/
|
2016-10-09 08:09:09 +00:00
|
|
|
private $documents = [];
|
2016-09-30 09:30:08 +00:00
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
/**
|
|
|
|
* An associative array [string => PhpDocument]
|
|
|
|
* that maps fully qualified symbol names to loaded PhpDocuments
|
|
|
|
*
|
|
|
|
* @var PhpDocument[]
|
|
|
|
*/
|
2016-10-09 08:09:09 +00:00
|
|
|
private $definitions = [];
|
2016-10-08 12:59:08 +00:00
|
|
|
|
2016-09-30 09:30:08 +00:00
|
|
|
/**
|
|
|
|
* Instance of the PHP parser
|
|
|
|
*
|
|
|
|
* @var ParserAbstract
|
|
|
|
*/
|
|
|
|
private $parser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the language server client interface
|
|
|
|
*
|
|
|
|
* @var LanguageClient
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
|
|
|
public function __construct(LanguageClient $client)
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
|
|
|
|
$lexer = new Lexer(['usedAttributes' => ['comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos']]);
|
|
|
|
$this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7, $lexer, ['throwOnError' => false]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the document indicated by uri. Instantiates a new document if none exists.
|
|
|
|
*
|
|
|
|
* @param string $uri
|
|
|
|
* @return LanguageServer\PhpDocument
|
|
|
|
*/
|
|
|
|
public function getDocument(string $uri)
|
|
|
|
{
|
|
|
|
if (!isset($this->documents[$uri])) {
|
|
|
|
$this->documents[$uri] = new PhpDocument($uri, $this, $this->client, $this->parser);
|
|
|
|
}
|
|
|
|
return $this->documents[$uri];
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:59:08 +00:00
|
|
|
/**
|
|
|
|
* Adds a document as the container for a specific symbol
|
|
|
|
*
|
|
|
|
* @param string $fqn The fully qualified name of the symbol
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addDefinitionDocument(string $fqn, PhpDocument $document)
|
|
|
|
{
|
|
|
|
$this->definitions[$fqn] = $document;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the document where a symbol is defined
|
|
|
|
*
|
|
|
|
* @param string $fqn The fully qualified name of the symbol
|
|
|
|
* @return PhpDocument|null
|
|
|
|
*/
|
|
|
|
public function getDefinitionDocument(string $fqn)
|
|
|
|
{
|
|
|
|
return $this->definitions[$fqn] ?? null;
|
|
|
|
}
|
|
|
|
|
2016-10-09 08:09:09 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the given FQN is defined in the project
|
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Finds symbols in all documents, filtered by query parameter.
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @return SymbolInformation[]
|
|
|
|
*/
|
|
|
|
public function findSymbols(string $query)
|
|
|
|
{
|
|
|
|
$queryResult = [];
|
2016-10-08 10:51:55 +00:00
|
|
|
foreach ($this->documents as $uri => $document) {
|
2016-10-08 17:08:44 +00:00
|
|
|
$documentQueryResult = $document->findSymbols($query);
|
|
|
|
if ($documentQueryResult !== null) {
|
|
|
|
$queryResult = array_merge($queryResult, $documentQueryResult);
|
|
|
|
}
|
2016-09-30 09:30:08 +00:00
|
|
|
}
|
|
|
|
return $queryResult;
|
|
|
|
}
|
|
|
|
}
|