Skip to content
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

feat: Implement completion for the callable fields. #15879

Merged
merged 9 commits into from Dec 1, 2023

Conversation

dfireBird
Copy link
Contributor

Fixes #14656

PR is opened with basic changes. It could be improved by having a new SymbolKind for the callable fields and implementing a separate render function similar to the render_method for the new SymbolKind.
It could also be done without any changes to the SymbolKind of course, have the new function called based on the type of field.
I prefer the former method.

Please give any thoughts or changes you think is appropriate for this method. I could start working on that in this same PR.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 12, 2023
Copy link
Contributor

@DropDemBits DropDemBits left a comment

Choose a reason for hiding this comment

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

Some nits and notes, but overall LGTM.

crates/ide-completion/src/render.rs Outdated Show resolved Hide resolved
crates/ide-completion/src/render.rs Outdated Show resolved Hide resolved
crates/ide-completion/src/render.rs Outdated Show resolved Hide resolved
crates/ide-completion/src/render.rs Outdated Show resolved Hide resolved
crates/ide-completion/src/completions/dot.rs Outdated Show resolved Hide resolved
@dfireBird
Copy link
Contributor Author

I've now added the tests for both the cases you mentioned and made the refactors as well.

Copy link
Contributor

@DropDemBits DropDemBits left a comment

Choose a reason for hiding this comment

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

Just one last nit that I missed in the first review, and otherwise LGTM.

crates/ide-completion/src/render.rs Outdated Show resolved Hide resolved
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

There is one interesting case that we should also have a test for which is foo.bar::<>, I think this should result in a DotAccessKind::Method { has_parens: false } (the only case where its false I believe as otherwise missing parens will result in a field access node).

crates/ide-completion/src/completions/dot.rs Outdated Show resolved Hide resolved
crates/ide-completion/src/completions/dot.rs Show resolved Hide resolved
crates/ide-completion/src/render.rs Outdated Show resolved Hide resolved
@bors
Copy link
Collaborator

bors commented Nov 21, 2023

☔ The latest upstream changes (presumably #15825) made this pull request unmergeable. Please resolve the merge conflicts.

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 21, 2023
@dfireBird
Copy link
Contributor Author

dfireBird commented Nov 23, 2023

For the foo.bar::<> case, it should complete to (foo).bar::<>() right?
In current implementation it completes to (foo).bar()::<>.

@Veykril
Copy link
Member

Veykril commented Nov 23, 2023

For the foo.bar::<> case, it should complete to (foo).bar::<>() right? In current implementation it completes to (foo).bar()::<>.

I should complete to foo.bar::<>(), no reason to parenthesize the receiver

@dfireBird
Copy link
Contributor Author

dfireBird commented Nov 28, 2023

Sorry I was busy with some thing and couldn't find time to fix the tests. I have fixed the tests now.

I'll create a follow up PR for the foo.baz::<> case, once this PR is merged.

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Sorry for all the review rounds and thanks for sticking with it, this should be the last :)

|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
is_field_access,
);

if let DotAccessKind::Method { .. } = dot_access.kind {
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
Copy link
Member

Choose a reason for hiding this comment

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

This cov mark makes little sense to be tested like this now (since the else branch is gone, which was what this mark wanted to assert is not being called)

"#,
r#"
struct S { va_field: u32, fn_field: fn() }
fn foo() { (S { va_field: 0, fn_field: || {} }).fn_field() }
Copy link
Member

Choose a reason for hiding this comment

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

This is why I wanted the check_edit test :^) The output here is wrong, this should be

Suggested change
fn foo() { (S { va_field: 0, fn_field: || {} }).fn_field() }
fn foo() { (S { va_field: 0, fn_field: || {} }.fn_field)() }

This is how you call fields in rust (to disambiguate from method calls)

Copy link
Contributor Author

@dfireBird dfireBird Dec 1, 2023

Choose a reason for hiding this comment

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

Sorry for all the review rounds and thanks for sticking with it, this should be the last :)

No worries, this is just lesson for me to put good a PR next time.

I keep getting confused where to put the end parenthesis. I somehow finally decided this was correct thing, my bad.

Copy link
Member

Choose a reason for hiding this comment

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

No worries, this is just lesson for me to put good a PR next time.

All good, the r-a code base takes time to get accustomed to, and the ide-completion crate is a part where I put more review rounds in in terms of quality than ide-assists for example, so I am a bit more thorough here

I keep getting confused where to put the end parenthesis. I somehow finally decided this was correct thing, my bad.

Understandable, it is a rarely used quirk of the language.

if let Some(receiver) = ctx.completion.sema.original_ast_node(receiver.clone()) {
let range = receiver.syntax().text_range();
builder.insert(range.start(), "(".to_string());
builder.insert(range.end(), ")".to_string());
Copy link
Member

Choose a reason for hiding this comment

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

To fix the wrong edit we should probably just check the parent of the (original ast node) receiver here, if its a field add the closing parenthesis after the field etc.

Copy link
Contributor Author

@dfireBird dfireBird Dec 1, 2023

Choose a reason for hiding this comment

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

Yeah, I should do something like this, right? I suppose storing the range is kinda useless now.

builder.insert(receiver.syntax().text_range().start(), "(".to_string());
builder.insert(ctx.source_range().end(), ")".to_string());

Copy link
Member

Choose a reason for hiding this comment

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

Ye that should work I think

@Veykril
Copy link
Member

Veykril commented Dec 1, 2023

Thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Dec 1, 2023

📌 Commit b7effe5 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Dec 1, 2023

⌛ Testing commit b7effe5 with merge e402c49...

@bors
Copy link
Collaborator

bors commented Dec 1, 2023

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing e402c49 to master...

@bors bors merged commit e402c49 into rust-lang:master Dec 1, 2023
10 checks passed
@lnicola lnicola changed the title Implement completion for the callable fields. feat: Implement completion for the callable fields. Dec 3, 2023
@lnicola
Copy link
Member

lnicola commented Dec 4, 2023

image

@dfireBird dfireBird deleted the fix-14656 branch December 5, 2023 16:18
bors added a commit that referenced this pull request Jan 2, 2024
fix: make callable fields not complete in method access no parens case

Follow up PR for #15879

Fixes the callable field completion appearing in the method access with no parens case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Complete callable fields when used in a method call position
6 participants