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: Bool to enum assist #15484
feat: Bool to enum assist #15484
Conversation
|
||
/// Replaces all usages of the target identifier, both when read and written to. | ||
fn replace_usages(edit: &mut SourceChangeBuilder, usages: &UsageSearchResult) { | ||
for (_, references) in usages.iter() { |
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 think it would be nice to handle cross file and module references too, especially in the case of constants. I had a version before where I would just check to see if the ctx
file is different than the usage file, then I would insert an import to the new enum, which seemed to work out alright for basic files but this doesn't handle modules within a file so I was wondering if there is another approach that would be nicer?
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.
Maybe handling a test like this could be nice?
#[test]
fn const_in_module() {
check_assist(
bool_to_enum,
r#"
fn main() {
if foo::FOO {
println!("foo");
}
}
mod foo {
pub const $0FOO: bool = true;
}
"#,
r#"
use foo::Bool;
fn main() {
if foo::FOO == Bool::True {
println!("foo");
}
}
mod foo {
#[derive(PartialEq, Eq)]
pub enum $0Bool { True, False }
pub const FOO: Bool = Bool::True;
}
"#,
)
}
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.
Two approaches may be:
- Use
ctx.sema.scope
to check if the definition and usage are in different modules and then do the appropriateinsert_use
. - Use
is_
methods for each of the variants, as done ingenerate_enum_is_method
so that there are no imports needed.
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.
Handling it cross modules properly would be great yes, you might be able to check on crates\ide-assists\src\handlers\extract_struct_from_enum_variant.rs
how things are done there. Iirc it needs to import stuff as well when rewriting other files
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 see, thanks for the pointer! I think I've got it to work in a similar way now using the import approach. The only problem is I can't seem to get the tabstop snippet to show up in the right place because of the added import, so I've just removed the tabstop for now.
|
||
/// Replaces all usages of the target identifier, both when read and written to. | ||
fn replace_usages(edit: &mut SourceChangeBuilder, usages: &UsageSearchResult) { | ||
for (_, references) in usages.iter() { |
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.
Handling it cross modules properly would be great yes, you might be able to check on crates\ide-assists\src\handlers\extract_struct_from_enum_variant.rs
how things are done there. Iirc it needs to import stuff as well when rewriting other files
☔ The latest upstream changes (presumably #15524) made this pull request unmergeable. Please resolve the merge conflicts. |
ca1cd18
to
8db4fcc
Compare
8db4fcc
to
2e13aed
Compare
@rustbot review |
Thanks! Really looking forward to using this assist 🎉 |
☀️ Test successful - checks-actions |
This adds the
bool_to_enum
assist, which converts the type of boolean local variables, fields, constants and statics to a newenum
type, making it easier to distinguish the meaning oftrue
andfalse
by renaming the variants.Closes #14779