From b9aeea2523d8c46cb16eb8086b08e8e2a53d3ee7 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Thu, 3 Nov 2016 11:52:51 +0100 Subject: [PATCH] Add globWorkspace helper --- src/LanguageServer.php | 57 ++++++++++++++++--------- src/utils.php | 19 --------- tests/Utils/RecursiveFileSearchTest.php | 21 --------- 3 files changed, 37 insertions(+), 60 deletions(-) delete mode 100644 tests/Utils/RecursiveFileSearchTest.php diff --git a/src/LanguageServer.php b/src/LanguageServer.php index cdc7641..ce74c0b 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -18,6 +18,8 @@ use function Sabre\Event\coroutine; use Exception; use Throwable; use Generator; +use Webmozart\Glob\Iterator\GlobIterator; +use Webmozart\PathUril\Path; class LanguageServer extends AdvancedJsonRpc\Dispatcher { @@ -182,32 +184,15 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher private function indexProject() { return coroutine(function () { - if ($this->clientCapabilities->xglobProvider) { - $textDocuments = yield $this->client->workspace->xglob('**/*.php'); - $uris = array_map(function ($textDocument) { - return $textDocument->uri; - }, $textDocuments); - } else { - $uris = array_map(function ($path) { - return pathToUri($path); - }, findFilesRecursive($this->rootPath, '/^.+\.php$/i')); - } + $textDocuments = yield $this->globWorkspace('**/*.php'); $count = count($uris); $startTime = microtime(true); - foreach ($uris as $i => $uri) { + foreach ($textDocuments as $i => $textDocument) { // Give LS to the chance to handle requests while indexing Loop\tick(); - - try { - $shortName = substr(uriToPath($uri), strlen($this->rootPath) + 1); - } catch (Exception $e) { - $shortName = $uri; - } - - - $this->client->window->logMessage(MessageType::INFO, "Parsing file $i/$count: $shortName."); + $this->client->window->logMessage(MessageType::INFO, "Parsing file $i/$count: {$textDocument->uri}"); try { $this->project->loadDocument($uri); } catch (Exception $e) { @@ -220,4 +205,36 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher $this->client->window->logMessage(MessageType::INFO, "All PHP files parsed in $duration seconds. $mem MiB allocated."); }); } + + /** + * Returns all files matching a glob pattern. + * If the client does not support workspace/xglob, it falls back to globbing the file system directly. + * + * @param string $pattern + * @return Promise + */ + private function globWorkspace(string $pattern): Promise + { + if ($this->clientCapabilities->xglobProvider) { + // Use xglob request + return $this->client->workspace->xglob($pattern); + } else { + // Use the file system + $promise = new Promise; + $textDocuments = []; + $pattern = Path::makeAbsolute($pattern, $this->rootPath); + $iterator = new GlobIterator($pattern); + $next = function () use ($iterator, &$textDocuments, $promise, &$next) { + if (!$iterator->valid()) { + $promise->resolve($textDocuments); + return; + } + $textDocuments[] = new TextDocumentIdentifier(pathToUri($iterator->current())); + $iterator->next(); + Loop\setTimeout($next, 0); + }; + Loop\setTimeout($next, 0); + return $promise; + } + } } diff --git a/src/utils.php b/src/utils.php index 810fbc1..a190c8c 100644 --- a/src/utils.php +++ b/src/utils.php @@ -5,25 +5,6 @@ namespace LanguageServer; use InvalidArgumentException; -/** - * Recursively Searches files with matching filename, starting at $path. - * - * @param string $path - * @param string $pattern - * @return array - */ -function findFilesRecursive(string $path, string $pattern): array -{ - $dir = new \RecursiveDirectoryIterator($path); - $ite = new \RecursiveIteratorIterator($dir); - $files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH); - $fileList = []; - foreach ($files as $file) { - $fileList = array_merge($fileList, $file); - } - return $fileList; -} - /** * Transforms an absolute file path into a URI as used by the language server protocol. * diff --git a/tests/Utils/RecursiveFileSearchTest.php b/tests/Utils/RecursiveFileSearchTest.php deleted file mode 100644 index 671ebb1..0000000 --- a/tests/Utils/RecursiveFileSearchTest.php +++ /dev/null @@ -1,21 +0,0 @@ -assertEquals([ - $path . DIRECTORY_SEPARATOR . 'a.txt', - $path . DIRECTORY_SEPARATOR . 'search' . DIRECTORY_SEPARATOR . 'b.txt', - $path . DIRECTORY_SEPARATOR . 'search' . DIRECTORY_SEPARATOR . 'here' . DIRECTORY_SEPARATOR . 'c.txt', - ], $files); - } -}