Skip to content

[SourceKit] Expand editor placeholder to trailing closures inside a TryExpr #65376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/SourceKit/CodeExpand/code-expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ foo(x: <#T##() -> Void#>, y: <#T##Int#>)
// CHECK-NEXT: <#code#>
// CHECK-NEXT: }, y: Int)

try foo(x: <#T##() -> Void#>)
// CHECK: try foo {
// CHECK-NEXT: <#code#>
// CHECK-NEXT: }

await foo(x: <#T##() -> Void#>)
// CHECK: await foo {
// CHECK-NEXT: <#code#>
// CHECK-NEXT: }

!foo(x: <#T##() -> Void#>)
// CHECK: !foo {
// CHECK-NEXT: <#code#>
// CHECK-NEXT: }


anArr.indexOfObjectPassingTest(<#T##predicate: ((AnyObject!, Int, UnsafePointer<ObjCBool>) -> Bool)?##((AnyObject!, Int, UnsafePointer<ObjCBool>) -> Bool)?#>)
// CHECK: anArr.indexOfObjectPassingTest { <#AnyObject!#>, <#Int#>, <#UnsafePointer<ObjCBool>#> in
// CHECK-NEXT: <#code#>
Expand Down
9 changes: 8 additions & 1 deletion tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,14 @@ class PlaceholderExpansionScanner {
auto SR = E->getSourceRange();
if (SR.isValid() && SM.rangeContainsTokenLoc(SR, TargetLoc) &&
!checkCallExpr(E) && !EnclosingCallAndArg.first) {
OuterExpr = E;
if (!isa<TryExpr>(E) && !isa<AwaitExpr>(E) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSemanticsProvidingExpr covers all IdentityExpr. FWIW you probably want to reset OuterExpr in these cases as well, ie. even with this change something like the following would fail:

bar(try foo(x: <#T##() -> Void#>))

And I haven't thought too hard about other cases, but TBH it doesn't really matter, we'll replace this with the swift-syntax one soon :).

Copy link
Member Author

@ahoppen ahoppen Apr 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to use getSemanticsProvidingExpr because IIRC that includes e.g. .self. And IIUC we don’t want to expand

foo(<#T##() -> Void#>).self

to

foo { <#code#> }.self

Or at least that’s what I understood the purpose of OuterExpr is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be fine with .self. I'm not really sure I agree with what we're trying to prevent here anyway. Ie. the comment says that outer(inner(<#closure#>)) shouldn't expand to trailing closure for inner. But that also seems fine to me 🤷. Certainly the swift-syntax replacement doesn't currently care about that. So something we can talk about later.

Like I said, I'm perfectly happy with what you have now.

!isa<PrefixUnaryExpr>(E)) {
// We don't want to expand to trailing closures if the call is
// nested inside another expression that has closing characters,
// like a `)` for a function call. This is not the case for
// `try`, `await` and prefix operator applications.
OuterExpr = E;
}
}
return true;
}
Expand Down