@@ -1566,16 +1566,29 @@ class PlaceholderExpansionScanner {
1566
1566
}
1567
1567
1568
1568
bool shouldUseTrailingClosureInTuple (TupleExpr *TE,
1569
- SourceLoc PlaceHolderStartLoc) {
1570
- if (!TE->getElements ().empty ()) {
1571
- for (unsigned I = 0 , N = TE->getNumElements (); I < N; ++ I) {
1572
- bool IsLast = I == N - 1 ;
1573
- Expr *E = TE->getElement (I);
1574
- if (IsLast) {
1575
- return E->getStartLoc () == PlaceHolderStartLoc;
1576
- } else if (containClosure (E)) {
1577
- return false ;
1569
+ SourceLoc PlaceHolderStartLoc,
1570
+ bool &isWrappedWithBraces) {
1571
+ if (TE->getElements ().empty ())
1572
+ return false ;
1573
+
1574
+ for (unsigned I = 0 , N = TE->getNumElements (); I < N; ++ I) {
1575
+ bool IsLast = I == N - 1 ;
1576
+ Expr *E = TE->getElement (I);
1577
+
1578
+ // Placeholders wrapped in braces {<#T##() -> Int#>} can also
1579
+ // be valid for trailing syntax.
1580
+ if (auto CE = dyn_cast<ClosureExpr>(E)) {
1581
+ if (CE->hasSingleExpressionBody () &&
1582
+ CE->getSingleExpressionBody ()->getStartLoc ()
1583
+ == PlaceHolderStartLoc) {
1584
+ // We found the placeholder.
1585
+ isWrappedWithBraces = true ;
1586
+ return IsLast;
1578
1587
}
1588
+ } else if (IsLast) {
1589
+ return E->getStartLoc () == PlaceHolderStartLoc;
1590
+ } else if (containClosure (E)) {
1591
+ return false ;
1579
1592
}
1580
1593
}
1581
1594
return false ;
@@ -1590,6 +1603,7 @@ class PlaceholderExpansionScanner {
1590
1603
bool scan (SourceFile &SF, unsigned BufID, unsigned Offset,
1591
1604
unsigned Length, std::function<void (Expr *Args,
1592
1605
bool UseTrailingClosure,
1606
+ bool isWrappedWithBraces,
1593
1607
ArrayRef<Param>,
1594
1608
CharSourceRange)> Callback,
1595
1609
std::function<bool(EditorPlaceholderExpr*)> NonClosureCallback) {
@@ -1606,18 +1620,21 @@ class PlaceholderExpansionScanner {
1606
1620
// and if the call parens can be removed in that case.
1607
1621
// We'll first find the enclosing CallExpr, and then do further analysis.
1608
1622
bool UseTrailingClosure = false ;
1623
+ bool isWrappedWithBraces = false ;
1609
1624
auto ECE = enclosingCallExprArg (SF, PlaceholderStartLoc);
1610
1625
Expr *Args = ECE.first ;
1611
1626
if (Args && ECE.second ) {
1612
1627
if (isa<ParenExpr>(Args)) {
1613
1628
UseTrailingClosure = true ;
1614
1629
} else if (auto *TE = dyn_cast<TupleExpr>(Args)) {
1615
- UseTrailingClosure = shouldUseTrailingClosureInTuple (TE,
1616
- PlaceholderStartLoc);
1630
+ UseTrailingClosure = shouldUseTrailingClosureInTuple (
1631
+ TE, PlaceholderStartLoc,
1632
+ isWrappedWithBraces);
1617
1633
}
1618
1634
}
1619
1635
1620
- Callback (Args, UseTrailingClosure, TargetClosureInfo.Params ,
1636
+ Callback (Args, UseTrailingClosure, isWrappedWithBraces,
1637
+ TargetClosureInfo.Params ,
1621
1638
TargetClosureInfo.ReturnTypeRange );
1622
1639
return true ;
1623
1640
}
@@ -1922,7 +1939,7 @@ void SwiftEditorDocument::expandPlaceholder(unsigned Offset, unsigned Length,
1922
1939
1923
1940
Scanner.scan (SF, BufID, Offset, Length,
1924
1941
[&](Expr *Args,
1925
- bool UseTrailingClosure,
1942
+ bool UseTrailingClosure, bool isWrappedWithBraces,
1926
1943
ArrayRef<PlaceholderExpansionScanner::Param> ClosureParams,
1927
1944
CharSourceRange ClosureReturnTypeRange) {
1928
1945
@@ -1966,8 +1983,10 @@ void SwiftEditorDocument::expandPlaceholder(unsigned Offset, unsigned Length,
1966
1983
unsigned End = SM.getLocOffsetInBuffer (Args->getEndLoc (), BufID);
1967
1984
EffectiveLength = (End + 1 ) - EffectiveOffset;
1968
1985
}
1969
-
1970
- OS << " { " ;
1986
+ // Trailing closure syntax handling will replace braces anyway.
1987
+ bool printBraces = !isWrappedWithBraces || UseTrailingClosure;
1988
+ if (printBraces)
1989
+ OS << " { " ;
1971
1990
1972
1991
bool ReturningVoid = isReturningVoid (SM, ClosureReturnTypeRange);
1973
1992
@@ -2009,7 +2028,8 @@ void SwiftEditorDocument::expandPlaceholder(unsigned Offset, unsigned Length,
2009
2028
if (HasSignature)
2010
2029
OS << " in" ;
2011
2030
OS << " \n " << getCodePlaceholder () << " \n " ;
2012
- OS << " }" ;
2031
+ if (printBraces)
2032
+ OS << " }" ;
2013
2033
}
2014
2034
Consumer.handleSourceText (ExpansionStr);
2015
2035
Consumer.recordAffectedRange (EffectiveOffset, EffectiveLength);
0 commit comments