Skip to content

Commit

Permalink
Fixes #6013, implement workspace/didChangeWatchedFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
tm1000 committed Jun 29, 2021
1 parent 8c13752 commit 79fbaa2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Psalm/Internal/LanguageServer/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Internal\LanguageServer\Server\TextDocument;
use Psalm\Internal\LanguageServer\Server\Workspace;
use Throwable;

use function Amp\asyncCoroutine;
Expand Down Expand Up @@ -51,6 +52,13 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
*/
public $textDocument;

/**
* Handles workspace/* method calls
*
* @var ?Server\Workspace
*/
public $workspace;

/**
* @var ProtocolReader
*/
Expand Down Expand Up @@ -221,6 +229,14 @@ function () {
);
}

if ($this->workspace === null) {
$this->workspace = new Workspace(
$this,
$codebase,
$this->project_analyzer->onchange_line_limit
);
}

$serverCapabilities = new ServerCapabilities();

$textDocumentSyncOptions = new TextDocumentSyncOptions();
Expand Down
73 changes: 73 additions & 0 deletions src/Psalm/Internal/LanguageServer/Server/Workspace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types = 1);
namespace Psalm\Internal\LanguageServer\Server;

use LanguageServerProtocol\FileChangeType;
use LanguageServerProtocol\FileEvent;
use Psalm\Codebase;
use Psalm\Internal\LanguageServer\LanguageServer;

/**
* Provides method handlers for all textDocument/* methods
*/
class Workspace
{
/**
* @var LanguageServer
*/
protected $server;

/**
* @var Codebase
*/
protected $codebase;

/** @var ?int */
protected $onchange_line_limit;

public function __construct(
LanguageServer $server,
Codebase $codebase,
?int $onchange_line_limit
) {
$this->server = $server;
$this->codebase = $codebase;
$this->onchange_line_limit = $onchange_line_limit;
}

/**
* The watched files notification is sent from the client to the server when the client
* detects changes to files and folders watched by the language client (note although
* the name suggest that only file events are sent it is about file system events
* which include folders as well). It is recommended that servers register for these
* file system events using the registration mechanism. In former implementations clients
* pushed file events without the server actively asking for it.
*
* @param FileEvent[] $changes
* @psalm-suppress PossiblyUnusedMethod
*/
public function didChangeWatchedFiles(array $changes): void
{
foreach ($changes as $change) {
$file_path = LanguageServer::uriToPath($change->uri);

if ($change->type === FileChangeType::DELETED) {
$this->codebase->invalidateInformationForFile($file_path);
return;
}

if (!$this->codebase->config->isInProjectDirs($file_path)) {
return;
}

if ($this->onchange_line_limit === 0) {
return;
}

//If the file is currently open then dont analyse it because its tracked by the client
if (!$this->codebase->file_provider->isOpen($file_path)) {
$this->server->queueFileAnalysis($file_path, $change->uri);
}
}
}
}

0 comments on commit 79fbaa2

Please sign in to comment.