From 81aed92ecfd32b2c6417e215d20bd4f3ed1fd088 Mon Sep 17 00:00:00 2001 From: Trevor Bortins Date: Mon, 6 Feb 2017 11:45:38 -0800 Subject: [PATCH] Sort by least number of slashes Change log message --- src/Indexer.php | 2 +- src/LanguageServer.php | 14 +++++--------- src/utils.php | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Indexer.php b/src/Indexer.php index 81b0f58..3f7cf4b 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -186,7 +186,7 @@ class Indexer $this->client->window->logMessage(MessageType::INFO, "Storing $packageKey in cache"); $this->cache->set($cacheKey, $index); } 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"); } } } diff --git a/src/LanguageServer.php b/src/LanguageServer.php index ac392dc..01f8d5c 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -25,6 +25,7 @@ use Exception; use Throwable; use Webmozart\PathUtil\Path; use Sabre\Uri; +use function LanguageServer\sortByLeastSlashes; class LanguageServer extends AdvancedJsonRpc\Dispatcher { @@ -206,12 +207,9 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher // Find composer.json if ($this->composerJson === null) { $composerJsonFiles = yield $this->filesFinder->find(Path::makeAbsolute('**/composer.json', $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.json. - usort($composerJsonFiles, function ($a, $b) { - return strlen($a) - strlen($b); - }); + usort($composerJsonFiles, 'LanguageServer\sortByLeastSlashes'); if (!empty($composerJsonFiles)) { $this->composerJson = json_decode(yield $this->contentRetriever->retrieve($composerJsonFiles[0])); @@ -222,11 +220,9 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher if ($this->composerLock === null) { $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. - usort($composerLockFiles, function ($a, $b) { - return strlen($a) - strlen($b); - }); + usort($composerLockFiles, 'LanguageServer\sortByLeastSlashes'); if (!empty($composerLockFiles)) { $this->composerLock = json_decode(yield $this->contentRetriever->retrieve($composerLockFiles[0])); diff --git a/src/utils.php b/src/utils.php index 8ab7beb..071be13 100644 --- a/src/utils.php +++ b/src/utils.php @@ -7,6 +7,7 @@ use Throwable; use InvalidArgumentException; use PhpParser\Node; 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. @@ -131,3 +132,19 @@ function stripStringOverlap(string $a, string $b): string } 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'], '/'); +}