10b57cec5SDimitry Andric //===--- RefactoringActions.cpp - Constructs refactoring actions ----------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "clang/Tooling/Refactoring/Extract/Extract.h" 100b57cec5SDimitry Andric #include "clang/Tooling/Refactoring/RefactoringAction.h" 110b57cec5SDimitry Andric #include "clang/Tooling/Refactoring/RefactoringOptions.h" 120b57cec5SDimitry Andric #include "clang/Tooling/Refactoring/Rename/RenamingAction.h" 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric namespace clang { 150b57cec5SDimitry Andric namespace tooling { 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric namespace { 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric class DeclNameOption final : public OptionalRefactoringOption<std::string> { 200b57cec5SDimitry Andric public: getName() const210b57cec5SDimitry Andric StringRef getName() const override { return "name"; } getDescription() const220b57cec5SDimitry Andric StringRef getDescription() const override { 230b57cec5SDimitry Andric return "Name of the extracted declaration"; 240b57cec5SDimitry Andric } 250b57cec5SDimitry Andric }; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with 280b57cec5SDimitry Andric // rules. 290b57cec5SDimitry Andric class ExtractRefactoring final : public RefactoringAction { 300b57cec5SDimitry Andric public: getCommand() const310b57cec5SDimitry Andric StringRef getCommand() const override { return "extract"; } 320b57cec5SDimitry Andric getDescription() const330b57cec5SDimitry Andric StringRef getDescription() const override { 340b57cec5SDimitry Andric return "(WIP action; use with caution!) Extracts code into a new function"; 350b57cec5SDimitry Andric } 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric /// Returns a set of refactoring actions rules that are defined by this 380b57cec5SDimitry Andric /// action. createActionRules() const390b57cec5SDimitry Andric RefactoringActionRules createActionRules() const override { 400b57cec5SDimitry Andric RefactoringActionRules Rules; 410b57cec5SDimitry Andric Rules.push_back(createRefactoringActionRule<ExtractFunction>( 420b57cec5SDimitry Andric CodeRangeASTSelectionRequirement(), 430b57cec5SDimitry Andric OptionRequirement<DeclNameOption>())); 440b57cec5SDimitry Andric return Rules; 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric }; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> { 490b57cec5SDimitry Andric public: getName() const500b57cec5SDimitry Andric StringRef getName() const override { return "old-qualified-name"; } getDescription() const510b57cec5SDimitry Andric StringRef getDescription() const override { 520b57cec5SDimitry Andric return "The old qualified name to be renamed"; 530b57cec5SDimitry Andric } 540b57cec5SDimitry Andric }; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric class NewQualifiedNameOption : public RequiredRefactoringOption<std::string> { 570b57cec5SDimitry Andric public: getName() const580b57cec5SDimitry Andric StringRef getName() const override { return "new-qualified-name"; } getDescription() const590b57cec5SDimitry Andric StringRef getDescription() const override { 600b57cec5SDimitry Andric return "The new qualified name to change the symbol to"; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric }; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric class NewNameOption : public RequiredRefactoringOption<std::string> { 650b57cec5SDimitry Andric public: getName() const660b57cec5SDimitry Andric StringRef getName() const override { return "new-name"; } getDescription() const670b57cec5SDimitry Andric StringRef getDescription() const override { 680b57cec5SDimitry Andric return "The new name to change the symbol to"; 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric }; 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with 730b57cec5SDimitry Andric // rules. 740b57cec5SDimitry Andric class LocalRename final : public RefactoringAction { 750b57cec5SDimitry Andric public: getCommand() const760b57cec5SDimitry Andric StringRef getCommand() const override { return "local-rename"; } 770b57cec5SDimitry Andric getDescription() const780b57cec5SDimitry Andric StringRef getDescription() const override { 790b57cec5SDimitry Andric return "Finds and renames symbols in code with no indexer support"; 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric /// Returns a set of refactoring actions rules that are defined by this 830b57cec5SDimitry Andric /// action. createActionRules() const840b57cec5SDimitry Andric RefactoringActionRules createActionRules() const override { 850b57cec5SDimitry Andric RefactoringActionRules Rules; 860b57cec5SDimitry Andric Rules.push_back(createRefactoringActionRule<RenameOccurrences>( 870b57cec5SDimitry Andric SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>())); 880b57cec5SDimitry Andric // FIXME: Use NewNameOption. 890b57cec5SDimitry Andric Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>( 900b57cec5SDimitry Andric OptionRequirement<OldQualifiedNameOption>(), 910b57cec5SDimitry Andric OptionRequirement<NewQualifiedNameOption>())); 920b57cec5SDimitry Andric return Rules; 930b57cec5SDimitry Andric } 940b57cec5SDimitry Andric }; 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric } // end anonymous namespace 970b57cec5SDimitry Andric createRefactoringActions()980b57cec5SDimitry Andricstd::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() { 990b57cec5SDimitry Andric std::vector<std::unique_ptr<RefactoringAction>> Actions; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric Actions.push_back(std::make_unique<LocalRename>()); 1020b57cec5SDimitry Andric Actions.push_back(std::make_unique<ExtractRefactoring>()); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric return Actions; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric createActiveActionRules()1070b57cec5SDimitry AndricRefactoringActionRules RefactoringAction::createActiveActionRules() { 1080b57cec5SDimitry Andric // FIXME: Filter out rules that are not supported by a particular client. 1090b57cec5SDimitry Andric return createActionRules(); 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric } // end namespace tooling 1130b57cec5SDimitry Andric } // end namespace clang 114