|
| 1 | +//===--- Refactoring.h - APIs for refactoring --------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_IDE_REFACTORING_H |
| 14 | +#define SWIFT_IDE_REFACTORING_H |
| 15 | + |
| 16 | +#include "llvm/ADT/StringRef.h" |
| 17 | +#include "swift/Basic/LLVM.h" |
| 18 | +#include "swift/AST/DiagnosticConsumer.h" |
| 19 | +#include "swift/IDE/Utils.h" |
| 20 | + |
| 21 | +namespace swift { |
| 22 | + class ModuleDecl; |
| 23 | + class SourceLoc; |
| 24 | + class SourceManager; |
| 25 | + |
| 26 | +namespace ide { |
| 27 | + struct SemaToken; |
| 28 | + |
| 29 | +enum class RefactoringKind : int8_t { |
| 30 | + None, |
| 31 | +#define REFACTORING(KIND, NAME, ID) KIND, |
| 32 | +#include "RefactoringKinds.def" |
| 33 | +}; |
| 34 | + |
| 35 | +struct RangeConfig { |
| 36 | + unsigned BufferId; |
| 37 | + unsigned Line; |
| 38 | + unsigned Column; |
| 39 | + unsigned Length; |
| 40 | + SourceLoc getStart(SourceManager &SM); |
| 41 | + SourceLoc getEnd(SourceManager &SM); |
| 42 | +}; |
| 43 | + |
| 44 | +enum class NameUsage { |
| 45 | + Unknown, |
| 46 | + Reference, |
| 47 | + Definition, |
| 48 | + Call |
| 49 | +}; |
| 50 | + |
| 51 | +struct RenameLoc { |
| 52 | + unsigned Line; |
| 53 | + unsigned Column; |
| 54 | + NameUsage Usage; |
| 55 | + StringRef OldName; |
| 56 | + StringRef NewName; // May be empty. |
| 57 | + const bool IsFunctionLike; |
| 58 | + const bool IsNonProtocolType; |
| 59 | +}; |
| 60 | + |
| 61 | +struct RefactoringOptions { |
| 62 | + RefactoringKind Kind; |
| 63 | + RangeConfig Range; |
| 64 | + std::string PreferredName; |
| 65 | + RefactoringOptions(RefactoringKind Kind) : Kind(Kind) {} |
| 66 | +}; |
| 67 | + |
| 68 | +// TODO: Merge with NoteRegion – range needs to change to start/end line/column |
| 69 | +struct RenameRangeDetail { |
| 70 | + CharSourceRange Range; |
| 71 | + RefactoringRangeKind RangeKind; |
| 72 | + Optional<unsigned> Index; |
| 73 | +}; |
| 74 | + |
| 75 | +enum class RenameAvailableKind { |
| 76 | + Available, |
| 77 | + Unavailable_system_symbol, |
| 78 | + Unavailable_has_no_location, |
| 79 | + Unavailable_has_no_name, |
| 80 | + Unavailable_has_no_accessibility, |
| 81 | + Unavailable_decl_from_clang, |
| 82 | +}; |
| 83 | + |
| 84 | +struct RenameAvailabiliyInfo { |
| 85 | + RefactoringKind Kind; |
| 86 | + RenameAvailableKind AvailableKind; |
| 87 | + RenameAvailabiliyInfo(RefactoringKind Kind, RenameAvailableKind AvailableKind) : |
| 88 | + Kind(Kind), AvailableKind(AvailableKind) {} |
| 89 | + RenameAvailabiliyInfo(RefactoringKind Kind) : |
| 90 | + RenameAvailabiliyInfo(Kind, RenameAvailableKind::Available) {} |
| 91 | +}; |
| 92 | + |
| 93 | +class FindRenameRangesConsumer { |
| 94 | +public: |
| 95 | + virtual void accept(SourceManager &SM, RegionType RegionType, |
| 96 | + ArrayRef<RenameRangeDetail> Ranges) = 0; |
| 97 | + virtual ~FindRenameRangesConsumer() = default; |
| 98 | +}; |
| 99 | + |
| 100 | +class FindRenameRangesAnnotatingConsumer : public FindRenameRangesConsumer { |
| 101 | + struct Implementation; |
| 102 | + Implementation &Impl; |
| 103 | + |
| 104 | +public: |
| 105 | + FindRenameRangesAnnotatingConsumer(SourceManager &SM, unsigned BufferId, |
| 106 | + llvm::raw_ostream &OS); |
| 107 | + ~FindRenameRangesAnnotatingConsumer(); |
| 108 | + void accept(SourceManager &SM, RegionType RegionType, |
| 109 | + ArrayRef<RenameRangeDetail> Ranges) override; |
| 110 | +}; |
| 111 | + |
| 112 | +StringRef getDescriptiveRefactoringKindName(RefactoringKind Kind); |
| 113 | + |
| 114 | +StringRef getDescriptiveRenameUnavailableReason(RenameAvailableKind Kind); |
| 115 | + |
| 116 | +bool refactorSwiftModule(ModuleDecl *M, RefactoringOptions Opts, |
| 117 | + SourceEditConsumer &EditConsumer, |
| 118 | + DiagnosticConsumer &DiagConsumer); |
| 119 | + |
| 120 | +int syntacticRename(SourceFile *SF, llvm::ArrayRef<RenameLoc> RenameLocs, |
| 121 | + SourceEditConsumer &EditConsumer, |
| 122 | + DiagnosticConsumer &DiagConsumer); |
| 123 | + |
| 124 | +int findSyntacticRenameRanges(SourceFile *SF, |
| 125 | + llvm::ArrayRef<RenameLoc> RenameLocs, |
| 126 | + FindRenameRangesConsumer &RenameConsumer, |
| 127 | + DiagnosticConsumer &DiagConsumer); |
| 128 | + |
| 129 | +int findLocalRenameRanges(SourceFile *SF, RangeConfig Range, |
| 130 | + FindRenameRangesConsumer &RenameConsumer, |
| 131 | + DiagnosticConsumer &DiagConsumer); |
| 132 | + |
| 133 | +ArrayRef<RefactoringKind> |
| 134 | +collectAvailableRefactorings(SourceFile *SF, RangeConfig Range, |
| 135 | + bool &RangeStartMayNeedRename, |
| 136 | + std::vector<RefactoringKind> &Scratch, |
| 137 | + llvm::ArrayRef<DiagnosticConsumer*> DiagConsumers); |
| 138 | + |
| 139 | +ArrayRef<RefactoringKind> |
| 140 | +collectAvailableRefactorings(SourceFile *SF, SemaToken Tok, |
| 141 | + std::vector<RefactoringKind> &Scratch, |
| 142 | + bool ExcludeRename); |
| 143 | + |
| 144 | +ArrayRef<RenameAvailabiliyInfo> |
| 145 | +collectRenameAvailabilityInfo(const ValueDecl *VD, |
| 146 | + std::vector<RenameAvailabiliyInfo> &Scratch); |
| 147 | + |
| 148 | +} // namespace ide |
| 149 | +} // namespace swift |
| 150 | + |
| 151 | +#endif // SWIFT_IDE_REFACTORING_H |
0 commit comments