1 //===--- RefactoringActions.cpp - Constructs refactoring actions ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Tooling/Refactoring/Extract/Extract.h"
10 #include "clang/Tooling/Refactoring/RefactoringAction.h"
11 #include "clang/Tooling/Refactoring/RefactoringOptions.h"
12 #include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
19 class DeclNameOption final : public OptionalRefactoringOption<std::string> {
21 StringRef getName() const { return "name"; }
22 StringRef getDescription() const {
23 return "Name of the extracted declaration";
27 // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
29 class ExtractRefactoring final : public RefactoringAction {
31 StringRef getCommand() const override { return "extract"; }
33 StringRef getDescription() const override {
34 return "(WIP action; use with caution!) Extracts code into a new function";
37 /// Returns a set of refactoring actions rules that are defined by this
39 RefactoringActionRules createActionRules() const override {
40 RefactoringActionRules Rules;
41 Rules.push_back(createRefactoringActionRule<ExtractFunction>(
42 CodeRangeASTSelectionRequirement(),
43 OptionRequirement<DeclNameOption>()));
48 class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> {
50 StringRef getName() const override { return "old-qualified-name"; }
51 StringRef getDescription() const override {
52 return "The old qualified name to be renamed";
56 class NewQualifiedNameOption : public RequiredRefactoringOption<std::string> {
58 StringRef getName() const override { return "new-qualified-name"; }
59 StringRef getDescription() const override {
60 return "The new qualified name to change the symbol to";
64 class NewNameOption : public RequiredRefactoringOption<std::string> {
66 StringRef getName() const override { return "new-name"; }
67 StringRef getDescription() const override {
68 return "The new name to change the symbol to";
72 // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
74 class LocalRename final : public RefactoringAction {
76 StringRef getCommand() const override { return "local-rename"; }
78 StringRef getDescription() const override {
79 return "Finds and renames symbols in code with no indexer support";
82 /// Returns a set of refactoring actions rules that are defined by this
84 RefactoringActionRules createActionRules() const override {
85 RefactoringActionRules Rules;
86 Rules.push_back(createRefactoringActionRule<RenameOccurrences>(
87 SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>()));
88 // FIXME: Use NewNameOption.
89 Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>(
90 OptionRequirement<OldQualifiedNameOption>(),
91 OptionRequirement<NewQualifiedNameOption>()));
96 } // end anonymous namespace
98 std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() {
99 std::vector<std::unique_ptr<RefactoringAction>> Actions;
101 Actions.push_back(std::make_unique<LocalRename>());
102 Actions.push_back(std::make_unique<ExtractRefactoring>());
107 RefactoringActionRules RefactoringAction::createActiveActionRules() {
108 // FIXME: Filter out rules that are not supported by a particular client.
109 return createActionRules();
112 } // end namespace tooling
113 } // end namespace clang