Skip to content

Commit 308270c

Browse files
committed
[Refactoring] Move ConvertGuardExprToIfLetExpr to its own file
1 parent fc3f7a0 commit 308270c

File tree

3 files changed

+104
-86
lines changed

3 files changed

+104
-86
lines changed

lib/Refactoring/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_swift_host_library(swiftRefactoring STATIC
22
CollapseNestedIfStmt.cpp
33
ConvertStringConcatenationToInterpolation.cpp
4+
ConvertGuardExprToIfLetExpr.cpp
45
ConvertToSwitchStmt.cpp
56
ConvertToTernaryExpr.cpp
67
ExtractRepeatedExpr.cpp
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
#include "swift/AST/Stmt.h"
15+
16+
using namespace swift::refactoring;
17+
18+
bool RefactoringActionConvertGuardExprToIfLetExpr::isApplicable(
19+
const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) {
20+
if (Info.Kind != RangeKind::SingleStatement &&
21+
Info.Kind != RangeKind::MultiStatement)
22+
return false;
23+
24+
if (Info.ContainedNodes.empty())
25+
return false;
26+
27+
GuardStmt *guardStmt = nullptr;
28+
29+
if (Info.ContainedNodes.size() > 0) {
30+
if (auto S = Info.ContainedNodes[0].dyn_cast<Stmt *>()) {
31+
guardStmt = dyn_cast<GuardStmt>(S);
32+
}
33+
}
34+
35+
if (!guardStmt)
36+
return false;
37+
38+
auto CondList = guardStmt->getCond();
39+
40+
if (CondList.size() == 1) {
41+
auto E = CondList[0];
42+
auto P = E.getPatternOrNull();
43+
if (P && E.getKind() == swift::StmtConditionElement::CK_PatternBinding)
44+
return true;
45+
}
46+
47+
return false;
48+
}
49+
50+
bool RefactoringActionConvertGuardExprToIfLetExpr::performChange() {
51+
52+
// Get guard stmt
53+
auto S = RangeInfo.ContainedNodes[0].dyn_cast<Stmt *>();
54+
GuardStmt *Guard = dyn_cast<GuardStmt>(S);
55+
56+
// Get guard condition
57+
auto CondList = Guard->getCond();
58+
59+
// Get guard condition source
60+
SourceRange range = CondList[0].getSourceRange();
61+
SourceManager &SM = RangeInfo.RangeContext->getASTContext().SourceMgr;
62+
auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, range);
63+
64+
SmallString<64> DeclBuffer;
65+
llvm::raw_svector_ostream OS(DeclBuffer);
66+
67+
StringRef Space = " ";
68+
StringRef NewLine = "\n";
69+
70+
OS << tok::kw_if << Space;
71+
OS << CondCharRange.str().str() << Space;
72+
OS << tok::l_brace << NewLine;
73+
74+
// Get nodes after guard to place them at if-let body
75+
if (RangeInfo.ContainedNodes.size() > 1) {
76+
auto S = RangeInfo.ContainedNodes[1].getSourceRange();
77+
S.widen(RangeInfo.ContainedNodes.back().getSourceRange());
78+
auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, S);
79+
OS << BodyCharRange.str().str() << NewLine;
80+
}
81+
OS << tok::r_brace;
82+
83+
// Get guard body
84+
auto Body = dyn_cast_or_null<BraceStmt>(Guard->getBody());
85+
86+
if (Body && Body->getNumElements() > 1) {
87+
auto firstElement = Body->getFirstElement();
88+
auto lastElement = Body->getLastElement();
89+
SourceRange bodyRange = firstElement.getSourceRange();
90+
bodyRange.widen(lastElement.getSourceRange());
91+
auto BodyCharRange =
92+
Lexer::getCharSourceRangeFromSourceRange(SM, bodyRange);
93+
OS << Space << tok::kw_else << Space << tok::l_brace << NewLine;
94+
OS << BodyCharRange.str().str() << NewLine;
95+
OS << tok::r_brace;
96+
}
97+
98+
// Replace guard to if-let
99+
auto ReplaceRange = RangeInfo.ContentRange;
100+
EditConsumer.accept(SM, ReplaceRange, DeclBuffer.str());
101+
102+
return false;
103+
}

