diff --git a/nixd/lib/Controller/AST.h b/nixd/lib/Controller/AST.h index 2c6a3152b..3536f8197 100644 --- a/nixd/lib/Controller/AST.h +++ b/nixd/lib/Controller/AST.h @@ -39,6 +39,12 @@ struct NotAnIdiomException : IdiomSelectorException { struct VLAException : std::exception {}; +struct NoLocationForBuiltinVariable : std::exception { + [[nodiscard]] const char *what() const noexcept override { + return "builtins are defined in the interpreter, not in the Nix files"; + } +}; + /// \brief No such variable. struct NoSuchVarException : VLAException { [[nodiscard]] const char *what() const noexcept override { diff --git a/nixd/lib/Controller/Definition.cpp b/nixd/lib/Controller/Definition.cpp index 0d7a6255b..95bbebea4 100644 --- a/nixd/lib/Controller/Definition.cpp +++ b/nixd/lib/Controller/Definition.cpp @@ -115,6 +115,9 @@ const Definition &findVarDefinition(const ExprVar &Var, /// \brief Convert nixf::Definition to lspserver::Location Location convertToLocation(llvm::StringRef Src, const Definition &Def, URIForFile URI) { + if (!Def.syntax()) + throw NoLocationForBuiltinVariable(); + assert(Def.syntax()); return Location{ .uri = std::move(URI), .range = toLSPRange(Src, Def.syntax()->range()), @@ -292,9 +295,10 @@ std::vector mergeVec(std::vector A, const std::vector &B) { return A; } -Locations defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA, - const ParentMapAnalysis &PM, AttrSetClient &NixpkgsClient, - const URIForFile &URI, llvm::StringRef Src) { +llvm::Expected +defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA, + const ParentMapAnalysis &PM, AttrSetClient &NixpkgsClient, + const URIForFile &URI, llvm::StringRef Src) { try { Locations StaticLocs = defineVarStatic(Var, VLA, URI, Src); @@ -309,8 +313,9 @@ Locations defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA, } } catch (std::exception &E) { elog("definition/static: {0}", E.what()); + return Locations{}; } - return {}; + return error("unreachable code! Please submit an issue"); } /// \brief Squash a vector into smaller json variant. diff --git a/nixd/tools/nixd/test/definition/builtin.md b/nixd/tools/nixd/test/definition/builtin.md new file mode 100644 index 000000000..cccd8ee04 --- /dev/null +++ b/nixd/tools/nixd/test/definition/builtin.md @@ -0,0 +1,67 @@ +# RUN: nixd --lit-test < %s | FileCheck %s + +<-- initialize(0) + +```json +{ + "jsonrpc":"2.0", + "id":0, + "method":"initialize", + "params":{ + "processId":123, + "rootPath":"", + "capabilities":{ + }, + "trace":"off" + } +} +``` + + +<-- textDocument/didOpen + +```json +{ + "jsonrpc":"2.0", + "method":"textDocument/didOpen", + "params":{ + "textDocument":{ + "uri":"file:///basic.nix", + "languageId":"nix", + "version":1, + "text":"builtins" + } + } +} +``` + +<-- textDocument/definition(2) + + +```json +{ + "jsonrpc":"2.0", + "id":2, + "method":"textDocument/definition", + "params":{ + "textDocument":{ + "uri":"file:///basic.nix" + }, + "position":{ + "line": 0, + "character":3 + } + } +} +``` + +``` + CHECK: "id": 2, +CHECK-NEXT: "jsonrpc": "2.0", +CHECK-NEXT: "result": null +``` + + +```json +{"jsonrpc":"2.0","method":"exit"} +```