nixd
Loading...
Searching...
No Matches
InheritToBinding.cpp
Go to the documentation of this file.
1/// \file
2/// \brief Implementation of inherit to explicit binding code action.
3
4#include "InheritToBinding.h"
5#include "Utils.h"
6
7#include "../Convert.h"
8
10
11#include <sstream>
12
13namespace nixd {
14
17 const std::string &FileURI, llvm::StringRef Src,
18 std::vector<lspserver::CodeAction> &Actions) {
19 // Find if we're inside an Inherit node
20 const nixf::Node *InheritNode = PM.upTo(N, nixf::Node::NK_Inherit);
21 if (!InheritNode)
22 return;
23
24 const auto &Inherit = static_cast<const nixf::Inherit &>(*InheritNode);
25
26 // Get the list of names in the inherit statement
27 const auto &Names = Inherit.names();
28
29 // Only handle single-name inherit statements
30 // Multi-name inherits would require more complex handling
31 if (Names.size() != 1)
32 return;
33
34 const auto &Name = Names[0];
35
36 // Defensive null check
37 if (!Name)
38 return;
39
40 // Only handle static names (not interpolated)
41 if (!Name->isStatic())
42 return;
43
44 const std::string &AttrName = Name->staticName();
45
46 // Build the replacement text
47 std::ostringstream NewText;
48 NewText << AttrName << " = ";
49
50 if (Inherit.expr()) {
51 // inherit (expr) name; -> name = expr.name;
52 NewText << Inherit.expr()->src(Src) << "." << AttrName;
53 } else {
54 // inherit name; -> name = name;
55 NewText << AttrName;
56 }
57 NewText << ";";
58
59 // Create the code action
60 Actions.emplace_back(createSingleEditAction(
61 "Convert to explicit binding",
63 toLSPRange(Src, Inherit.range()), NewText.str()));
64}
65
66} // namespace nixd
Convert between LSP and nixf types.
Code action for converting inherit to explicit binding.
Shared utilities for code actions.
const std::vector< std::shared_ptr< AttrName > > & names() const
Definition Attrs.h:154
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
Definition ParentMap.cpp:27
void addInheritToBindingAction(const nixf::Node &N, const nixf::ParentMapAnalysis &PM, const std::string &FileURI, llvm::StringRef Src, std::vector< lspserver::CodeAction > &Actions)
Add code action to convert inherit to explicit binding.
lspserver::CodeAction createSingleEditAction(const std::string &Title, llvm::StringLiteral Kind, const std::string &FileURI, const lspserver::Range &EditRange, std::string NewText)
Create a CodeAction with a single text edit.
Definition Utils.cpp:10
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
Definition Convert.cpp:40
static const llvm::StringLiteral REFACTOR_REWRITE_KIND