Skip to content

[CodeComplete] Match argument labels when completing function arguments #36538

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
Mar 26, 2021

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Mar 22, 2021

Previously, we always assumed that the position in the arguments would match the position in the parameter, which isn’t true in case of defaulted arguments. Try matching parameters based on argument labels to give better completion results.

Fixes rdar://60346573

@ahoppen ahoppen force-pushed the pr/complete-defaulted-labels branch from 26ab904 to 48d660c Compare March 22, 2021 21:38
@ahoppen ahoppen requested a review from rintaro March 22, 2021 21:38
Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this issue!

Comment on lines 771 to 805
for (unsigned i = PosInParams; i < Params.size(); ++i) {
if (Params[i].getLabel() == ArgName) {
// We have found a label match. Advance the position in the params
// to point to the param after the one with this label.
PosInParams = i + 1;
FoundLabelMatch = true;
break;
}
}
Copy link
Member

@rintaro rintaro Mar 23, 2021

Choose a reason for hiding this comment

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

If I understand correctly, we look for the label even if there are non-matching not defaulted parameters.
So this suggest z: <#Int#>

class C {
  func foo(x: Int, y: Int, z: Int)  {}
}
func test(c: C) {
  c.foo(y: 1, #^HERE^#)
}

which is reasonable. If we don't have a test case like this, please add it while you are here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it does. I added the test case you suggested.

auto ArgName = tuple->getElementName(PosInArgs);
bool FoundLabelMatch = false;
for (unsigned i = PosInParams; i < Params.size(); ++i) {
if (Params[i].getLabel() == ArgName) {
Copy link
Member

Choose a reason for hiding this comment

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

class C {
  func foo(x: Int..., y: Int, z: Int)  {}
}
func test(c: C) {
  c.foo(x: 10, 20, 30, y: 40, #^HERE^#)
}

When PosInArgs is 1, this loop ends up with ++PosInParams which is incorrect. I think we need some special handling for variadic parameters.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good thinking! I added special handling for variadics so that variadic parameters consume all unlabelled arguments following them.

Also added the test case you suggested.

@ahoppen ahoppen force-pushed the pr/complete-defaulted-labels branch from 48d660c to 385917e Compare March 23, 2021 07:23
Previously, we always assumed that the position in the arguments would match the position in the parameter, which isn’t true in case of defaulted arguments. Try matching parameters based on argument labels to give better completion results.

Fixes rdar://60346573
@ahoppen ahoppen force-pushed the pr/complete-defaulted-labels branch from 385917e to 433d756 Compare March 25, 2021 21:26
@ahoppen
Copy link
Member Author

ahoppen commented Mar 25, 2021

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Mar 26, 2021

@swift-ci Please smoke test Windows

@ahoppen ahoppen merged commit 74b5eb7 into swiftlang:main Mar 26, 2021
@ahoppen ahoppen deleted the pr/complete-defaulted-labels branch March 26, 2021 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants