Skip to content

Commit f6a7e74

Browse files
committed
[Refactoring] Add utility method for printing tuples
1 parent 9cb79eb commit f6a7e74

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
@@ -5440,6 +5440,20 @@ class AsyncConverter : private SourceEntityWalker {
54405440
return TopHandler.isValid();
54415441
}
54425442

5443+
/// Prints a tuple of elements, or a lone single element if only one is
5444+
/// present, using the provided printing function.
5445+
template <typename T, typename PrintFn>
5446+
void addTupleOf(ArrayRef<T> Elements, llvm::raw_ostream &OS,
5447+
PrintFn PrintElt) {
5448+
if (Elements.size() == 1) {
5449+
PrintElt(Elements[0]);
5450+
return;
5451+
}
5452+
OS << tok::l_paren;
5453+
llvm::interleave(Elements, PrintElt, [&]() { OS << tok::comma << " "; });
5454+
OS << tok::r_paren;
5455+
}
5456+
54435457

54445458
/// Retrieves the location for the start of a comment attached to the token
54455459
/// at the provided location, or the location itself if there is no comment.
@@ -6083,16 +6097,9 @@ class AsyncConverter : private SourceEntityWalker {
60836097
}
60846098
OS << " ";
60856099
}
6086-
if (SuccessParams.size() > 1)
6087-
OS << tok::l_paren;
6088-
OS << newNameFor(SuccessParams.front());
6089-
for (const auto Param : SuccessParams.drop_front()) {
6090-
OS << tok::comma << " ";
6091-
OS << newNameFor(Param);
6092-
}
6093-
if (SuccessParams.size() > 1) {
6094-
OS << tok::r_paren;
6095-
}
6100+
// 'res =' or '(res1, res2, ...) ='
6101+
addTupleOf(SuccessParams, OS,
6102+
[&](auto &Param) { OS << newNameFor(Param); });
60966103
OS << " " << tok::equal << " ";
60976104
}
60986105

@@ -6416,19 +6423,10 @@ class AsyncConverter : private SourceEntityWalker {
64166423
/// Adds the result type of a refactored async function that previously
64176424
/// returned results via a completion handler described by \p HandlerDesc.
64186425
void addAsyncFuncReturnType(const AsyncHandlerDesc &HandlerDesc) {
6426+
// Type or (Type1, Type2, ...)
64196427
SmallVector<Type, 2> Scratch;
6420-
auto ReturnTypes = HandlerDesc.getAsyncReturnTypes(Scratch);
6421-
if (ReturnTypes.size() > 1) {
6422-
OS << tok::l_paren;
6423-
}
6424-
6425-
llvm::interleave(
6426-
ReturnTypes, [&](Type Ty) { Ty->print(OS); },
6427-
[&]() { OS << tok::comma << " "; });
6428-
6429-
if (ReturnTypes.size() > 1) {
6430-
OS << tok::r_paren;
6431-
}
6428+
addTupleOf(HandlerDesc.getAsyncReturnTypes(Scratch), OS,
6429+
[&](auto Ty) { Ty->print(OS); });
64326430
}
64336431

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

0 commit comments

Comments
 (0)