Skip to content

Assist to convert nested function to closure. - #13467

Closed
mdx97 wants to merge 6 commits into
rust-lang:masterfrom
mdx97:mathew/convert-nested-function-to-closure
Closed

mdx97 wants to merge 6 commits into
rust-lang:masterfrom
mdx97:mathew/convert-nested-function-to-closure

Conversation

@mdx97

@mdx97 mdx97 commented Oct 22, 2022

Copy link
Copy Markdown
Contributor

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

bors commented Nov 1, 2022

Copy link
Copy Markdown
Contributor

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

@Veykril Veykril left a comment

Copy link
Copy Markdown
Member

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 thread crates/ide-assists/src/handlers/convert_nested_function_to_closure.rs Outdated
Comment on lines +68 to +72
function
.syntax()
.parent()
.map(|p| p.ancestors().any(|a| a.kind() == SyntaxKind::FN))
.unwrap_or(false)

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread crates/ide-assists/src/handlers/convert_nested_function_to_closure.rs Outdated
Comment thread crates/ide-assists/src/handlers/convert_nested_function_to_closure.rs Outdated
@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

Veykril commented Jan 9, 2023 •

Copy link
Copy Markdown
Member

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

jplatte commented Mar 14, 2023

Copy link
Copy Markdown
Contributor

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

@mdx97

mdx97 commented Mar 30, 2023

Copy link
Copy Markdown
Contributor Author

@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

Veykril commented Apr 5, 2023

Copy link
Copy Markdown
Member

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