1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
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" |
13 | |
14 | namespace clang { |
15 | namespace tooling { |
16 | |
17 | namespace { |
18 | |
19 | class DeclNameOption final : public OptionalRefactoringOption<std::string> { |
20 | public: |
21 | StringRef getName() const { return "name"; } |
22 | StringRef getDescription() const { |
23 | return "Name of the extracted declaration"; |
24 | } |
25 | }; |
26 | |
27 | |
28 | |
29 | class final : public RefactoringAction { |
30 | public: |
31 | StringRef getCommand() const override { return "extract"; } |
32 | |
33 | StringRef () const override { |
34 | return "(WIP action; use with caution!) Extracts code into a new function"; |
35 | } |
36 | |
37 | |
38 | |
39 | RefactoringActionRules () const override { |
40 | RefactoringActionRules Rules; |
41 | Rules.push_back(createRefactoringActionRule<ExtractFunction>( |
42 | CodeRangeASTSelectionRequirement(), |
43 | OptionRequirement<DeclNameOption>())); |
44 | return Rules; |
45 | } |
46 | }; |
47 | |
48 | class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> { |
49 | public: |
50 | StringRef getName() const override { return "old-qualified-name"; } |
51 | StringRef getDescription() const override { |
52 | return "The old qualified name to be renamed"; |
53 | } |
54 | }; |
55 | |
56 | class NewQualifiedNameOption : public RequiredRefactoringOption<std::string> { |
57 | public: |
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"; |
61 | } |
62 | }; |
63 | |
64 | class NewNameOption : public RequiredRefactoringOption<std::string> { |
65 | public: |
66 | StringRef getName() const override { return "new-name"; } |
67 | StringRef getDescription() const override { |
68 | return "The new name to change the symbol to"; |
69 | } |
70 | }; |
71 | |
72 | |
73 | |
74 | class LocalRename final : public RefactoringAction { |
75 | public: |
76 | StringRef getCommand() const override { return "local-rename"; } |
77 | |
78 | StringRef getDescription() const override { |
79 | return "Finds and renames symbols in code with no indexer support"; |
80 | } |
81 | |
82 | |
83 | |
84 | RefactoringActionRules createActionRules() const override { |
85 | RefactoringActionRules Rules; |
86 | Rules.push_back(createRefactoringActionRule<RenameOccurrences>( |
87 | SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>())); |
88 | |
89 | Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>( |
90 | OptionRequirement<OldQualifiedNameOption>(), |
91 | OptionRequirement<NewQualifiedNameOption>())); |
92 | return Rules; |
93 | } |
94 | }; |
95 | |
96 | } |
97 | |
98 | std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() { |
99 | std::vector<std::unique_ptr<RefactoringAction>> Actions; |
100 | |
101 | Actions.push_back(llvm::make_unique<LocalRename>()); |
102 | Actions.push_back(llvm::make_unique<ExtractRefactoring>()); |
103 | |
104 | return Actions; |
105 | } |
106 | |
107 | RefactoringActionRules RefactoringAction::createActiveActionRules() { |
108 | |
109 | return createActionRules(); |
110 | } |
111 | |
112 | } |
113 | } |
114 | |