-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
base: main
Are you sure you want to change the base?
Value doc #625
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
|
@@ -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"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty |
||
constexpr inline std::string_view Exit = "exit"; | ||
|
||
} // namespace rpcMethod | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -87,12 +87,15 @@ class NixpkgsHoverProvider { | |
return OS.str(); | ||
} | ||
|
||
static std::string mkValueMarkdown(const ValueDescription &ValueDesc) { | ||
return ValueDesc.Doc; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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; | ||
} | ||
}; | ||
|
||
|
@@ -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)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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, | ||
|
@@ -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()); | ||
|
@@ -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())); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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