Skip to content

Commit 555f8da

Browse files
committed
[Refactoring] Add utility method for printing tuples
1 parent 8c5c1ae commit 555f8da

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5432,6 +5432,20 @@ class AsyncConverter : private SourceEntityWalker {
54325432
return TopHandler.isValid();
54335433
}
54345434

5435+
/// Prints a tuple of elements, or a lone single element if only one is
5436+
/// present, using the provided printing function.
5437+
template <typename T, typename PrintFn>
5438+
void addTupleOf(ArrayRef<T> Elements, llvm::raw_ostream &OS,
5439+
PrintFn PrintElt) {
5440+
if (Elements.size() == 1) {
5441+
PrintElt(Elements[0]);
5442+
return;
5443+
}
5444+
OS << tok::l_paren;
5445+
llvm::interleave(Elements, PrintElt, [&]() { OS << tok::comma << " "; });
5446+
OS << tok::r_paren;
5447+
}
5448+
54355449

54365450
/// Retrieves the location for the start of a comment attached to the token
54375451
/// at the provided location, or the location itself if there is no comment.
@@ -6075,16 +6089,9 @@ class AsyncConverter : private SourceEntityWalker {
60756089
}
60766090
OS << " ";
60776091
}
6078-
if (SuccessParams.size() > 1)
6079-
OS << tok::l_paren;
6080-
OS << newNameFor(SuccessParams.front());
6081-
for (const auto Param : SuccessParams.drop_front()) {
6082-
OS << tok::comma << " ";
6083-
OS << newNameFor(Param);
6084-
}
6085-
if (SuccessParams.size() > 1) {
6086-
OS << tok::r_paren;
6087-
}
6092+
// 'res =' or '(res1, res2, ...) ='
6093+
addTupleOf(SuccessParams, OS,
6094+
[&](auto &Param) { OS << newNameFor(Param); });
60886095
OS << " " << tok::equal << " ";
60896096
}
60906097

@@ -6408,19 +6415,10 @@ class AsyncConverter : private SourceEntityWalker {
64086415
/// Adds the result type of a refactored async function that previously
64096416
/// returned results via a completion handler described by \p HandlerDesc.
64106417
void addAsyncFuncReturnType(const AsyncHandlerDesc &HandlerDesc) {
6418+
// Type or (Type1, Type2, ...)
64116419
SmallVector<Type, 2> Scratch;
6412-
auto ReturnTypes = HandlerDesc.getAsyncReturnTypes(Scratch);
6413-
if (ReturnTypes.size() > 1) {
6414-
OS << tok::l_paren;
6415-
}
6416-
6417-
llvm::interleave(
6418-
ReturnTypes, [&](Type Ty) { Ty->print(OS); },
6419-
[&]() { OS << tok::comma << " "; });
6420-
6421-
if (ReturnTypes.size() > 1) {
6422-
OS << tok::r_paren;
6423-
}
6420+
addTupleOf(HandlerDesc.getAsyncReturnTypes(Scratch), OS,
6421+
[&](auto Ty) { Ty->print(OS); });
64246422
}
64256423

64266424
/// If \p FD is generic, adds a type annotation with the return type of the

0 commit comments

Comments
 (0)