Skip to content

Commit af12b9c

Browse files
committed
use getDoc from EvalState to retrieve docs generally.
1 parent 22d6cc4 commit af12b9c

File tree

7 files changed

+81
-143
lines changed

7 files changed

+81
-143
lines changed

nixd/include/nixd/Eval/AttrSetProvider.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ class AttrSetProvider : public lspserver::LSPServer {
6666
/// FIXME: suppport list names. i.e. `foo.*.submodule`
6767
void onOptionComplete(const AttrPathCompleteParams &Params,
6868
lspserver::Callback<OptionCompleteResponse> Reply);
69-
70-
/// \brief Provide information on builtin values.
71-
void onBuiltinInfo(const AttrPathInfoParams &AttrPath,
72-
lspserver::Callback<BuiltinInfoResponse> Reply);
73-
74-
/// \brief Complete builtin values.
75-
void onBuiltinComplete(const AttrPathCompleteParams &Params,
76-
lspserver::Callback<BuiltinCompleteResponse> Reply);
7769
};
7870

7971
} // namespace nixd

nixd/include/nixd/Protocol/AttrSet.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ constexpr inline std::string_view AttrPathComplete = "attrset/attrpathComplete";
3333
constexpr inline std::string_view OptionInfo = "attrset/optionInfo";
3434
constexpr inline std::string_view OptionComplete = "attrset/optionComplete";
3535

36-
/// \brief ↩︎ Request information
37-
constexpr inline std::string_view BuiltinInfo = "attrset/builtinInfo";
38-
39-
/// \brief ↩︎ Request completion of builtins.
40-
constexpr inline std::string_view BuiltinComplete = "attrset/builtinComplete";
41-
4236
constexpr inline std::string_view Exit = "exit";
4337

4438
} // namespace rpcMethod
@@ -86,12 +80,24 @@ llvm::json::Value toJSON(const ValueMeta &Params);
8680
bool fromJSON(const llvm::json::Value &Params, ValueMeta &R,
8781
llvm::json::Path P);
8882

83+
/// \brief Using nix's ":doc" method to retrive value's additional information.
84+
struct ValueDescription {
85+
std::string Doc;
86+
std::int64_t Arity;
87+
};
88+
89+
llvm::json::Value toJSON(const ValueDescription &Params);
90+
bool fromJSON(const llvm::json::Value &Params, ValueDescription &R,
91+
llvm::json::Path P);
92+
8993
struct AttrPathInfoResponse {
9094
/// \brief General value description
9195
ValueMeta Meta;
9296

9397
/// \brief Package description of the attribute path, if available.
9498
PackageDescription PackageDesc;
99+
100+
std::optional<ValueDescription> ValueDesc;
95101
};
96102

97103
llvm::json::Value toJSON(const AttrPathInfoResponse &Params);
@@ -144,16 +150,4 @@ using OptionInfoResponse = OptionDescription;
144150

145151
using OptionCompleteResponse = std::vector<OptionField>;
146152

147-
struct BuiltinDescription {
148-
std::string Doc;
149-
std::int64_t Arity;
150-
};
151-
152-
llvm::json::Value toJSON(const BuiltinDescription &Params);
153-
bool fromJSON(const llvm::json::Value &Params, BuiltinDescription &R,
154-
llvm::json::Path P);
155-
156-
using BuiltinInfoResponse = BuiltinDescription;
157-
using BuiltinCompleteResponse = std::vector<std::string>;
158-
159153
} // namespace nixd

nixd/lib/Eval/AttrSetProvider.cpp

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ std::vector<std::string> completeNames(nix::Value &Scope,
184184
return Names;
185185
}
186186

187+
std::optional<ValueDescription> describeValue(nix::EvalState &State,
188+
nix::Value &V) {
189+
const auto Doc = State.getDoc(V);
190+
if (!Doc) {
191+
return std::nullopt;
192+
} else {
193+
return ValueDescription{
194+
.Doc = Doc->doc,
195+
.Arity = static_cast<int64_t>(Doc->arity),
196+
};
197+
}
198+
}
199+
187200
} // namespace
188201

189202
AttrSetProvider::AttrSetProvider(std::unique_ptr<InboundPort> In,
@@ -200,10 +213,6 @@ AttrSetProvider::AttrSetProvider(std::unique_ptr<InboundPort> In,
200213
&AttrSetProvider::onOptionInfo);
201214
Registry.addMethod(rpcMethod::OptionComplete, this,
202215
&AttrSetProvider::onOptionComplete);
203-
Registry.addMethod(rpcMethod::BuiltinInfo, this,
204-
&AttrSetProvider::onBuiltinInfo);
205-
Registry.addMethod(rpcMethod::BuiltinComplete, this,
206-
&AttrSetProvider::onBuiltinComplete);
207216
}
208217

