diff --git a/nixd/lib/Controller/Definition.cpp b/nixd/lib/Controller/Definition.cpp index 9d54f8668..937bc3a79 100644 --- a/nixd/lib/Controller/Definition.cpp +++ b/nixd/lib/Controller/Definition.cpp @@ -355,43 +355,40 @@ const Definition &nixd::findDefinition(const Node &N, return findVarDefinition(*Var, VLA); } -#define Check(x) CheckReturn(x, nullptr) void Controller::onDefinition(const TextDocumentPositionParams &Params, Callback Reply) { + using CheckTy = Locations; auto Action = [Reply = std::move(Reply), URI = Params.textDocument.uri, Pos = toNixfPosition(Params.position), this]() mutable { const auto File = URI.file().str(); - return Reply([&]() -> llvm::Expected { - const auto TU = Check(getTU(File)); - const auto AST = Check(getAST(*TU)); + return Reply(squash([&]() -> llvm::Expected { + const auto TU = CheckDefault(getTU(File)); + const auto AST = CheckDefault(getAST(*TU)); const auto &VLA = *TU->variableLookup(); const auto &PM = *TU->parentMap(); - const auto &N = *Check(AST->descend({Pos, Pos})); - const auto &UpExpr = *Check(PM.upExpr(N)); - - return squash([&]() -> llvm::Expected { - // Special case for inherited names. - if (const ExprVar *Var = findInheritVar(N, PM, VLA)) - return defineVar(*Var, VLA, PM, *nixpkgsClient(), URI, TU->src()); - - switch (UpExpr.kind()) { - case Node::NK_ExprVar: { - const auto &Var = static_cast(UpExpr); - return defineVar(Var, VLA, PM, *nixpkgsClient(), URI, TU->src()); - } - case Node::NK_ExprSelect: { - const auto &Sel = static_cast(UpExpr); - return defineSelect(Sel, VLA, PM, *nixpkgsClient()); - } - case Node::NK_ExprAttrs: - return defineAttrPath(N, PM, OptionsLock, Options); - default: - break; - } - return error("unknown node type for definition"); - }()); - }()); + const auto &N = *CheckDefault(AST->descend({Pos, Pos})); + const auto &UpExpr = *CheckDefault(PM.upExpr(N)); + + // Special case for inherited names. + if (const ExprVar *Var = findInheritVar(N, PM, VLA)) + return defineVar(*Var, VLA, PM, *nixpkgsClient(), URI, TU->src()); + + switch (UpExpr.kind()) { + case Node::NK_ExprVar: { + const auto &Var = static_cast(UpExpr); + return defineVar(Var, VLA, PM, *nixpkgsClient(), URI, TU->src()); + } + case Node::NK_ExprSelect: { + const auto &Sel = static_cast(UpExpr); + return defineSelect(Sel, VLA, PM, *nixpkgsClient()); + } + case Node::NK_ExprAttrs: + return defineAttrPath(N, PM, OptionsLock, Options); + default: + break; + } + return error("unknown node type for definition"); + }())); }; boost::asio::post(Pool, std::move(Action)); } -#undef Check