Skip to content

Commit f17d3d3

Browse files
authored
Merge pull request #74191 from hamishknight/trailing-closure-complete
[Completion] A couple of trailing closure fixes
2 parents baf2760 + 1d20392 commit f17d3d3

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

lib/IDE/ArgumentCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
5050
const AnyFunctionType::Param *TypeParam = &ParamsToPass[Idx];
5151
bool Required = !Res.DeclParamIsOptional[Idx];
5252

53-
if (Res.FirstTrailingClosureIndex && Idx > *Res.FirstTrailingClosureIndex &&
53+
if (Res.FirstTrailingClosureIndex &&
54+
Res.ArgIdx > *Res.FirstTrailingClosureIndex &&
5455
!TypeParam->getPlainType()
5556
->lookThroughAllOptionalTypes()
5657
->is<AnyFunctionType>()) {

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,8 @@ void CodeCompletionCallbacksImpl::postfixCompletion(SourceLoc CompletionLoc,
14841484
// closure. In that case, also suggest labels for additional trailing
14851485
// closures.
14861486
if (auto AE = dyn_cast<ApplyExpr>(ParsedExpr)) {
1487-
if (AE->getArgs()->hasAnyTrailingClosures()) {
1487+
if (AE->getArgs()->hasAnyTrailingClosures() &&
1488+
Kind == CompletionKind::PostfixExpr) {
14881489
ASTContext &Ctx = CurDeclContext->getASTContext();
14891490

14901491
// Modify the call that has the code completion expression as an

test/IDE/complete_multiple_trailingclosure.swift

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,40 @@ func testGlobalFunc() {
1818
// GLOBALFUNC_AFTERLABEL-NOT: Begin completions
1919
}
2020

21+
struct S {
22+
func member() {}
23+
}
24+
func globalFunc2(x: Int = 0, _ fn: () -> Void) -> S {}
25+
func globalFunc3(x: Int, _ fn: () -> Void) -> S {}
26+
27+
func testNonFuncArg() {
28+
do {
29+
// Don't complete for a non-function default argument.
30+
globalFunc2 {} #^GLOBALFUNC2^#
31+
// GLOBALFUNC2: Begin completions, 2 items
32+
// GLOBALFUNC2-DAG: Decl[InstanceMethod]/CurrNominal: .member()[#Void#]; name=member()
33+
// GLOBALFUNC2-DAG: Keyword[self]/CurrNominal: .self[#S#]; name=self
34+
35+
globalFunc2 {}
36+
.#^GLOBALFUNC2_DOT^#
37+
// GLOBALFUNC2_DOT: Begin completions, 2 items
38+
// GLOBALFUNC2_DOT-DAG: Decl[InstanceMethod]/CurrNominal: member()[#Void#]; name=member()
39+
// GLOBALFUNC2_DOT-DAG: Keyword[self]/CurrNominal: self[#S#]; name=self
40+
}
41+
do {
42+
globalFunc3 {} #^GLOBALFUNC3^#
43+
// GLOBALFUNC3: Begin completions, 2 items
44+
// GLOBALFUNC3-DAG: Decl[InstanceMethod]/CurrNominal: .member()[#Void#]; name=member()
45+
// GLOBALFUNC3-DAG: Keyword[self]/CurrNominal: .self[#S#]; name=self
46+
47+
globalFunc3 {}
48+
.#^GLOBALFUNC3_DOT^#
49+
// GLOBALFUNC3_DOT: Begin completions, 2 items
50+
// GLOBALFUNC3_DOT-DAG: Decl[InstanceMethod]/CurrNominal: member()[#Void#]; name=member()
51+
// GLOBALFUNC3_DOT-DAG: Keyword[self]/CurrNominal: self[#S#]; name=self
52+
}
53+
}
54+
2155
struct SimpleEnum {
2256
case foo, bar
2357

@@ -99,6 +133,17 @@ func testOptionalInit() {
99133
// INIT_OPTIONAL_NEWLINE-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
100134
}
101135

136+
func testOptionalInitDot() {
137+
// When there's a dot, we don't complete for the closure argument.
138+
TestStruct2 {
139+
2
140+
}
141+
.#^INIT_OPTIONAL_DOT^#
142+
// INIT_OPTIONAL_DOT: Begin completions, 2 items
143+
// INIT_OPTIONAL_DOT-DAG: Keyword[self]/CurrNominal: self[#TestStruct2#]; name=self
144+
// INIT_OPTIONAL_DOT-DAG: Decl[InstanceMethod]/CurrNominal: testStructMethod()[#Void#]; name=testStructMethod()
145+
}
146+
102147
struct TestStruct3 {
103148
init(fn1: () -> Int, fn2: () -> String, fn3: () -> String) {}
104149
func testStructMethod() {}
@@ -171,10 +216,9 @@ func testFallbackPostfix() {
171216
let _ = MyStruct4 {
172217
1
173218
} #^INIT_FALLBACK_1^#
174-
// INIT_FALLBACK_1: Begin completions, 3 items
219+
// INIT_FALLBACK_1: Begin completions, 2 items
175220
// INIT_FALLBACK_1-DAG: Keyword[self]/CurrNominal: .self[#MyStruct4<Int>#]; name=self
176221
// INIT_FALLBACK_1-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: .testStructMethod()[#Void#]; name=testStructMethod()
177-
// INIT_FALLBACK_1-DAG: Pattern/Local/Flair[ArgLabels]: {#arg1: Int#}[#Int#]; name=arg1:
178222
let _ = MyStruct4(name: "test") {
179223
""
180224
} arg3: {

0 commit comments

Comments
 (0)