1 //===--- RefactoringActions.cpp - Constructs refactoring actions ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Tooling/Refactoring/Extract/Extract.h"
11 #include "clang/Tooling/Refactoring/RefactoringAction.h"
12 #include "clang/Tooling/Refactoring/RefactoringOptions.h"
13 #include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
14 
15 namespace clang {
16 namespace tooling {
17 
18 namespace {
19 
20 class DeclNameOption final : public OptionalRefactoringOption<std::string> {
21 public:
getName() const22   StringRef getName() const { return "name"; }
getDescription() const23   StringRef getDescription() const {
24     return "Name of the extracted declaration";
25   }
26 };
27 
28 // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
29 // rules.
30 class ExtractRefactoring final : public RefactoringAction {
31 public:
getCommand() const32   StringRef getCommand() const override { return "extract"; }
33 
getDescription() const34   StringRef getDescription() const override {
35     return "(WIP action; use with caution!) Extracts code into a new function";
36   }
37 
38   /// Returns a set of refactoring actions rules that are defined by this
39   /// action.
createActionRules() const40   RefactoringActionRules createActionRules() const override {
41     RefactoringActionRules Rules;
42     Rules.push_back(createRefactoringActionRule<ExtractFunction>(
43         CodeRangeASTSelectionRequirement(),
44         OptionRequirement<DeclNameOption>()));
45     return Rules;
46   }
47 };
48 
49 class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> {
50 public:
getName() const51   StringRef getName() const override { return "old-qualified-name"; }
getDescription() const52   StringRef getDescription() const override {
53     return "The old qualified name to be renamed";
54   }
55 };
56 
57 class NewQualifiedNameOption : public RequiredRefactoringOption<std::string> {
58 public:
getName() const59   StringRef getName() const override { return "new-qualified-name"; }
getDescription() const60   StringRef getDescription() const override {
61     return "The new qualified name to change the symbol to";
62   }
63 };
64 
65 class NewNameOption : public RequiredRefactoringOption<std::string> {
66 public:
getName() const67   StringRef getName() const override { return "new-name"; }
getDescription() const68   StringRef getDescription() const override {
69     return "The new name to change the symbol to";
70   }
71 };
72 
73 // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
74 // rules.
75 class LocalRename final : public RefactoringAction {
76 public:
getCommand() const77   StringRef getCommand() const override { return "local-rename"; }
78 
getDescription() const79   StringRef getDescription() const override {
80     return "Finds and renames symbols in code with no indexer support";
81   }
82 
83   /// Returns a set of refactoring actions rules that are defined by this
84   /// action.
createActionRules() const85   RefactoringActionRules createActionRules() const override {
86     RefactoringActionRules Rules;
87     Rules.push_back(createRefactoringActionRule<RenameOccurrences>(
88         SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>()));
89     // FIXME: Use NewNameOption.
90     Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>(
91         OptionRequirement<OldQualifiedNameOption>(),
92         OptionRequirement<NewQualifiedNameOption>()));
93     return Rules;
94   }
95 };
96 
97 } // end anonymous namespace
98 
createRefactoringActions()99 std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() {
100   std::vector<std::unique_ptr<RefactoringAction>> Actions;
101 
102   Actions.push_back(llvm::make_unique<LocalRename>());
103   Actions.push_back(llvm::make_unique<ExtractRefactoring>());
104 
105   return Actions;
106 }
107 
createActiveActionRules()108 RefactoringActionRules RefactoringAction::createActiveActionRules() {
109   // FIXME: Filter out rules that are not supported by a particular client.
110   return createActionRules();
111 }
112 
113 } // end namespace tooling
114 } // end namespace clang
115