Skip to content

Commit 5369402

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

File tree

3 files changed

+107
-88
lines changed

3 files changed

+107
-88
lines changed

lib/Refactoring/CMakeLists.txt

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

lib/Refactoring/Refactoring.cpp

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,94 +1569,6 @@ bool RefactoringActionExpandTernaryExpr::performChange() {
15691569
return false; //don't abort
15701570
}
15711571

1572-
bool RefactoringActionConvertIfLetExprToGuardExpr::
1573-
isApplicable(const ResolvedRangeInfo &Info, DiagnosticEngine &Diag) {
1574-
1575-
if (Info.Kind != RangeKind::SingleStatement
1576-
&& Info.Kind != RangeKind::MultiStatement)
1577-
return false;
1578-
1579-
if (Info.ContainedNodes.empty())
1580-
return false;
1581-
1582-
IfStmt *If = nullptr;
1583-
1584-
if (Info.ContainedNodes.size() == 1) {
1585-
if (auto S = Info.ContainedNodes[0].dyn_cast<Stmt*>()) {
1586-
If = dyn_cast<IfStmt>(S);
1587-
}
1588-
}
1589-
1590-
if (!If)
1591-
return false;
1592-
1593-
auto CondList = If->getCond();
1594-
1595-
if (CondList.size() == 1) {
1596-
auto E = CondList[0];
1597-
auto P = E.getKind();
1598-
if (P == swift::StmtConditionElement::CK_PatternBinding) {
1599-
auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt());
1600-
if (Body)
1601-
return true;
1602-
}
1603-
}
1604-
1605-
return false;
1606-
}
1607-
1608-
bool RefactoringActionConvertIfLetExprToGuardExpr::performChange() {
1609-
1610-
auto S = RangeInfo.ContainedNodes[0].dyn_cast<Stmt*>();
1611-
IfStmt *If = dyn_cast<IfStmt>(S);
1612-
auto CondList = If->getCond();
1613-
1614-
// Get if-let condition
1615-
SourceRange range = CondList[0].getSourceRange();
1616-
SourceManager &SM = RangeInfo.RangeContext->getASTContext().SourceMgr;
1617-
auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, range);
1618-
1619-
auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt());
1620-
1621-
// Get if-let then body.
1622-
auto firstElement = Body->getFirstElement();
1623-
auto lastElement = Body->getLastElement();
1624-
SourceRange bodyRange = firstElement.getSourceRange();
1625-
bodyRange.widen(lastElement.getSourceRange());
1626-
auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, bodyRange);
1627-
1628-
SmallString<64> DeclBuffer;
1629-
llvm::raw_svector_ostream OS(DeclBuffer);
1630-
1631-
StringRef Space = " ";
1632-
StringRef NewLine = "\n";
1633-
1634-
OS << tok::kw_guard << Space;
1635-
OS << CondCharRange.str().str() << Space;
1636-
OS << tok::kw_else << Space;
1637-
OS << tok::l_brace << NewLine;
1638-
1639-
// Get if-let else body.
1640-
if (auto *ElseBody = dyn_cast_or_null<BraceStmt>(If->getElseStmt())) {
1641-
auto firstElseElement = ElseBody->getFirstElement();
1642-
auto lastElseElement = ElseBody->getLastElement();
1643-
SourceRange elseBodyRange = firstElseElement.getSourceRange();
1644-
elseBodyRange.widen(lastElseElement.getSourceRange());
1645-
auto ElseBodyCharRange = Lexer::getCharSourceRangeFromSourceRange(SM, elseBodyRange);
1646-
OS << ElseBodyCharRange.str().str() << NewLine;
1647-
}
1648-
1649-
OS << tok::kw_return << NewLine;
1650-
OS << tok::r_brace << NewLine;
1651-
OS << BodyCharRange.str().str();
1652-
1653-
// Replace if-let to guard
1654-
auto ReplaceRange = RangeInfo.ContentRange;
1655-
EditConsumer.accept(SM, ReplaceRange, DeclBuffer.str());
1656-
1657-
return false;
1658-
}
1659-
16601572
/// The helper class analyzes a given nominal decl or an extension decl to
16611573
/// decide whether stubs are required to filled in and the context in which
16621574
/// these stubs should be filled.

0 commit comments

Comments
 (0)