lib/Refactoring/Refactoring.cpp

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,92 +1657,6 @@ bool RefactoringActionConvertIfLetExprToGuardExpr::performChange() {
16571657
return false;
16581658
}
16591659

1660-
bool RefactoringActionConvertGuardExprToIfLetExpr::
1661-
isApplicable(const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) {
1662-
if (Info.Kind != RangeKind::SingleStatement
1663-
&& Info.Kind != RangeKind::MultiStatement)
1664-
return false;
1665-
1666-
if (Info.ContainedNodes.empty())
1667-
return false;
1668-
1669-
GuardStmt *guardStmt = nullptr;
1670-
1671-
if (Info.ContainedNodes.size() > 0) {
1672-
if (auto S = Info.ContainedNodes[0].dyn_cast<Stmt*>()) {
1673-
guardStmt = dyn_cast<GuardStmt>(S);
1674-
}
1675-
}
1676-
1677-
if (!guardStmt)
1678-
return false;
1679-
1680-
auto CondList = guardStmt->getCond();
1681-
1682-
if (CondList.size() == 1) {
1683-
auto E = CondList[0];
1684-
auto P = E.getPatternOrNull();
1685-
if (P && E.getKind() == swift::StmtConditionElement::CK_PatternBinding)
1686-
return true;
1687-
}
1688-
1689-
return false;
1690-
}
1691-
1692-
bool RefactoringActionConvertGuardExprToIfLetExpr::performChange() {
1693-
1694-
// Get guard stmt
1695-
auto S = RangeInfo.ContainedNodes[0].dyn_cast<Stmt*>();
1696-
GuardStmt *Guard = dyn_cast<GuardStmt>(S);
1697-
1698-
// Get guard condition
1699-
auto CondList = Guard->getCond();
1700-
1701-
// Get guard condition source
1702-
SourceRange range = CondList[0].getSourceRange();
1703-
SourceManager &SM = RangeInfo.RangeContext->getASTContext().SourceMgr;
1704-
auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, range);
1705-
1706-
SmallString<64> DeclBuffer;
1707-
llvm::raw_svector_ostream OS(DeclBuffer);
1708-
1709-
StringRef Space = " ";
1710-
StringRef NewLine = "\n";
1711-
1712-
OS << tok::kw_if << Space;
1713-
OS << CondCharRange.str().str() << Space;
1714-
OS << tok::l_brace << NewLine;
1715-
1716-
// Get nodes after guard to place them at if-let body
1717-
if (RangeInfo.ContainedNodes.size() > 1) {
1718-
auto S = RangeInfo.ContainedNodes[1].getSourceRange();
1719-
S.widen(RangeInfo.ContainedNodes.back().getSourceRange());
1720-
auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, S);
1721-
OS << BodyCharRange.str().str() << NewLine;
1722-
}
1723-
OS << tok::r_brace;
1724-
1725-
// Get guard body
1726-
auto Body = dyn_cast_or_null<BraceStmt>(Guard->getBody());
1727-
1728-
if (Body && Body->getNumElements() > 1) {
1729-
auto firstElement = Body->getFirstElement();
1730-
auto lastElement = Body->getLastElement();
1731-
SourceRange bodyRange = firstElement.getSourceRange();
1732-
bodyRange.widen(lastElement.getSourceRange());
1733-
auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, bodyRange);
1734-
OS << Space << tok::kw_else << Space << tok::l_brace << NewLine;
1735-
OS << BodyCharRange.str().str() << NewLine;
1736-
OS << tok::r_brace;
1737-
}
1738-
1739-
// Replace guard to if-let
1740-
auto ReplaceRange = RangeInfo.ContentRange;
1741-
EditConsumer.accept(SM, ReplaceRange, DeclBuffer.str());
1742-
1743-
return false;
1744-
}
1745-
17461660
/// The helper class analyzes a given nominal decl or an extension decl to
17471661
/// decide whether stubs are required to filled in and the context in which
17481662
/// these stubs should be filled.

0 commit comments

Comments
 (0)