Skip to content

Commit 7579af5

Browse files
committed
[CodeCompletion] Fix call arguments completion in overloaded case
In `<expr> '(' <code-completion-token>` case, we usually complete call arguments. If '<expr>' isn't typechecked, for example, because of overloading, we used to give up arguments completions. Now, use possible callee informations from the context type analyzer. This increases the chance to provide accurate completions. rdar://problem/43703157 (cherry picked from commit c55d6ce)
1 parent 58b85c5 commit 7579af5

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5275,6 +5275,7 @@ class CodeCompletionTypeContextAnalyzer {
52755275
// Results populated by Analyze()
52765276
SmallVector<Type, 2> PossibleTypes;
52775277
SmallVector<StringRef, 2> PossibleNames;
5278+
SmallVector<FunctionTypeAndDecl, 2> PossibleCallees;
52785279

52795280
void recordPossibleType(Type ty) {
52805281
if (!ty || ty->is<ErrorType>())
@@ -5288,7 +5289,7 @@ class CodeCompletionTypeContextAnalyzer {
52885289

52895290
bool collectArgumentExpectation(DeclContext &DC, Expr *E, Expr *CCExpr) {
52905291
// Collect parameter lists for possible func decls.
5291-
SmallVector<FunctionTypeAndDecl, 4> Candidates;
5292+
SmallVector<FunctionTypeAndDecl, 2> Candidates;
52925293
Expr *Arg = nullptr;
52935294
if (auto *applyExpr = dyn_cast<ApplyExpr>(E)) {
52945295
if (!collectPossibleCalleesForApply(DC, applyExpr, Candidates))
@@ -5299,6 +5300,7 @@ class CodeCompletionTypeContextAnalyzer {
52995300
return false;
53005301
Arg = subscriptExpr->getIndex();
53015302
}
5303+
PossibleCallees.append(Candidates.begin(), Candidates.end());
53025304

53035305
// Determine the position of code completion token in call argument.
53045306
unsigned Position;
@@ -5535,6 +5537,9 @@ class CodeCompletionTypeContextAnalyzer {
55355537

55365538
ArrayRef<Type> getPossibleTypes() const { return PossibleTypes; }
55375539
ArrayRef<StringRef> getPossibleNames() const { return PossibleNames; }
5540+
ArrayRef<FunctionTypeAndDecl> getPossibleCallees() const {
5541+
return PossibleCallees;
5542+
}
55385543
};
55395544

55405545
} // end anonymous namespace
@@ -5706,13 +5711,16 @@ void CodeCompletionCallbacksImpl::doneParsing() {
57065711
Lookup.setExpectedTypes(TypeAnalyzer.getPossibleTypes());
57075712
}
57085713

5709-
if (ExprType) {
5710-
if (ShouldCompleteCallPatternAfterParen) {
5714+
if (ShouldCompleteCallPatternAfterParen) {
5715+
if (ExprType) {
57115716
Lookup.getValueExprCompletions(*ExprType, ReferencedDecl.getDecl());
57125717
} else {
5713-
// Add argument labels, then fallthrough to get values.
5714-
Lookup.addArgNameCompletionResults(TypeAnalyzer.getPossibleNames());
5718+
for (auto &typeAndDecl : TypeAnalyzer.getPossibleCallees())
5719+
Lookup.getValueExprCompletions(typeAndDecl.first, typeAndDecl.second);
57155720
}
5721+
} else {
5722+
// Add argument labels, then fallthrough to get values.
5723+
Lookup.addArgNameCompletionResults(TypeAnalyzer.getPossibleNames());
57165724
}
57175725

57185726
if (!Lookup.FoundFunctionCalls ||

test/IDE/complete_call_arg.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// RUN-FIXME: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOAD2 | %FileCheck %s -check-prefix=OVERLOAD2
1212
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOAD3 | %FileCheck %s -check-prefix=OVERLOAD3
1313
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOAD4 | %FileCheck %s -check-prefix=OVERLOAD4
14+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOAD5 | %FileCheck %s -check-prefix=OVERLOAD5
15+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOAD6 | %FileCheck %s -check-prefix=OVERLOAD6
16+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OVERLOAD7 | %FileCheck %s -check-prefix=OVERLOAD6
1417

1518
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MEMBER1 | %FileCheck %s -check-prefix=MEMBER1
1619
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=MEMBER2 | %FileCheck %s -check-prefix=MEMBER2
@@ -215,12 +218,24 @@ class C3 {
215218
func f2() {
216219
foo2(C2I, #^OVERLOAD2^#)
217220
}
218-
func f2() {
221+
func f3() {
219222
foo2(C1I, b1: #^OVERLOAD3^#)
220223
}
221-
func f2() {
224+
func f4() {
222225
foo2(C2I, b2: #^OVERLOAD4^#)
223226
}
227+
228+
func f5() {
229+
foo2(#^OVERLOAD5^#
230+
}
231+
232+
func overloaded(_ a1: C1, b1: C2) {}
233+
func overloaded(a2: C2, b2: C1) {}
234+
235+
func f6(obj: C3) {
236+
overloaded(#^OVERLOAD6^#
237+
obj.overloaded(#^OVERLOAD7^#
238+
}
224239
}
225240

226241
// OVERLOAD1: Begin completions, 1 items
@@ -251,6 +266,20 @@ class C3 {
251266
// FIXME: This should be a negative test case
252267
// NEGATIVE_OVERLOAD4-NOT: Decl[Class]{{.*}} C2
253268

269+
// OVERLOAD5: Begin completions
270+
// OVERLOAD5-DAG: Pattern/CurrModule: ['(']{#(a): C1#}, {#b1: C2#}[')'][#Void#]; name=a: C1, b1: C2
271+
// OVERLOAD5-DAG: Pattern/CurrModule: ['(']{#(a): C2#}, {#b2: C1#}[')'][#Void#]; name=a: C2, b2: C1
272+
// OVERLOAD5-DAG: Decl[InstanceVar]/CurrNominal/TypeRelation[Identical]: C1I[#C1#]; name=C1I
273+
// OVERLOAD5-DAG: Decl[InstanceVar]/CurrNominal/TypeRelation[Identical]: C2I[#C2#]; name=C2I
274+
// OVERLOAD5: End completions
275+
276+
// OVERLOAD6: Begin completions
277+
// OVERLOAD6-DAG: Pattern/CurrModule: ['(']{#(a1): C1#}, {#b1: C2#}[')'][#Void#]; name=a1: C1, b1: C2
278+
// OVERLOAD6-DAG: Pattern/CurrModule: ['(']{#a2: C2#}, {#b2: C1#}[')'][#Void#]; name=a2: C2, b2: C1
279+
// OVERLOAD6-DAG: Decl[InstanceVar]/CurrNominal/TypeRelation[Identical]: C1I[#C1#]; name=C1I
280+
// OVERLOAD6-DAG: Decl[InstanceVar]/CurrNominal: C2I[#C2#]; name=C2I
281+
// OVERLOAD6: End completions
282+
254283
class C4 {
255284
func f1(_ G : Gen) {
256285
foo(1, b1: G.#^MEMBER1^#

0 commit comments

Comments
 (0)