13 #include "clang/AST/ExprObjC.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/SourceLocation.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Tooling/Core/Replacement.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Error.h"
35 class ObjCLocalizeStringLiteral :
public Tweak {
37 const char *id() const override final;
38 Intent intent()
const override {
return Intent::Refactor; }
40 bool prepare(
const Selection &
Inputs)
override;
41 Expected<Tweak::Effect> apply(
const Selection &
Inputs)
override;
42 std::string title()
const override;
45 const clang::ObjCStringLiteral *Str =
nullptr;
50 bool ObjCLocalizeStringLiteral::prepare(
const Selection &
Inputs) {
51 const SelectionTree::Node *N =
Inputs.ASTSelection.commonAncestor();
56 if (N->ASTNode.get<StringLiteral>()) {
60 Str = dyn_cast_or_null<ObjCStringLiteral>(N->ASTNode.get<Stmt>());
64 Expected<Tweak::Effect>
65 ObjCLocalizeStringLiteral::apply(
const Selection &
Inputs) {
69 auto Toks = TB.spelledForExpanded(TB.expandedTokens(Str->getSourceRange()));
70 if (!Toks || Toks->empty())
71 return llvm::createStringError(llvm::inconvertibleErrorCode(),
72 "Failed to find tokens to replace.");
74 auto Reps = tooling::Replacements(tooling::Replacement(
75 SM, Toks->front().location(), 0,
"NSLocalizedString("));
77 if (
auto Err = Reps.add(
78 tooling::Replacement(SM, Toks->back().endLocation(), 0,
", @\"\")")))
79 return std::move(Err);
80 return Effect::mainFileEdit(SM, std::move(Reps));
83 std::string ObjCLocalizeStringLiteral::title()
const {
84 return "Wrap in NSLocalizedString";