Skip to content

Commit b6c986a

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 63e4367 commit b6c986a

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
@@ -6286,22 +6286,46 @@ class AsyncConverter : private SourceEntityWalker {
62866286
/// 'await' keyword.
62876287
void addCallToAsyncMethod(const FuncDecl *FD,
62886288
const AsyncHandlerDesc &HandlerDesc) {
6289+
// The call to the async function is the same as the call to the old
6290+
// completion handler function, minus the completion handler arg.
6291+
addForwardingCallTo(FD, HandlerDesc, /*HandlerReplacement*/ "");
6292+
}
6293+
6294+
/// Adds a forwarding call to the old completion handler function, with
6295+
/// \p HandlerReplacement that allows for a custom replacement or, if empty,
6296+
/// removal of the completion handler closure.
6297+
void addForwardingCallTo(
6298+
const FuncDecl *FD, const AsyncHandlerDesc &HandlerDesc,
6299+
StringRef HandlerReplacement, bool CanUseTrailingClosure = true) {
62896300
OS << FD->getBaseName() << tok::l_paren;
6290-
bool FirstParam = true;
6291-
for (auto Param : *FD->getParameters()) {
6301+
6302+
auto *Params = FD->getParameters();
6303+
for (auto Param : *Params) {
62926304
if (Param == HandlerDesc.getHandler()) {
6293-
/// We don't need to pass the completion handler to the async method.
6294-
continue;
6305+
/// If we're not replacing the handler with anything, drop it.
6306+
if (HandlerReplacement.empty())
6307+
continue;
6308+
6309+
// If this is the last param, and we can use a trailing closure, do so.
6310+
if (CanUseTrailingClosure && Param == Params->back()) {
6311+
OS << tok::r_paren << " ";
6312+
OS << HandlerReplacement;
6313+
return;
6314+
}
6315+
// Otherwise fall through to do the replacement.
62956316
}
6296-
if (!FirstParam) {
6317+
6318+
if (Param != Params->front())
62976319
OS << tok::comma << " ";
6298-
} else {
6299-
FirstParam = false;
6300-
}
6301-
if (!Param->getArgumentName().empty()) {
6320+
6321+
if (!Param->getArgumentName().empty())
63026322
OS << Param->getArgumentName() << tok::colon << " ";
6323+
6324+
if (Param == HandlerDesc.getHandler()) {
6325+
OS << HandlerReplacement;
6326+
} else {
6327+
OS << Param->getParameterName();
63036328
}
6304-
OS << Param->getParameterName();
63056329
}
63066330
OS << tok::r_paren;
63076331
}

0 commit comments

Comments
 (0)