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