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/Controller: report error on textDocument/definition on builtins #581

Merged
merged 2 commits into from
Aug 11, 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
6 changes: 6 additions & 0 deletions nixd/lib/Controller/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions nixd/lib/Controller/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -292,9 +295,10 @@ std::vector<T> mergeVec(std::vector<T> A, const std::vector<T> &B) {
return A;
}

Locations defineVar(const ExprVar &Var, const VariableLookupAnalysis &VLA,
const ParentMapAnalysis &PM, AttrSetClient &NixpkgsClient,
const URIForFile &URI, llvm::StringRef Src) {
llvm::Expected<Locations>
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);

Expand All @@ -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.
Expand Down
67 changes: 67 additions & 0 deletions nixd/tools/nixd/test/definition/builtin.md
Original file line number Diff line number Diff line change
@@ -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"}
```
Loading