Skip to content

Commit

Permalink
Fix diagnostics not recomputed on sourcemap/luaurc change
Browse files Browse the repository at this point in the history
Mainly affects clients using push diagnostics (sublime text)
  • Loading branch information
JohnnyMorganz committed Oct 28, 2022
1 parent a6f73dd commit 787e47f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fixed documentation comments of parent function being attached to a nested function
- Use location to determine which parameter is active in signature help
- Correctly handle highlighting variadic arguments in signature help
- [Sublime Text] Fixed push diagnostics not being recomputed when sourcemap or `.luaurc` changes

## [1.12.1] - 2022-10-18

Expand Down
80 changes: 48 additions & 32 deletions src/LanguageServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,36 +378,8 @@ void LanguageServer::onInitialized(const lsp::InitializedParams& params)
// Update the workspace setup with the new configuration
workspace->setupWithConfiguration(config);

// Handle diagnostics if in push-mode
if ((!client->capabilities.textDocument || !client->capabilities.textDocument->diagnostic))
{
// Recompute workspace diagnostics if requested
if (config.diagnostics.workspace)
{
auto diagnostics = workspace->workspaceDiagnostics({});
for (const auto& report : diagnostics.items)
{
if (report.kind == lsp::DocumentDiagnosticReportKind::Full)
{
client->publishDiagnostics(lsp::PublishDiagnosticsParams{report.uri, report.version, report.items});
}
}
}
// Recompute diagnostics for all currently opened files
else
{
for (const auto& [file, document] : workspace->fileResolver.managedFiles)
{
auto filePath = workspace->fileResolver.resolveToRealPath(file);
if (filePath)
{

auto uri = Uri::file(*filePath);
this->pushDiagnostics(workspace, uri, document.version());
}
}
}
}
// Refresh diagnostics
this->recomputeDiagnostics(workspace, config);

// Refresh inlay hint if changed
if (!oldConfig || oldConfig->inlayHints != config.inlayHints)
Expand Down Expand Up @@ -467,6 +439,46 @@ void LanguageServer::pushDiagnostics(WorkspaceFolderPtr& workspace, const lsp::D
}
}

/// Recompute all necessary diagnostics when we detect a configuration (or sourcemap) change
void LanguageServer::recomputeDiagnostics(WorkspaceFolderPtr& workspace, const ClientConfiguration& config)
{
// Handle diagnostics if in push-mode
if ((!client->capabilities.textDocument || !client->capabilities.textDocument->diagnostic))
{
// Recompute workspace diagnostics if requested
if (config.diagnostics.workspace)
{
auto diagnostics = workspace->workspaceDiagnostics({});
for (const auto& report : diagnostics.items)
{
if (report.kind == lsp::DocumentDiagnosticReportKind::Full)
{
client->publishDiagnostics(lsp::PublishDiagnosticsParams{report.uri, report.version, report.items});
}
}
}
// Recompute diagnostics for all currently opened files
else
{
for (const auto& [file, document] : workspace->fileResolver.managedFiles)
{
auto filePath = workspace->fileResolver.resolveToRealPath(file);
if (filePath)
{

auto uri = Uri::file(*filePath);
this->pushDiagnostics(workspace, uri, document.version());
}
}
}
}
else
{
client->terminateWorkspaceDiagnostics();
client->refreshWorkspaceDiagnostics();
}
}

void LanguageServer::onDidOpenTextDocument(const lsp::DidOpenTextDocumentParams& params)
{
// Start managing the file in-memory
Expand Down Expand Up @@ -619,21 +631,25 @@ void LanguageServer::onDidChangeWatchedFiles(const lsp::DidChangeWatchedFilesPar
for (const auto& change : params.changes)
{
auto workspace = findWorkspace(change.uri);
auto config = client->getConfiguration(workspace->rootUri);
auto filePath = change.uri.fsPath();
// Flag sourcemap changes
if (filePath.filename() == "sourcemap.json")
{
client->sendLogMessage(lsp::MessageType::Info, "Registering sourcemap changed for workspace " + workspace->name);
workspace->updateSourceMap();

// Recompute diagnostics
this->recomputeDiagnostics(workspace, config);
}
else if (filePath.filename() == ".luaurc")
{
client->sendLogMessage(
lsp::MessageType::Info, "Acknowledge config changed for workspace " + workspace->name + ", clearing configuration cache");
workspace->fileResolver.clearConfigCache();

// Send client request to refresh diagnostics
client->refreshWorkspaceDiagnostics();
// Recompute diagnostics
this->recomputeDiagnostics(workspace, config);
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ bool WorkspaceFolder::updateSourceMap()
}
}

// Signal diagnostics refresh
client->terminateWorkspaceDiagnostics();
client->refreshWorkspaceDiagnostics();

return true;
}
else
Expand Down
1 change: 1 addition & 0 deletions src/include/LSP/LanguageServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class LanguageServer
void onInitialized(const lsp::InitializedParams& params);

void pushDiagnostics(WorkspaceFolderPtr& workspace, const lsp::DocumentUri& uri, const size_t version);
void recomputeDiagnostics(WorkspaceFolderPtr& workspace, const ClientConfiguration& config);

void onDidOpenTextDocument(const lsp::DidOpenTextDocumentParams& params);
void onDidChangeTextDocument(const lsp::DidChangeTextDocumentParams& params);
Expand Down

0 comments on commit 787e47f

Please sign in to comment.