-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathRefactoring.cpp
223 lines (196 loc) · 8.68 KB
/
Refactoring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Refactoring/Refactoring.h"
#include "RefactoringActions.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/SourceManager.h"
#include "swift/IDE/IDERequests.h"
#include "swift/Parse/Lexer.h"
using namespace swift;
using namespace swift::ide;
using namespace swift::refactoring;
StringRef getDefaultPreferredName(RefactoringKind Kind) {
switch(Kind) {
case RefactoringKind::None:
llvm_unreachable("Should be a valid refactoring kind");
case RefactoringKind::GlobalRename:
case RefactoringKind::LocalRename:
return "newName";
case RefactoringKind::ExtractExpr:
case RefactoringKind::ExtractRepeatedExpr:
return "extractedExpr";
case RefactoringKind::ExtractFunction:
return "extractedFunc";
default:
return "";
}
}
static SmallVector<RefactorAvailabilityInfo, 0>
collectRefactoringsAtCursor(SourceFile *SF, unsigned Line, unsigned Column,
ArrayRef<DiagnosticConsumer *> DiagConsumers) {
// Prepare the tool box.
ASTContext &Ctx = SF->getASTContext();
SourceManager &SM = Ctx.SourceMgr;
DiagnosticEngine DiagEngine(SM);
std::for_each(DiagConsumers.begin(), DiagConsumers.end(),
[&](DiagnosticConsumer *Con) { DiagEngine.addConsumer(*Con); });
SourceLoc Loc = SM.getLocForLineCol(SF->getBufferID(), Line, Column);
if (Loc.isInvalid())
return {};
ResolvedCursorInfoPtr Tok =
evaluateOrDefault(SF->getASTContext().evaluator,
CursorInfoRequest{CursorInfoOwner(
SF, Lexer::getLocForStartOfToken(SM, Loc))},
new ResolvedCursorInfo());
return collectRefactorings(Tok, /*ExcludeRename=*/false);
}
static bool collectRangeStartRefactorings(const ResolvedRangeInfo &Info) {
switch (Info.Kind) {
case RangeKind::SingleExpression:
case RangeKind::SingleStatement:
case RangeKind::SingleDecl:
case RangeKind::PartOfExpression:
return true;
case RangeKind::MultiStatement:
case RangeKind::MultiTypeMemberDecl:
case RangeKind::Invalid:
return false;
}
}
StringRef swift::ide::
getDescriptiveRefactoringKindName(RefactoringKind Kind) {
switch(Kind) {
case RefactoringKind::None:
llvm_unreachable("Should be a valid refactoring kind");
#define REFACTORING(KIND, NAME, ID) case RefactoringKind::KIND: return NAME;
#include "swift/Refactoring/RefactoringKinds.def"
}
llvm_unreachable("unhandled kind");
}
StringRef swift::ide::getDescriptiveRenameUnavailableReason(
RefactorAvailableKind Kind) {
switch(Kind) {
case RefactorAvailableKind::Available:
return "";
case RefactorAvailableKind::Unavailable_system_symbol:
return "symbol from system module cannot be renamed";
case RefactorAvailableKind::Unavailable_has_no_location:
return "symbol without a declaration location cannot be renamed";
case RefactorAvailableKind::Unavailable_has_no_name:
return "cannot find the name of the symbol";
case RefactorAvailableKind::Unavailable_has_no_accessibility:
return "cannot decide the accessibility of the symbol";
case RefactorAvailableKind::Unavailable_decl_from_clang:
return "cannot rename a Clang symbol from its Swift reference";
case RefactorAvailableKind::Unavailable_decl_in_macro:
return "cannot rename a symbol declared in a macro";
}
llvm_unreachable("unhandled kind");
}
SourceLoc swift::ide::RangeConfig::getStart(SourceManager &SM) {
return SM.getLocForLineCol(BufferID, Line, Column);
}
SourceLoc swift::ide::RangeConfig::getEnd(SourceManager &SM) {
return getStart(SM).getAdvancedLoc(Length);
}
SmallVector<RefactorAvailabilityInfo, 0>
swift::ide::collectRefactorings(ResolvedCursorInfoPtr CursorInfo,
bool ExcludeRename) {
SmallVector<RefactorAvailabilityInfo, 0> Infos;
DiagnosticEngine DiagEngine(
CursorInfo->getSourceFile()->getASTContext().SourceMgr);
// Only macro expansion is available within generated buffers
if (CursorInfo->getSourceFile()->Kind == SourceFileKind::MacroExpansion) {
if (RefactoringActionInlineMacro::isApplicable(CursorInfo, DiagEngine)) {
Infos.emplace_back(RefactoringKind::InlineMacro,
RefactorAvailableKind::Available);
}
return Infos;
}
if (!ExcludeRename) {
if (auto Info = getRenameInfo(CursorInfo)) {
Infos.push_back(std::move(Info->Availability));
}
}
#define CURSOR_REFACTORING(KIND, NAME, ID) \
if (RefactoringKind::KIND != RefactoringKind::LocalRename && \
RefactoringAction##KIND::isApplicable(CursorInfo, DiagEngine)) \
Infos.emplace_back(RefactoringKind::KIND, RefactorAvailableKind::Available);
#include "swift/Refactoring/RefactoringKinds.def"
return Infos;
}
SmallVector<RefactorAvailabilityInfo, 0>
swift::ide::collectRefactorings(SourceFile *SF, RangeConfig Range,
bool &CollectRangeStartRefactorings,
ArrayRef<DiagnosticConsumer *> DiagConsumers) {
if (Range.Length == 0)
return collectRefactoringsAtCursor(SF, Range.Line, Range.Column,
DiagConsumers);
// No refactorings are available within generated buffers
if (SF->Kind == SourceFileKind::MacroExpansion ||
SF->Kind == SourceFileKind::DefaultArgument)
return {};
// Prepare the tool box.
ASTContext &Ctx = SF->getASTContext();
SourceManager &SM = Ctx.SourceMgr;
DiagnosticEngine DiagEngine(SM);
std::for_each(DiagConsumers.begin(), DiagConsumers.end(),
[&](DiagnosticConsumer *Con) { DiagEngine.addConsumer(*Con); });
ResolvedRangeInfo Result = evaluateOrDefault(SF->getASTContext().evaluator,
RangeInfoRequest(RangeInfoOwner({SF,
Range.getStart(SF->getASTContext().SourceMgr),
Range.getEnd(SF->getASTContext().SourceMgr)})),
ResolvedRangeInfo());
bool enableInternalRefactoring = getenv("SWIFT_ENABLE_INTERNAL_REFACTORING_ACTIONS");
SmallVector<RefactorAvailabilityInfo, 0> Infos;
#define RANGE_REFACTORING(KIND, NAME, ID) \
if (RefactoringAction##KIND::isApplicable(Result, DiagEngine)) \
Infos.emplace_back(RefactoringKind::KIND, RefactorAvailableKind::Available);
#define INTERNAL_RANGE_REFACTORING(KIND, NAME, ID) \
if (enableInternalRefactoring) \
RANGE_REFACTORING(KIND, NAME, ID)
#include "swift/Refactoring/RefactoringKinds.def"
CollectRangeStartRefactorings = collectRangeStartRefactorings(Result);
return Infos;
}
bool swift::ide::
refactorSwiftModule(ModuleDecl *M, RefactoringOptions Opts,
SourceEditConsumer &EditConsumer,
DiagnosticConsumer &DiagConsumer) {
assert(Opts.Kind != RefactoringKind::None && "should have a refactoring kind.");
// Use the default name if not specified.
if (Opts.PreferredName.empty()) {
Opts.PreferredName = getDefaultPreferredName(Opts.Kind).str();
}
switch (Opts.Kind) {
#define SEMANTIC_REFACTORING(KIND, NAME, ID) \
case RefactoringKind::KIND: { \
RefactoringAction##KIND Action(M, Opts, EditConsumer, DiagConsumer); \
if (RefactoringKind::KIND == RefactoringKind::LocalRename || \
RefactoringKind::KIND == RefactoringKind::ExpandMacro || \
Action.isApplicable()) \
return Action.performChange(); \
return true; \
}
#include "swift/Refactoring/RefactoringKinds.def"
case RefactoringKind::LocalRename:
case RefactoringKind::GlobalRename:
case RefactoringKind::FindGlobalRenameRanges:
case RefactoringKind::FindLocalRenameRanges:
llvm_unreachable("not a valid refactoring kind");
case RefactoringKind::None:
llvm_unreachable("should not enter here.");
}
llvm_unreachable("unhandled kind");
}