blob: 73a3118396208e9efd4a047751aeb7cdf2ba6238 [file] [log] [blame]
Alex Lorenzb54ef6a2017-09-14 10:06:52 +00001//===--- 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
Alex Lorenz0beca4d2017-10-27 18:19:11 +000010#include "clang/Tooling/Refactoring/Extract/Extract.h"
Alex Lorenzb54ef6a2017-09-14 10:06:52 +000011#include "clang/Tooling/Refactoring/RefactoringAction.h"
Alex Lorenz0beca4d2017-10-27 18:19:11 +000012#include "clang/Tooling/Refactoring/RefactoringOptions.h"
13#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
Alex Lorenzb54ef6a2017-09-14 10:06:52 +000014
15namespace clang {
16namespace tooling {
17
Alex Lorenz0beca4d2017-10-27 18:19:11 +000018namespace {
19
20class DeclNameOption final : public OptionalRefactoringOption<std::string> {
21public:
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.
30class ExtractRefactoring final : public RefactoringAction {
31public:
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
49class NewNameOption : public RequiredRefactoringOption<std::string> {
50public:
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.
59class LocalRename final : public RefactoringAction {
60public:
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
Alex Lorenzb54ef6a2017-09-14 10:06:52 +000078
79std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() {
80 std::vector<std::unique_ptr<RefactoringAction>> Actions;
81
Alex Lorenz0beca4d2017-10-27 18:19:11 +000082 Actions.push_back(llvm::make_unique<LocalRename>());
83 Actions.push_back(llvm::make_unique<ExtractRefactoring>());
Alex Lorenzb54ef6a2017-09-14 10:06:52 +000084
85 return Actions;
86}
87
88RefactoringActionRules 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