Skip to content

[CodeCompletion] Always suggest call pattern for nested function calls #71587

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 1 commit into from
Feb 26, 2024
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
21 changes: 21 additions & 0 deletions lib/IDE/ArgumentCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ static bool isExpressionResultTypeUnconstrained(const Solution &S, Expr *E) {
}
}

/// Returns whether `E` has a parent expression with arguments.
static bool hasParentCallLikeExpr(Expr *E, ConstraintSystem &CS) {
E = CS.getParentExpr(E);
while (E) {
if (E->getArgs() || isa<ParenExpr>(E) || isa<TupleExpr>(E) || isa<CollectionExpr>(E)) {
return true;
}
E = CS.getParentExpr(E);
}
return false;
}

void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);

Expand Down Expand Up @@ -284,6 +296,15 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
// and the code completion token doesn’t have a label, we have a case like
// `Point(|)`. Suggest the entire function signature.
IncludeSignature = true;
} else if (!ParentCall->getArgs()->empty() &&
ParentCall->getArgs()->getExpr(0) == CompletionExpr &&
!ParentCall->getArgs()->get(0).hasLabel()) {
if (hasParentCallLikeExpr(ParentCall, CS)) {
// We are completing in cases like `bar(arg: foo(|, option: 1)`
// In these cases, we don’t know if `option` belongs to the call to `foo`
// or `bar`. Be defensive and also suggest the signature.
IncludeSignature = true;
}
}

Results.push_back(
Expand Down
35 changes: 35 additions & 0 deletions test/IDE/complete_call_arg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,38 @@ struct AtStartOfFunctionCallWithExistingParams {
// AT_START_OF_CALL_ONE_EXISTING_ARGUMENT-DAG: Pattern/Local/Flair[ArgLabels]: {#a: Int#}[#Int#]; name=a:
}
}

struct NestedCallsWithoutClosingParen {
func foo(arg: Int, arg2: Int) -> Int { 1 }
func bar(arg: Int, option: Int) -> Int { 1 }

func test() {
bar(arg: foo(#^NESTED_CALL_AT_START^#, option: 1)
// NESTED_CALL_AT_START-DAG: Decl[InstanceMethod]/CurrNominal/Flair[ArgLabels]/TypeRelation[Convertible]: ['(']{#arg: Int#}, {#arg2: Int#}[')'][#Int#];

bar(arg: 1 + foo(#^NESTED_CALL_IN_BINARY_OP?check=NESTED_CALL_AT_START^#, option: 1)

bar(arg: foo(#^NESTED_CALL_AFTER_MEMBER_ACCESS?check=NESTED_CALL_AT_START^#, option: 1)

bar(arg: foo(arg: 1, #^NESTED_CALL_AT_SECOND_ARG^#, option: 1)
// NESTED_CALL_AT_SECOND_ARG-DAG: Pattern/Local/Flair[ArgLabels]: {#arg2: Int#}[#Int#];
}

func testInDictionaryLiteral() {
let a = 1
let b = 2
_ = [a: foo(#^IN_DICTIONARY_LITERAL?check=NESTED_CALL_AT_START^#, b: 1]
}

func testInArrayLiteral() {
_ = [foo(#^IN_ARRAY_LITERAL?check=NESTED_CALL_AT_START^#, 1]
}

func testInParen() {
_ = (foo(#^IN_PAREN?check=NESTED_CALL_AT_START^#)
}

func testInTuple() {
_ = (foo(#^IN_TUPLE?check=NESTED_CALL_AT_START^#, 1)
}
}
3 changes: 1 addition & 2 deletions test/SourceKit/CodeComplete/complete_call_pattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ func test() {

// RUN: %sourcekitd-test -req=complete -pos=7:11 %s -- %s | %FileCheck %s
// RUN: %sourcekitd-test -req=complete.open -pos=7:11 %s -- %s | %FileCheck %s
// RUN: %sourcekitd-test -req=complete.open -pos=7:11 %s -- %s | %FileCheck %s

// CHECK: key.kind: source.lang.swift.pattern
// CHECK: key.kind: source.lang.swift.decl.function.constructor
// CHECK-NEXT: key.name: "foo:"
2 changes: 1 addition & 1 deletion validation-test/IDE/issues_fixed/issue-57149.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ struct ContentView: View {
}
}

// COMPLETE: Pattern/Local/Flair[ArgLabels]: {#foo: Int#}[#Int#];
// COMPLETE: Decl[InstanceMethod]/Super/Flair[ArgLabels]/TypeRelation[Convertible]: ['(']{#foo: Int#}[')'][#Never#]; name=foo: