Skip to content

Commit d5b29f2

Browse files
committed
Split definition of refactoring actions to its own header file
1 parent 7c72e7d commit d5b29f2

File tree

2 files changed

+135
-109
lines changed

2 files changed

+135
-109
lines changed

lib/Refactoring/Refactoring.cpp

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- Refactoring.cpp ---------------------------------------------------===//
1+
//===----------------------------------------------------------------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Refactoring/Refactoring.h"
14+
#include "RefactoringActions.h"
1415
#include "swift/AST/ASTContext.h"
1516
#include "swift/AST/ASTPrinter.h"
1617
#include "swift/AST/Decl.h"
@@ -41,8 +42,7 @@
4142
using namespace swift;
4243
using namespace swift::ide;
4344
using namespace swift::index;
44-
45-
namespace {
45+
using namespace swift::refactoring;
4646

4747
class ContextFinder : public SourceEntityWalker {
4848
SourceFile &SF;
@@ -855,110 +855,6 @@ RenameRangeCollector::indexSymbolToRenameLoc(const index::IndexSymbol &symbol,
855855
isFunctionLike, isNonProtocolType};
856856
}
857857

858-
/// Get the source file that corresponds to the given buffer.
859-
SourceFile *getContainingFile(ModuleDecl *M, RangeConfig Range) {
860-
auto &SM = M->getASTContext().SourceMgr;
861-
// TODO: We should add an ID -> SourceFile mapping.
862-
return M->getSourceFileContainingLocation(
863-
SM.getRangeForBuffer(Range.BufferID).getStart());
864-
}
865-
866-
class RefactoringAction {
867-
protected:
868-
ModuleDecl *MD;
869-
SourceFile *TheFile;
870-
SourceEditConsumer &EditConsumer;
871-
ASTContext &Ctx;
872-
SourceManager &SM;
873-
DiagnosticEngine DiagEngine;
874-
SourceLoc StartLoc;
875-
StringRef PreferredName;
876-
public:
877-
RefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
878-
SourceEditConsumer &EditConsumer,
879-
DiagnosticConsumer &DiagConsumer);
880-
virtual ~RefactoringAction() = default;
881-
virtual bool performChange() = 0;
882-
};
883-
884-
RefactoringAction::
885-
RefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
886-
SourceEditConsumer &EditConsumer,
887-
DiagnosticConsumer &DiagConsumer): MD(MD),
888-
TheFile(getContainingFile(MD, Opts.Range)),
889-
EditConsumer(EditConsumer), Ctx(MD->getASTContext()),
890-
SM(MD->getASTContext().SourceMgr), DiagEngine(SM),
891-
StartLoc(Lexer::getLocForStartOfToken(SM, Opts.Range.getStart(SM))),
892-
PreferredName(Opts.PreferredName) {
893-
DiagEngine.addConsumer(DiagConsumer);
894-
}
895-
896-
/// Different from RangeBasedRefactoringAction, TokenBasedRefactoringAction takes
897-
/// the input of a given token, e.g., a name or an "if" key word. Contextual
898-
/// refactoring kinds can suggest applicable refactorings on that token, e.g.
899-
/// rename or reverse if statement.
900-
class TokenBasedRefactoringAction : public RefactoringAction {
901-
protected:
902-
ResolvedCursorInfoPtr CursorInfo;
903-
904-
public:
905-
TokenBasedRefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
906-
SourceEditConsumer &EditConsumer,
907-
DiagnosticConsumer &DiagConsumer) :
908-
RefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {
909-
// Resolve the sema token and save it for later use.
910-
CursorInfo =
911-
evaluateOrDefault(TheFile->getASTContext().evaluator,
912-
CursorInfoRequest{CursorInfoOwner(TheFile, StartLoc)},
913-
new ResolvedCursorInfo());
914-
}
915-
};
916-
917-
#define CURSOR_REFACTORING(KIND, NAME, ID) \
918-
class RefactoringAction##KIND : public TokenBasedRefactoringAction { \
919-
public: \
920-
RefactoringAction##KIND(ModuleDecl *MD, RefactoringOptions &Opts, \
921-
SourceEditConsumer &EditConsumer, \
922-
DiagnosticConsumer &DiagConsumer) \
923-
: TokenBasedRefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {} \
924-
bool performChange() override; \
925-
static bool isApplicable(ResolvedCursorInfoPtr Info, \
926-
DiagnosticEngine &Diag); \
927-
bool isApplicable() { \
928-
return RefactoringAction##KIND::isApplicable(CursorInfo, DiagEngine); \
929-
} \
930-
};
931-
#include "swift/Refactoring/RefactoringKinds.def"
932-
933-
class RangeBasedRefactoringAction : public RefactoringAction {
934-
protected:
935-
ResolvedRangeInfo RangeInfo;
936-
public:
937-
RangeBasedRefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
938-
SourceEditConsumer &EditConsumer,
939-
DiagnosticConsumer &DiagConsumer) :
940-
RefactoringAction(MD, Opts, EditConsumer, DiagConsumer),
941-
RangeInfo(evaluateOrDefault(MD->getASTContext().evaluator,
942-
RangeInfoRequest(RangeInfoOwner(TheFile, Opts.Range.getStart(SM), Opts.Range.getEnd(SM))),
943-
ResolvedRangeInfo())) {}
944-
};
945-
946-
#define RANGE_REFACTORING(KIND, NAME, ID) \
947-
class RefactoringAction##KIND: public RangeBasedRefactoringAction { \
948-
public: \
949-
RefactoringAction##KIND(ModuleDecl *MD, RefactoringOptions &Opts, \
950-
SourceEditConsumer &EditConsumer, \
951-
DiagnosticConsumer &DiagConsumer) : \
952-
RangeBasedRefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {} \
953-
bool performChange() override; \
954-
static bool isApplicable(const ResolvedRangeInfo &Info, \
955-
DiagnosticEngine &Diag); \
956-
bool isApplicable() { \
957-
return RefactoringAction##KIND::isApplicable(RangeInfo, DiagEngine) ; \
958-
} \
959-
};
960-
#include "swift/Refactoring/RefactoringKinds.def"
961-
962858
bool RefactoringActionLocalRename::isApplicable(
963859
ResolvedCursorInfoPtr CursorInfo, DiagnosticEngine &Diag) {
964860
llvm::Optional<RenameInfo> Info = getRenameInfo(CursorInfo);
@@ -8801,8 +8697,6 @@ bool RefactoringActionInlineMacro::performChange() {
88018697
return expandMacro(SM, CursorInfo, EditConsumer, /*adjustExpansion=*/true);
88028698
}
88038699

8804-
} // end of anonymous namespace
8805-
88068700
StringRef swift::ide::
88078701
getDescriptiveRefactoringKindName(RefactoringKind Kind) {
88088702
switch(Kind) {

lib/Refactoring/RefactoringActions.h

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
//===--------------------------------------------------------------------===////
2+
// This source file is part of the Swift.org open source project
3+
//
4+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
5+
// Licensed under Apache License v2.0 with Runtime Library Exception
6+
//
7+
// See https://swift.org/LICENSE.txt for license information
8+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#include "swift/AST/ASTContext.h"
13+
#include "swift/AST/SourceFile.h"
14+
#include "swift/Basic/SourceManager.h"
15+
#include "swift/IDE/IDERequests.h"
16+
#include "swift/Parse/Lexer.h"
17+
#include "swift/Refactoring/Refactoring.h"
18+
19+
namespace swift {
20+
namespace refactoring {
21+
22+
using namespace swift;
23+
using namespace swift::ide;
24+
25+
namespace {
26+
27+
/// Get the source file that corresponds to the given buffer.
28+
SourceFile *getContainingFile(ModuleDecl *M, RangeConfig Range) {
29+
auto &SM = M->getASTContext().SourceMgr;
30+
// TODO: We should add an ID -> SourceFile mapping.
31+
return M->getSourceFileContainingLocation(
32+
SM.getRangeForBuffer(Range.BufferID).getStart());
33+
}
34+
} // namespace
35+
36+
class RefactoringAction {
37+
protected:
38+
ModuleDecl *MD;
39+
SourceFile *TheFile;
40+
SourceEditConsumer &EditConsumer;
41+
ASTContext &Ctx;
42+
SourceManager &SM;
43+
DiagnosticEngine DiagEngine;
44+
SourceLoc StartLoc;
45+
StringRef PreferredName;
46+
47+
public:
48+
RefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
49+
SourceEditConsumer &EditConsumer,
50+
DiagnosticConsumer &DiagConsumer)
51+
: MD(MD), TheFile(getContainingFile(MD, Opts.Range)),
52+
EditConsumer(EditConsumer), Ctx(MD->getASTContext()),
53+
SM(MD->getASTContext().SourceMgr), DiagEngine(SM),
54+
StartLoc(Lexer::getLocForStartOfToken(SM, Opts.Range.getStart(SM))),
55+
PreferredName(Opts.PreferredName) {
56+
DiagEngine.addConsumer(DiagConsumer);
57+
}
58+
virtual ~RefactoringAction() = default;
59+
virtual bool performChange() = 0;
60+
};
61+
62+
/// Different from RangeBasedRefactoringAction, TokenBasedRefactoringAction
63+
/// takes the input of a given token, e.g., a name or an "if" key word.
64+
/// Contextual refactoring kinds can suggest applicable refactorings on that
65+
/// token, e.g. rename or reverse if statement.
66+
class TokenBasedRefactoringAction : public RefactoringAction {
67+
protected:
68+
ResolvedCursorInfoPtr CursorInfo;
69+
70+
public:
71+
TokenBasedRefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
72+
SourceEditConsumer &EditConsumer,
73+
DiagnosticConsumer &DiagConsumer)
74+
: RefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {
75+
// Resolve the sema token and save it for later use.
76+
CursorInfo =
77+
evaluateOrDefault(TheFile->getASTContext().evaluator,
78+
CursorInfoRequest{CursorInfoOwner(TheFile, StartLoc)},
79+
new ResolvedCursorInfo());
80+
}
81+
};
82+
83+
#define CURSOR_REFACTORING(KIND, NAME, ID) \
84+
class RefactoringAction##KIND : public TokenBasedRefactoringAction { \
85+
public: \
86+
RefactoringAction##KIND(ModuleDecl *MD, RefactoringOptions &Opts, \
87+
SourceEditConsumer &EditConsumer, \
88+
DiagnosticConsumer &DiagConsumer) \
89+
: TokenBasedRefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {} \
90+
bool performChange() override; \
91+
static bool isApplicable(ResolvedCursorInfoPtr Info, \
92+
DiagnosticEngine &Diag); \
93+
bool isApplicable() { \
94+
return RefactoringAction##KIND::isApplicable(CursorInfo, DiagEngine); \
95+
} \
96+
};
97+
#include "swift/Refactoring/RefactoringKinds.def"
98+
99+
class RangeBasedRefactoringAction : public RefactoringAction {
100+
protected:
101+
ResolvedRangeInfo RangeInfo;
102+
103+
public:
104+
RangeBasedRefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
105+
SourceEditConsumer &EditConsumer,
106+
DiagnosticConsumer &DiagConsumer)
107+
: RefactoringAction(MD, Opts, EditConsumer, DiagConsumer),
108+
RangeInfo(evaluateOrDefault(
109+
MD->getASTContext().evaluator,
110+
RangeInfoRequest(RangeInfoOwner(TheFile, Opts.Range.getStart(SM),
111+
Opts.Range.getEnd(SM))),
112+
ResolvedRangeInfo())) {}
113+
};
114+
115+
#define RANGE_REFACTORING(KIND, NAME, ID) \
116+
class RefactoringAction##KIND : public RangeBasedRefactoringAction { \
117+
public: \
118+
RefactoringAction##KIND(ModuleDecl *MD, RefactoringOptions &Opts, \
119+
SourceEditConsumer &EditConsumer, \
120+
DiagnosticConsumer &DiagConsumer) \
121+
: RangeBasedRefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {} \
122+
bool performChange() override; \
123+
static bool isApplicable(const ResolvedRangeInfo &Info, \
124+
DiagnosticEngine &Diag); \
125+
bool isApplicable() { \
126+
return RefactoringAction##KIND::isApplicable(RangeInfo, DiagEngine); \
127+
} \
128+
};
129+
#include "swift/Refactoring/RefactoringKinds.def"
130+
131+
} // end namespace refactoring
132+
} // end namespace swift

0 commit comments

Comments
 (0)