|
| 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 CallExpr *findTrailingClosureTarget(SourceManager &SM, |
| 20 | + ResolvedCursorInfoPtr CursorInfo) { |
| 21 | + if (CursorInfo->getKind() == CursorInfoKind::StmtStart) |
| 22 | + // StmtStart postion can't be a part of CallExpr. |
| 23 | + return nullptr; |
| 24 | + |
| 25 | + // Find inner most CallExpr |
| 26 | + ContextFinder Finder( |
| 27 | + *CursorInfo->getSourceFile(), CursorInfo->getLoc(), [](ASTNode N) { |
| 28 | + return N.isStmt(StmtKind::Brace) || N.isExpr(ExprKind::Call); |
| 29 | + }); |
| 30 | + Finder.resolve(); |
| 31 | + auto contexts = Finder.getContexts(); |
| 32 | + if (contexts.empty()) |
| 33 | + return nullptr; |
| 34 | + |
| 35 | + // If the innermost context is a statement (which will be a BraceStmt per |
| 36 | + // the filtering condition above), drop it. |
| 37 | + if (contexts.back().is<Stmt *>()) { |
| 38 | + contexts = contexts.drop_back(); |
| 39 | + } |
| 40 | + |
| 41 | + if (contexts.empty() || !contexts.back().is<Expr *>()) |
| 42 | + return nullptr; |
| 43 | + CallExpr *CE = cast<CallExpr>(contexts.back().get<Expr *>()); |
| 44 | + |
| 45 | + // The last argument is a non-trailing closure? |
| 46 | + auto *Args = CE->getArgs(); |
| 47 | + if (Args->empty() || Args->hasAnyTrailingClosures()) |
| 48 | + return nullptr; |
| 49 | + |
| 50 | + auto *LastArg = Args->back().getExpr(); |
| 51 | + if (auto *ICE = dyn_cast<ImplicitConversionExpr>(LastArg)) |
| 52 | + LastArg = ICE->getSyntacticSubExpr(); |
| 53 | + |
| 54 | + if (isa<ClosureExpr>(LastArg) || isa<CaptureListExpr>(LastArg)) |
| 55 | + return CE; |
| 56 | + return nullptr; |
| 57 | +} |
| 58 | + |
| 59 | +bool RefactoringActionTrailingClosure::isApplicable( |
| 60 | + ResolvedCursorInfoPtr CursorInfo, DiagnosticEngine &Diag) { |
| 61 | + SourceManager &SM = CursorInfo->getSourceFile()->getASTContext().SourceMgr; |
| 62 | + return findTrailingClosureTarget(SM, CursorInfo); |
| 63 | +} |
| 64 | + |
| 65 | +bool RefactoringActionTrailingClosure::performChange() { |
| 66 | + auto *CE = findTrailingClosureTarget(SM, CursorInfo); |
| 67 | + if (!CE) |
| 68 | + return true; |
| 69 | + |
| 70 | + auto *ArgList = CE->getArgs()->getOriginalArgs(); |
| 71 | + auto LParenLoc = ArgList->getLParenLoc(); |
| 72 | + auto RParenLoc = ArgList->getRParenLoc(); |
| 73 | + |
| 74 | + if (LParenLoc.isInvalid() || RParenLoc.isInvalid()) |
| 75 | + return true; |
| 76 | + |
| 77 | + auto NumArgs = ArgList->size(); |
| 78 | + if (NumArgs == 0) |
| 79 | + return true; |
| 80 | + |
| 81 | + auto *ClosureArg = ArgList->getExpr(NumArgs - 1); |
| 82 | + if (auto *ICE = dyn_cast<ImplicitConversionExpr>(ClosureArg)) |
| 83 | + ClosureArg = ICE->getSyntacticSubExpr(); |
| 84 | + |
| 85 | + // Replace: |
| 86 | + // * Open paren with ' ' if the closure is sole argument. |
| 87 | + // * Comma with ') ' otherwise. |
| 88 | + if (NumArgs > 1) { |
| 89 | + auto *PrevArg = ArgList->getExpr(NumArgs - 2); |
| 90 | + CharSourceRange PreRange( |
| 91 | + SM, Lexer::getLocForEndOfToken(SM, PrevArg->getEndLoc()), |
| 92 | + ClosureArg->getStartLoc()); |
| 93 | + EditConsumer.accept(SM, PreRange, ") "); |
| 94 | + } else { |
| 95 | + CharSourceRange PreRange(SM, LParenLoc, ClosureArg->getStartLoc()); |
| 96 | + EditConsumer.accept(SM, PreRange, " "); |
| 97 | + } |
| 98 | + // Remove original closing paren. |
| 99 | + CharSourceRange PostRange( |
| 100 | + SM, Lexer::getLocForEndOfToken(SM, ClosureArg->getEndLoc()), |
| 101 | + Lexer::getLocForEndOfToken(SM, RParenLoc)); |
| 102 | + EditConsumer.remove(SM, PostRange); |
| 103 | + return false; |
| 104 | +} |
0 commit comments