From 79fbaa225244bdd59c3dfdd7799e9d56df66c5e3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 28 Jun 2021 17:34:43 -0700 Subject: [PATCH 1/2] Fixes #6013, implement workspace/didChangeWatchedFiles --- .../LanguageServer/LanguageServer.php | 16 ++++ .../LanguageServer/Server/Workspace.php | 73 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 src/Psalm/Internal/LanguageServer/Server/Workspace.php diff --git a/src/Psalm/Internal/LanguageServer/LanguageServer.php b/src/Psalm/Internal/LanguageServer/LanguageServer.php index ebc0e03c0..4137dde7a 100644 --- a/src/Psalm/Internal/LanguageServer/LanguageServer.php +++ b/src/Psalm/Internal/LanguageServer/LanguageServer.php @@ -19,6 +19,7 @@ use LanguageServerProtocol\TextDocumentSyncOptions; 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; @@ -51,6 +52,13 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher */ public $textDocument; + /** + * Handles workspace/* method calls + * + * @var ?Server\Workspace + */ + public $workspace; + /** * @var ProtocolReader */ @@ -221,6 +229,14 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher ); } + if ($this->workspace === null) { + $this->workspace = new Workspace( + $this, + $codebase, + $this->project_analyzer->onchange_line_limit + ); + } + $serverCapabilities = new ServerCapabilities(); $textDocumentSyncOptions = new TextDocumentSyncOptions(); diff --git a/src/Psalm/Internal/LanguageServer/Server/Workspace.php b/src/Psalm/Internal/LanguageServer/Server/Workspace.php new file mode 100644 index 000000000..796cb9f6d --- /dev/null +++ b/src/Psalm/Internal/LanguageServer/Server/Workspace.php @@ -0,0 +1,73 @@ +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); + } + } + } +} From 468e474f449c828abc6337615166db7dd524f33b Mon Sep 17 00:00:00 2001 From: Andrew Nagy Date: Mon, 28 Jun 2021 19:08:02 -0700 Subject: [PATCH 2/2] Update src/Psalm/Internal/LanguageServer/Server/Workspace.php Co-authored-by: Bruce Weirdan --- src/Psalm/Internal/LanguageServer/Server/Workspace.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Internal/LanguageServer/Server/Workspace.php b/src/Psalm/Internal/LanguageServer/Server/Workspace.php index 796cb9f6d..b44ebe499 100644 --- a/src/Psalm/Internal/LanguageServer/Server/Workspace.php +++ b/src/Psalm/Internal/LanguageServer/Server/Workspace.php @@ -8,7 +8,7 @@ use Psalm\Codebase; use Psalm\Internal\LanguageServer\LanguageServer; /** - * Provides method handlers for all textDocument/* methods + * Provides method handlers for all workspace/* methods */ class Workspace {