-
-
Notifications
You must be signed in to change notification settings - Fork 50
Value doc #625
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
Draft
inclyc
wants to merge
4
commits into
main
Choose a base branch
from
value-doc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Value doc #625
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9fbb2f7
nixd/tools/nixd-attrset-eval: provide `getDoc` information by C++ nix
inclyc 8b7fc6f
draft: hover by "select" expressions.
inclyc 00a38f1
draft: reply value-doc
inclyc 6bb44d0
fixup: don't use `getDoc()`, provide our own describe() method instead
inclyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
empty