nixd
Loading...
Searching...
No Matches
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
10
11namespace nixd {
12
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(
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",
36 FileURI, toLSPRange(Src, AN.range()), Name));
37 }
38 }
39}
40
41} // namespace nixd
Code action for quoting/unquoting attribute names.
Convert between LSP and nixf types.
Shared utilities for code actions.
const Node * upTo(const Node &N, Node::NodeKind Kind) const
Search up until some kind of node is found.
Definition ParentMap.cpp:27
bool isValidNixIdentifier(const std::string &S)
Check if a string is a valid Nix identifier that can be unquoted.
Definition Utils.cpp:27
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
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
lspserver::Range toLSPRange(llvm::StringRef Code, const nixf::LexerCursorRange &R)
Definition Convert.cpp:40
static const llvm::StringLiteral REFACTOR_REWRITE_KIND