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) {
66 auto &SM = Inputs.AST->getSourceManager();
67 auto &LangOpts = Inputs.AST->getASTContext().getLangOpts();
68 auto Reps = tooling::Replacements(tooling::Replacement(
69 SM, CharSourceRange::getCharRange(Str->getBeginLoc()),
70 "NSLocalizedString(", LangOpts));
71 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
72 Str->getEndLoc(), 0, Inputs.AST->getSourceManager(), LangOpts);
73 if (
auto Err = Reps.add(tooling::Replacement(
74 SM, CharSourceRange::getCharRange(EndLoc),
", @\"\")", LangOpts)))
75 return std::move(Err);
76 return Effect::mainFileEdit(SM, std::move(Reps));
79 std::string ObjCLocalizeStringLiteral::title()
const {
80 return "Wrap in NSLocalizedString";
#define REGISTER_TWEAK(Subclass)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//