Skip to content

Commit e4a7a93

Browse files
committed
[Refactoring] Generalize addCallToAsyncMethod
Factor out an `addForwardingCallTo` method that allows the printing of a call to the old completion handler function, with a given replacement for the completion handler arg. `addCallToAsyncMethod` then calls this with an empty replacement for the handler, removing it from the call.
1 parent 4e21041 commit e4a7a93

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6278,22 +6278,46 @@ class AsyncConverter : private SourceEntityWalker {
62786278
/// 'await' keyword.
62796279
void addCallToAsyncMethod(const FuncDecl *FD,
62806280
const AsyncHandlerDesc &HandlerDesc) {
6281+
// The call to the async function is the same as the call to the old
6282+
// completion handler function, minus the completion handler arg.
6283+
addForwardingCallTo(FD, HandlerDesc, /*HandlerReplacement*/ "");
6284+
}
6285+
6286+
/// Adds a forwarding call to the old completion handler function, with
6287+
/// \p HandlerReplacement that allows for a custom replacement or, if empty,
6288+
/// removal of the completion handler closure.
6289+
void addForwardingCallTo(
6290+
const FuncDecl *FD, const AsyncHandlerDesc &HandlerDesc,
6291+
StringRef HandlerReplacement, bool CanUseTrailingClosure = true) {
62816292
OS << FD->getBaseName() << tok::l_paren;
6282-
bool FirstParam = true;
6283-
for (auto Param : *FD->getParameters()) {
6293+
6294+
auto *Params = FD->getParameters();
6295+
for (auto Param : *Params) {
62846296
if (Param == HandlerDesc.getHandler()) {
6285-
/// We don't need to pass the completion handler to the async method.
6286-
continue;
6297+
/// If we're not replacing the handler with anything, drop it.
6298+
if (HandlerReplacement.empty())
6299+
continue;
6300+
6301+
// If this is the last param, and we can use a trailing closure, do so.
6302+
if (CanUseTrailingClosure && Param == Params->back()) {
6303+
OS << tok::r_paren << " ";
6304+
OS << HandlerReplacement;
6305+
return;
6306+
}
6307+
// Otherwise fall through to do the replacement.
62876308
}
6288-
if (!FirstParam) {
6309+
6310+
if (Param != Params->front())
62896311
OS << tok::comma << " ";
6290-
} else {
6291-
FirstParam = false;
6292-
}
6293-
if (!Param->getArgumentName().empty()) {
6312+
6313+
if (!Param->getArgumentName().empty())
62946314
OS << Param->getArgumentName() << tok::colon << " ";
6315+
6316+
if (Param == HandlerDesc.getHandler()) {
6317+
OS << HandlerReplacement;
6318+
} else {
6319+
OS << Param->getParameterName();
62956320
}
6296-
OS << Param->getParameterName();
62976321
}
62986322
OS << tok::r_paren;
62996323
}

0 commit comments

Comments
 (0)