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