-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix: skip the generate function assist when dealing with an enum variant #11995
fix: skip the generate function assist when dealing with an enum variant #11995
Conversation
The issue didn't ask about us skipping that part, but instead given the capitalization of the name offering to create an enum variant instead. |
Oh okay, I thought so initially but due to the link to I will expand this PR to fully accommodate this feature then. To make sure I am on the right track, is it fair to say this warrants a separate assist, as opposed to modifying the |
Ye I think making that a new assist is fine. It doesn't need much of the things |
☔ The latest upstream changes (presumably #12118) made this pull request unmergeable. Please resolve the merge conflicts. |
if let hir::Adt::Enum(_) = adt { | ||
return 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.
We shouldn't disable this assist for enums in general, we should disable it if the type is an enum and the function name is PascalCase
, that is the function name starts with an uppercase letter
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.
Likewise the generate_enum_variant
assist should only trigger on enums when the function name starts with an uppercase letter
let range = adt.source(ctx.db())?.syntax().original_file_range(ctx.db()); | ||
let file = ctx.sema.parse(range.file_id); | ||
let adt_ast: ast::Enum = | ||
ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?; | ||
|
||
Some(adt_ast) |
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 can be simplified with
adt.source(ctx.db())?.original_ast_node(ctx.db())?
Generate enum variant assist So, this is kind of a weird PR! I'm a complete newcomer to the `rust-analyzer` codebase, and so I browsed the "good first issue" tag, and found #11635. Then I found two separate folks had taken stabs at it, most recently `@maartenflippo` — and there had been a review 3 days ago, but no activity in a little while, and the PR needed to be rebased since the crates were renamed from `snake_case` to `kebab-case`. So to get acquainted with the codebase I typed this PR by hand, looking at the diff in #11995, and I also added a doc-test (that passes). I haven't taken into account the comments `@Veykril` left in #11995, but I don't want to steal any of `@maartenflippo's` thunder! Closing this PR is perfectly fine. Or Maarten could use it as a "restart point"? Or I could finish it up, whichever feels best to everyone. I think what remains to be done in this PR, at least, is: * [x] Only disable the "generate function" assist if the name is `PascalCase` * [x] Only enable the "generate variant" assistant if the name is `PascalCase` * [x] Simplify with `adt.source()` as mentioned here: #11995 (comment) * [ ] Add more tests for edge cases? Are there cases where simply adding one more indent level than the enum's indent level is not good enough? Some nested trickery I'm not thinking of right now? Anyway. This PR can go in any direction. You can tell me "no, tackle your own issue!" And I'll go do that and still be happy I got to take a look at rust-analyzer some by doing this. Or you can tell me "okay, now _you_ finish it", and I guess I'll try and finish it :) Closes #11635
Hey, #12334 build upon your PR and has been merged, keeping your commit and authorship of said commit. I hope you don't mind that since your work done here is still addressed as yours. Thanks for the work you've done! |
Fixes #11635. Being my first PR, I have gone through the development docs, and hopefully I haven't missed any critical points.
From a style perspective, the check could also be done by adding the following match arm:
However, this is rather long so I was unsure which one to go for.