nixd
Loading...
Searching...
No Matches
NoogleDoc.cpp
Go to the documentation of this file.
1/// \file
2/// \brief Implementation of noogle.dev documentation code action.
3
4#include "NoogleDoc.h"
5
7
8#include <llvm/Support/JSON.h>
9
10namespace nixd {
11
12namespace {
13
14/// \brief Construct noogle.dev URL for a lib.* function path.
15/// Examples:
16/// - {"lib", "optionalString"} -> "https://noogle.dev/f/lib/optionalString"
17/// - {"lib", "strings", "optionalString"} ->
18/// "https://noogle.dev/f/lib/strings/optionalString"
19std::string buildNoogleUrl(const std::vector<std::string> &Path) {
20 std::string Url = "https://noogle.dev/f";
21 for (const auto &Segment : Path) {
22 Url += "/";
23 Url += Segment;
24 }
25 return Url;
26}
27
28} // namespace
29
31 std::vector<lspserver::CodeAction> &Actions) {
32 // Find if we're inside an ExprSelect
33 const nixf::Node *SelectNode = PM.upTo(N, nixf::Node::NK_ExprSelect);
34 if (!SelectNode)
35 return;
36
37 const auto &Sel = static_cast<const nixf::ExprSelect &>(*SelectNode);
38
39 // Check base expression is ExprVar with name "lib"
40 if (Sel.expr().kind() != nixf::Node::NK_ExprVar)
41 return;
42
43 const auto &Var = static_cast<const nixf::ExprVar &>(Sel.expr());
44 if (Var.id().name() != "lib")
45 return;
46
47 // Check path exists and has at least one attribute
48 if (!Sel.path())
49 return;
50
51 const nixf::AttrPath &Path = *Sel.path();
52 if (Path.names().empty())
53 return;
54
55 // Build the function path, checking all names are static
56 std::vector<std::string> FunctionPath;
57 FunctionPath.reserve(Path.names().size() + 1);
58 FunctionPath.emplace_back("lib");
59
60 for (const auto &Name : Path.names()) {
61 if (!Name->isStatic())
62 return; // Dynamic attribute, can't construct URL
63 FunctionPath.emplace_back(Name->staticName());
64 }
65
66 // Construct the noogle.dev URL
67 std::string NoogleUrl = buildNoogleUrl(FunctionPath);
68
69 // Create a code action that will open the URL
70 // Note: The actual URL opening is handled by the client via
71 // window/showDocument
72 Actions.emplace_back(lspserver::CodeAction{
73 .title = "Open Noogle documentation for " + FunctionPath.back(),
74 .kind = std::string(lspserver::CodeAction::REFACTOR_KIND),
75 .data = llvm::json::Object{{"noogleUrl", NoogleUrl}},
76 });
77}
78
79} // namespace nixd
Code action for opening noogle.dev documentation.
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
Definition ParentMap.cpp:27
std::string Path
Definition Path.h:24
void addNoogleDocAction(const nixf::Node &N, const nixf::ParentMapAnalysis &PM, std::vector< lspserver::CodeAction > &Actions)
Add a code action to open noogle.dev documentation for lib.* functions.
Definition NoogleDoc.cpp:30
static const llvm::StringLiteral REFACTOR_KIND