rust/crates/ide_assists/src/handlers/generate_enum_is_method.rs

251 lines
5.3 KiB
Rust
Raw Normal View History

2021-02-14 04:15:20 -06:00
use stdx::to_lower_snake_case;
use syntax::ast::VisibilityOwner;
2021-02-04 18:57:39 -06:00
use syntax::ast::{self, AstNode, NameOwner};
2021-02-14 11:28:22 -06:00
use crate::{
utils::{add_method_to_adt, find_struct_impl},
2021-02-14 11:28:22 -06:00
AssistContext, AssistId, AssistKind, Assists,
};
2021-02-04 18:57:39 -06:00
// Assist: generate_enum_is_method
2021-02-04 18:57:39 -06:00
//
// Generate an `is_` method for an enum variant.
//
// ```
// enum Version {
// Undefined,
// Minor$0,
// Major,
// }
// ```
// ->
// ```
// enum Version {
// Undefined,
// Minor,
// Major,
// }
//
// impl Version {
// /// Returns `true` if the version is [`Minor`].
2021-02-04 18:57:39 -06:00
// fn is_minor(&self) -> bool {
// matches!(self, Self::Minor)
// }
// }
// ```
pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2021-02-04 18:57:39 -06:00
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
let variant_name = variant.name()?;
let parent_enum = ast::Adt::Enum(variant.parent_enum());
let pattern_suffix = match variant.kind() {
ast::StructKind::Record(_) => " { .. }",
ast::StructKind::Tuple(_) => "(..)",
ast::StructKind::Unit => "",
};
2021-02-04 18:57:39 -06:00
let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string());
let fn_name = format!("is_{}", &to_lower_snake_case(&variant_name.text()));
2021-02-04 18:57:39 -06:00
// Return early if we've found an existing new fn
2021-06-12 22:54:16 -05:00
let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?;
2021-02-04 18:57:39 -06:00
let target = variant.syntax().text_range();
acc.add(
AssistId("generate_enum_is_method", AssistKind::Generate),
2021-02-04 18:57:39 -06:00
"Generate an `is_` method for an enum variant",
target,
|builder| {
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
2021-02-14 04:15:20 -06:00
let method = format!(
" /// Returns `true` if the {} is [`{}`].
{}fn {}(&self) -> bool {{
matches!(self, Self::{}{})
2021-02-04 18:57:39 -06:00
}}",
enum_lowercase_name, variant_name, vis, fn_name, variant_name, pattern_suffix,
2021-02-04 18:57:39 -06:00
);
2021-02-14 04:15:20 -06:00
add_method_to_adt(builder, &parent_enum, impl_def, &method);
2021-02-04 18:57:39 -06:00
},
)
}
#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_not_applicable};
use super::*;
#[test]
2021-02-14 04:06:42 -06:00
fn test_generate_enum_is_from_variant() {
2021-02-04 18:57:39 -06:00
check_assist(
generate_enum_is_method,
2021-02-04 18:57:39 -06:00
r#"
enum Variant {
Undefined,
Minor$0,
Major,
}"#,
r#"enum Variant {
Undefined,
Minor,
Major,
}
impl Variant {
/// Returns `true` if the variant is [`Minor`].
2021-02-04 18:57:39 -06:00
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}"#,
);
}
#[test]
2021-02-14 04:06:42 -06:00
fn test_generate_enum_is_already_implemented() {
check_assist_not_applicable(
generate_enum_is_method,
2021-02-04 18:57:39 -06:00
r#"
enum Variant {
Undefined,
Minor$0,
Major,
}
impl Variant {
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}"#,
);
}
#[test]
2021-02-14 04:06:42 -06:00
fn test_generate_enum_is_from_tuple_variant() {
check_assist(
generate_enum_is_method,
2021-02-04 18:57:39 -06:00
r#"
enum Variant {
Undefined,
Minor(u32)$0,
Major,
}"#,
r#"enum Variant {
Undefined,
Minor(u32),
Major,
}
impl Variant {
/// Returns `true` if the variant is [`Minor`].
fn is_minor(&self) -> bool {
matches!(self, Self::Minor(..))
}
}"#,
);
}
#[test]
2021-02-14 04:06:42 -06:00
fn test_generate_enum_is_from_record_variant() {
check_assist(
generate_enum_is_method,
r#"
enum Variant {
Undefined,
Minor { foo: i32 }$0,
Major,
}"#,
r#"enum Variant {
Undefined,
Minor { foo: i32 },
Major,
}
impl Variant {
/// Returns `true` if the variant is [`Minor`].
fn is_minor(&self) -> bool {
matches!(self, Self::Minor { .. })
}
2021-02-04 18:57:39 -06:00
}"#,
);
}
#[test]
2021-02-14 04:06:42 -06:00
fn test_generate_enum_is_from_variant_with_one_variant() {
2021-02-04 18:57:39 -06:00
check_assist(
generate_enum_is_method,
2021-02-04 18:57:39 -06:00
r#"enum Variant { Undefi$0ned }"#,
r#"
enum Variant { Undefined }
impl Variant {
/// Returns `true` if the variant is [`Undefined`].
2021-02-04 18:57:39 -06:00
fn is_undefined(&self) -> bool {
matches!(self, Self::Undefined)
}
}"#,
);
}
#[test]
2021-02-14 04:06:42 -06:00
fn test_generate_enum_is_from_variant_with_visibility_marker() {
2021-02-04 18:57:39 -06:00
check_assist(
generate_enum_is_method,
2021-02-04 18:57:39 -06:00
r#"
pub(crate) enum Variant {
Undefined,
Minor$0,
Major,
}"#,
r#"pub(crate) enum Variant {
Undefined,
Minor,
Major,
}
impl Variant {
/// Returns `true` if the variant is [`Minor`].
2021-02-04 18:57:39 -06:00
pub(crate) fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}"#,
);
}
#[test]
2021-02-14 04:06:42 -06:00
fn test_multiple_generate_enum_is_from_variant() {
check_assist(
generate_enum_is_method,
r#"
enum Variant {
Undefined,
Minor,
Major$0,
}
impl Variant {
/// Returns `true` if the variant is [`Minor`].
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}"#,
r#"enum Variant {
Undefined,
Minor,
Major,
}
impl Variant {
/// Returns `true` if the variant is [`Minor`].
fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
/// Returns `true` if the variant is [`Major`].
fn is_major(&self) -> bool {
matches!(self, Self::Major)
}
2021-02-04 18:57:39 -06:00
}"#,
);
}
}