create generate is, as, try_into group

This commit is contained in:
Jake Vossen 2022-03-31 21:19:45 -06:00
parent a1d684e951
commit bccf013010
2 changed files with 28 additions and 19 deletions

View File

@ -1,3 +1,4 @@
use ide_db::assists::GroupLabel;
use stdx::to_lower_snake_case; use stdx::to_lower_snake_case;
use syntax::ast::HasVisibility; use syntax::ast::HasVisibility;
use syntax::ast::{self, AstNode, HasName}; use syntax::ast::{self, AstNode, HasName};
@ -54,7 +55,8 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?; let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?;
let target = variant.syntax().text_range(); let target = variant.syntax().text_range();
acc.add( acc.add_group(
&GroupLabel("Generate `is_`,`as_`,`try_into_`".to_owned()),
AssistId("generate_enum_is_method", AssistKind::Generate), AssistId("generate_enum_is_method", AssistKind::Generate),
"Generate an `is_` method for an enum variant", "Generate an `is_` method for an enum variant",
target, target,

View File

@ -1,3 +1,4 @@
use ide_db::assists::GroupLabel;
use itertools::Itertools; use itertools::Itertools;
use stdx::to_lower_snake_case; use stdx::to_lower_snake_case;
use syntax::ast::HasVisibility; use syntax::ast::HasVisibility;
@ -139,31 +140,37 @@ fn generate_enum_projection_method(
let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?; let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?;
let target = variant.syntax().text_range(); let target = variant.syntax().text_range();
acc.add(AssistId(assist_id, AssistKind::Generate), assist_description, target, |builder| { acc.add_group(
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); &GroupLabel("Generate `is_`,`as_`,`try_into_`".to_owned()),
let method = format!( AssistId(assist_id, AssistKind::Generate),
" {0}fn {1}({2}) -> {3}{4}{5} {{ assist_description,
target,
|builder| {
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
let method = format!(
" {0}fn {1}({2}) -> {3}{4}{5} {{
if let Self::{6}{7} = self {{ if let Self::{6}{7} = self {{
{8}({9}) {8}({9})
}} else {{ }} else {{
{10} {10}
}} }}
}}", }}",
vis, vis,
fn_name, fn_name,
props.self_param, props.self_param,
props.return_prefix, props.return_prefix,
field_type.syntax(), field_type.syntax(),
props.return_suffix, props.return_suffix,
variant_name, variant_name,
pattern_suffix, pattern_suffix,
props.happy_case, props.happy_case,
bound_name, bound_name,
props.sad_case, props.sad_case,
); );
add_method_to_adt(builder, &parent_enum, impl_def, &method); add_method_to_adt(builder, &parent_enum, impl_def, &method);
}) },
)
} }
#[cfg(test)] #[cfg(test)]