12#include "clang/AST/ExprObjC.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Basic/SourceManager.h"
15#include "clang/Tooling/Core/Replacement.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Casting.h"
18#include "llvm/Support/Error.h"
30class ObjCLocalizeStringLiteral :
public Tweak {
32 const char *id() const final;
33 llvm::StringLiteral kind()
const override {
37 bool prepare(
const Selection &Inputs)
override;
38 Expected<Tweak::Effect> apply(
const Selection &Inputs)
override;
39 std::string title()
const override;
42 const clang::ObjCStringLiteral *Str =
nullptr;
47bool ObjCLocalizeStringLiteral::prepare(
const Selection &Inputs) {
48 const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
53 if (N->ASTNode.get<StringLiteral>()) {
57 Str = dyn_cast_or_null<ObjCStringLiteral>(N->ASTNode.get<Stmt>());
61Expected<Tweak::Effect>
62ObjCLocalizeStringLiteral::apply(
const Selection &Inputs) {
63 auto *
AST = Inputs.AST;
64 auto &SM =
AST->getSourceManager();
65 const auto &TB =
AST->getTokens();
66 auto Toks = TB.spelledForExpanded(TB.expandedTokens(Str->getSourceRange()));
67 if (!Toks || Toks->empty())
68 return error(
"Failed to find tokens to replace.");
70 auto Reps = tooling::Replacements(tooling::Replacement(
71 SM, Toks->front().location(), 0,
"NSLocalizedString("));
73 if (
auto Err = Reps.add(
74 tooling::Replacement(SM, Toks->back().endLocation(), 0,
", @\"\")")))
75 return std::move(Err);
76 return Effect::mainFileEdit(SM, std::move(Reps));
79std::string ObjCLocalizeStringLiteral::title()
const {
80 return "Wrap in NSLocalizedString";
#define REGISTER_TWEAK(Subclass)
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
static const llvm::StringLiteral REFACTOR_KIND