17 const std::string &FileURI, llvm::StringRef Src,
18 std::vector<lspserver::CodeAction> &Actions) {
20 const nixf::Node *BindingNode = PM.
upTo(N, nixf::Node::NK_Binding);
24 const auto &Bind =
static_cast<const nixf::Binding &
>(*BindingNode);
28 if (Names.size() != 1)
32 const auto &AttrNameNode = Names[0];
33 if (!AttrNameNode || !AttrNameNode->isStatic())
36 const std::string &AttrName = AttrNameNode->staticName();
45 if (
Value.kind() == nixf::Node::NK_ExprVar) {
47 if (Var.id().name() == AttrName) {
51 FileURI,
toLSPRange(Src, Bind.range()), std::move(NewText)));
58 if (
Value.kind() == nixf::Node::NK_ExprSelect) {
62 if (Sel.defaultExpr())
70 const auto &SelPath = Sel.
path()->
names();
75 const auto &LastSegment = SelPath.back();
76 if (!LastSegment || !LastSegment->isStatic())
79 if (LastSegment->staticName() != AttrName)
86 std::string SourceText =
90 for (
size_t I = 0; I + 1 < SelPath.size(); ++I) {
91 const auto &Segment = SelPath[I];
92 if (!Segment || !Segment->isStatic())
101 "Convert to `inherit (" + SourceText +
")`",
103 toLSPRange(Src, Bind.range()), std::move(NewText)));
Code action for converting bindings to inherit syntax.
Convert between LSP and nixf types.
Shared utilities for code actions.
const std::vector< std::shared_ptr< AttrName > > & names() const
const AttrPath & path() const
std::size_t offset() const
Offset in the source file, starting from 0.
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
void addConvertToInheritAction(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 binding to inherit syntax.
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.
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
std::string quoteNixAttrKey(const std::string &Key)
Quote and escape a Nix attribute key if necessary.
static const llvm::StringLiteral REFACTOR_REWRITE_KIND