1
0
Fork 0

Sort by least number of slashes

Change log message
pull/281/head
Trevor Bortins 2017-02-06 11:45:38 -08:00
parent f43e0dc34a
commit 81aed92ecf
3 changed files with 23 additions and 10 deletions

View File

@ -186,7 +186,7 @@ class Indexer
$this->client->window->logMessage(MessageType::INFO, "Storing $packageKey in cache"); $this->client->window->logMessage(MessageType::INFO, "Storing $packageKey in cache");
$this->cache->set($cacheKey, $index); $this->cache->set($cacheKey, $index);
} else { } else {
$this->client->window->logMessage(MessageType::INFO, "Cannot cache $packageName, cache key was null. Either you are using a 'dev-' version or your composer.lock is missing references."); $this->client->window->logMessage(MessageType::WARNING, "Could not compute cache key for $packageName");
} }
} }
} }

View File

@ -25,6 +25,7 @@ use Exception;
use Throwable; use Throwable;
use Webmozart\PathUtil\Path; use Webmozart\PathUtil\Path;
use Sabre\Uri; use Sabre\Uri;
use function LanguageServer\sortByLeastSlashes;
class LanguageServer extends AdvancedJsonRpc\Dispatcher class LanguageServer extends AdvancedJsonRpc\Dispatcher
{ {
@ -206,12 +207,9 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
// Find composer.json // Find composer.json
if ($this->composerJson === null) { if ($this->composerJson === null) {
$composerJsonFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.json', $rootPath)); $composerJsonFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.json', $rootPath));
// If we sort our findings by number of slashes (least to greatest),
// If we sort our findings by string length (shortest to longest),
// the first entry will be the project's root composer.json. // the first entry will be the project's root composer.json.
usort($composerJsonFiles, function ($a, $b) { usort($composerJsonFiles, 'LanguageServer\sortByLeastSlashes');
return strlen($a) - strlen($b);
});
if (!empty($composerJsonFiles)) { if (!empty($composerJsonFiles)) {
$this->composerJson = json_decode(yield $this->contentRetriever->retrieve($composerJsonFiles[0])); $this->composerJson = json_decode(yield $this->contentRetriever->retrieve($composerJsonFiles[0]));
@ -222,11 +220,9 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
if ($this->composerLock === null) { if ($this->composerLock === null) {
$composerLockFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.lock', $rootPath)); $composerLockFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.lock', $rootPath));
// If we sort our findings by string length (shortest to longest), // If we sort our findings by number of slashes (least to greatest),
// the first entry will be the project's root composer.lock. // the first entry will be the project's root composer.lock.
usort($composerLockFiles, function ($a, $b) { usort($composerLockFiles, 'LanguageServer\sortByLeastSlashes');
return strlen($a) - strlen($b);
});
if (!empty($composerLockFiles)) { if (!empty($composerLockFiles)) {
$this->composerLock = json_decode(yield $this->contentRetriever->retrieve($composerLockFiles[0])); $this->composerLock = json_decode(yield $this->contentRetriever->retrieve($composerLockFiles[0]));

View File

@ -7,6 +7,7 @@ use Throwable;
use InvalidArgumentException; use InvalidArgumentException;
use PhpParser\Node; use PhpParser\Node;
use Sabre\Event\{Loop, Promise, EmitterInterface}; use Sabre\Event\{Loop, Promise, EmitterInterface};
use function Sabre\Uri\parse;
/** /**
* Transforms an absolute file path into a URI as used by the language server protocol. * Transforms an absolute file path into a URI as used by the language server protocol.
@ -131,3 +132,19 @@ function stripStringOverlap(string $a, string $b): string
} }
return $b; return $b;
} }
/**
* Use for sorting an array of URIs by number of segments
* in ascending order.
*
* Example:
* usort($uriList, 'LanguageServer\sortByLeastSlashes');
*
* @param $a string
* @param $b string
* @return integer
*/
function sortByLeastSlashes($a, $b)
{
return substr_count(parse($a)['path'], '/') - substr_count(parse($b)['path'], '/');
}