1
0
Fork 0

Change Project map to FQN => URI

Removes PhpDocument::load(), isLoaded(), unload()
pull/63/head
Felix Becker 2016-10-11 00:53:59 +02:00
parent 58fb1b9e13
commit 8ad59a914a
13 changed files with 99 additions and 104 deletions

View File

@ -153,7 +153,7 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
$shortName = substr($file, strlen($rootPath) + 1); $shortName = substr($file, strlen($rootPath) + 1);
$this->client->window->logMessage(MessageType::INFO, "Parsing file $fileNum/$numTotalFiles: $shortName."); $this->client->window->logMessage(MessageType::INFO, "Parsing file $fileNum/$numTotalFiles: $shortName.");
$this->project->getDocument($uri)->updateContent(file_get_contents($file), false); $this->project->loadDocument($uri);
Loop\setTimeout($processFile, 0); Loop\setTimeout($processFile, 0);
} else { } else {

View File

@ -77,45 +77,13 @@ class PhpDocument
* @param LanguageClient $client The LanguageClient instance (to report errors etc) * @param LanguageClient $client The LanguageClient instance (to report errors etc)
* @param Parser $parser The PHPParser instance * @param Parser $parser The PHPParser instance
*/ */
public function __construct(string $uri, Project $project, LanguageClient $client, Parser $parser) public function __construct(string $uri, string $content, Project $project, LanguageClient $client, Parser $parser)
{ {
$this->uri = $uri; $this->uri = $uri;
$this->project = $project; $this->project = $project;
$this->client = $client; $this->client = $client;
$this->parser = $parser; $this->parser = $parser;
} $this->updateContent($content);
/**
* Returns true if the content of this document is being held in memory
*
* @return bool
*/
public function isLoaded()
{
return isset($this->content);
}
/**
* Loads the content from disk and saves statements and definitions in memory
*
* @return void
*/
public function load()
{
$this->updateContent(file_get_contents(uriToPath($this->getUri())), true);
}
/**
* Unloads the content, statements and definitions from memory
*
* @return void
*/
public function unload()
{
unset($this->content);
unset($this->statements);
unset($this->definitions);
unset($this->references);
} }
/** /**
@ -126,12 +94,9 @@ class PhpDocument
* @param bool $keepInMemory Wether to keep content, statements and definitions in memory or only update project definitions * @param bool $keepInMemory Wether to keep content, statements and definitions in memory or only update project definitions
* @return void * @return void
*/ */
public function updateContent(string $content, bool $keepInMemory = true) public function updateContent(string $content)
{ {
$keepInMemory = $keepInMemory || $this->isLoaded(); $this->content = $content;
if ($keepInMemory) {
$this->content = $content;
}
$stmts = null; $stmts = null;
$errors = []; $errors = [];
try { try {
@ -180,13 +145,11 @@ class PhpDocument
// Register this document on the project for all the symbols defined in it // Register this document on the project for all the symbols defined in it
foreach ($definitionCollector->definitions as $fqn => $node) { foreach ($definitionCollector->definitions as $fqn => $node) {
$this->project->addDefinitionDocument($fqn, $this); $this->project->addDefinitionDocument($fqn, $this->uri);
} }
if ($keepInMemory) { $this->statements = $stmts;
$this->statements = $stmts; $this->definitions = $definitionCollector->definitions;
$this->definitions = $definitionCollector->definitions;
}
} }
} }
@ -211,9 +174,6 @@ class PhpDocument
*/ */
public function getContent() public function getContent()
{ {
if (!isset($this->content)) {
throw new Exception('Content is not loaded');
}
return $this->content; return $this->content;
} }
@ -224,9 +184,6 @@ class PhpDocument
*/ */
public function &getStatements() public function &getStatements()
{ {
if (!isset($this->statements)) {
$this->parse($this->getContent());
}
return $this->statements; return $this->statements;
} }
@ -251,7 +208,7 @@ class PhpDocument
$traverser = new NodeTraverser; $traverser = new NodeTraverser;
$finder = new NodeAtPositionFinder($position); $finder = new NodeAtPositionFinder($position);
$traverser->addVisitor($finder); $traverser->addVisitor($finder);
$traverser->traverse($this->getStatements()); $traverser->traverse($this->statements);
return $finder->node; return $finder->node;
} }
@ -263,7 +220,7 @@ class PhpDocument
*/ */
public function getDefinitionByFqn(string $fqn) public function getDefinitionByFqn(string $fqn)
{ {
return $this->getDefinitions()[$fqn] ?? null; return $this->definitions[$fqn] ?? null;
} }
/** /**
@ -274,9 +231,6 @@ class PhpDocument
*/ */
public function &getDefinitions() public function &getDefinitions()
{ {
if (!isset($this->definitions)) {
throw new Exception('Definitions of this document are not loaded');
}
return $this->definitions; return $this->definitions;
} }
@ -288,7 +242,7 @@ class PhpDocument
*/ */
public function isDefined(string $fqn): bool public function isDefined(string $fqn): bool
{ {
return isset($this->getDefinitions()[$fqn]); return isset($this->definitions[$fqn]);
} }
/** /**
@ -481,9 +435,6 @@ class PhpDocument
return null; return null;
} }
$document = $this->project->getDefinitionDocument($fqn); $document = $this->project->getDefinitionDocument($fqn);
if (!$document->isLoaded()) {
$document->load();
}
if (!isset($document)) { if (!isset($document)) {
return null; return null;
} }

View File

@ -18,10 +18,9 @@ class Project
private $documents = []; private $documents = [];
/** /**
* An associative array [string => PhpDocument] * An associative array that maps fully qualified symbol names to document URIs
* that maps fully qualified symbol names to loaded PhpDocuments
* *
* @var PhpDocument[] * @var string[]
*/ */
private $definitions = []; private $definitions = [];
@ -48,7 +47,8 @@ class Project
} }
/** /**
* Returns the document indicated by uri. Instantiates a new document if none exists. * Returns the document indicated by uri.
* If the document is not open, tries to read it from disk, but the document is not added the list of open documents.
* *
* @param string $uri * @param string $uri
* @return LanguageServer\PhpDocument * @return LanguageServer\PhpDocument
@ -56,9 +56,58 @@ class Project
public function getDocument(string $uri) public function getDocument(string $uri)
{ {
if (!isset($this->documents[$uri])) { if (!isset($this->documents[$uri])) {
$this->documents[$uri] = new PhpDocument($uri, $this, $this->client, $this->parser); return $this->loadDocument($uri);
} else {
return $this->documents[$uri];
} }
return $this->documents[$uri]; }
/**
* Reads a document from disk.
* The document is NOT added to the list of open documents, but definitions are registered.
*
* @param string $uri
* @return LanguageServer\PhpDocument
*/
public function loadDocument(string $uri)
{
$content = file_get_contents(uriToPath($uri));
if (isset($this->documents[$uri])) {
$document = $this->documents[$uri];
$document->updateContent($content);
} else {
$document = new PhpDocument($uri, $content, $this, $this->client, $this->parser);
}
return $document;
}
/**
* Ensures a document is loaded and added to the list of open documents.
*
* @param string $uri
* @param string $content
* @return void
*/
public function openDocument(string $uri, string $content)
{
if (isset($this->documents[$uri])) {
$document = $this->documents[$uri];
$document->updateContent($content);
} else {
$document = new PhpDocument($uri, $content, $this, $this->client, $this->parser);
$this->documents[$uri] = $document;
}
return $document;
}
public function closeDocument(string $uri)
{
unset($this->documents[$uri]);
}
public function isDocumentOpen(string $uri)
{
return isset($this->documents[$uri]);
} }
/** /**
@ -67,9 +116,9 @@ class Project
* @param string $fqn The fully qualified name of the symbol * @param string $fqn The fully qualified name of the symbol
* @return void * @return void
*/ */
public function addDefinitionDocument(string $fqn, PhpDocument $document) public function addDefinitionDocument(string $fqn, string $uri)
{ {
$this->definitions[$fqn] = $document; $this->definitions[$fqn] = $uri;
} }
/** /**
@ -80,12 +129,12 @@ class Project
*/ */
public function getDefinitionDocument(string $fqn) public function getDefinitionDocument(string $fqn)
{ {
return $this->definitions[$fqn] ?? null; return isset($this->definitions[$fqn]) ? $this->getDocument($this->definitions[$fqn]) : null;
} }
/** /**
* Returns an associative array [string => PhpDocument] * Returns an associative array [string => string] that maps fully qualified symbol names
* that maps fully qualified symbol names to loaded PhpDocuments * to URIs of the document where the symbol is defined
* *
* @return PhpDocument[] * @return PhpDocument[]
*/ */

