23#include <boost/asio/post.hpp>
25#include <llvm/ADT/StringRef.h>
26#include <llvm/Support/CommandLine.h>
33using namespace llvm::cl;
37opt<bool> EnableInlayHints{
"inlay-hints", desc(
"Enable/Disable inlay-hints"),
41class NixpkgsInlayHintsProvider {
42 AttrSetClient &NixpkgsProvider;
43 const VariableLookupAnalysis &VLA;
44 const ParentMapAnalysis &PMA;
47 std::optional<nixf::PositionRange> Range;
49 std::vector<InlayHint> &Hints;
51 bool rangeOK(
const nixf::PositionRange &R) {
54 return Range->contains(R);
60 NixpkgsInlayHintsProvider(AttrSetClient &NixpkgsProvider,
61 const VariableLookupAnalysis &VLA,
62 const ParentMapAnalysis &PMA,
63 std::optional<lspserver::Range> Range,
64 std::vector<InlayHint> &Hints, llvm::StringRef Src)
65 : NixpkgsProvider(NixpkgsProvider), VLA(VLA), PMA(PMA), Hints(Hints),
71 void dfs(
const Node *N) {
74 if (N->
kind() == Node::NK_ExprVar) {
81 std::binary_semaphore Ready(0);
82 const std::string &Name =
static_cast<const ExprVar &
>(*N).id().name();
83 AttrPathInfoResponse R;
84 auto OnReply = [&Ready, &R](llvm::Expected<AttrPathInfoResponse> Resp) {
92 NixpkgsProvider.attrpathInfo({Name}, std::move(OnReply));
99 .label =
": " + *Version,
100 .kind = InlayHintKind::Designator,
103 Hints.emplace_back(std::move(H));
108 for (
const Node *Ch : N->
children())
116 Callback<std::vector<InlayHint>> Reply) {
118 using CheckTy = std::vector<InlayHint>;
121 if (!EnableInlayHints)
122 return Reply(std::vector<InlayHint>{});
124 auto Action = [Reply = std::move(Reply), URI = Params.
textDocument.
uri,
125 Range = Params.
range,
this]()
mutable {
126 const auto File = URI.file();
127 return Reply([&]() -> llvm::Expected<CheckTy> {
131 std::vector<InlayHint> Response;
132 NixpkgsInlayHintsProvider NP(*nixpkgsClient(), *TU->variableLookup(),
133 *TU->parentMap(), Range, Response,
139 boost::asio::post(Pool, std::move(Action));
This file declares some common analysis (tree walk) on the AST.
#define CheckDefault(x)
Variant of CheckReturn, but returns default constructed CheckTy
Convert between LSP and nixf types.
PositionRange positionRange() const
LexerCursorRange range() const
virtual ChildVector children() const =0
Whether current platform treats paths case insensitively.
llvm::unique_function< void(llvm::Expected< T >)> Callback
nixf::PositionRange toNixfRange(const lspserver::Range &P)
llvm::cl::OptionCategory NixdCategory
lspserver::Position toLSPPosition(llvm::StringRef Code, const nixf::LexerCursor &P)
bool havePackageScope(const nixf::Node &N, const nixf::VariableLookupAnalysis &VLA, const nixf::ParentMapAnalysis &PM)
Determine whether or not some node has enclosed "with pkgs; [ ]".
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
A parameter literal used in inlay hint requests.
TextDocumentIdentifier textDocument
The text document.
std::optional< Range > range
URIForFile uri
The text document's URI.
PackageDescription PackageDesc
Package description of the attribute path, if available.
std::optional< std::string > Version