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

Value doc #625

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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 libnixt/include/nixt/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@ selectStringViews(nix::EvalState &State, nix::Value &V,
return selectSymbols(State, V, toSymbols(State.symbols, AttrPath));
}

/// TODO: use https://github.com/NixOS/nix/pull/11914 on nix version bump
/// \brief Get nix's `builtins` constant
inline nix::Value &getBuiltins(const nix::EvalState &State) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe remove, not used in this PR

return *State.baseEnv.values[0];
}

} // namespace nixt
14 changes: 14 additions & 0 deletions nixd/include/nixd/Protocol/AttrSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#pragma once

#include <cstdint>
#include <optional>
#include <string>
#include <vector>
Expand Down Expand Up @@ -31,6 +32,7 @@ constexpr inline std::string_view AttrPathInfo = "attrset/attrpathInfo";
constexpr inline std::string_view AttrPathComplete = "attrset/attrpathComplete";
constexpr inline std::string_view OptionInfo = "attrset/optionInfo";
constexpr inline std::string_view OptionComplete = "attrset/optionComplete";

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty

constexpr inline std::string_view Exit = "exit";

} // namespace rpcMethod
Expand Down Expand Up @@ -78,12 +80,24 @@ llvm::json::Value toJSON(const ValueMeta &Params);
bool fromJSON(const llvm::json::Value &Params, ValueMeta &R,
llvm::json::Path P);

/// \brief Using nix's ":doc" method to retrive value's additional information.
struct ValueDescription {
std::string Doc;
std::int64_t Arity;
};

llvm::json::Value toJSON(const ValueDescription &Params);
bool fromJSON(const llvm::json::Value &Params, ValueDescription &R,
llvm::json::Path P);

struct AttrPathInfoResponse {
/// \brief General value description
ValueMeta Meta;

/// \brief Package description of the attribute path, if available.
PackageDescription PackageDesc;

std::optional<ValueDescription> ValueDesc;
};

llvm::json::Value toJSON(const AttrPathInfoResponse &Params);
Expand Down
68 changes: 43 additions & 25 deletions nixd/lib/Eval/AttrSetProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <nix/attr-path.hh>
#include <nix/common-eval-args.hh>
#include <nix/eval.hh>
#include <nix/nixexpr.hh>
#include <nix/store-api.hh>
#include <nixt/Value.h>
Expand Down Expand Up @@ -156,6 +157,43 @@ void fillOptionDescription(nix::EvalState &State, nix::Value &V,
}
}

std::vector<std::string> completeNames(nix::Value &Scope,
const nix::EvalState &State,
std::string_view Prefix) {
int Num = 0;
std::vector<std::string> Names;

// FIXME: we may want to use "Trie" to speedup the string searching.
// However as my (roughtly) profiling the critical in this loop is
// evaluating package details.
// "Trie"s may not beneficial because it cannot speedup eval.
for (const auto *AttrPtr : Scope.attrs()->lexicographicOrder(State.symbols)) {
const nix::Attr &Attr = *AttrPtr;
const std::string_view Name = State.symbols[Attr.name];
if (Name.starts_with(Prefix)) {
++Num;
Names.emplace_back(Name);
// We set this a very limited number as to speedup
if (Num > MaxItems)
break;
}
}
return Names;
}

std::optional<ValueDescription> describeValue(nix::EvalState &State,
nix::Value &V) {
const auto Doc = State.getDoc(V);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nix::EvalState::getDoc() is invoked here

if (!Doc) {
return std::nullopt;
} else {
return ValueDescription{
.Doc = Doc->doc,
.Arity = static_cast<int64_t>(Doc->arity),
};
}
}

} // namespace

