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

Assist to convert nested function to closure. #13467

Closed

Conversation

mdx97
Copy link
Contributor

@mdx97 mdx97 commented Oct 22, 2022

I'm a bit worried about the has_semicolon function. The syntax node returned for AssistsContext::find_node_at_offset doesn't seem to capture the semicolon of the nested function if it has one. The current implementation was the only workaround that I could think of. If there is a better way let me know! Maybe we have to fix something upstream in Rowan?

closes #13230

@mdx97 mdx97 force-pushed the mathew/convert-nested-function-to-closure branch from f690e59 to 7cb1192 Compare October 22, 2022 22:00
@bors
Copy link
Contributor

bors commented Nov 1, 2022

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

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.

We should also check that the function has no generic parameters, as we can't transform that into a closure (+ a test that verifies this)

Comment on lines +68 to +72
function
.syntax()
.parent()
.map(|p| p.ancestors().any(|a| a.kind() == SyntaxKind::FN))
.unwrap_or(false)
Copy link
Member

Choose a reason for hiding this comment

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

This will trigger incorrectly if we have an associated function nested in a function (very rare that someone does this but it could happen), so it might be nicer to do the following

function.ancestors().skip(1).find_map(ast::Item::cast).unwrap_or(false, |it| matches!(it, ast::item::Fn(_))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what that snippet is supposed to do, namely because there are two arguments that are passed to unwrap_or. Would it be easier to just check if the function is an associated function?

Copy link
Member

Choose a reason for hiding this comment

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

oh my bad, this was supposed to be a map_or_else. But yes it would be easier to just check if it is an assocaited function actually, that is go from the ast::Fn to the hir version and check if that is an assoc item.

@Veykril Veykril added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 3, 2022
@mdx97 mdx97 requested a review from Veykril December 16, 2022 02:42
@lowr lowr added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 20, 2022
@Veykril
Copy link
Member

Veykril commented Jan 9, 2023

Sorry for the late review, was a bit busy.
Could you remove the merge commit? We try to keep our commit history merge commit free if possible (and do rebases instead).

@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 Jan 9, 2023
@jplatte
Copy link
Contributor

jplatte commented Mar 14, 2023

Ping @mdx97, are you going to come back to this? I can take over and rebase if not.

@mdx97
Copy link
Contributor Author

mdx97 commented Mar 30, 2023

@jplatte You can take this over if you'd like. I don't really have a timeline on when I could get back to this.

@Veykril
Copy link
Member

Veykril commented Apr 5, 2023

Closing in favor of #14455

@Veykril Veykril closed this Apr 5, 2023
bors added a commit that referenced this pull request Apr 5, 2023
…Veykril

Convert nested function to closure assist

Continuation of / closes #13467.
Resolves #13230.

r? `@Veykril`
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.

Feature request: "convert function to closure" assist
6 participants