Skip to content

Commit 4db2b60

Browse files
committed
[expand-placeholder] Add support for multiple-trailing closures
Since placeholder expansion works with a single placeholder, which is somewhat at odds with multiple-trailing closures, we eagerly attempt to expand all consecutive placeholders of closure type. That is, if the API has multiple closure parameters at the end, expanding any one of them will transform all of them to the new syntax. Example ``` foo(a: <#T##()->()#>, b: <#T##()->()#>) ``` expanding *either* parameter will produce the following: ``` foo { <#code#> } b: { <#code#> } ``` (caveat: the indentation is not part of placeholder expansion, but it's added here for clarity) At least for now we do not attempt to corral an existing closure into the new syntax, so for ``` foo(a: { bar() }, b: <#T##()->()#>) ``` The exansion will be ``` foo(a: { bar() }) { <#code#> } ``` as it was before. rdar://59688632
1 parent a23c308 commit 4db2b60

File tree

3 files changed

+337
-100
lines changed

3 files changed

+337
-100
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// RUN: %sourcekitd-test -req=expand-placeholder %s | %FileCheck %s
2+
3+
withMulti1Labeled(a: <#T##() -> ()#>, b: <#T##() -> ()#>)
4+
// CHECK: withMulti1Labeled {
5+
// CHECK-NEXT: <#code#>
6+
// CHECK-NEXT: } b: {
7+
// CHECK-NEXT: <#code#>
8+
// CHECK-NEXT: }
9+
10+
withMulti2UnlabeledFirst(<#T##() -> ()#>, b: <#T##() -> ()#>)
11+
// CHECK: withMulti2UnlabeledFirst {
12+
// CHECK-NEXT: <#code#>
13+
// CHECK-NEXT: } b: {
14+
// CHECK-NEXT: <#code#>
15+
// CHECK-NEXT: }
16+
17+
withMulti2UnlabeledFirst(_: <#T##() -> ()#>, b: <#T##() -> ()#>)
18+
// CHECK: withMulti2UnlabeledFirst {
19+
// CHECK-NEXT: <#code#>
20+
// CHECK-NEXT: } b: {
21+
// CHECK-NEXT: <#code#>
22+
// CHECK-NEXT: }
23+
24+
// FIXME: we may ban second argument unlabeled.
25+
withMulti2Unlabled(_: <#T##() -> ()#>, _: <#T##() -> ()#>)
26+
// CHECK: withMulti2Unlabled {
27+
// CHECK-NEXT: <#code#>
28+
// CHECK-NEXT: } _: {
29+
// CHECK-NEXT: <#code#>
30+
// CHECK-NEXT: }
31+
32+
// FIXME: we may ban second argument unlabeled.
33+
withMulti2Unlabled(<#T##() -> ()#>, <#T##() -> ()#>)
34+
// CHECK: withMulti2Unlabled {
35+
// CHECK-NEXT: <#code#>
36+
// CHECK-NEXT: } _: {
37+
// CHECK-NEXT: <#code#>
38+
// CHECK-NEXT: }
39+
40+
withMulti3SecondArg(a: <#T##__skip__##() -> ()#>, b: <#T##() -> ()#>)
41+
// CHECK: withMulti3SecondArg {
42+
// CHECK-NEXT: <#code#>
43+
// CHECK-NEXT: } b: {
44+
// CHECK-NEXT: <#code#>
45+
// CHECK-NEXT: }
46+
47+
withMulti4MiddleArg(a: <#T##() -> ()#>, b: <#T##__skip__##() -> ()#>, c: <#T##() -> ()#>)
48+
// CHECK: withMulti4MiddleArg {
49+
// CHECK-NEXT: <#code#>
50+
// CHECK-NEXT: } b: {
51+
// CHECK-NEXT: <#code#>
52+
// CHECK-NEXT: } c: {
53+
// CHECK-NEXT: <#code#>
54+
// CHECK-NEXT: }
55+
56+
singleAlreadyExpand(a: { print("hi") }, b: <#T##() -> ()#>)
57+
// CHECK: singleAlreadyExpand(a: { print("hi") }) {
58+
// CHECK-NEXT: <#code#>
59+
// CHECK-NEXT: }
60+
61+
nonTrailing1(a: <#T##() -> ()#>, b: { print("hi") })
62+
// CHECK: nonTrailing1(a: {
63+
// CHECK-NEXT: <#code#>
64+
// CHECK-NEXT: }, b: { print("hi") })
65+
66+
nonTrailingAndTrailing1(a: <#T##() -> ()#>, b: { print("hi") }, c: <#T##() -> ()#>)
67+
// CHECK: nonTrailingAndTrailing1(a: {
68+
// CHECK-NEXT: <#code#>
69+
// CHECK-NEXT: }, b: { print("hi") }) {
70+
// CHECK-NEXT: <#code#>
71+
// CHECK-NEXT: }
72+
73+
singleNonClosure(a: <#T##Int#>, b: <#T##() -> ()#>)
74+
// CHECK: singleNonClosure(a: Int) {
75+
// CHECK-NEXT: <#code#>
76+
// CHECK-NEXT: }
77+
78+
nonTrailing2(a: <#T##() -> ()#>, b: <#T##Int#>)
79+
// CHECK: nonTrailing2(a: {
80+
// CHECK-NEXT: <#code#>
81+
// CHECK-NEXT: }, b: Int)
82+
83+
nonTrailingAndTrailing2(a: <#T##() -> ()#>, b: <#T##Int#> c: <#T##() -> ()#>)
84+
// CHECK: nonTrailingAndTrailing2(a: {
85+
// CHECK-NEXT: <#code#>
86+
// CHECK-NEXT: }, b: Int) {
87+
// CHECK-NEXT: <#code#>
88+
// CHECK-NEXT: }
89+
90+
91+
withTypesAndLabels1(a: <#T##(_ booly: Bool, inty: Int) -> ()#>, b: <#T##(solo: Xyz) -> ()#>)
92+
// CHECK: withTypesAndLabels1 { (booly, inty) in
93+
// CHECK-NEXT: <#code#>
94+
// CHECK-NEXT: } b: { (solo) in
95+
// CHECK-NEXT: <#code#>
96+
// CHECK-NEXT: }
97+
98+
func reset_parser1() {}
99+
100+
withTypes1(a: <#T##(Bool, Int) -> ()#>, b: <#T##() -> Int#>)
101+
// CHECK: withTypes1 { (<#Bool#>, <#Int#>) in
102+
// CHECK-NEXT: <#code#>
103+
// CHECK-NEXT: } b: { () -> Int in
104+
// CHECK-NEXT: <#code#>
105+
// CHECK-NEXT: }
106+
107+
func reset_parser2() {}
108+
109+
mixedFull1(arg1: <#T##Something#>, arg2: <#T##Other#>, callback1: <#T##() -> Void#>, callback2: <#T##() -> Void#>)
110+
// CHECK: mixedFull1(arg1: Something, arg2: Other) {
111+
// CHECK-NEXT: <#code#>
112+
// CHECK-NEXT: } callback2: {
113+
// CHECK-NEXT: <#code#>
114+
// CHECK-NEXT: }
115+
116+
mixedFull2(arg1: 1, arg2: "2"/*comment*/, callback1: <#T##() -> Void#>, callback2: <#T##() -> Void#>)
117+
// CHECK: mixedFull2(arg1: 1, arg2: "2") {
118+
// CHECK-NEXT: <#code#>
119+
// CHECK-NEXT: } callback2: {
120+
// CHECK-NEXT: <#code#>
121+
// CHECK-NEXT: }

test/SourceKit/CodeExpand/code-expand.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,42 @@ func f1() {
6767
// CHECK-NEXT: <#code#>
6868
// CHECK-NEXT: }
6969

70-
// FIXME: whether we get a trailing closure or not depends on the order we
71-
// expand the placeholders.
7270
func f1() {
7371
bar(<#T##__skip__: () -> ()##() -> ()#>, <#T##d: () -> ()##() -> ()#>)
7472
}
75-
// CHECK: bar(<#T##__skip__: () -> ()##() -> ()#>, {
73+
// CHECK: bar {
7674
// CHECK-NEXT: <#code#>
77-
// CHECK-NEXT: })
75+
// CHECK-NEXT: } _: {
76+
// CHECK-NEXT: <#code#>
77+
// CHECK-NEXT: }
7878

7979
func f1() {
8080
bar(<#T##d: () -> ()##() -> ()#>, <#T##d: () -> ()##() -> ()#>)
8181
}
82-
// CHECK: bar({
82+
// CHECK: bar {
8383
// CHECK-NEXT: <#code#>
84-
// CHECK-NEXT: }) {
84+
// CHECK-NEXT: } _: {
8585
// CHECK-NEXT: <#code#>
8686
// CHECK-NEXT: }
8787

88-
// FIXME: whether we get a trailing closure or not depends on the order we
89-
// expand the placeholders.
9088
func f1() {
9189
bar(a : <#T##__skip__: () -> ()##() -> ()#>, b : <#T##d: () -> ()##() -> ()#>)
9290
}
93-
// CHECK: bar(a : <#T##__skip__: () -> ()##() -> ()#>, b : {
91+
// CHECK: bar {
92+
// CHECK-NEXT: <#code#>
93+
// CHECK-NEXT: } b: {
9494
// CHECK-NEXT: <#code#>
95-
// CHECK-NEXT: })
95+
// CHECK-NEXT: }
9696

9797
func f1() {
9898
bar(a : <#T##d: () -> ()##() -> ()#>, b : <#T##d: () -> ()##() -> ()#>)
9999
}
100-
// CHECK: bar(a : {
100+
// CHECK: bar {
101101
// CHECK-NEXT: <#code#>
102-
// CHECK-NEXT: }) {
102+
// CHECK-NEXT: } b: {
103103
// CHECK-NEXT: <#code#>
104104
// CHECK-NEXT: }
105105

106-
107106
func f1() {
108107
bar(a : {}}, <#T##d: () -> ()##() -> ()#>)
109108
}

0 commit comments

Comments
 (0)