RefactoringActions.cpp 3.7 KB

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