1
0
Fork 0

refactor uriInVendorDir to getPackageName

pull/281/head
Trevor Bortins 2017-02-07 08:21:34 -08:00
parent 682cb2a072
commit d95874d432
5 changed files with 18 additions and 14 deletions

View File

@ -3,6 +3,8 @@ declare(strict_types = 1);
namespace LanguageServer\Index;
use function LanguageServer\getPackageName;
/**
* A project index manages the source and dependency indexes
*/
@ -44,8 +46,8 @@ class ProjectIndex extends AbstractAggregateIndex
*/
public function getIndexForUri(string $uri): Index
{
if (\LanguageServer\uriInVendorDir($this->composerJson, $uri, $matches)) {
$packageName = $matches[1];
$packageName = getPackageName($this->composerJson, $uri);
if ($packageName) {
return $this->dependenciesIndex->getDependencyIndex($packageName);
}
return $this->sourceIndex;

View File

@ -118,9 +118,9 @@ class Indexer
$deps = [];
foreach ($uris as $uri) {
if ($this->composerLock !== null && uriInVendorDir($this->composerJson, $uri, $matches)) {
$packageName = getPackageName($this->composerJson, $uri);
if ($this->composerLock !== null && $packageName) {
// Dependency file
$packageName = $matches[1];
if (!isset($deps[$packageName])) {
$deps[$packageName] = [];
}

View File

@ -9,6 +9,7 @@ use LanguageServer\Protocol\{SymbolInformation, SymbolDescriptor, ReferenceInfor
use Sabre\Event\Promise;
use function Sabre\Event\coroutine;
use function LanguageServer\waitForEvent;
use function LanguageServer\getPackageName;
/**
* Provides method handlers for all workspace/* methods
@ -123,8 +124,7 @@ class Workspace
$symbol->$prop = $val;
}
// Find out package name
uriInVendorDir($this->composerJson, $def->symbolInformation->location->uri, $matches);
$packageName = $matches[1];
$packageName = getPackageName($this->composerJson, $def->symbolInformation->location->uri);
foreach ($this->composerLock->packages as $package) {
if ($package->name === $packageName) {
$symbol->package = $package;

View File

@ -169,12 +169,13 @@ function isVendored(PhpDocument $document, \stdClass $composerJson = null): bool
* @param \stdClass|null $composerJson
* @param string $uri
* @param array $matches
* @return int
* @return string
*/
function uriInVendorDir(\stdClass $composerJson = null, string $uri, &$matches): int
function getPackageName(\stdClass $composerJson = null, string $uri): ?string
{
$vendorDir = str_replace('/', '\/', getVendorDir($composerJson));
return preg_match("/\/$vendorDir\/([^\/]+\/[^\/]+)\//", $uri, $matches);
preg_match("/\/$vendorDir\/([^\/]+\/[^\/]+)\//", $uri, $matches);
return isset($matches[1]) ? $matches[1] : null;
}
/**

View File

@ -12,6 +12,7 @@ use LanguageServer\ContentRetriever\FileSystemContentRetriever;
use LanguageServer\Protocol\{SymbolKind, Position, ClientCapabilities};
use LanguageServer\Index\{Index, ProjectIndex, DependenciesIndex};
use PhpParser\Node;
use function LanguageServer\isVendored;
class PhpDocumentTest extends TestCase
{
@ -42,18 +43,18 @@ class PhpDocumentTest extends TestCase
public function testIsVendored()
{
$document = $this->createDocument('file:///dir/vendor/x.php', "<?php\n$\$a = new SomeClass;");
$this->assertEquals(true, \LanguageServer\isVendored($document));
$this->assertEquals(true, isVendored($document));
$document = $this->createDocument('file:///c:/dir/vendor/x.php', "<?php\n$\$a = new SomeClass;");
$this->assertEquals(true, \LanguageServer\isVendored($document));
$this->assertEquals(true, isVendored($document));
$document = $this->createDocument('file:///vendor/x.php', "<?php\n$\$a = new SomeClass;");
$this->assertEquals(true, \LanguageServer\isVendored($document));
$this->assertEquals(true, isVendored($document));
$document = $this->createDocument('file:///dir/vendor.php', "<?php\n$\$a = new SomeClass;");
$this->assertEquals(false, \LanguageServer\isVendored($document));
$this->assertEquals(false, isVendored($document));
$document = $this->createDocument('file:///dir/x.php', "<?php\n$\$a = new SomeClass;");
$this->assertEquals(false, \LanguageServer\isVendored($document));
$this->assertEquals(false, isVendored($document));
}
}