Skip to content
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
16 changes: 16 additions & 0 deletions nixd/lib/Controller/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,19 @@ nixd::FindAttrPathResult nixd::findAttrPath(const nixf::Node &N,

return R::NotAttrPath;
}

nixd::FindAttrPathResult
nixd::findAttrPathForOptions(const nixf::Node &N,
const nixf::ParentMapAnalysis &PM,
std::vector<std::string> &Path) {

const auto R = findAttrPath(N, PM, Path);
if (R == FindAttrPathResult::OK) {
// FIXME: Only do this in NixOS module files and in flakes only in module
// functions passed to nixpkgs.lib.nixosSystem.
if (!Path.empty() && Path[0] == "config") {
Path.erase(Path.begin());
}
}
return R;
}
8 changes: 8 additions & 0 deletions nixd/lib/Controller/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,12 @@ FindAttrPathResult findAttrPath(const nixf::Node &N,
const nixf::ParentMapAnalysis &PM,
std::vector<std::string> &Path);

/// \brief Heuristically find attrpath suitable for "attrpath" completion.
/// Strips "config." from the start to support config sections in NixOS modules.
///
/// \param[out] Path the attrpath.
FindAttrPathResult findAttrPathForOptions(const nixf::Node &N,
const nixf::ParentMapAnalysis &PM,
std::vector<std::string> &Path);

} // namespace nixd
2 changes: 1 addition & 1 deletion nixd/lib/Controller/Completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void completeAttrPath(const Node &N, const ParentMapAnalysis &PM,
std::vector<lspserver::CompletionItem> &Items) {
std::vector<std::string> Scope;
using PathResult = FindAttrPathResult;
auto R = findAttrPath(N, PM, Scope);
auto R = findAttrPathForOptions(N, PM, Scope);
if (R == PathResult::OK) {
// Construct request.
std::string Prefix = Scope.back();
Expand Down
2 changes: 1 addition & 1 deletion nixd/lib/Controller/Definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Locations defineAttrPath(const Node &N, const ParentMapAnalysis &PM,
Controller::OptionMapTy &Options) {
using PathResult = FindAttrPathResult;
std::vector<std::string> Scope;
auto R = findAttrPath(N, PM, Scope);
auto R = findAttrPathForOptions(N, PM, Scope);
Locations Locs;
if (R == PathResult::OK) {
std::lock_guard _(OptionsLock);
Expand Down
2 changes: 1 addition & 1 deletion nixd/lib/Controller/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void Controller::onHover(const TextDocumentPositionParams &Params,
}

auto Scope = std::vector<std::string>();
const auto R = findAttrPath(N, PM, Scope);
const auto R = findAttrPathForOptions(N, PM, Scope);
if (R == FindAttrPathResult::OK) {
std::lock_guard _(OptionsLock);
for (const auto &[_, Client] : Options) {
Expand Down
81 changes: 81 additions & 0 deletions nixd/tools/nixd/test/completion/options-in-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# RUN: nixd --lit-test \
# RUN: --nixos-options-expr="{ foo.bar = { _type = \"option\"; }; }" \
# RUN: < %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:///completion.nix",
"languageId":"nix",
"version":1,
"text":"{ config = { fo }; }"
}
}
}
```

```json
{
"jsonrpc": "2.0",
"id": 1,
"method": "textDocument/completion",
"params": {
"textDocument": {
"uri": "file:///completion.nix"
},
"position": {
"line": 0,
"character": 15
},
"context": {
"triggerKind": 1
}
}
}
```

```
CHECK: "id": 1,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": {
CHECK-NEXT: "isIncomplete": false,
CHECK-NEXT: "items": [
CHECK-NEXT: {
CHECK-NEXT: "data": "",
CHECK-NEXT: "detail": "nixos",
CHECK-NEXT: "kind": 7,
CHECK-NEXT: "label": "foo",
CHECK-NEXT: "score": 0
CHECK-NEXT: }
CHECK-NEXT: ]
CHECK-NEXT: }
```


```json
{"jsonrpc":"2.0","method":"exit"}
```
82 changes: 82 additions & 0 deletions nixd/tools/nixd/test/hover/options-in-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# RUN: nixd --lit-test \
# RUN: --nixos-options-expr="{ foo = { _type = \"option\"; description = \"test option\"; type.description = \"test type\"; }; }" \
# RUN: < %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:///test.nix",
"languageId":"nix",
"version":1,
"text":"{ config = { foo }; }"
}
}
}
```

```json
{
"jsonrpc": "2.0",
"id": 2,
"method": "textDocument/hover",
"params":{
"textDocument":{
"uri": "file:///test.nix"
},
"position":{
"line": 0,
"character": 15
}
}
}
```

```
CHECK: "id": 2,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": {
CHECK-NEXT: "contents": {
CHECK-NEXT: "kind": "markdown",
CHECK-NEXT: "value": " (test type)\n\ntest option"
CHECK-NEXT: },
CHECK-NEXT: "range": {
CHECK-NEXT: "end": {
CHECK-NEXT: "character": 16,
CHECK-NEXT: "line": 0
CHECK-NEXT: },
CHECK-NEXT: "start": {
CHECK-NEXT: "character": 13,
CHECK-NEXT: "line": 0
CHECK-NEXT: }
CHECK-NEXT: }
CHECK-NEXT: }
```


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