209218
void AttrSetProvider::onEvalExpr(
@@ -234,9 +243,11 @@ void AttrSetProvider::onAttrPathInfo(
234243

235244
nix::Value &V = nixt::selectStrings(state(), Nixpkgs, AttrPath);
236245
state().forceValue(V, nix::noPos);
246+
237247
return RespT{
238248
.Meta = metadataOf(state(), V),
239249
.PackageDesc = describePackage(state(), V),
250+
.ValueDesc = describeValue(state(), V),
240251
};
241252
} catch (const nix::BaseError &Err) {
242253
return error(Err.info().msg.str());
@@ -349,33 +360,3 @@ void AttrSetProvider::onOptionComplete(
349360
return;
350361
}
351362
}
352-
void nixd::AttrSetProvider::onBuiltinInfo(
353-
const AttrPathInfoParams &AttrPath,
354-
lspserver::Callback<BuiltinDescription> Reply) {
355-
return Reply([&]() -> llvm::Expected<BuiltinDescription> {
356-
auto &Builtins = nixt::getBuiltins(state());
357-
if (AttrPath.empty())
358-
return error("attrpath is empty!");
359-
360-
auto &TheBuiltin = nixt::selectStrings(state(), Builtins, AttrPath);
361-
const auto &Doc = state().getDoc(TheBuiltin);
362-
if (!Doc) {
363-
return error("No documentation available");
364-
} else {
365-
return BuiltinDescription{
366-
.Doc = Doc->doc,
367-
.Arity = static_cast<int64_t>(Doc->arity),
368-
};
369-
}
370-
}());
371-
}
372-
373-
void nixd::AttrSetProvider::onBuiltinComplete(
374-
const AttrPathCompleteParams &Params,
375-
lspserver::Callback<BuiltinCompleteResponse> Reply) {
376-
return Reply([&]() -> llvm::Expected<BuiltinCompleteResponse> {
377-
auto &Builtins = nixt::getBuiltins(state());
378-
auto &Scope = nixt::selectStrings(state(), Builtins, Params.Scope);
379-
return completeNames(Scope, state(), Params.Prefix);
380-
}());
381-
}

nixd/lib/Protocol/AttrSet.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Value nixd::toJSON(const AttrPathInfoResponse &Params) {
9797
return Object{
9898
{"Meta", Params.Meta},
9999
{"PackageDesc", Params.PackageDesc},
100+
{"ValueDesc", Params.ValueDesc},
100101
};
101102
}
102103

@@ -106,6 +107,7 @@ bool nixd::fromJSON(const llvm::json::Value &Params, AttrPathInfoResponse &R,
106107
return O //
107108
&& O.map("Meta", R.Meta) //
108109
&& O.mapOptional("PackageDesc", R.PackageDesc) //
110+
&& O.mapOptional("ValueDesc", R.ValueDesc) //
109111
;
110112
}
111113

@@ -121,13 +123,13 @@ bool nixd::fromJSON(const llvm::json::Value &Params, AttrPathCompleteParams &R,
121123
;
122124
}
123125

124-
llvm::json::Value nixd::toJSON(const BuiltinDescription &Params) {
126+
llvm::json::Value nixd::toJSON(const ValueDescription &Params) {
125127
return Object{
126128
{"arity", Params.Arity},
127129
{"doc", Params.Doc},
128130
};
129131
}
130-
bool nixd::fromJSON(const llvm::json::Value &Params, BuiltinDescription &R,
132+
bool nixd::fromJSON(const llvm::json::Value &Params, ValueDescription &R,
131133
llvm::json::Path P) {
132134

133135
ObjectMapper O(Params, P);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# RUN: nixd-attrset-eval --lit-test < %s | FileCheck %s
2+
3+
4+
```json
5+
{
6+
"jsonrpc":"2.0",
7+
"id":0,
8+
"method":"attrset/evalExpr",
9+
"params": "{ hello = /** some markdown docs */x: y: x; }"
10+
}
11+
```
12+
13+
14+
```json
15+
{
16+
"jsonrpc":"2.0",
17+
"id":1,
18+
"method":"attrset/attrpathInfo",
19+
"params": [ "hello" ]
20+
}
21+
```
22+
23+
```
24+
CHECK: "id": 1,
25+
CHECK-NEXT: "jsonrpc": "2.0",
26+
CHECK-NEXT: "result": {
27+
CHECK-NEXT: "Meta": {
28+
CHECK-NEXT: "Location": null,
29+
CHECK-NEXT: "Type": 9
30+
CHECK-NEXT: },
31+
CHECK-NEXT: "PackageDesc": {
32+
CHECK-NEXT: "Description": null,
33+
CHECK-NEXT: "Homepage": null,
34+
CHECK-NEXT: "LongDescription": null,
35+
CHECK-NEXT: "Name": null,
36+
CHECK-NEXT: "PName": null,
37+
CHECK-NEXT: "Position": null,
38+
CHECK-NEXT: "Version": null
39+
CHECK-NEXT: },
40+
CHECK-NEXT: "ValueDesc": {
41+
CHECK-NEXT: "arity": 0,
42+
CHECK-NEXT: "doc": "Function `hello`\\\n … defined at «string»:1:36\n\nsome markdown docs \n"
43+
CHECK-NEXT: }
44+
CHECK-NEXT: }
45+
```
46+
47+
```json
48+
{"jsonrpc":"2.0","method":"exit"}
49+
```
50+

nixd/tools/nixd-attrset-eval/test/builtin-completion.md

Lines changed: 0 additions & 56 deletions
This file was deleted.

nixd/tools/nixd-attrset-eval/test/builtin-info.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)