1 use stdx::{format_to, to_lower_snake_case};
2 use syntax::ast::VisibilityOwner;
3 use syntax::ast::{self, AstNode, NameOwner};
7 utils::{find_impl_block, find_struct_impl},
8 AssistContext, AssistId, AssistKind, Assists,
11 // Assist: generate_enum_match_method
13 // Generate an `is_` method for an enum variant.
31 // /// Returns `true` if the version is [`Minor`].
32 // fn is_minor(&self) -> bool {
33 // matches!(self, Self::Minor)
37 pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 let variant = ctx.find_node_at_offset::<ast::Variant>()?;
39 let variant_name = variant.name()?;
40 let parent_enum = variant.parent_enum();
41 if !matches!(variant.kind(), ast::StructKind::Unit) {
42 mark::hit!(test_gen_enum_match_on_non_unit_variant_not_implemented);
46 let enum_lowercase_name = to_lower_snake_case(&parent_enum.name()?.to_string());
47 let fn_name = to_lower_snake_case(&variant_name.to_string());
49 // Return early if we've found an existing new fn
50 let impl_def = find_struct_impl(
52 &ast::Adt::Enum(parent_enum.clone()),
53 format!("is_{}", fn_name).as_str(),
56 let target = variant.syntax().text_range();
58 AssistId("generate_enum_match_method", AssistKind::Generate),
59 "Generate an `is_` method for an enum variant",
62 let mut buf = String::with_capacity(512);
64 if impl_def.is_some() {
68 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
71 " /// Returns `true` if the {} is [`{}`].
72 {}fn is_{}(&self) -> bool {{
73 matches!(self, Self::{})
82 let start_offset = impl_def
83 .and_then(|impl_def| find_impl_block(impl_def, &mut buf))
85 buf = generate_impl_text(&parent_enum, &buf);
86 parent_enum.syntax().text_range().end()
89 builder.insert(start_offset, buf);
94 // Generates the surrounding `impl Type { <code> }` including type and lifetime
96 fn generate_impl_text(strukt: &ast::Enum, code: &str) -> String {
97 let mut buf = String::with_capacity(code.len());
98 buf.push_str("\n\nimpl ");
99 buf.push_str(strukt.name().unwrap().text());
100 format_to!(buf, " {{\n{}\n}}", code);
106 use test_utils::mark;
108 use crate::tests::{check_assist, check_assist_not_applicable};
112 fn check_not_applicable(ra_fixture: &str) {
113 check_assist_not_applicable(generate_enum_match_method, ra_fixture)
117 fn test_generate_enum_match_from_variant() {
119 generate_enum_match_method,
133 /// Returns `true` if the variant is [`Minor`].
134 fn is_minor(&self) -> bool {
135 matches!(self, Self::Minor)
142 fn test_generate_enum_match_already_implemented() {
143 check_not_applicable(
152 fn is_minor(&self) -> bool {
153 matches!(self, Self::Minor)
160 fn test_add_from_impl_no_element() {
161 mark::check!(test_gen_enum_match_on_non_unit_variant_not_implemented);
162 check_not_applicable(
173 fn test_generate_enum_match_from_variant_with_one_variant() {
175 generate_enum_match_method,
176 r#"enum Variant { Undefi$0ned }"#,
178 enum Variant { Undefined }
181 /// Returns `true` if the variant is [`Undefined`].
182 fn is_undefined(&self) -> bool {
183 matches!(self, Self::Undefined)
190 fn test_generate_enum_match_from_variant_with_visibility_marker() {
192 generate_enum_match_method,
194 pub(crate) enum Variant {
199 r#"pub(crate) enum Variant {
206 /// Returns `true` if the variant is [`Minor`].
207 pub(crate) fn is_minor(&self) -> bool {
208 matches!(self, Self::Minor)