Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixd: suppress AST is null error (VSCode pops up) #531

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions nixd/include/nixd/Controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,11 @@ class Controller : public lspserver::LSPServer {
llvm::StringMap<std::shared_ptr<NixTU>> TUs;

template <class T>
std::shared_ptr<NixTU> getTU(std::string File, lspserver::Callback<T> &Reply,
bool Ignore = true) {
std::shared_ptr<NixTU> getTU(std::string File,
lspserver::Callback<T> &Reply) {
using lspserver::error;
std::lock_guard G(TUsLock);
if (!TUs.count(File)) [[unlikely]] {
if (!Ignore) {
Reply(error("cannot find corresponding AST on file {0}", File));
return nullptr;
}
Reply(T{}); // Reply a default constructed response.
lspserver::elog("cannot get translation unit: {0}", File);
return nullptr;
Expand All @@ -131,7 +127,8 @@ class Controller : public lspserver::LSPServer {
lspserver::Callback<T> &Reply) {
using lspserver::error;
if (!TU.ast()) {
Reply(error("AST is null on this unit"));
Reply(T{});
lspserver::elog("AST is null on this unit");
return nullptr;
}
return TU.ast();
Expand Down
2 changes: 1 addition & 1 deletion nixd/lib/Controller/CodeAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void Controller::onCodeAction(const lspserver::CodeActionParams &Params,
std::string File(Params.textDocument.uri.file());
Range Range = Params.range;
auto Action = [Reply = std::move(Reply), File, Range, this]() mutable {
if (auto TU = getTU(File, Reply, /*Ignore=*/true)) {
if (auto TU = getTU(File, Reply)) {
std::vector<nixf::Diagnostic> Diagnostics = TU->diagnostics();
std::vector<CodeAction> Actions;
Actions.reserve(Diagnostics.size());
Expand Down
3 changes: 1 addition & 2 deletions nixd/lib/Controller/DocumentSymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ void Controller::onDocumentSymbol(const DocumentSymbolParams &Params,
Callback<std::vector<DocumentSymbol>> Reply) {
auto Action = [Reply = std::move(Reply), URI = Params.textDocument.uri,
this]() mutable {
if (std::shared_ptr<NixTU> TU =
getTU(URI.file().str(), Reply, /*Ignore=*/true)) {
if (std::shared_ptr<NixTU> TU = getTU(URI.file().str(), Reply)) {
if (std::shared_ptr<Node> AST = getAST(*TU, Reply)) {
std::vector<DocumentSymbol> Symbols;
collect(AST.get(), Symbols, *TU->variableLookup());
Expand Down
3 changes: 1 addition & 2 deletions nixd/lib/Controller/SemanticTokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ void Controller::onSemanticTokens(const SemanticTokensParams &Params,
Callback<SemanticTokens> Reply) {
auto Action = [Reply = std::move(Reply), URI = Params.textDocument.uri,
this]() mutable {
if (std::shared_ptr<NixTU> TU =
getTU(URI.file().str(), Reply, /*Ignore=*/true)) {
if (std::shared_ptr<NixTU> TU = getTU(URI.file().str(), Reply)) {
if (std::shared_ptr<Node> AST = getAST(*TU, Reply)) {
SemanticTokenBuilder Builder(*TU->variableLookup());
Builder.dfs(AST.get());
Expand Down
Loading