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:
22   StringRef getName() const { return "name"; }
23   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:
32   StringRef getCommand() const override { return "extract"; }
33 
34   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.
40   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 NewNameOption : public RequiredRefactoringOption<std::string> {
50 public:
51   StringRef getName() const override { return "new-name"; }
52   StringRef getDescription() const override {
53     return "The new name to change the symbol to";
54   }
55 };
56 
57 // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
58 // rules.
59 class LocalRename final : public RefactoringAction {
60 public:
61   StringRef getCommand() const override { return "local-rename"; }
62 
63   StringRef getDescription() const override {
64     return "Finds and renames symbols in code with no indexer support";
65   }
66 
67   /// Returns a set of refactoring actions rules that are defined by this
68   /// action.
69   RefactoringActionRules createActionRules() const override {
70     RefactoringActionRules Rules;
71     Rules.push_back(createRefactoringActionRule<RenameOccurrences>(
72         SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>()));
73     return Rules;
74   }
75 };
76 
77 } // end anonymous namespace
78 
79 std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() {
80   std::vector<std::unique_ptr<RefactoringAction>> Actions;
81 
82   Actions.push_back(llvm::make_unique<LocalRename>());
83   Actions.push_back(llvm::make_unique<ExtractRefactoring>());
84 
85   return Actions;
86 }
87 
88 RefactoringActionRules RefactoringAction::createActiveActionRules() {
89   // FIXME: Filter out rules that are not supported by a particular client.
90   return createActionRules();
91 }
92 
93 } // end namespace tooling
94 } // end namespace clang
95