1
0
Fork 0
pull/136/head
Felix Becker 2016-11-12 14:31:57 +01:00
parent cdb2c46f68
commit d9adfea422
4 changed files with 39 additions and 29 deletions

View File

@ -32,7 +32,8 @@
"symfony/debug": "^3.1", "symfony/debug": "^3.1",
"netresearch/jsonmapper": "^1.0", "netresearch/jsonmapper": "^1.0",
"webmozart/path-util": "^2.3", "webmozart/path-util": "^2.3",
"webmozart/glob": "^4.1" "webmozart/glob": "^4.1",
"sabre/uri": "^2.0"
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"prefer-stable": true, "prefer-stable": true,

View File

@ -30,16 +30,16 @@ class Workspace
} }
/** /**
* Returns a list of all files in the workspace that match any of the given glob patterns * Returns a list of all files in a directory
* *
* @param string[] $patterns Glob patterns * @param string $base The base directory (defaults to the workspace)
* @return Promise <TextDocumentIdentifier[]> Array of documents that match the glob patterns * @return Promise <TextDocumentIdentifier[]> Array of documents
*/ */
public function xglob(array $patterns): Promise public function xfiles(string $base = null): Promise
{ {
return $this->handler->request( return $this->handler->request(
'workspace/xglob', 'workspace/xfiles',
['patterns' => $patterns] ['base' => $base]
)->then(function (array $textDocuments) { )->then(function (array $textDocuments) {
return $this->mapper->mapArray($textDocuments, [], TextDocumentIdentifier::class); return $this->mapper->mapArray($textDocuments, [], TextDocumentIdentifier::class);
}); });

View File

@ -20,7 +20,9 @@ use Exception;
use RuntimeException; use RuntimeException;
use Throwable; use Throwable;
use Webmozart\Glob\Iterator\GlobIterator; use Webmozart\Glob\Iterator\GlobIterator;
use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path; use Webmozart\PathUtil\Path;
use Sabre\Uri;
class LanguageServer extends AdvancedJsonRpc\Dispatcher class LanguageServer extends AdvancedJsonRpc\Dispatcher
{ {
@ -215,19 +217,24 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
private function findPhpFiles(): Promise private function findPhpFiles(): Promise
{ {
return coroutine(function () { return coroutine(function () {
$textDocuments = [];
$pattern = Path::makeAbsolute('**/*.php', $this->rootPath);
if ($this->clientCapabilities->xfilesProvider) { if ($this->clientCapabilities->xfilesProvider) {
// Use xfiles request // Use xfiles request
return yield $this->client->workspace->xfiles($patterns); foreach (yield $this->client->workspace->xfiles() as $textDocument) {
$path = Uri\parse($textDocument->uri)['path'];
if (Glob::match($path, $pattern)) {
$textDocuments[] = $textDocument;
}
}
} else { } else {
// Use the file system // Use the file system
$textDocuments = [];
$pattern = Path::makeAbsolute('**/*.php', $this->rootPath);
foreach (new GlobIterator($pattern) as $path) { foreach (new GlobIterator($pattern) as $path) {
$textDocuments[] = new TextDocumentIdentifier(pathToUri($path)); $textDocuments[] = new TextDocumentIdentifier(pathToUri($path));
yield timeout(); yield timeout();
} }
return $textDocuments;
} }
return $textDocuments;
}); });
} }
} }

View File

@ -5,7 +5,8 @@ namespace LanguageServer\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\LanguageServer; use LanguageServer\LanguageServer;
use LanguageServer\Protocol\{Message, ClientCapabilities, TextDocumentSyncKind, MessageType, Content}; use LanguageServer\Protocol\{
Message, ClientCapabilities, TextDocumentSyncKind, MessageType, TextDocumentItem, TextDocumentIdentifier};
use AdvancedJsonRpc; use AdvancedJsonRpc;
use Webmozart\Glob\Glob; use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path; use Webmozart\PathUtil\Path;
@ -71,31 +72,32 @@ class LanguageServerTest extends TestCase
$promise->wait(); $promise->wait();
} }
public function testIndexingWithGlobAndContentRequests() public function testIndexingWithFilesAndContentRequests()
{ {
$promise = new Promise; $promise = new Promise;
$globCalled = false; $filesCalled = false;
$contentCalled = false; $contentCalled = false;
$rootPath = realpath(__DIR__ . '/../fixtures'); $rootPath = realpath(__DIR__ . '/../fixtures');
$input = new MockProtocolStream; $input = new MockProtocolStream;
$output = new MockProtocolStream; $output = new MockProtocolStream;
$output->on('message', function (Message $msg) use ($promise, $input, $rootPath, &$globCalled, &$contentCalled) { $output->on('message', function (Message $msg) use ($promise, $input, $rootPath, &$filesCalled, &$contentCalled) {
if ($msg->body->method === 'textDocument/xcontent') { if ($msg->body->method === 'textDocument/xcontent') {
// Document content requested // Document content requested
$contentCalled = true; $contentCalled = true;
$input->write(new Message(new AdvancedJsonRpc\SuccessResponse( $textDocumentItem = new TextDocumentItem;
$msg->body->id, $textDocumentItem->uri = $msg->body->params->textDocument->uri;
new Content(file_get_contents($msg->body->params->textDocument->uri)) $textDocumentItem->version = 1;
))); $textDocumentItem->languageId = 'php';
} else if ($msg->body->method === 'workspace/xglob') { $textDocumentItem->text = file_get_contents($msg->body->params->textDocument->uri);
$input->write(new Message(new AdvancedJsonRpc\SuccessResponse($msg->body->id, $textDocumentItem)));
} else if ($msg->body->method === 'workspace/xfiles') {
// Glob requested // Glob requested
$globCalled = true; $filesCalled = true;
$files = array_map( $pattern = Path::makeAbsolute('**/*.php', $msg->body->params->base ?? $rootPath);
'\\LanguageServer\\pathToUri', $files = [];
array_merge(...array_map(function (string $pattern) use ($rootPath) { foreach (Glob::glob($pattern) as $path) {
return Glob::glob(Path::makeAbsolute($pattern, $rootPath)); $files[] = new TextDocumentIdentifier(pathToUri($path));
}, $msg->body->params->patterns)) }
);
$input->write(new Message(new AdvancedJsonRpc\SuccessResponse($msg->body->id, $files))); $input->write(new Message(new AdvancedJsonRpc\SuccessResponse($msg->body->id, $files)));
} else if ($msg->body->method === 'window/logMessage') { } else if ($msg->body->method === 'window/logMessage') {
// Message logged // Message logged
@ -112,11 +114,11 @@ class LanguageServerTest extends TestCase
}); });
$server = new LanguageServer($input, $output); $server = new LanguageServer($input, $output);
$capabilities = new ClientCapabilities; $capabilities = new ClientCapabilities;
$capabilities->xglobProvider = true; $capabilities->xfilesProvider = true;
$capabilities->xcontentProvider = true; $capabilities->xcontentProvider = true;
$server->initialize(getmypid(), $capabilities, $rootPath); $server->initialize(getmypid(), $capabilities, $rootPath);
$promise->wait(); $promise->wait();
$this->assertTrue($globCalled); $this->assertTrue($filesCalled);
$this->assertTrue($contentCalled); $this->assertTrue($contentCalled);
} }
} }