Skip to content

Commit

Permalink
nixd: support goto def for inherit-ed attrs (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Apr 9, 2024
1 parent c8840dd commit 7ac5fae
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
48 changes: 47 additions & 1 deletion nixd/lib/Controller/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "Convert.h"

#include "nixd/Controller/Controller.h"
#include "nixf/Basic/Nodes/Attrs.h"
#include "nixf/Basic/Nodes/Basic.h"
#include "nixf/Sema/VariableLookup.h"

#include <llvm/Support/Error.h>
Expand Down Expand Up @@ -67,12 +69,56 @@ const Definition *findSelfDefinition(const Node &N,
return nullptr;
}

// Special case, variable in "inherit"
// inherit name
// ^~~~<--- this is an "AttrName", not variable.
const ExprVar *findInheritVar(const Node &N, const ParentMapAnalysis &PMA,
const VariableLookupAnalysis &VLA) {
if (const Node *Up = PMA.upTo(N, Node::NK_Inherit)) {
const Node *UpAn = PMA.upTo(N, Node::NK_AttrName);
if (!UpAn)
return nullptr;
const auto &Inh = static_cast<const Inherit &>(*Up);
const auto &AN = static_cast<const AttrName &>(*UpAn);

// Skip:
//
// inherit (expr) name1 name2;
//
if (Inh.expr())
return nullptr;

// Skip dynamic.
if (!AN.isStatic())
return nullptr;

// This attrname will be desugared into an "ExprVar".
Up = PMA.upTo(Inh, Node::NK_ExprAttrs);
if (!Up)
return nullptr;

const SemaAttrs &SA = static_cast<const ExprAttrs &>(*Up).sema();
const Node *Var = SA.staticAttrs().at(AN.staticName()).value();
assert(Var->kind() == Node::NK_ExprVar);
return static_cast<const ExprVar *>(Var);
}
return nullptr;
}

const ExprVar *findVar(const Node &N, const ParentMapAnalysis &PMA,
const VariableLookupAnalysis &VLA) {
if (const ExprVar *InVar = findInheritVar(N, PMA, VLA))
return InVar;

return static_cast<const ExprVar *>(PMA.upTo(N, Node::NK_ExprVar));
}

} // namespace

Expected<const Definition &>
nixd::findDefinition(const Node &N, const ParentMapAnalysis &PMA,
const VariableLookupAnalysis &VLA) {
const Node *Var = PMA.upTo(N, Node::NK_ExprVar);
const ExprVar *Var = findVar(N, PMA, VLA);
if (!Var) [[unlikely]] {
if (const Definition *Def = findSelfDefinition(N, PMA, VLA))
return *Def;
Expand Down
77 changes: 77 additions & 0 deletions nixd/tools/nixd/test/definition-inherit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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":"let x = 1; in { inherit x; }"
}
}
}
```

<-- textDocument/definition(2)


```json
{
"jsonrpc":"2.0",
"id":2,
"method":"textDocument/definition",
"params":{
"textDocument":{
"uri":"file:///basic.nix"
},
"position":{
"line": 0,
"character":24
}
}
}
```

```
CHECK: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": {
CHECK-NEXT: "range": {
CHECK-NEXT: "end": {
CHECK-NEXT: "character": 5,
CHECK-NEXT: "line": 0
CHECK-NEXT: },
CHECK-NEXT: "start": {
CHECK-NEXT: "character": 4,
CHECK-NEXT: "line": 0
CHECK-NEXT: }
CHECK-NEXT: },
```


```json
{"jsonrpc":"2.0","method":"exit"}
```

0 comments on commit 7ac5fae

Please sign in to comment.