Skip to content

Commit

Permalink
Show diagnostics for definitions files which failed to load
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Jun 24, 2022
1 parent 964b432 commit 6879c31
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added

- Added `luau-lsp.sourcemap.enabled` option which dictates whether sourcemap-related features are enabled
- Diagnostics will now be provided for definitions files which errored

### Changed

- Moved definitions file loading to post-initialization

## [1.5.2] - 2022-06-22

Expand Down
5 changes: 5 additions & 0 deletions src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ void Client::applyEdit(const lsp::ApplyWorkspaceEditParams& params, std::optiona
sendRequest(nextRequestId++, "workspace/applyEdit", params, handler);
}

void Client::publishDiagnostics(const lsp::PublishDiagnosticsParams& params)
{
sendNotification("textDocument/publishDiagnostics", params);
}

void Client::refreshWorkspaceDiagnostics()
{
if (capabilities.workspace && capabilities.workspace->diagnostics && capabilities.workspace->diagnostics->refreshSupport)
Expand Down
16 changes: 6 additions & 10 deletions src/LanguageServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ void LanguageServer::onInitialized(const lsp::InitializedParams& params)
{
if (report.kind == lsp::DocumentDiagnosticReportKind::Full)
{
client->sendNotification(
"textDocument/publishDiagnostics", lsp::PublishDiagnosticsParams{report.uri, report.version, report.items});
client->publishDiagnostics(lsp::PublishDiagnosticsParams{report.uri, report.version, report.items});
}
}
}
Expand Down Expand Up @@ -373,16 +372,15 @@ void LanguageServer::pushDiagnostics(WorkspaceFolderPtr& workspace, const lsp::D
// Convert the diagnostics report into a series of diagnostics published for each relevant file
lsp::DocumentDiagnosticParams params{lsp::TextDocumentIdentifier{uri}};
auto diagnostics = workspace->documentDiagnostics(params);
client->sendNotification("textDocument/publishDiagnostics", lsp::PublishDiagnosticsParams{uri, version, diagnostics.items});
client->publishDiagnostics(lsp::PublishDiagnosticsParams{uri, version, diagnostics.items});

if (!diagnostics.relatedDocuments.empty())
{
for (const auto& [uri, diagnostics] : diagnostics.relatedDocuments)
{
if (diagnostics.kind == lsp::DocumentDiagnosticReportKind::Full)
{
client->sendNotification(
"textDocument/publishDiagnostics", lsp::PublishDiagnosticsParams{Uri::parse(uri), std::nullopt, diagnostics.items});
client->publishDiagnostics(lsp::PublishDiagnosticsParams{Uri::parse(uri), std::nullopt, diagnostics.items});
}
}
}
Expand Down Expand Up @@ -419,8 +417,7 @@ void LanguageServer::onDidChangeTextDocument(const lsp::DidChangeTextDocumentPar
{
// Convert the diagnostics report into a series of diagnostics published for each relevant file
auto diagnostics = workspace->documentDiagnostics(lsp::DocumentDiagnosticParams{params.textDocument});
client->sendNotification("textDocument/publishDiagnostics",
lsp::PublishDiagnosticsParams{params.textDocument.uri, params.textDocument.version, diagnostics.items});
client->publishDiagnostics(lsp::PublishDiagnosticsParams{params.textDocument.uri, params.textDocument.version, diagnostics.items});

// Compute diagnostics for reverse dependencies
// TODO: should we put this inside documentDiagnostics so it works in the pull based model as well? (its a reverse BFS which is expensive)
Expand Down Expand Up @@ -453,8 +450,7 @@ void LanguageServer::onDidChangeTextDocument(const lsp::DidChangeTextDocumentPar
{
if (diagnostics.kind == lsp::DocumentDiagnosticReportKind::Full)
{
client->sendNotification(
"textDocument/publishDiagnostics", lsp::PublishDiagnosticsParams{Uri::parse(uri), std::nullopt, diagnostics.items});
client->publishDiagnostics(lsp::PublishDiagnosticsParams{Uri::parse(uri), std::nullopt, diagnostics.items});
}
}
}
Expand All @@ -470,7 +466,7 @@ void LanguageServer::onDidCloseTextDocument(const lsp::DidCloseTextDocumentParam
// If this was an ignored file then lets clear the diagnostics for it
if (workspace->isIgnoredFile(params.textDocument.uri.fsPath()))
{
client->sendNotification("textDocument/publishDiagnostics", lsp::PublishDiagnosticsParams{params.textDocument.uri, std::nullopt, {}});
client->publishDiagnostics(lsp::PublishDiagnosticsParams{params.textDocument.uri, std::nullopt, {}});
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/LuauExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,18 @@ lsp::Diagnostic createLintDiagnostic(const Luau::LintWarning& lint)
return diagnostic;
}

lsp::Diagnostic createParseErrorDiagnostic(const Luau::ParseError& error)
{
lsp::Diagnostic diagnostic;
diagnostic.source = "Luau";
diagnostic.code = "SyntaxError";
diagnostic.message = "SyntaxError: " + error.getMessage();
diagnostic.severity = lsp::DiagnosticSeverity::Error;
diagnostic.range = {convertPosition(error.getLocation().begin), convertPosition(error.getLocation().end)};
diagnostic.codeDescription = {Uri::parse("https://luau-lang.org/syntax")};
return diagnostic;
}

struct FindSymbolReferences : public Luau::AstVisitor
{
const Luau::Symbol symbol;
Expand Down
24 changes: 21 additions & 3 deletions src/Workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,28 @@ void WorkspaceFolder::initialize()
auto result = types::registerDefinitions(frontend.typeChecker, definitionsFile);
types::registerDefinitions(frontend.typeCheckerForAutocomplete, definitionsFile);

if (!result.success)
auto uri = Uri::file(definitionsFile);

if (result.success)
{
// Clear any set diagnostics
client->publishDiagnostics({uri, std::nullopt, {}});
}
else
{
client->sendWindowMessage(lsp::MessageType::Error, "Failed to read definitions file. Extended types will not be provided");
// TODO: Display diagnostics? We can't right now since this is currently called during initialisation, need to move
client->sendWindowMessage(lsp::MessageType::Error,
"Failed to read definitions file " + definitionsFile.generic_string() + ". Extended types will not be provided");

// Display relevant diagnostics
std::vector<lsp::Diagnostic> diagnostics;
for (auto& error : result.parseResult.errors)
diagnostics.emplace_back(createParseErrorDiagnostic(error));

if (result.module)
for (auto& error : result.module->errors)
diagnostics.emplace_back(createTypeErrorDiagnostic(error));

client->publishDiagnostics({uri, std::nullopt, diagnostics});
}
}
Luau::freeze(frontend.typeChecker.globalTypes);
Expand Down
1 change: 1 addition & 0 deletions src/include/LSP/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Client
// TODO: this function only supports getting requests for workspaces
void requestConfiguration(const std::vector<lsp::DocumentUri>& uris);
void applyEdit(const lsp::ApplyWorkspaceEditParams& params, std::optional<ResponseHandler> handler = std::nullopt);
void publishDiagnostics(const lsp::PublishDiagnosticsParams& params);
void refreshWorkspaceDiagnostics();

void setTrace(const lsp::SetTraceParams& params);
Expand Down
3 changes: 2 additions & 1 deletion src/include/LSP/LuauExt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ Luau::Position convertPosition(const lsp::Position& position);
lsp::Position convertPosition(const Luau::Position& position);

lsp::Diagnostic createTypeErrorDiagnostic(const Luau::TypeError& error);
lsp::Diagnostic createLintDiagnostic(const Luau::LintWarning& lint);
lsp::Diagnostic createLintDiagnostic(const Luau::LintWarning& lint);
lsp::Diagnostic createParseErrorDiagnostic(const Luau::ParseError& error);

0 comments on commit 6879c31

Please sign in to comment.