nixd
Toggle main menu visibility
Loading...
Searching...
No Matches
nixd
lib
Controller
CodeActions
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
9
#include <
nixf/Basic/Nodes/Attrs.h
>
10
11
#include <sstream>
12
13
namespace
nixd
{
14
15
void
addInheritToBindingAction
(
const
nixf::Node
&N,
16
const
nixf::ParentMapAnalysis
&PM,
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"
,
62
lspserver::CodeAction::REFACTOR_REWRITE_KIND
, FileURI,
63
toLSPRange
(Src,
Inherit
.range()), NewText.str()));
64
}
65
66
}
// namespace nixd
Attrs.h
Convert.h
Convert between LSP and nixf types.
InheritToBinding.h
Code action for converting inherit to explicit binding.
Utils.h
Shared utilities for code actions.
nixf::Inherit
Definition
Attrs.h:145
nixf::Inherit::names
const std::vector< std::shared_ptr< AttrName > > & names() const
Definition
Attrs.h:154
nixf::Node
Definition
Basic.h:12
nixf::ParentMapAnalysis
Definition
ParentMap.h:15
nixf::ParentMapAnalysis::upTo
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
Definition
ParentMap.cpp:27
nixd
Definition
CommandLine/Configuration.h:9
nixd::addInheritToBindingAction
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.
Definition
InheritToBinding.cpp:15
nixd::FindAttrPathResult::Inherit
@ Inherit
Definition
AST.h:121
nixd::createSingleEditAction
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
nixd::toLSPRange
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
Definition
Convert.cpp:40
lspserver::CodeAction::REFACTOR_REWRITE_KIND
static const llvm::StringLiteral REFACTOR_REWRITE_KIND
Definition
lspserver/include/lspserver/Protocol.h:1131
Generated by
1.17.0