Skip to content

Commit dd6ddde

Browse files
committed
[Refactoring] Move some more code from RefactoringActions.h to a .cpp file
1 parent 33b72bd commit dd6ddde

File tree

3 files changed

+62
-37
lines changed

3 files changed

+62
-37
lines changed

lib/Refactoring/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ add_swift_host_library(swiftRefactoring STATIC
1212
Async/ScopedDeclCollector.cpp
1313
Async/Utils.cpp
1414
CollapseNestedIfStmt.cpp
15-
ConvertStringConcatenationToInterpolation.cpp
16-
ConvertToComputedProperty.cpp
1715
ConvertGuardExprToIfLetExpr.cpp
1816
ConvertIfLetExprToGuardExpr.cpp
17+
ConvertStringConcatenationToInterpolation.cpp
18+
ConvertToComputedProperty.cpp
1919
ConvertToDoCatch.cpp
2020
ConvertToSwitchStmt.cpp
2121
ConvertToTernaryExpr.cpp
@@ -24,15 +24,16 @@ add_swift_host_library(swiftRefactoring STATIC
2424
ExpandTernaryExpr.cpp
2525
ExtractExpr.cpp
2626
ExtractExprBase.cpp
27-
ExtractRepeatedExpr.cpp
2827
ExtractFunction.cpp
28+
ExtractRepeatedExpr.cpp
2929
FillProtocolStubs.cpp
3030
FindRenameRangesAnnotatingConsumer.cpp
3131
LocalizeString.cpp
3232
LocalRename.cpp
3333
MemberwiseInitLocalRefactoring.cpp
3434
MoveMembersToExtension.cpp
3535
Refactoring.cpp
36+
RefactoringAction.cpp
3637
Renamer.cpp
3738
ReplaceBodiesWithFatalError.cpp
3839
SimplifyNumberLiteral.cpp

lib/Refactoring/RefactoringAction.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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+
#include "RefactoringActions.h"
14+
15+
using namespace swift::refactoring;
16+
17+
/// Get the source file that corresponds to the given buffer.
18+
SourceFile *getContainingFile(ModuleDecl *M, RangeConfig Range) {
19+
auto &SM = M->getASTContext().SourceMgr;
20+
// TODO: We should add an ID -> SourceFile mapping.
21+
return M->getSourceFileContainingLocation(
22+
SM.getRangeForBuffer(Range.BufferID).getStart());
23+
}
24+
25+
RefactoringAction::RefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
26+
SourceEditConsumer &EditConsumer,
27+
DiagnosticConsumer &DiagConsumer)
28+
: MD(MD), TheFile(getContainingFile(MD, Opts.Range)),
29+
EditConsumer(EditConsumer), Ctx(MD->getASTContext()),
30+
SM(MD->getASTContext().SourceMgr), DiagEngine(SM),
31+
StartLoc(Lexer::getLocForStartOfToken(SM, Opts.Range.getStart(SM))),
32+
PreferredName(Opts.PreferredName) {
33+
DiagEngine.addConsumer(DiagConsumer);
34+
}
35+
36+
TokenBasedRefactoringAction::TokenBasedRefactoringAction(
37+
ModuleDecl *MD, RefactoringOptions &Opts, SourceEditConsumer &EditConsumer,
38+
DiagnosticConsumer &DiagConsumer)
39+
: RefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {
40+
// Resolve the sema token and save it for later use.
41+
CursorInfo =
42+
evaluateOrDefault(TheFile->getASTContext().evaluator,
43+
CursorInfoRequest{CursorInfoOwner(TheFile, StartLoc)},
44+
new ResolvedCursorInfo());
45+
}
46+
47+
RangeBasedRefactoringAction::RangeBasedRefactoringAction(
48+
ModuleDecl *MD, RefactoringOptions &Opts, SourceEditConsumer &EditConsumer,
49+
DiagnosticConsumer &DiagConsumer)
50+
: RefactoringAction(MD, Opts, EditConsumer, DiagConsumer),
51+
RangeInfo(evaluateOrDefault(
52+
MD->getASTContext().evaluator,
53+
RangeInfoRequest(RangeInfoOwner(TheFile, Opts.Range.getStart(SM),
54+
Opts.Range.getEnd(SM))),
55+
ResolvedRangeInfo())) {}

lib/Refactoring/RefactoringActions.h

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ namespace refactoring {
2525
using namespace swift;
2626
using namespace swift::ide;
2727

28-
namespace {
29-
30-
/// Get the source file that corresponds to the given buffer.
31-
SourceFile *getContainingFile(ModuleDecl *M, RangeConfig Range) {
32-
auto &SM = M->getASTContext().SourceMgr;
33-
// TODO: We should add an ID -> SourceFile mapping.
34-
return M->getSourceFileContainingLocation(
35-
SM.getRangeForBuffer(Range.BufferID).getStart());
36-
}
37-
} // namespace
38-
3928
class RefactoringAction {
4029
protected:
4130
ModuleDecl *MD;
@@ -50,14 +39,7 @@ class RefactoringAction {
5039
public:
5140
RefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
5241
SourceEditConsumer &EditConsumer,
53-
DiagnosticConsumer &DiagConsumer)
54-
: MD(MD), TheFile(getContainingFile(MD, Opts.Range)),
55-
EditConsumer(EditConsumer), Ctx(MD->getASTContext()),
56-
SM(MD->getASTContext().SourceMgr), DiagEngine(SM),
57-
StartLoc(Lexer::getLocForStartOfToken(SM, Opts.Range.getStart(SM))),
58-
PreferredName(Opts.PreferredName) {
59-
DiagEngine.addConsumer(DiagConsumer);
60-
}
42+
DiagnosticConsumer &DiagConsumer);
6143
virtual ~RefactoringAction() = default;
6244
virtual bool performChange() = 0;
6345
};
@@ -73,14 +55,7 @@ class TokenBasedRefactoringAction : public RefactoringAction {
7355
public:
7456
TokenBasedRefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
7557
SourceEditConsumer &EditConsumer,
76-
DiagnosticConsumer &DiagConsumer)
77-
: RefactoringAction(MD, Opts, EditConsumer, DiagConsumer) {
78-
// Resolve the sema token and save it for later use.
79-
CursorInfo =
80-
evaluateOrDefault(TheFile->getASTContext().evaluator,
81-
CursorInfoRequest{CursorInfoOwner(TheFile, StartLoc)},
82-
new ResolvedCursorInfo());
83-
}
58+
DiagnosticConsumer &DiagConsumer);
8459
};
8560

8661
#define CURSOR_REFACTORING(KIND, NAME, ID) \
@@ -106,13 +81,7 @@ class RangeBasedRefactoringAction : public RefactoringAction {
10681
public:
10782
RangeBasedRefactoringAction(ModuleDecl *MD, RefactoringOptions &Opts,
10883
SourceEditConsumer &EditConsumer,
109-
DiagnosticConsumer &DiagConsumer)
110-
: RefactoringAction(MD, Opts, EditConsumer, DiagConsumer),
111-
RangeInfo(evaluateOrDefault(
112-
MD->getASTContext().evaluator,
113-
RangeInfoRequest(RangeInfoOwner(TheFile, Opts.Range.getStart(SM),
114-
Opts.Range.getEnd(SM))),
115-
ResolvedRangeInfo())) {}
84+
DiagnosticConsumer &DiagConsumer);
11685
};
11786

11887
#define RANGE_REFACTORING(KIND, NAME, ID) \

0 commit comments

Comments
 (0)