1 //===--- RefactoringActionRules.h - Clang refactoring library -------------===//
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 #ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULES_H
10 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULES_H
12 #include "clang/Tooling/Refactoring/RefactoringActionRule.h"
13 #include "clang/Tooling/Refactoring/RefactoringActionRulesInternal.h"
18 /// Creates a new refactoring action rule that constructs and invokes the
19 /// \c RuleType rule when all of the requirements are satisfied.
21 /// This function takes in a list of values whose type derives from
22 /// \c RefactoringActionRuleRequirement. These values describe the initiation
23 /// requirements that have to be satisfied by the refactoring engine before
24 /// the provided action rule can be constructed and invoked. The engine
25 /// verifies that the requirements are satisfied by evaluating them (using the
26 /// 'evaluate' member function) and checking that the results don't contain
27 /// any errors. Once all requirements are satisfied, the provided refactoring
28 /// rule is constructed by passing in the values returned by the requirements'
29 /// evaluate functions as arguments to the constructor. The rule is then invoked
30 /// immediately after construction.
32 /// The separation of requirements, their evaluation and the invocation of the
33 /// refactoring action rule allows the refactoring clients to:
34 /// - Disable refactoring action rules whose requirements are not supported.
35 /// - Gather the set of options and define a command-line / visual interface
36 /// that allows users to input these options without ever invoking the
38 template <typename RuleType, typename... RequirementTypes>
39 std::unique_ptr<RefactoringActionRule>
40 createRefactoringActionRule(const RequirementTypes &... Requirements);
42 /// A set of refactoring action rules that should have unique initiation
44 using RefactoringActionRules =
45 std::vector<std::unique_ptr<RefactoringActionRule>>;
47 /// A type of refactoring action rule that produces source replacements in the
48 /// form of atomic changes.
50 /// This action rule is typically used for local refactorings that replace
51 /// source in a single AST unit.
52 class SourceChangeRefactoringRule : public RefactoringActionRuleBase {
54 void invoke(RefactoringResultConsumer &Consumer,
55 RefactoringRuleContext &Context) final override {
56 Expected<AtomicChanges> Changes = createSourceReplacements(Context);
58 Consumer.handleError(Changes.takeError());
60 Consumer.handle(std::move(*Changes));
64 virtual Expected<AtomicChanges>
65 createSourceReplacements(RefactoringRuleContext &Context) = 0;
68 /// A type of refactoring action rule that finds a set of symbol occurrences
69 /// that reference a particular symbol.
71 /// This action rule is typically used for an interactive rename that allows
72 /// users to specify the new name and the set of selected occurrences during
74 class FindSymbolOccurrencesRefactoringRule : public RefactoringActionRuleBase {
76 void invoke(RefactoringResultConsumer &Consumer,
77 RefactoringRuleContext &Context) final override {
78 Expected<SymbolOccurrences> Occurrences = findSymbolOccurrences(Context);
80 Consumer.handleError(Occurrences.takeError());
82 Consumer.handle(std::move(*Occurrences));
86 virtual Expected<SymbolOccurrences>
87 findSymbolOccurrences(RefactoringRuleContext &Context) = 0;
90 } // end namespace tooling
91 } // end namespace clang
93 #endif // LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULES_H