Alex Lorenz | b54ef6a | 2017-09-14 10:06:52 +0000 | [diff] [blame] | 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 | |
Alex Lorenz | 0beca4d | 2017-10-27 18:19:11 +0000 | [diff] [blame] | 10 | #include "clang/Tooling/Refactoring/Extract/Extract.h" |
Alex Lorenz | b54ef6a | 2017-09-14 10:06:52 +0000 | [diff] [blame] | 11 | #include "clang/Tooling/Refactoring/RefactoringAction.h" |
Alex Lorenz | 0beca4d | 2017-10-27 18:19:11 +0000 | [diff] [blame] | 12 | #include "clang/Tooling/Refactoring/RefactoringOptions.h" |
| 13 | #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" |
Alex Lorenz | b54ef6a | 2017-09-14 10:06:52 +0000 | [diff] [blame] | 14 | |
| 15 | namespace clang { |
| 16 | namespace tooling { |
| 17 | |
Alex Lorenz | 0beca4d | 2017-10-27 18:19:11 +0000 | [diff] [blame] | 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 | |
Haojian Wu | 200458f | 2017-11-08 08:56:56 +0000 | [diff] [blame] | 49 | class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> { |
| 50 | public: |
| 51 | StringRef getName() const override { return "old-qualified-name"; } |
| 52 | 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: |
| 59 | StringRef getName() const override { return "new-qualified-name"; } |
| 60 | StringRef getDescription() const override { |
| 61 | return "The new qualified name to change the symbol to"; |
| 62 | } |
| 63 | }; |
| 64 | |
Alex Lorenz | 0beca4d | 2017-10-27 18:19:11 +0000 | [diff] [blame] | 65 | class NewNameOption : public RequiredRefactoringOption<std::string> { |
| 66 | public: |
| 67 | StringRef getName() const override { return "new-name"; } |
| 68 | 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: |
| 77 | StringRef getCommand() const override { return "local-rename"; } |
| 78 | |
| 79 | 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. |
| 85 | RefactoringActionRules createActionRules() const override { |
| 86 | RefactoringActionRules Rules; |
| 87 | Rules.push_back(createRefactoringActionRule<RenameOccurrences>( |
| 88 | SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>())); |
Haojian Wu | 200458f | 2017-11-08 08:56:56 +0000 | [diff] [blame] | 89 | // FIXME: Use NewNameOption. |
| 90 | Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>( |
| 91 | OptionRequirement<OldQualifiedNameOption>(), |
| 92 | OptionRequirement<NewQualifiedNameOption>())); |
Alex Lorenz | 0beca4d | 2017-10-27 18:19:11 +0000 | [diff] [blame] | 93 | return Rules; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | } // end anonymous namespace |
Alex Lorenz | b54ef6a | 2017-09-14 10:06:52 +0000 | [diff] [blame] | 98 | |
| 99 | std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() { |
| 100 | std::vector<std::unique_ptr<RefactoringAction>> Actions; |
| 101 | |
Alex Lorenz | 0beca4d | 2017-10-27 18:19:11 +0000 | [diff] [blame] | 102 | Actions.push_back(llvm::make_unique<LocalRename>()); |
| 103 | Actions.push_back(llvm::make_unique<ExtractRefactoring>()); |
Alex Lorenz | b54ef6a | 2017-09-14 10:06:52 +0000 | [diff] [blame] | 104 | |
| 105 | return Actions; |
| 106 | } |
| 107 | |
| 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 |