RefactoringActions.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===--- RefactoringActions.cpp - Constructs refactoring actions ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  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. namespace clang {
  14. namespace tooling {
  15. namespace {
  16. class DeclNameOption final : public OptionalRefactoringOption<std::string> {
  17. public:
  18. StringRef getName() const { return "name"; }
  19. StringRef getDescription() const {
  20. return "Name of the extracted declaration";
  21. }
  22. };
  23. // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
  24. // rules.
  25. class ExtractRefactoring final : public RefactoringAction {
  26. public:
  27. StringRef getCommand() const override { return "extract"; }
  28. StringRef getDescription() const override {
  29. return "(WIP action; use with caution!) Extracts code into a new function";
  30. }
  31. /// Returns a set of refactoring actions rules that are defined by this
  32. /// action.
  33. RefactoringActionRules createActionRules() const override {
  34. RefactoringActionRules Rules;
  35. Rules.push_back(createRefactoringActionRule<ExtractFunction>(
  36. CodeRangeASTSelectionRequirement(),
  37. OptionRequirement<DeclNameOption>()));
  38. return Rules;
  39. }
  40. };
  41. class OldQualifiedNameOption : public RequiredRefactoringOption<std::string> {
  42. public:
  43. StringRef getName() const override { return "old-qualified-name"; }
  44. StringRef getDescription() const override {
  45. return "The old qualified name to be renamed";
  46. }
  47. };
  48. class NewQualifiedNameOption : public RequiredRefactoringOption<std::string> {
  49. public:
  50. StringRef getName() const override { return "new-qualified-name"; }
  51. StringRef getDescription() const override {
  52. return "The new qualified name to change the symbol to";
  53. }
  54. };
  55. class NewNameOption : public RequiredRefactoringOption<std::string> {
  56. public:
  57. StringRef getName() const override { return "new-name"; }
  58. StringRef getDescription() const override {
  59. return "The new name to change the symbol to";
  60. }
  61. };
  62. // FIXME: Rewrite the Actions to avoid duplication of descriptions/names with
  63. // rules.
  64. class LocalRename final : public RefactoringAction {
  65. public:
  66. StringRef getCommand() const override { return "local-rename"; }
  67. StringRef getDescription() const override {
  68. return "Finds and renames symbols in code with no indexer support";
  69. }
  70. /// Returns a set of refactoring actions rules that are defined by this
  71. /// action.
  72. RefactoringActionRules createActionRules() const override {
  73. RefactoringActionRules Rules;
  74. Rules.push_back(createRefactoringActionRule<RenameOccurrences>(
  75. SourceRangeSelectionRequirement(), OptionRequirement<NewNameOption>()));
  76. // FIXME: Use NewNameOption.
  77. Rules.push_back(createRefactoringActionRule<QualifiedRenameRule>(
  78. OptionRequirement<OldQualifiedNameOption>(),
  79. OptionRequirement<NewQualifiedNameOption>()));
  80. return Rules;
  81. }
  82. };
  83. } // end anonymous namespace
  84. std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() {
  85. std::vector<std::unique_ptr<RefactoringAction>> Actions;
  86. Actions.push_back(llvm::make_unique<LocalRename>());
  87. Actions.push_back(llvm::make_unique<ExtractRefactoring>());
  88. return Actions;
  89. }
  90. RefactoringActionRules RefactoringAction::createActiveActionRules() {
  91. // FIXME: Filter out rules that are not supported by a particular client.
  92. return createActionRules();
  93. }
  94. } // end namespace tooling
  95. } // end namespace clang