AttrSetProvider::AttrSetProvider(std::unique_ptr<InboundPort> In,
Expand Down Expand Up @@ -202,9 +240,11 @@ void AttrSetProvider::onAttrPathInfo(

nix::Value &V = nixt::selectStrings(state(), Nixpkgs, AttrPath);
state().forceValue(V, nix::noPos);

return RespT{
.Meta = metadataOf(state(), V),
.PackageDesc = describePackage(state(), V),
.ValueDesc = describeValue(state(), V),
};
} catch (const nix::BaseError &Err) {
return error(Err.info().msg.str());
Expand All @@ -227,33 +267,11 @@ void AttrSetProvider::onAttrPathComplete(
return;
}

std::vector<std::string> Names;
int Num = 0;

// FIXME: we may want to use "Trie" to speedup the string searching.
// However as my (roughtly) profiling the critical in this loop is
// evaluating package details.
// "Trie"s may not beneficial becausae it cannot speedup eval.
for (const auto *AttrPtr :
Scope.attrs()->lexicographicOrder(state().symbols)) {
const nix::Attr &Attr = *AttrPtr;
const std::string_view Name = state().symbols[Attr.name];
if (Name.starts_with(Params.Prefix)) {
++Num;
Names.emplace_back(Name);
// We set this a very limited number as to speedup
if (Num > MaxItems)
break;
}
}
Reply(std::move(Names));
return;
return Reply(completeNames(Scope, state(), Params.Prefix));
} catch (const nix::BaseError &Err) {
Reply(error(Err.info().msg.str()));
return;
return Reply(error(Err.info().msg.str()));
} catch (const std::exception &Err) {
Reply(error(Err.what()));
return;
return Reply(error(Err.what()));
}
}

Expand Down
18 changes: 18 additions & 0 deletions nixd/lib/Protocol/AttrSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Value nixd::toJSON(const AttrPathInfoResponse &Params) {
return Object{
{"Meta", Params.Meta},
{"PackageDesc", Params.PackageDesc},
{"ValueDesc", Params.ValueDesc},
};
}

Expand All @@ -106,6 +107,7 @@ bool nixd::fromJSON(const llvm::json::Value &Params, AttrPathInfoResponse &R,
return O //
&& O.map("Meta", R.Meta) //
&& O.mapOptional("PackageDesc", R.PackageDesc) //
&& O.mapOptional("ValueDesc", R.ValueDesc) //
;
}

Expand All @@ -120,3 +122,19 @@ bool nixd::fromJSON(const llvm::json::Value &Params, AttrPathCompleteParams &R,
&& O.map("Prefix", R.Prefix) //
;
}

llvm::json::Value nixd::toJSON(const ValueDescription &Params) {
return Object{
{"arity", Params.Arity},
{"doc", Params.Doc},
};
}
bool nixd::fromJSON(const llvm::json::Value &Params, ValueDescription &R,
llvm::json::Path P) {

ObjectMapper O(Params, P);
return O //
&& O.map("arity", R.Arity) //
&& O.map("doc", R.Doc) //
;
}
50 changes: 50 additions & 0 deletions nixd/tools/nixd-attrset-eval/test/attrs-info-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# RUN: nixd-attrset-eval --lit-test < %s | FileCheck %s


```json
{
"jsonrpc":"2.0",
"id":0,
"method":"attrset/evalExpr",
"params": "{ hello = /** some markdown docs */x: y: x; }"
}
```


```json
{
"jsonrpc":"2.0",
"id":1,
"method":"attrset/attrpathInfo",
"params": [ "hello" ]
}
```

```
CHECK: "id": 1,
CHECK-NEXT: "jsonrpc": "2.0",
CHECK-NEXT: "result": {
CHECK-NEXT: "Meta": {
CHECK-NEXT: "Location": null,
CHECK-NEXT: "Type": 9
CHECK-NEXT: },
CHECK-NEXT: "PackageDesc": {
CHECK-NEXT: "Description": null,
CHECK-NEXT: "Homepage": null,
CHECK-NEXT: "LongDescription": null,
CHECK-NEXT: "Name": null,
CHECK-NEXT: "PName": null,
CHECK-NEXT: "Position": null,
CHECK-NEXT: "Version": null
CHECK-NEXT: },
CHECK-NEXT: "ValueDesc": {
CHECK-NEXT: "arity": 0,
CHECK-NEXT: "doc": "Function `hello`\\\n … defined at «string»:1:36\n\nsome markdown docs \n"
CHECK-NEXT: }
CHECK-NEXT: }
```

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