Skip to content

Commit 5dc7a79

Browse files
committed
[Refactoring] Move ConvertToDoCatch to its own file
1 parent 1be0c50 commit 5dc7a79

File tree

3 files changed

+74
-56
lines changed

3 files changed

+74
-56
lines changed

lib/Refactoring/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_swift_host_library(swiftRefactoring STATIC
55
ConvertStringConcatenationToInterpolation.cpp
66
ConvertGuardExprToIfLetExpr.cpp
77
ConvertIfLetExprToGuardExpr.cpp
8+
ConvertToDoCatch.cpp
89
ConvertToSwitchStmt.cpp
910
ConvertToTernaryExpr.cpp
1011
ExpandSwitchCases.cpp

lib/Refactoring/ConvertToDoCatch.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 "ContextFinder.h"
14+
#include "RefactoringActions.h"
15+
#include "swift/AST/Stmt.h"
16+
17+
using namespace swift::refactoring;
18+
19+
static CharSourceRange
20+
findSourceRangeToWrapInCatch(const ResolvedExprStartCursorInfo &CursorInfo,
21+
SourceFile *TheFile, SourceManager &SM) {
22+
Expr *E = CursorInfo.getTrailingExpr();
23+
if (!E)
24+
return CharSourceRange();
25+
auto Node = ASTNode(E);
26+
auto NodeChecker = [](ASTNode N) { return N.isStmt(StmtKind::Brace); };
27+
ContextFinder Finder(*TheFile, Node, NodeChecker);
28+
Finder.resolve();
29+
auto Contexts = Finder.getContexts();
30+
if (Contexts.empty())
31+
return CharSourceRange();
32+
auto TargetNode = Contexts.back();
33+
BraceStmt *BStmt = dyn_cast<BraceStmt>(TargetNode.dyn_cast<Stmt *>());
34+
auto ConvertToCharRange = [&SM](SourceRange SR) {
35+
return Lexer::getCharSourceRangeFromSourceRange(SM, SR);
36+
};
37+
assert(BStmt);
38+
auto ExprRange = ConvertToCharRange(E->getSourceRange());
39+
// Check elements of the deepest BraceStmt, pick one that covers expression.
40+
for (auto Elem : BStmt->getElements()) {
41+
auto ElemRange = ConvertToCharRange(Elem.getSourceRange());
42+
if (ElemRange.contains(ExprRange))
43+
TargetNode = Elem;
44+
}
45+
return ConvertToCharRange(TargetNode.getSourceRange());
46+
}
47+
48+
bool RefactoringActionConvertToDoCatch::isApplicable(ResolvedCursorInfoPtr Tok,
49+
DiagnosticEngine &Diag) {
50+
auto ExprStartInfo = dyn_cast<ResolvedExprStartCursorInfo>(Tok);
51+
if (!ExprStartInfo || !ExprStartInfo->getTrailingExpr())
52+
return false;
53+
return isa<ForceTryExpr>(ExprStartInfo->getTrailingExpr());
54+
}
55+
56+
bool RefactoringActionConvertToDoCatch::performChange() {
57+
auto ExprStartInfo = cast<ResolvedExprStartCursorInfo>(CursorInfo);
58+
auto *TryExpr = dyn_cast<ForceTryExpr>(ExprStartInfo->getTrailingExpr());
59+
assert(TryExpr);
60+
auto Range = findSourceRangeToWrapInCatch(*ExprStartInfo, TheFile, SM);
61+
if (!Range.isValid())
62+
return true;
63+
// Wrap given range in do catch block.
64+
EditConsumer.accept(SM, Range.getStart(), "do {\n");
65+
EditorConsumerInsertStream OS(EditConsumer, SM, Range.getEnd());
66+
OS << "\n} catch {\n" << getCodePlaceholder() << "\n}";
67+
68+
// Delete ! from try! expression
69+
auto ExclaimLen = getKeywordLen(tok::exclaim_postfix);
70+
auto ExclaimRange = CharSourceRange(TryExpr->getExclaimLoc(), ExclaimLen);
71+
EditConsumer.remove(SM, ExclaimRange);
72+
return false;
73+
}

lib/Refactoring/Refactoring.cpp

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -566,62 +566,6 @@ collectRefactoringsAtCursor(SourceFile *SF, unsigned Line, unsigned Column,
566566
return collectRefactorings(Tok, /*ExcludeRename=*/false);
567567
}
568568

569-
static CharSourceRange
570-
findSourceRangeToWrapInCatch(const ResolvedExprStartCursorInfo &CursorInfo,
571-
SourceFile *TheFile, SourceManager &SM) {
572-
Expr *E = CursorInfo.getTrailingExpr();
573-
if (!E)
574-
return CharSourceRange();
575-
auto Node = ASTNode(E);
576-
auto NodeChecker = [](ASTNode N) { return N.isStmt(StmtKind::Brace); };
577-
ContextFinder Finder(*TheFile, Node, NodeChecker);
578-
Finder.resolve();
579-
auto Contexts = Finder.getContexts();
580-
if (Contexts.empty())
581-
return CharSourceRange();
582-
auto TargetNode = Contexts.back();
583-
BraceStmt *BStmt = dyn_cast<BraceStmt>(TargetNode.dyn_cast<Stmt*>());
584-
auto ConvertToCharRange = [&SM](SourceRange SR) {
585-
return Lexer::getCharSourceRangeFromSourceRange(SM, SR);
586-
};
587-
assert(BStmt);
588-
auto ExprRange = ConvertToCharRange(E->getSourceRange());
589-
// Check elements of the deepest BraceStmt, pick one that covers expression.
590-
for (auto Elem: BStmt->getElements()) {
591-
auto ElemRange = ConvertToCharRange(Elem.getSourceRange());
592-
if (ElemRange.contains(ExprRange))
593-
TargetNode = Elem;
594-
}
595-
return ConvertToCharRange(TargetNode.getSourceRange());
596-
}
597-
598-
bool RefactoringActionConvertToDoCatch::isApplicable(ResolvedCursorInfoPtr Tok,
599-
DiagnosticEngine &Diag) {
600-
auto ExprStartInfo = dyn_cast<ResolvedExprStartCursorInfo>(Tok);
601-
if (!ExprStartInfo || !ExprStartInfo->getTrailingExpr())
602-
return false;
603-
return isa<ForceTryExpr>(ExprStartInfo->getTrailingExpr());
604-
}
605-
606-
bool RefactoringActionConvertToDoCatch::performChange() {
607-
auto ExprStartInfo = cast<ResolvedExprStartCursorInfo>(CursorInfo);
608-
auto *TryExpr = dyn_cast<ForceTryExpr>(ExprStartInfo->getTrailingExpr());
609-
assert(TryExpr);
610-
auto Range = findSourceRangeToWrapInCatch(*ExprStartInfo, TheFile, SM);
611-
if (!Range.isValid())
612-
return true;
613-
// Wrap given range in do catch block.
614-
EditConsumer.accept(SM, Range.getStart(), "do {\n");
615-
EditorConsumerInsertStream OS(EditConsumer, SM, Range.getEnd());
616-
OS << "\n} catch {\n" << getCodePlaceholder() << "\n}";
617-
618-
// Delete ! from try! expression
619-
auto ExclaimLen = getKeywordLen(tok::exclaim_postfix);
620-
auto ExclaimRange = CharSourceRange(TryExpr->getExclaimLoc(), ExclaimLen);
621-
EditConsumer.remove(SM, ExclaimRange);
622-
return false;
623-
}
624-
625569
/// Given a cursor position, this function tries to collect a number literal
626570
/// expression immediately following the cursor.
627571
static NumberLiteralExpr *getTrailingNumberLiteral(ResolvedCursorInfoPtr Tok) {

0 commit comments

Comments
 (0)