nixd
Toggle main menu visibility
Loading...
Searching...
No Matches
nixd
lib
Controller
CodeActions
AttrName.cpp
Go to the documentation of this file.
1
/// \file
2
/// \brief Implementation of attribute name quote/unquote code actions.
3
4
#include "
AttrName.h
"
5
#include "
Utils.h
"
6
7
#include "
../Convert.h
"
8
9
#include <
nixf/Basic/Nodes/Attrs.h
>
10
11
namespace
nixd
{
12
13
void
addAttrNameActions
(
const
nixf::Node
&N,
const
nixf::ParentMapAnalysis
&PM,
14
const
std::string &FileURI, llvm::StringRef Src,
15
std::vector<lspserver::CodeAction> &Actions) {
16
// Find if we're inside an AttrName
17
const
nixf::Node
*AttrNameNode = PM.
upTo
(N, nixf::Node::NK_AttrName);
18
if
(!AttrNameNode)
19
return
;
20
21
const
auto
&AN =
static_cast<
const
nixf::AttrName
&
>
(*AttrNameNode);
22
23
if
(AN.kind() ==
nixf::AttrName::ANK_ID
) {
24
// Offer to quote: foo -> "foo"
25
const
std::string &Name = AN.id()->name();
26
Actions.emplace_back(
createSingleEditAction
(
27
"Quote attribute name"
,
lspserver::CodeAction::REFACTOR_REWRITE_KIND
,
28
FileURI,
toLSPRange
(Src, AN.range()),
"\""
+ Name +
"\""
));
29
}
else
if
(AN.kind() ==
nixf::AttrName::ANK_String
&& AN.isStatic()) {
30
// Offer to unquote if valid identifier: "foo" -> foo
31
const
std::string &Name = AN.staticName();
32
if
(
isValidNixIdentifier
(Name)) {
33
Actions.emplace_back(
34
createSingleEditAction
(
"Unquote attribute name"
,
35
lspserver::CodeAction::REFACTOR_REWRITE_KIND
,
36
FileURI,
toLSPRange
(Src, AN.range()), Name));
37
}
38
}
39
}
40
41
}
// namespace nixd
AttrName.h
Code action for quoting/unquoting attribute names.
Attrs.h
Convert.h
Convert between LSP and nixf types.
Utils.h
Shared utilities for code actions.
nixf::AttrName
Definition
Attrs.h:10
nixf::AttrName::ANK_ID
@ ANK_ID
Definition
Attrs.h:12
nixf::AttrName::ANK_String
@ ANK_String
Definition
Attrs.h:12
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::isValidNixIdentifier
bool isValidNixIdentifier(const std::string &S)
Check if a string is a valid Nix identifier that can be unquoted.
Definition
Utils.cpp:27
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::addAttrNameActions
void addAttrNameActions(const nixf::Node &N, const nixf::ParentMapAnalysis &PM, const std::string &FileURI, llvm::StringRef Src, std::vector< lspserver::CodeAction > &Actions)
Add refactoring code actions for attribute names (quote/unquote).
Definition
AttrName.cpp:13
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