Skip to content

Commit 0987156

Browse files
authored
Merge pull request #65376 from ahoppen/ahoppen/expand-to-trailing-closure-in-try
[SourceKit] Expand editor placeholder to trailing closures inside a `TryExpr`
2 parents b8444be + aef35b9 commit 0987156

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

test/SourceKit/CodeExpand/code-expand.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ foo(x: <#T##() -> Void#>, y: <#T##Int#>)
1010
// CHECK-NEXT: <#code#>
1111
// CHECK-NEXT: }, y: Int)
1212

13+
try foo(x: <#T##() -> Void#>)
14+
// CHECK: try foo {
15+
// CHECK-NEXT: <#code#>
16+
// CHECK-NEXT: }
17+
18+
await foo(x: <#T##() -> Void#>)
19+
// CHECK: await foo {
20+
// CHECK-NEXT: <#code#>
21+
// CHECK-NEXT: }
22+
23+
!foo(x: <#T##() -> Void#>)
24+
// CHECK: !foo {
25+
// CHECK-NEXT: <#code#>
26+
// CHECK-NEXT: }
27+
28+
1329
anArr.indexOfObjectPassingTest(<#T##predicate: ((AnyObject!, Int, UnsafePointer<ObjCBool>) -> Bool)?##((AnyObject!, Int, UnsafePointer<ObjCBool>) -> Bool)?#>)
1430
// CHECK: anArr.indexOfObjectPassingTest { <#AnyObject!#>, <#Int#>, <#UnsafePointer<ObjCBool>#> in
1531
// CHECK-NEXT: <#code#>

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,14 @@ class PlaceholderExpansionScanner {
16581658
auto SR = E->getSourceRange();
16591659
if (SR.isValid() && SM.rangeContainsTokenLoc(SR, TargetLoc) &&
16601660
!checkCallExpr(E) && !EnclosingCallAndArg.first) {
1661-
OuterExpr = E;
1661+
if (!isa<TryExpr>(E) && !isa<AwaitExpr>(E) &&
1662+
!isa<PrefixUnaryExpr>(E)) {
1663+
// We don't want to expand to trailing closures if the call is
1664+
// nested inside another expression that has closing characters,
1665+
// like a `)` for a function call. This is not the case for
1666+
// `try`, `await` and prefix operator applications.
1667+
OuterExpr = E;
1668+
}
16621669
}
16631670
return true;
16641671
}

0 commit comments

Comments
 (0)