mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Auto merge of #15484 - rmehri01:14779_bool_to_enum_assist, r=Veykril
feat: Bool to enum assist This adds the `bool_to_enum` assist, which converts the type of boolean local variables, fields, constants and statics to a new `enum` type, making it easier to distinguish the meaning of `true` and `false` by renaming the variants. Closes #14779
This commit is contained in:
commit
df75809a85
4 changed files with 1572 additions and 0 deletions
1514
crates/ide-assists/src/handlers/bool_to_enum.rs
Normal file
1514
crates/ide-assists/src/handlers/bool_to_enum.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -115,6 +115,7 @@ mod handlers {
|
|||
mod apply_demorgan;
|
||||
mod auto_import;
|
||||
mod bind_unused_param;
|
||||
mod bool_to_enum;
|
||||
mod change_visibility;
|
||||
mod convert_bool_then;
|
||||
mod convert_comment_block;
|
||||
|
@ -227,6 +228,7 @@ mod handlers {
|
|||
apply_demorgan::apply_demorgan,
|
||||
auto_import::auto_import,
|
||||
bind_unused_param::bind_unused_param,
|
||||
bool_to_enum::bool_to_enum,
|
||||
change_visibility::change_visibility,
|
||||
convert_bool_then::convert_bool_then_to_if,
|
||||
convert_bool_then::convert_if_to_bool_then,
|
||||
|
|
|
@ -280,6 +280,34 @@ fn some_function(x: i32) {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_bool_to_enum() {
|
||||
check_doc_test(
|
||||
"bool_to_enum",
|
||||
r#####"
|
||||
fn main() {
|
||||
let $0bool = true;
|
||||
|
||||
if bool {
|
||||
println!("foo");
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Bool { True, False }
|
||||
|
||||
let bool = Bool::True;
|
||||
|
||||
if bool == Bool::True {
|
||||
println!("foo");
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_change_visibility() {
|
||||
check_doc_test(
|
||||
|
|
|
@ -972,6 +972,11 @@ pub fn tuple_field(visibility: Option<ast::Visibility>, ty: ast::Type) -> ast::T
|
|||
ast_from_text(&format!("struct f({visibility}{ty});"))
|
||||
}
|
||||
|
||||
pub fn variant_list(variants: impl IntoIterator<Item = ast::Variant>) -> ast::VariantList {
|
||||
let variants = variants.into_iter().join(", ");
|
||||
ast_from_text(&format!("enum f {{ {variants} }}"))
|
||||
}
|
||||
|
||||
pub fn variant(name: ast::Name, field_list: Option<ast::FieldList>) -> ast::Variant {
|
||||
let field_list = match field_list {
|
||||
None => String::new(),
|
||||
|
@ -1036,6 +1041,19 @@ pub fn struct_(
|
|||
ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
|
||||
}
|
||||
|
||||
pub fn enum_(
|
||||
visibility: Option<ast::Visibility>,
|
||||
enum_name: ast::Name,
|
||||
variant_list: ast::VariantList,
|
||||
) -> ast::Enum {
|
||||
let visibility = match visibility {
|
||||
None => String::new(),
|
||||
Some(it) => format!("{it} "),
|
||||
};
|
||||
|
||||
ast_from_text(&format!("{visibility}enum {enum_name} {variant_list}"))
|
||||
}
|
||||
|
||||
pub fn attr_outer(meta: ast::Meta) -> ast::Attr {
|
||||
ast_from_text(&format!("#[{meta}]"))
|
||||
}
|
||||
|
@ -1148,6 +1166,16 @@ pub mod tokens {
|
|||
lit.syntax().first_child_or_token().unwrap().into_token().unwrap()
|
||||
}
|
||||
|
||||
pub fn ident(text: &str) -> SyntaxToken {
|
||||
assert_eq!(text.trim(), text);
|
||||
let path: ast::Path = super::ext::ident_path(text);
|
||||
path.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == IDENT)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn single_newline() -> SyntaxToken {
|
||||
let res = SOURCE_FILE
|
||||
.tree()
|
||||
|
|
Loading…
Reference in a new issue