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 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
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
200 changes: 138 additions & 62 deletions nixd/lib/Controller/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class NixpkgsHoverProvider {
///
/// FIXME: there are many markdown generation in language server.
/// Maybe we can add structured generating first?
static std::string mkMarkdown(const PackageDescription &Package) {
static std::string mkPackageMarkdown(const PackageDescription &Package) {
std::ostringstream OS;
// Make each field a new section

Expand Down Expand Up @@ -87,12 +87,15 @@ class NixpkgsHoverProvider {
return OS.str();
}

static std::string mkValueMarkdown(const ValueDescription &ValueDesc) {
return ValueDesc.Doc;
Copy link
Member Author

Choose a reason for hiding this comment

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

here, the documentation is replied directly without any formatting/tokenization.

}

public:
NixpkgsHoverProvider(AttrSetClient &NixpkgsClient)
: NixpkgsClient(NixpkgsClient) {}

std::optional<std::string> resolvePackage(std::vector<std::string> Scope,
std::string Name) {
std::optional<std::string> resolvePackage(const Selector &Sel) {
std::binary_semaphore Ready(0);
std::optional<AttrPathInfoResponse> Desc;
auto OnReply = [&Ready, &Desc](llvm::Expected<AttrPathInfoResponse> Resp) {
Expand All @@ -102,14 +105,114 @@ class NixpkgsHoverProvider {
elog("nixpkgs provider: {0}", Resp.takeError());
Ready.release();
};
Scope.emplace_back(std::move(Name));
NixpkgsClient.attrpathInfo(Scope, std::move(OnReply));
NixpkgsClient.attrpathInfo(Sel, std::move(OnReply));
Ready.acquire();

if (!Desc)
return std::nullopt;

return mkMarkdown(Desc->PackageDesc);
if (const auto ValueDesc = Desc->ValueDesc) {
return ValueDesc->Doc;
}

return mkPackageMarkdown(Desc->PackageDesc);
}
};

class HoverProvider {
const NixTU &TU;
const VariableLookupAnalysis &VLA;
const ParentMapAnalysis &PM;

[[nodiscard]] std::optional<Hover>
mkHover(std::optional<std::string> Doc, nixf::LexerCursorRange Range) const {
if (!Doc)
return std::nullopt;
return Hover{
.contents =
MarkupContent{
.kind = MarkupKind::Markdown,
.value = std::move(*Doc),
},
.range = toLSPRange(TU.src(), Range),
};
}

public:
HoverProvider(const NixTU &TU, const VariableLookupAnalysis &VLA,
const ParentMapAnalysis &PM)
: TU(TU), VLA(VLA), PM(PM) {}

std::optional<Hover> hoverVar(const nixf::Node &N,
AttrSetClient &Client) const {
if (havePackageScope(N, VLA, PM)) {
// Ask nixpkgs client what's current package documentation.
auto NHP = NixpkgsHoverProvider(Client);
auto [Scope, Name] = getScopeAndPrefix(N, PM);
Scope.emplace_back(Name);
return mkHover(NHP.resolvePackage(Scope), N.range());
}

return std::nullopt;
}

std::optional<Hover> hoverSelect(const nixf::ExprSelect &Select,
AttrSetClient &Client) const {
// The base expr for selecting.
const nixf::Expr &BaseExpr = Select.expr();

if (BaseExpr.kind() != Node::NK_ExprVar) {
return std::nullopt;
}

const auto &Var = static_cast<const nixf::ExprVar &>(BaseExpr);
try {
Selector Sel =
idioms::mkSelector(Select, idioms::mkVarSelector(Var, VLA, PM));
auto NHP = NixpkgsHoverProvider(Client);
return mkHover(NHP.resolvePackage(Sel), Select.range());
} catch (std::exception &E) {
log("hover/select skipped, reason: {0}", E.what());
}
return std::nullopt;
}

std::optional<Hover>
hoverAttrPath(const nixf::Node &N, std::mutex &OptionsLock,
const Controller::OptionMapTy &Options) const {
auto Scope = std::vector<std::string>();
const auto R = findAttrPath(N, PM, Scope);
if (R == FindAttrPathResult::OK) {
std::lock_guard _(OptionsLock);
for (const auto &[_, Client] : Options) {
if (AttrSetClient *C = Client->client()) {
OptionsHoverProvider OHP(*C);
std::optional<OptionDescription> Desc = OHP.resolveHover(Scope);
std::string Docs;
if (Desc) {
if (Desc->Type) {
std::string TypeName = Desc->Type->Name.value_or("");
std::string TypeDesc = Desc->Type->Description.value_or("");
Docs += llvm::formatv("{0} ({1})", TypeName, TypeDesc);
} else {
Docs += "? (missing type)";
}
if (Desc->Description) {
Docs += "\n\n" + Desc->Description.value_or("");
}
return Hover{
.contents =
MarkupContent{
.kind = MarkupKind::Markdown,
.value = std::move(Docs),
},
.range = toLSPRange(TU.src(), N.range()),
};
}
}
}
}
return std::nullopt;
}
};

Expand All @@ -130,65 +233,38 @@ void Controller::onHover(const TextDocumentPositionParams &Params,
const auto Name = std::string(N.name());
const auto &VLA = *TU->variableLookup();
const auto &PM = *TU->parentMap();
if (havePackageScope(N, VLA, PM) && nixpkgsClient()) {
// Ask nixpkgs client what's current package documentation.
auto NHP = NixpkgsHoverProvider(*nixpkgsClient());
const auto [Scope, Name] = getScopeAndPrefix(N, PM);
if (std::optional<std::string> Doc = NHP.resolvePackage(Scope, Name)) {
return Hover{
.contents =
MarkupContent{
.kind = MarkupKind::Markdown,
.value = std::move(*Doc),
},
.range = toLSPRange(TU->src(), N.range()),
};
}
}
const auto &UpExpr = *CheckDefault(PM.upExpr(N));

auto Scope = std::vector<std::string>();
const auto R = findAttrPath(N, PM, Scope);
if (R == FindAttrPathResult::OK) {
std::lock_guard _(OptionsLock);
for (const auto &[_, Client] : Options) {
if (AttrSetClient *C = Client->client()) {
OptionsHoverProvider OHP(*C);
std::optional<OptionDescription> Desc = OHP.resolveHover(Scope);
std::string Docs;
if (Desc) {
if (Desc->Type) {
std::string TypeName = Desc->Type->Name.value_or("");
std::string TypeDesc = Desc->Type->Description.value_or("");
Docs += llvm::formatv("{0} ({1})", TypeName, TypeDesc);
} else {
Docs += "? (missing type)";
}
if (Desc->Description) {
Docs += "\n\n" + Desc->Description.value_or("");
}
return Hover{
.contents =
MarkupContent{
.kind = MarkupKind::Markdown,
.value = std::move(Docs),
},
.range = toLSPRange(TU->src(), N.range()),
};
}
}
const auto Provider = HoverProvider(*TU, VLA, PM);

const auto HoverByCase = [&]() -> std::optional<Hover> {
switch (UpExpr.kind()) {
case Node::NK_ExprVar:
return Provider.hoverVar(N, *nixpkgsClient());
case Node::NK_ExprSelect:
return Provider.hoverSelect(
static_cast<const nixf::ExprSelect &>(UpExpr), *nixpkgsClient());
case Node::NK_ExprAttrs:
return Provider.hoverAttrPath(N, OptionsLock, Options);
default:
return std::nullopt;
}
}
}();

// Reply it's kind by static analysis
// FIXME: support more.
return Hover{
.contents =
MarkupContent{
.kind = MarkupKind::Markdown,
.value = "`" + Name + "`",
},
.range = toLSPRange(TU->src(), N.range()),
};
if (HoverByCase) {
return HoverByCase.value();
} else {
// Reply it's kind by static analysis
// FIXME: support more.
return Hover{
.contents =
MarkupContent{
.kind = MarkupKind::Markdown,
.value = "`" + Name + "`",
},
.range = toLSPRange(TU->src(), N.range()),
};
}
}());
};
boost::asio::post(Pool, std::move(Action));
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
Loading
Loading