Skip to content

Generate enum variant assist #12334

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
May 22, 2022
Merged

Generate enum variant assist #12334

merged 6 commits into from
May 22, 2022

Conversation

fasterthanlime
Copy link
Contributor

@fasterthanlime fasterthanlime commented May 20, 2022

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:

  • Only disable the "generate function" assist if the name is PascalCase
  • Only enable the "generate variant" assistant if the name is PascalCase
  • Simplify with adt.source() as mentioned here: fix: skip the generate function assist when dealing with an enum variant #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

fasterthanlime and others added 4 commits May 21, 2022 01:18
This also disables "generate function" when what we clearly want is to
generate an enum variant.

Co-authored-by: Maarten Flippo <maartenflippo@outlook.com>
@fasterthanlime fasterthanlime marked this pull request as ready for review May 20, 2022 23:43
}

let name_ref = path.segment()?.name_ref()?;
if name_ref.text().as_str().chars().next()?.is_ascii_lowercase() {
Copy link
Member

@Veykril Veykril May 22, 2022

Choose a reason for hiding this comment

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

Suggested change
if name_ref.text().as_str().chars().next()?.is_ascii_lowercase() {
if name_ref.text().as_str().starts_with(char::is_lowercase) {

Similar can be done for the uppercase check in generate_function. Going with the non-ascii version probably makes more more sense here (though I don't really know of anyone NOT using the latin alphabet set for identifiers so it really shouldn't matter)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 796c4d8

@Veykril
Copy link
Member

Veykril commented May 22, 2022

Or you can tell me "okay, now you finish it", and I guess I'll try and finish it :)

I suppose there is no need to say this anymore 😄

Since you have included the original commit author for the rebased commit I personally think this is fine. Thanks!

You are more than welcome to pick up your own issues from here on of course (and ask for guidance/mentoring on some that seem interesting to you :) )

@bors delegate+

@bors
Copy link
Contributor

bors commented May 22, 2022

✌️ @fasterthanlime can now approve this pull request

Comment on lines 77 to 84
let text = format!("{}{},\n{}", prefix, name_ref, enum_indent_level);

acc.add(
AssistId("generate_enum_variant", AssistKind::Generate),
"Generate variant",
target,
|builder| builder.insert(offset, text),
)
Copy link
Member

Choose a reason for hiding this comment

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

Ah, missed this. Let's pull the text creation into the closure since that is evaluated lazily.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in ae2c0db

@fasterthanlime
Copy link
Contributor Author

fasterthanlime commented May 22, 2022

Took care of those last two review comments — the only thing I'm still unhappy with it the indentation in the method generation assist, but I suppose that's for a separate PR:

#[test]
fn applicable_for_enum_method() {
check_assist(
generate_function,
r"
enum Foo {}
fn main() {
Foo::new$0();
}
",
r"
enum Foo {}
fn main() {
Foo::new();
}
impl Foo {
fn new() ${0:-> _} {
todo!()
}
}
",

For now I just made the test match the assist's output.

@Veykril
Copy link
Member

Veykril commented May 22, 2022

Indentation and whitespace formatting is a big annoyance for us in assists still. We try to fix that up to some degree manually (within reasonable limits of code readability). So having non-perfect output for these is alright for the time being. We are planning on having a proper formatter that works on our syntax trees in the future to get rid of all those manual fixups.

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented May 22, 2022

📌 Commit ae2c0db has been approved by Veykril

@bors
Copy link
Contributor

bors commented May 22, 2022

⌛ Testing commit ae2c0db with merge 4b0f9c5...

@bors
Copy link
Contributor

bors commented May 22, 2022

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 4b0f9c5 to master...

@lnicola
Copy link
Member

lnicola commented May 23, 2022

generate-variant.mp4

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.

New assist: add enum variant
4 participants