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: goto definition for derivations #113

Merged
merged 2 commits into from
Jun 13, 2023
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
2 changes: 2 additions & 0 deletions lib/nixd/include/nixd/nix/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace nix::nixd {

bool isOption(EvalState &State, Value &V);

bool isDerivation(EvalState &State, Value &V);

std::optional<std::string> attrPathStr(nix::EvalState &State, nix::Value &V,
const std::string &AttrPath) noexcept;

Expand Down
23 changes: 23 additions & 0 deletions lib/nixd/src/ServerWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,29 @@ void Server::onEvalDefinition(
auto State = IER->Session->getState();

auto *Node = AST->lookupPosition(Params.position);

// If the expression evaluates to a "derivation", try to bring our user
// to the location which defines the package.
try {
auto V = AST->getValue(Node);
if (nix::nixd::isDerivation(*State, V)) {
if (auto S = nix::nixd::attrPathStr(*State, V, "meta.position")) {
llvm::StringRef PositionStr = S.value();
auto [Path, LineStr] = PositionStr.split(':');
int Line;
if (LLVM_LIKELY(!LineStr.getAsInteger(10, Line))) {
lspserver::Position Position{.line = Line, .character = 0};
RR.Response = Location{URIForFile::canonicalize(Path, Path),
{Position, Position}};
return;
}
}
}
} catch (...) {
lspserver::vlog("no associated value on this expression");
}

// Otherwise, we try to find the location binds to the variable.
if (const auto *EVar = dynamic_cast<const nix::ExprVar *>(Node)) {
if (EVar->fromWith)
return;
Expand Down
12 changes: 12 additions & 0 deletions lib/nixd/src/nix/Value.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "nixd/nix/Value.h"

#include <nix/eval.hh>

namespace nix::nixd {

bool isOption(EvalState &State, Value &V) {
Expand All @@ -12,6 +14,16 @@ bool isOption(EvalState &State, Value &V) {
return S && S.value() == "option";
};

bool isDerivation(EvalState &State, Value &V) {
State.forceValue(V, noPos);
if (V.type() != ValueType::nAttrs)
return false;

// Derivations has a special attribute "type" == "derivation"
auto S = attrPathStr(State, V, "type");
return S && S.value() == "derivation";
}

std::optional<std::string> attrPathStr(nix::EvalState &State, nix::Value &V,
const std::string &AttrPath) noexcept {
try {
Expand Down
91 changes: 91 additions & 0 deletions tools/nixd/test/goto-definition-derivation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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

Testing this nix file, a dummy derivation.

```nix
let
foo = {
type = "derivation";
meta.position = "/foo.nix:4";
};
in
foo
#^ expected location: foo.nix:4
```

```json
{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file:///derivation.nix",
"languageId": "nix",
"version": 1,
"text": "let\r\n foo = {\r\n type = \"derivation\";\r\n meta.position = \"\/foo.nix:4\";\r\n };\r\nin\r\nfoo"
}
}
}
```

<-- textDocument/definition

```json
{
"jsonrpc":"2.0",
"id":1,
"method":"textDocument/definition",
"params":{
"textDocument":{
"uri":"file:///derivation.nix"
},
"position":{
"line":6,
"character":0
}
}
}
```


```
CHECK: "id": 1,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": {
CHECK-NEXT: "range": {
CHECK-NEXT: "end": {
CHECK-NEXT: "character": 0,
CHECK-NEXT: "line": 4
CHECK-NEXT: },
CHECK-NEXT: "start": {
CHECK-NEXT: "character": 0,
CHECK-NEXT: "line": 4
CHECK-NEXT: }
CHECK-NEXT: },
CHECK-NEXT: "uri": "file:///foo.nix"
CHECK-NEXT: }
```

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