1
0
Fork 0
php-language-server/src/Project.php

115 lines
3.0 KiB
PHP
Raw Normal View History

<?php
declare(strict_types = 1);
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
*
* @var PhpDocument[]
*/
private $documents = [];
2016-10-08 12:59:08 +00:00
/**
* An associative array [string => PhpDocument]
* that maps fully qualified symbol names to loaded PhpDocuments
*
* @var PhpDocument[]
*/
private $definitions = [];
2016-10-08 12:59: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;
}
/**
* 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]);
}
/**
* Finds symbols in all documents, filtered by query parameter.
*
* @param string $query
* @return SymbolInformation[]
*/
public function findSymbols(string $query)
{
$queryResult = [];
foreach ($this->documents as $uri => $document) {
$documentQueryResult = $document->findSymbols($query);
if ($documentQueryResult !== null) {
$queryResult = array_merge($queryResult, $documentQueryResult);
}
}
return $queryResult;
}
}