View File

@ -67,7 +67,7 @@ class TextDocument
*/ */
public function didOpen(TextDocumentItem $textDocument) public function didOpen(TextDocumentItem $textDocument)
{ {
$this->project->getDocument($textDocument->uri)->updateContent($textDocument->text); $this->project->openDocument($textDocument->uri, $textDocument->text);
} }
/** /**
@ -92,7 +92,7 @@ class TextDocument
*/ */
public function didClose(TextDocumentIdentifier $textDocument) public function didClose(TextDocumentIdentifier $textDocument)
{ {
$this->project->getDocument($textDocument->uri)->unload(); $this->project->closeDocument($textDocument->uri);
} }
/** /**

View File

@ -54,12 +54,9 @@ class Workspace
public function symbol(string $query): array public function symbol(string $query): array
{ {
$symbols = []; $symbols = [];
foreach ($this->project->getDefinitionDocuments() as $fqn => $document) { foreach ($this->project->getDefinitionDocuments() as $fqn => $uri) {
if ($query === '' || stripos($fqn, $query) !== false) { if ($query === '' || stripos($fqn, $query) !== false) {
if (!$document->isLoaded()) { $symbols[] = SymbolInformation::fromNode($this->project->getDocument($uri)->getDefinitionByFqn($fqn), $fqn);
$document->load();
}
$symbols[] = SymbolInformation::fromNode($document->getDefinitionByFqn($fqn), $fqn);
} }
} }
return $symbols; return $symbols;

View File

@ -9,6 +9,7 @@ use PhpParser\NodeVisitor\NameResolver;
use LanguageServer\{LanguageClient, Project, PhpDocument}; use LanguageServer\{LanguageClient, Project, PhpDocument};
use LanguageServer\Tests\MockProtocolStream; use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\NodeVisitor\{ReferencesAdder, DefinitionCollector}; use LanguageServer\NodeVisitor\{ReferencesAdder, DefinitionCollector};
use function LanguageServer\pathToUri;
class DefinitionCollectorTest extends TestCase class DefinitionCollectorTest extends TestCase
{ {
@ -17,13 +18,14 @@ class DefinitionCollectorTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$document = new PhpDocument('symbols', $project, $client, $parser); $uri = pathToUri(realpath(__DIR__ . '/../../fixtures/symbols.php'));
$document = $project->loadDocument($uri);
$traverser = new NodeTraverser; $traverser = new NodeTraverser;
$traverser->addVisitor(new NameResolver); $traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ReferencesAdder($document)); $traverser->addVisitor(new ReferencesAdder($document));
$definitionCollector = new DefinitionCollector; $definitionCollector = new DefinitionCollector;
$traverser->addVisitor($definitionCollector); $traverser->addVisitor($definitionCollector);
$stmts = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/symbols.php')); $stmts = $parser->parse(file_get_contents($uri));
$traverser->traverse($stmts); $traverser->traverse($stmts);
$defs = $definitionCollector->definitions; $defs = $definitionCollector->definitions;
$this->assertEquals([ $this->assertEquals([
@ -55,13 +57,14 @@ class DefinitionCollectorTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$document = new PhpDocument('references', $project, $client, $parser); $uri = pathToUri(realpath(__DIR__ . '/../../fixtures/references.php'));
$document = $project->loadDocument($uri);
$traverser = new NodeTraverser; $traverser = new NodeTraverser;
$traverser->addVisitor(new NameResolver); $traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ReferencesAdder($document)); $traverser->addVisitor(new ReferencesAdder($document));
$definitionCollector = new DefinitionCollector; $definitionCollector = new DefinitionCollector;
$traverser->addVisitor($definitionCollector); $traverser->addVisitor($definitionCollector);
$stmts = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/references.php')); $stmts = $parser->parse(file_get_contents($uri));
$traverser->traverse($stmts); $traverser->traverse($stmts);
$defs = $definitionCollector->definitions; $defs = $definitionCollector->definitions;
$this->assertEquals(['TestNamespace\\whatever()'], array_keys($defs)); $this->assertEquals(['TestNamespace\\whatever()'], array_keys($defs));

View File

@ -24,17 +24,14 @@ class PhpDocumentTest extends TestCase
public function testParsesVariableVariables() public function testParsesVariableVariables()
{ {
$document = $this->project->getDocument('whatever'); $document = $this->project->openDocument('whatever', "<?php\n$\$a = 'foo';\n\$bar = 'baz';\n");
$document->updateContent("<?php\n$\$a = 'foo';\n\$bar = 'baz';\n");
$this->assertEquals([], $document->getDefinitions()); $this->assertEquals([], $document->getDefinitions());
} }
public function testGetNodeAtPosition() public function testGetNodeAtPosition()
{ {
$document = $this->project->getDocument('whatever'); $document = $this->project->openDocument('whatever', "<?php\n$\$a = new SomeClass;");
$document->updateContent("<?php\n$\$a = new SomeClass;");
$node = $document->getNodeAtPosition(new Position(1, 13)); $node = $document->getNodeAtPosition(new Position(1, 13));
$this->assertInstanceOf(Node\Name\FullyQualified::class, $node); $this->assertInstanceOf(Node\Name\FullyQualified::class, $node);
$this->assertEquals('SomeClass', (string)$node); $this->assertEquals('SomeClass', (string)$node);

View File

@ -8,6 +8,7 @@ use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, Client, LanguageClient, Project, PhpDocument}; use LanguageServer\{Server, Client, LanguageClient, Project, PhpDocument};
use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity, FormattingOptions}; use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity, FormattingOptions};
use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody}; use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody};
use function LanguageServer\pathToUri;
class ProjectTest extends TestCase class ProjectTest extends TestCase
{ {
@ -21,18 +22,18 @@ class ProjectTest extends TestCase
$this->project = new Project(new LanguageClient(new MockProtocolStream())); $this->project = new Project(new LanguageClient(new MockProtocolStream()));
} }
public function testGetDocumentCreatesNewDocument() public function testGetDocumentLoadsDocument()
{ {
$document = $this->project->getDocument('file:///document1.php'); $document = $this->project->getDocument(pathToUri(__FILE__));
$this->assertNotNull($document); $this->assertNotNull($document);
$this->assertInstanceOf(PhpDocument::class, $document); $this->assertInstanceOf(PhpDocument::class, $document);
} }
public function testGetDocumentCreatesDocumentOnce() public function testGetDocumentReturnsOpenedInstance()
{ {
$document1 = $this->project->getDocument('file:///document1.php'); $document1 = $this->project->openDocument(pathToUri(__FILE__), file_get_contents(__FILE__));
$document2 = $this->project->getDocument('file:///document1.php'); $document2 = $this->project->getDocument(pathToUri(__FILE__));
$this->assertSame($document1, $document2); $this->assertSame($document1, $document2);
} }

View File

@ -20,9 +20,9 @@ class DefinitionTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client); $this->textDocument = new Server\TextDocument($project, $client);
$project->getDocument('references')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/references.php')); $project->openDocument('references', file_get_contents(__DIR__ . '/../../../fixtures/references.php'));
$project->getDocument('symbols')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/symbols.php')); $project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../fixtures/symbols.php'));
$project->getDocument('use')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/use.php')); $project->openDocument('use', file_get_contents(__DIR__ . '/../../../fixtures/use.php'));
} }
public function testDefinitionForClassLike() public function testDefinitionForClassLike()

View File

@ -22,8 +22,7 @@ class DidChangeTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$textDocument = new Server\TextDocument($project, $client); $textDocument = new Server\TextDocument($project, $client);
$phpDocument = $project->getDocument('whatever'); $phpDocument = $project->openDocument('whatever', "<?php\necho 'Hello, World'\n");
$phpDocument->updateContent("<?php\necho 'Hello, World'\n");
$identifier = new VersionedTextDocumentIdentifier('whatever'); $identifier = new VersionedTextDocumentIdentifier('whatever');
$changeEvent = new TextDocumentContentChangeEvent(); $changeEvent = new TextDocumentContentChangeEvent();

View File

@ -16,8 +16,7 @@ class DidCloseTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$textDocument = new Server\TextDocument($project, $client); $textDocument = new Server\TextDocument($project, $client);
$phpDocument = $project->getDocument('whatever'); $phpDocument = $project->openDocument('whatever', 'hello world');
$phpDocument->updateContent('hello world');
$textDocumentItem = new TextDocumentItem(); $textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever'; $textDocumentItem->uri = 'whatever';
@ -28,7 +27,6 @@ class DidCloseTest extends TestCase
$textDocument->didClose(new TextDocumentIdentifier($textDocumentItem->uri)); $textDocument->didClose(new TextDocumentIdentifier($textDocumentItem->uri));
$this->expectException(Exception::class); $this->assertFalse($project->isDocumentOpen($textDocumentItem->uri));
$phpDocument->getContent();
} }
} }

View File

@ -20,7 +20,7 @@ class DocumentSymbolTest extends TestCase
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client); $this->textDocument = new Server\TextDocument($project, $client);
$project->getDocument('symbols')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/symbols.php')); $project->openDocument('symbols', file_get_contents(__DIR__ . '/../../../fixtures/symbols.php'));
} }
public function test() public function test()

View File

@ -34,8 +34,8 @@ class SymbolTest extends TestCase
$this->workspace = new Server\Workspace($project, $client); $this->workspace = new Server\Workspace($project, $client);
$this->symbolsUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/symbols.php')); $this->symbolsUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/symbols.php'));
$this->referencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php')); $this->referencesUri = pathToUri(realpath(__DIR__ . '/../../../fixtures/references.php'));
$project->getDocument($this->symbolsUri)->updateContent(file_get_contents($this->symbolsUri), false); $project->loadDocument($this->symbolsUri);
$project->getDocument($this->referencesUri)->updateContent(file_get_contents($this->referencesUri), false); $project->loadDocument($this->referencesUri);
} }
public function testEmptyQueryReturnsAllSymbols() public function testEmptyQueryReturnsAllSymbols()