@@ -2069,7 +2069,7 @@ bool RefactoringActionExpandTernaryExpr::performChange() {
2069
2069
}
2070
2070
2071
2071
bool RefactoringActionConvertIfLetExprToGuardExpr::
2072
- isApplicable (ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
2072
+ isApplicable (ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
2073
2073
2074
2074
if (Info.Kind != RangeKind::SingleStatement
2075
2075
&& Info.Kind != RangeKind::MultiStatement)
@@ -2082,7 +2082,7 @@ bool RefactoringActionConvertIfLetExprToGuardExpr::
2082
2082
2083
2083
if (Info.ContainedNodes .size () == 1 ) {
2084
2084
if (auto S = Info.ContainedNodes [0 ].dyn_cast <Stmt*>()) {
2085
- If = dyn_cast<IfStmt>(S);
2085
+ If = dyn_cast<IfStmt>(S);
2086
2086
}
2087
2087
}
2088
2088
@@ -2092,187 +2092,156 @@ bool RefactoringActionConvertIfLetExprToGuardExpr::
2092
2092
auto CondList = If->getCond ();
2093
2093
2094
2094
if (CondList.size () == 1 ) {
2095
- auto E = CondList[0 ];
2096
- auto P = E.getKind ();
2097
- if (P == swift::StmtConditionElement::CK_PatternBinding)
2095
+ auto E = CondList[0 ];
2096
+ auto P = E.getKind ();
2097
+ if (P == swift::StmtConditionElement::CK_PatternBinding) {
2098
+ auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt ());
2099
+ if (Body)
2098
2100
return true ;
2101
+ }
2099
2102
}
2100
2103
2101
2104
return false ;
2102
2105
}
2103
-
2104
- bool RefactoringActionConvertIfLetExprToGuardExpr::performChange () {
2105
2106
2106
- IfStmt *If = nullptr ;
2107
-
2108
- if (RangeInfo.ContainedNodes .size () == 1 ) {
2109
- if (auto S = RangeInfo.ContainedNodes [0 ].dyn_cast <Stmt*>()) {
2110
- If = dyn_cast<IfStmt>(S);
2111
- }
2112
- }
2113
-
2114
- if (!If)
2115
- return true ; // abort
2107
+ bool RefactoringActionConvertIfLetExprToGuardExpr::performChange () {
2116
2108
2109
+ auto S = RangeInfo.ContainedNodes [0 ].dyn_cast <Stmt*>();
2110
+ IfStmt *If = dyn_cast<IfStmt>(S);
2117
2111
auto CondList = If->getCond ();
2118
2112
2119
- if (CondList.size () != 1 )
2120
- return true ; // abort
2121
-
2122
- auto E = CondList[0 ];
2123
- auto P = E.getPatternOrNull ();
2124
- if (!P)
2125
- return true ; // abort
2126
-
2127
- // Get if-let condition
2128
- SourceRange range = E.getSourceRange ();
2129
- SourceManager &SM = RangeInfo.RangeContext ->getASTContext ().SourceMgr ;
2130
- auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
2131
-
2132
- auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt ());
2133
-
2134
- if (!Body)
2135
- return true ; // abort
2136
-
2137
- // Get if-let then body.
2138
- auto firstElement = Body->getElements ()[0 ];
2139
- auto lastElement = Body->getElements ().back ();
2140
- SourceRange bodyRange = firstElement.getSourceRange ();
2141
- bodyRange.widen (lastElement.getSourceRange ());
2142
- auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, bodyRange);
2143
-
2144
- llvm::SmallString<64 > DeclBuffer;
2145
- llvm::raw_svector_ostream OS (DeclBuffer);
2146
-
2147
- llvm::StringRef Space = " " ;
2148
- llvm::StringRef NewLine = " \n " ;
2149
-
2150
- OS << tok::kw_guard << Space;
2151
- OS << CondCharRange.str ().str () << Space;
2152
- OS << tok::kw_else << Space;
2153
- OS << tok::l_brace << NewLine;
2154
-
2155
- // Get if-let else body.
2156
- auto ElseBody = dyn_cast_or_null<BraceStmt>(If->getElseStmt ());
2157
- if (ElseBody) {
2158
- auto firstElseElement = ElseBody->getElements ()[0 ];
2159
- auto lastElseElement = ElseBody->getElements ().back ();
2160
- SourceRange elseBodyRange = firstElseElement.getSourceRange ();
2161
- elseBodyRange.widen (lastElseElement.getSourceRange ());
2162
- auto ElseBodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, elseBodyRange);
2163
- OS << ElseBodyCharRange.str ().str () << NewLine;
2164
- }
2165
-
2166
- OS << tok::kw_return << NewLine;
2167
- OS << tok::r_brace << NewLine;
2168
- OS << BodyCharRange.str ().str ();
2169
-
2170
- // Replace if-let to guard
2171
- auto ReplaceRange = RangeInfo.ContentRange ;
2172
- EditConsumer.accept (SM, ReplaceRange, DeclBuffer.str ());
2113
+ // Get if-let condition
2114
+ SourceRange range = CondList[0 ].getSourceRange ();
2115
+ SourceManager &SM = RangeInfo.RangeContext ->getASTContext ().SourceMgr ;
2116
+ auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
2117
+
2118
+ auto Body = dyn_cast_or_null<BraceStmt>(If->getThenStmt ());
2119
+
2120
+ // Get if-let then body.
2121
+ auto firstElement = Body->getElements ()[0 ];
2122
+ auto lastElement = Body->getElements ().back ();
2123
+ SourceRange bodyRange = firstElement.getSourceRange ();
2124
+ bodyRange.widen (lastElement.getSourceRange ());
2125
+ auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, bodyRange);
2126
+
2127
+ llvm::SmallString<64 > DeclBuffer;
2128
+ llvm::raw_svector_ostream OS (DeclBuffer);
2129
+
2130
+ llvm::StringRef Space = " " ;
2131
+ llvm::StringRef NewLine = " \n " ;
2132
+
2133
+ OS << tok::kw_guard << Space;
2134
+ OS << CondCharRange.str ().str () << Space;
2135
+ OS << tok::kw_else << Space;
2136
+ OS << tok::l_brace << NewLine;
2137
+
2138
+ // Get if-let else body.
2139
+ if (auto *ElseBody = dyn_cast_or_null<BraceStmt>(If->getElseStmt ())) {
2140
+ auto firstElseElement = ElseBody->getElements ()[0 ];
2141
+ auto lastElseElement = ElseBody->getElements ().back ();
2142
+ SourceRange elseBodyRange = firstElseElement.getSourceRange ();
2143
+ elseBodyRange.widen (lastElseElement.getSourceRange ());
2144
+ auto ElseBodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, elseBodyRange);
2145
+ OS << ElseBodyCharRange.str ().str () << NewLine;
2146
+ }
2147
+
2148
+ OS << tok::kw_return << NewLine;
2149
+ OS << tok::r_brace << NewLine;
2150
+ OS << BodyCharRange.str ().str ();
2151
+
2152
+ // Replace if-let to guard
2153
+ auto ReplaceRange = RangeInfo.ContentRange ;
2154
+ EditConsumer.accept (SM, ReplaceRange, DeclBuffer.str ());
2173
2155
2174
- return false ;
2156
+ return false ;
2175
2157
}
2176
2158
2177
2159
bool RefactoringActionConvertGuardExprToIfLetExpr::
2178
- isApplicable (ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
2179
- if (Info.Kind != RangeKind::SingleStatement
2180
- && Info.Kind != RangeKind::MultiStatement)
2181
- return false ;
2182
-
2183
- if (Info.ContainedNodes .empty ())
2184
- return false ;
2185
-
2186
- GuardStmt *guardStmt = nullptr ;
2187
-
2188
- if (Info.ContainedNodes .size () > 0 ) {
2189
- if (auto S = Info.ContainedNodes [0 ].dyn_cast <Stmt*>()) {
2190
- guardStmt = dyn_cast<GuardStmt>(S);
2191
- }
2192
- }
2160
+ isApplicable (ResolvedRangeInfo Info, DiagnosticEngine &Diag) {
2161
+ if (Info.Kind != RangeKind::SingleStatement
2162
+ && Info.Kind != RangeKind::MultiStatement)
2163
+ return false ;
2193
2164
2194
- if (!guardStmt )
2195
- return false ;
2165
+ if (Info. ContainedNodes . empty () )
2166
+ return false ;
2196
2167
2197
- auto CondList = guardStmt-> getCond () ;
2168
+ GuardStmt *guardStmt = nullptr ;
2198
2169
2199
- if (CondList.size () == 1 ) {
2200
- auto E = CondList[0 ];
2201
- auto P = E.getKind ();
2202
- if (P == swift::StmtConditionElement::CK_PatternBinding)
2203
- return true ;
2170
+ if (Info.ContainedNodes .size () > 0 ) {
2171
+ if (auto S = Info.ContainedNodes [0 ].dyn_cast <Stmt*>()) {
2172
+ guardStmt = dyn_cast<GuardStmt>(S);
2204
2173
}
2174
+ }
2205
2175
2176
+ if (!guardStmt)
2206
2177
return false ;
2207
- }
2208
-
2209
- bool RefactoringActionConvertGuardExprToIfLetExpr::performChange () {
2210
2178
2211
- // Get guard stmt
2212
- GuardStmt *Guard = nullptr ;
2213
- if (RangeInfo.ContainedNodes .size () > 0 ) {
2214
- if (auto S = RangeInfo.ContainedNodes [0 ].dyn_cast <Stmt*>()) {
2215
- Guard = dyn_cast<GuardStmt>(S);
2216
- }
2217
- }
2218
- if (!Guard)
2219
- return true ; // abort
2220
-
2221
- // Get guard condition
2222
- auto CondList = Guard->getCond ();
2223
- if (CondList.size () != 1 )
2224
- return true ; // abort
2179
+ auto CondList = guardStmt->getCond ();
2225
2180
2181
+ if (CondList.size () == 1 ) {
2226
2182
auto E = CondList[0 ];
2227
2183
auto P = E.getPatternOrNull ();
2228
- if (!P)
2229
- return true ; // abort
2230
-
2231
- // Get guard condition source
2232
- SourceRange range = E.getSourceRange ();
2233
- SourceManager &SM = RangeInfo.RangeContext ->getASTContext ().SourceMgr ;
2234
- auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
2235
-
2236
- llvm::SmallString<64 > DeclBuffer;
2237
- llvm::raw_svector_ostream OS (DeclBuffer);
2184
+ if (P && E.getKind () == swift::StmtConditionElement::CK_PatternBinding)
2185
+ return true ;
2186
+ }
2238
2187
2239
- llvm::StringRef Space = " " ;
2240
- llvm::StringRef NewLine = " \n " ;
2188
+ return false ;
2189
+ }
2241
2190
2242
- OS << tok::kw_if << Space;
2243
- OS << CondCharRange.str ().str () << Space;
2244
- OS << tok::l_brace << NewLine;
2191
+ bool RefactoringActionConvertGuardExprToIfLetExpr::performChange () {
2245
2192
2246
- // Get nodes after guard to place them at if-let body
2247
- if (RangeInfo.ContainedNodes .size () > 1 ) {
2248
- auto S = RangeInfo.ContainedNodes [1 ].getSourceRange ();
2249
- S.widen (RangeInfo.ContainedNodes .back ().getSourceRange ());
2250
- auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, S);
2251
- OS << BodyCharRange.str ().str () << NewLine;
2252
- }
2253
- OS << tok::r_brace;
2193
+ // Get guard stmt
2194
+ auto S = RangeInfo.ContainedNodes [0 ].dyn_cast <Stmt*>();
2195
+ GuardStmt *Guard = dyn_cast<GuardStmt>(S);
2254
2196
2255
- // Get guard body
2256
- auto Body = dyn_cast_or_null<BraceStmt>( Guard->getBody () );
2197
+ // Get guard condition
2198
+ auto CondList = Guard->getCond ( );
2257
2199
2258
- if (Body && Body->getElements ().size () > 1 ) {
2259
- auto firstElement = Body->getElements ()[0 ];
2260
- auto lastElement = Body->getElements ().back ();
2261
- SourceRange bodyRange = firstElement.getSourceRange ();
2262
- bodyRange.widen (lastElement.getSourceRange ());
2263
- auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, bodyRange);
2264
- OS << Space << tok::kw_else << Space << tok::l_brace << NewLine;
2265
- OS << BodyCharRange.str ().str () << NewLine;
2266
- OS << tok::r_brace;
2267
- }
2200
+ // Get guard condition source
2201
+ SourceRange range = CondList[0 ].getSourceRange ();
2202
+ SourceManager &SM = RangeInfo.RangeContext ->getASTContext ().SourceMgr ;
2203
+ auto CondCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, range);
2204
+
2205
+ llvm::SmallString<64 > DeclBuffer;
2206
+ llvm::raw_svector_ostream OS (DeclBuffer);
2207
+
2208
+ llvm::StringRef Space = " " ;
2209
+ llvm::StringRef NewLine = " \n " ;
2210
+
2211
+ OS << tok::kw_if << Space;
2212
+ OS << CondCharRange.str ().str () << Space;
2213
+ OS << tok::l_brace << NewLine;
2268
2214
2269
- // Replace guard to if-let
2270
- auto ReplaceRange = RangeInfo.ContentRange ;
2271
- EditConsumer.accept (SM, ReplaceRange, DeclBuffer.str ());
2215
+ // Get nodes after guard to place them at if-let body
2216
+ if (RangeInfo.ContainedNodes .size () > 1 ) {
2217
+ auto S = RangeInfo.ContainedNodes [1 ].getSourceRange ();
2218
+ S.widen (RangeInfo.ContainedNodes .back ().getSourceRange ());
2219
+ auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, S);
2220
+ OS << BodyCharRange.str ().str () << NewLine;
2221
+ }
2222
+ OS << tok::r_brace;
2272
2223
2273
- return false ;
2224
+ // Get guard body
2225
+ auto Body = dyn_cast_or_null<BraceStmt>(Guard->getBody ());
2226
+
2227
+ if (Body && Body->getElements ().size () > 1 ) {
2228
+ auto firstElement = Body->getElements ()[0 ];
2229
+ auto lastElement = Body->getElements ().back ();
2230
+ SourceRange bodyRange = firstElement.getSourceRange ();
2231
+ bodyRange.widen (lastElement.getSourceRange ());
2232
+ auto BodyCharRange = Lexer::getCharSourceRangeFromSourceRange (SM, bodyRange);
2233
+ OS << Space << tok::kw_else << Space << tok::l_brace << NewLine;
2234
+ OS << BodyCharRange.str ().str () << NewLine;
2235
+ OS << tok::r_brace;
2236
+ }
2237
+
2238
+ // Replace guard to if-let
2239
+ auto ReplaceRange = RangeInfo.ContentRange ;
2240
+ EditConsumer.accept (SM, ReplaceRange, DeclBuffer.str ());
2241
+
2242
+ return false ;
2274
2243
}
2275
-
2244
+
2276
2245
// / Struct containing info about an IfStmt that can be converted into an IfExpr.
2277
2246
struct ConvertToTernaryExprInfo {
2278
2247
ConvertToTernaryExprInfo () {}
0 commit comments