1e5dd7070Spatrick //===--- RefactoringActions.cpp - Constructs refactoring actions ----------===// 2e5dd7070Spatrick // 3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information. 5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e5dd7070Spatrick // 7e5dd7070Spatrick //===----------------------------------------------------------------------===// 8e5dd7070Spatrick 9e5dd7070Spatrick #include "clang/Tooling/Refactoring/Extract/Extract.h" 10e5dd7070Spatrick #include "clang/Tooling/Refactoring/RefactoringAction.h" 11e5dd7070Spatrick #include "clang/Tooling/Refactoring/RefactoringOptions.h" 12e5dd7070Spatrick #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" 13e5dd7070Spatrick 14e5dd7070Spatrick namespace clang { 15e5dd7070Spatrick namespace tooling { 16e5dd7070Spatrick 17e5dd7070Spatrick namespace { 18e5dd7070Spatrick 19e5dd7070Spatrick class DeclNameOption final : public OptionalRefactoringOption<std::string> { 20e5dd7070Spatrick public: getName() const21*a9ac8606Spatrick StringRef getName() const override { return "name"; } getDescription() const22*a9ac8606Spatrick StringRef getDescription() const override { 23e5dd7070Spatrick return "Name of the extracted declaration"; 24e5dd7070Spatrick } 25e5dd7070Spatrick }; 26e5dd7070Spatrick 27e5dd7070Spatrick // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with 28e5dd7070Spatrick // rules. 29e5dd7070Spatrick class ExtractRefactoring final : public RefactoringAction { 30e5dd7070Spatrick public: getCommand() const31e5dd7070Spatrick StringRef getCommand() const override { return "extract"; } 32e5dd7070Spatrick getDescription() const33e5dd7070Spatrick StringRef getDescription() const override { 34e5dd7070Spatrick return "(WIP action; use with caution!) Extracts code into a new function"; 35e5dd7070Spatrick } 36e5dd7070Spatrick 37e5dd7070Spatrick /// Returns a set of refactoring actions rules that are defined by this 38e5dd7070Spatrick /// action. createActionRules() const39e5dd7070Spatrick RefactoringActionRules createActionRules() const override { 40e5dd7070Spatrick RefactoringActionRules Rules; 41e5dd7070Spatrick Rules.push_back(createRefactoringActionRule<ExtractFunction>( 42e5dd7070Spatrick CodeRangeASTSelectionRequirement(), 43e5dd7070Spatrick OptionRequirement<DeclNameOption>())); 44e5dd7070Spatrick return Rules; 45e5dd7070Spatrick } 46e5dd7070Spatrick }; 47e5dd7070Spatrick 48e5dd7070Spatrick class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> { 49e5dd7070Spatrick public: getName() const50e5dd7070Spatrick StringRef getName() const override { return "old-qualified-name"; } getDescription() const51e5dd7070Spatrick StringRef getDescription() const override { 52e5dd7070Spatrick return "The old qualified name to be renamed"; 53e5dd7070Spatrick } 54e5dd7070Spatrick }; 55e5dd7070Spatrick 56e5dd7070Spatrick class NewQualifiedNameOption : public RequiredRefactoringOption<std::string> { 57e5dd7070Spatrick public: getName() const58e5dd7070Spatrick StringRef getName() const override { return "new-qualified-name"; } getDescription() const59e5dd7070Spatrick StringRef getDescription() const override { 60e5dd7070Spatrick return "The new qualified name to change the symbol to"; 61e5dd7070Spatrick } 62e5dd7070Spatrick }; 63e5dd7070Spatrick 64e5dd7070Spatrick class NewNameOption : public RequiredRefactoringOption<std::string> { 65e5dd7070Spatrick public: getName() const66e5dd7070Spatrick StringRef getName() const override { return "new-name"; } getDescription() const67e5dd7070Spatrick StringRef getDescription() const override { 68e5dd7070Spatrick return "The new name to change the symbol to"; 69e5dd7070Spatrick } 70e5dd7070Spatrick }; 71e5dd7070Spatrick 72e5dd7070Spatrick // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with 73e5dd7070Spatrick // rules. 74e5dd7070Spatrick class LocalRename final : public RefactoringAction { 75e5dd7070Spatrick public: getCommand() const76e5dd7070Spatrick StringRef getCommand() const override { return "local-rename"; } 77e5dd7070Spatrick getDescription() const78e5dd7070Spatrick StringRef getDescription() const override { 79e5dd7070Spatrick return "Finds and renames symbols in code with no indexer support"; 80e5dd7070Spatrick } 81e5dd7070Spatrick 82e5dd7070Spatrick /// Returns a set of refactoring actions rules that are defined by this 83e5dd7070Spatrick /// action. createActionRules() const84e5dd7070Spatrick RefactoringActionRules createActionRules() const override { 85e5dd7070Spatrick RefactoringActionRules Rules; 86e5dd7070Spatrick Rules.push_back(createRefactoringActionRule<RenameOccurrences>( 87e5dd7070Spatrick SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>())); 88e5dd7070Spatrick // FIXME: Use NewNameOption. 89e5dd7070Spatrick Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>( 90e5dd7070Spatrick OptionRequirement<OldQualifiedNameOption>(), 91e5dd7070Spatrick OptionRequirement<NewQualifiedNameOption>())); 92e5dd7070Spatrick return Rules; 93e5dd7070Spatrick } 94e5dd7070Spatrick }; 95e5dd7070Spatrick 96e5dd7070Spatrick } // end anonymous namespace 97e5dd7070Spatrick createRefactoringActions()98e5dd7070Spatrickstd::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() { 99e5dd7070Spatrick std::vector<std::unique_ptr<RefactoringAction>> Actions; 100e5dd7070Spatrick 101e5dd7070Spatrick Actions.push_back(std::make_unique<LocalRename>()); 102e5dd7070Spatrick Actions.push_back(std::make_unique<ExtractRefactoring>()); 103e5dd7070Spatrick 104e5dd7070Spatrick return Actions; 105e5dd7070Spatrick } 106e5dd7070Spatrick createActiveActionRules()107e5dd7070SpatrickRefactoringActionRules RefactoringAction::createActiveActionRules() { 108e5dd7070Spatrick // FIXME: Filter out rules that are not supported by a particular client. 109e5dd7070Spatrick return createActionRules(); 110e5dd7070Spatrick } 111e5dd7070Spatrick 112e5dd7070Spatrick } // end namespace tooling 113e5dd7070Spatrick } // end namespace clang 114