|
| 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 | +} |
0 commit comments