Skip to content

feat: support associated values in "Generate Enum Variant" assist #12837

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 6 commits into from
Aug 2, 2022

Conversation

DorianListens
Copy link
Contributor

@DorianListens DorianListens commented Jul 20, 2022

This change adds support for associated values to the "Generate Enum Variant" assist.

I've split the implementation out into 4 steps to make code review easier:

  • Add "add_variant" support to the structural ast editing system in edit_in_place
  • Migrate generate_enum_variant to use structural ast editing instead of string manipulation
  • Support tuple fields
  • Support record fields

Please let me know if I should leave the commits as-is, or squash before merging.

Fixes #12797

@DorianListens DorianListens force-pushed the dscheidt/generate-enum-data branch from db069f2 to 592d7ca Compare July 21, 2022 06:04
@DorianListens
Copy link
Contributor Author

Question: should I leave this as a "fix", or is it more of a "feat"? I can definitely see the argument either way, and am currently leaning towards changing this to a "feat".

Also, for the changelog, here are before and after examples:

Before:

Screen.Recording.2022-07-27.at.11.37.35.AM.mov

After:

Screen.Recording.2022-07-27.at.11.40.40.AM.mov

@Veykril
Copy link
Member

Veykril commented Jul 27, 2022

Well, it adds to a feature oppposed to just a bug fix, so tagging this as one sounds good to me (We aren't too strict about the tagging really)

@DorianListens DorianListens changed the title fix: support associated data in "Generate Enum Variant" assist feat: support associated data in "Generate Enum Variant" assist Jul 27, 2022
@DorianListens DorianListens force-pushed the dscheidt/generate-enum-data branch from 592d7ca to 292a133 Compare July 27, 2022 18:33
@DorianListens DorianListens changed the title feat: support associated data in "Generate Enum Variant" assist feat: support associated values in "Generate Enum Variant" assist Jul 27, 2022
@DorianListens DorianListens force-pushed the dscheidt/generate-enum-data branch from 292a133 to b38ece5 Compare July 28, 2022 06:29
@DorianListens
Copy link
Contributor Author

@fasterthanlime @maartenflippo Looks like y'all did the initial implementation for this assist, nice work! Pinging you here just in case you've got code review thoughts.

@Veykril
Copy link
Member

Veykril commented Jul 28, 2022

I'll try to get back to this tomorrow (I sometimes put off reviewing assist PRs as they are usually more work to review :) )

@DorianListens
Copy link
Contributor Author

I'll try to get back to this tomorrow (I sometimes put off reviewing assist PRs as they are usually more work to review :) )

No worries at all! I recognize I've mostly been opening assist related PRs, while the steering issue says that most of the focus right now is on completions. Hope I haven't caused you too much pain from context switching!

@Veykril
Copy link
Member

Veykril commented Jul 28, 2022

Oh don't worry about the steering issue. It doesn't dictate what we work on, it's more a guide of what would be good to focus on :)

@fasterthanlime
Copy link
Contributor

Looks like y'all did the initial implementation for this assist, nice work! Pinging you here just in case you've got code review thoughts.

No thoughts here, really excited about this feature though! (it's definitely a feature in my mind)

@DorianListens DorianListens force-pushed the dscheidt/generate-enum-data branch 2 times, most recently from b3cae38 to f76f2ea Compare July 30, 2022 03:51
Comment on lines -35 to -36
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
let path = path_expr.path()?;
Copy link
Member

Choose a reason for hiding this comment

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

This should check if the parent of the path is a PathExpr, RecordExpr, PathPat, RecordPat or UseTree (or whatever node of a use tree has leaf paths, don't remember). In other locations paths cannot be enum variants so we shouldn't trigger there (unless I missed them, but a PathType for example can't)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, that makes sense. The previous version only looked for PathExpr, which is too strict, but I can see how looking for Path by itself is not strict enough.

I'll see if I can come up with a PathType example that shouldn't ever work and add a test case for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I've done this now.

Comment on lines 91 to 102
fn make_field_list(ctx: &AssistContext<'_>, path: &ast::Path) -> Option<ast::FieldList> {
let scope = ctx.sema.scope(&path.syntax())?;
if let Some(call_expr) =
path.syntax().parent().and_then(|it| it.parent()).and_then(ast::CallExpr::cast)
{
make_tuple_field_list(call_expr, ctx, &scope)
} else if let Some(record_expr) = path.syntax().parent().and_then(ast::RecordExpr::cast) {
make_record_field_list(record_expr, ctx, &scope)
} else {
None
}
}
Copy link
Member

Choose a reason for hiding this comment

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

With the checks from my previous comment, you could maybe even have a classification enum for the parent that is being used here afterwards like

enum PathClass {
    RecordExpr(ast::RecordExpr),
    RecordPat(ast::RecordPat),
    ...
}

Also as you can see, this assist could apply to more than just expressions. But I think that might be better for a followup PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I hadn't really considered trying this in pattern position, but I suppose it could work, like in a match arm condition or something. 🤯

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 introduced this enum (I called it PathParent), and looked into supporting RecordPat and TupleStructPat, but I agree that those are better suited to an additional follow on change.

@Veykril
Copy link
Member

Veykril commented Aug 2, 2022

Looks good to me aside from the missing check.
@bors delegate+

@bors
Copy link
Contributor

bors commented Aug 2, 2022

✌️ @DorianListens can now approve this pull request

@DorianListens DorianListens force-pushed the dscheidt/generate-enum-data branch 2 times, most recently from 1076a98 to d9c45ac Compare August 2, 2022 16:50
@DorianListens DorianListens force-pushed the dscheidt/generate-enum-data branch from d9c45ac to 1980c11 Compare August 2, 2022 18:37
@DorianListens
Copy link
Contributor Author

@bors r=@Veykril

@bors
Copy link
Contributor

bors commented Aug 2, 2022

📌 Commit 1980c11 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Aug 2, 2022

⌛ Testing commit 1980c11 with merge 1c03f45...

@bors
Copy link
Contributor

bors commented Aug 2, 2022

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

@bors bors merged commit 1c03f45 into rust-lang:master Aug 2, 2022
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.

'Generate enum variant' ignores data signature
4 participants