Skip to content

Commit

Permalink
create generate is, as, try_into group
Browse files Browse the repository at this point in the history
  • Loading branch information
jakevossen5 committed Apr 1, 2022
1 parent a1d684e commit bccf013
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
4 changes: 3 additions & 1 deletion crates/ide_assists/src/handlers/generate_enum_is_method.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ide_db::assists::GroupLabel;
use stdx::to_lower_snake_case;
use syntax::ast::HasVisibility;
use syntax::ast::{self, AstNode, HasName};
Expand Down Expand Up @@ -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 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),
"Generate an `is_` method for an enum variant",
target,
Expand Down
43 changes: 25 additions & 18 deletions crates/ide_assists/src/handlers/generate_enum_projection_method.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ide_db::assists::GroupLabel;
use itertools::Itertools;
use stdx::to_lower_snake_case;
use syntax::ast::HasVisibility;
Expand Down Expand Up @@ -139,31 +140,37 @@ fn generate_enum_projection_method(
let impl_def = find_struct_impl(ctx, &parent_enum, &fn_name)?;

let target = variant.syntax().text_range();
acc.add(AssistId(assist_id, AssistKind::Generate), 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} {{
acc.add_group(
&GroupLabel("Generate `is_`,`as_`,`try_into_`".to_owned()),
AssistId(assist_id, AssistKind::Generate),
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 {{
{8}({9})
}} else {{
{10}
}}
}}",
vis,
fn_name,
props.self_param,
props.return_prefix,
field_type.syntax(),
props.return_suffix,
variant_name,
pattern_suffix,
props.happy_case,
bound_name,
props.sad_case,
);
vis,
fn_name,
props.self_param,
props.return_prefix,
field_type.syntax(),
props.return_suffix,
variant_name,
pattern_suffix,
props.happy_case,
bound_name,
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)]
Expand Down

0 comments on commit bccf013

Please sign in to comment.