-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat: support associated values in "Generate Enum Variant" assist #12837
Conversation
db069f2
to
592d7ca
Compare
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.movAfter: Screen.Recording.2022-07-27.at.11.40.40.AM.mov |
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) |
592d7ca
to
292a133
Compare
292a133
to
b38ece5
Compare
@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. |
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! |
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 :) |
No thoughts here, really excited about this feature though! (it's definitely a feature in my mind) |
b3cae38
to
f76f2ea
Compare
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; | ||
let path = path_expr.path()?; |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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. 🤯
There was a problem hiding this comment.
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.
Looks good to me aside from the missing check. |
✌️ @DorianListens can now approve this pull request |
1076a98
to
d9c45ac
Compare
PathType path parents don't support this assist
d9c45ac
to
1980c11
Compare
☀️ Test successful - checks-actions |
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:
edit_in_place
generate_enum_variant
to use structural ast editing instead of string manipulationPlease let me know if I should leave the commits as-is, or squash before merging.
Fixes #12797