From d4b6cbe5b6b9c90b577da7788669b6b6995ae524 Mon Sep 17 00:00:00 2001 From: Domantas Jadenkus Date: Mon, 15 Feb 2021 23:28:57 +0200 Subject: rename generate_enum_match_method file to match assist name --- .../src/handlers/generate_enum_is_method.rs | 250 +++++++++++++++++++++ .../src/handlers/generate_enum_match_method.rs | 250 --------------------- crates/ide_assists/src/lib.rs | 4 +- 3 files changed, 252 insertions(+), 252 deletions(-) create mode 100644 crates/ide_assists/src/handlers/generate_enum_is_method.rs delete mode 100644 crates/ide_assists/src/handlers/generate_enum_match_method.rs diff --git a/crates/ide_assists/src/handlers/generate_enum_is_method.rs b/crates/ide_assists/src/handlers/generate_enum_is_method.rs new file mode 100644 index 000000000..7e181a480 --- /dev/null +++ b/crates/ide_assists/src/handlers/generate_enum_is_method.rs @@ -0,0 +1,250 @@ +use stdx::to_lower_snake_case; +use syntax::ast::VisibilityOwner; +use syntax::ast::{self, AstNode, NameOwner}; + +use crate::{ + utils::{add_method_to_adt, find_struct_impl}, + AssistContext, AssistId, AssistKind, Assists, +}; + +// Assist: generate_enum_is_method +// +// 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`]. +// fn is_minor(&self) -> bool { +// matches!(self, Self::Minor) +// } +// } +// ``` +pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { + let variant = ctx.find_node_at_offset::()?; + 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 => "", + }; + + 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())); + + // Return early if we've found an existing new fn + let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?; + + let target = variant.syntax().text_range(); + acc.add( + AssistId("generate_enum_is_method", AssistKind::Generate), + "Generate an `is_` method for an enum variant", + target, + |builder| { + let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); + let method = format!( + " /// Returns `true` if the {} is [`{}`]. + {}fn {}(&self) -> bool {{ + matches!(self, Self::{}{}) + }}", + enum_lowercase_name, variant_name, vis, fn_name, variant_name, pattern_suffix, + ); + + add_method_to_adt(builder, &parent_enum, impl_def, &method); + }, + ) +} + +#[cfg(test)] +mod tests { + use crate::tests::{check_assist, check_assist_not_applicable}; + + use super::*; + + #[test] + fn test_generate_enum_is_from_variant() { + check_assist( + generate_enum_is_method, + r#" +enum Variant { + Undefined, + Minor$0, + Major, +}"#, + r#"enum Variant { + Undefined, + Minor, + Major, +} + +impl Variant { + /// Returns `true` if the variant is [`Minor`]. + fn is_minor(&self) -> bool { + matches!(self, Self::Minor) + } +}"#, + ); + } + + #[test] + fn test_generate_enum_is_already_implemented() { + check_assist_not_applicable( + generate_enum_is_method, + r#" +enum Variant { + Undefined, + Minor$0, + Major, +} + +impl Variant { + fn is_minor(&self) -> bool { + matches!(self, Self::Minor) + } +}"#, + ); + } + + #[test] + fn test_generate_enum_is_from_tuple_variant() { + check_assist( + generate_enum_is_method, + 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] + 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 { .. }) + } +}"#, + ); + } + + #[test] + fn test_generate_enum_is_from_variant_with_one_variant() { + check_assist( + generate_enum_is_method, + r#"enum Variant { Undefi$0ned }"#, + r#" +enum Variant { Undefined } + +impl Variant { + /// Returns `true` if the variant is [`Undefined`]. + fn is_undefined(&self) -> bool { + matches!(self, Self::Undefined) + } +}"#, + ); + } + + #[test] + fn test_generate_enum_is_from_variant_with_visibility_marker() { + check_assist( + generate_enum_is_method, + 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`]. + pub(crate) fn is_minor(&self) -> bool { + matches!(self, Self::Minor) + } +}"#, + ); + } + + #[test] + 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) + } +}"#, + ); + } +} diff --git a/crates/ide_assists/src/handlers/generate_enum_match_method.rs b/crates/ide_assists/src/handlers/generate_enum_match_method.rs deleted file mode 100644 index 7e181a480..000000000 --- a/crates/ide_assists/src/handlers/generate_enum_match_method.rs +++ /dev/null @@ -1,250 +0,0 @@ -use stdx::to_lower_snake_case; -use syntax::ast::VisibilityOwner; -use syntax::ast::{self, AstNode, NameOwner}; - -use crate::{ - utils::{add_method_to_adt, find_struct_impl}, - AssistContext, AssistId, AssistKind, Assists, -}; - -// Assist: generate_enum_is_method -// -// 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`]. -// fn is_minor(&self) -> bool { -// matches!(self, Self::Minor) -// } -// } -// ``` -pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - let variant = ctx.find_node_at_offset::()?; - 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 => "", - }; - - 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())); - - // Return early if we've found an existing new fn - let impl_def = find_struct_impl(&ctx, &parent_enum, &fn_name)?; - - let target = variant.syntax().text_range(); - acc.add( - AssistId("generate_enum_is_method", AssistKind::Generate), - "Generate an `is_` method for an enum variant", - target, - |builder| { - let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); - let method = format!( - " /// Returns `true` if the {} is [`{}`]. - {}fn {}(&self) -> bool {{ - matches!(self, Self::{}{}) - }}", - enum_lowercase_name, variant_name, vis, fn_name, variant_name, pattern_suffix, - ); - - add_method_to_adt(builder, &parent_enum, impl_def, &method); - }, - ) -} - -#[cfg(test)] -mod tests { - use crate::tests::{check_assist, check_assist_not_applicable}; - - use super::*; - - #[test] - fn test_generate_enum_is_from_variant() { - check_assist( - generate_enum_is_method, - r#" -enum Variant { - Undefined, - Minor$0, - Major, -}"#, - r#"enum Variant { - Undefined, - Minor, - Major, -} - -impl Variant { - /// Returns `true` if the variant is [`Minor`]. - fn is_minor(&self) -> bool { - matches!(self, Self::Minor) - } -}"#, - ); - } - - #[test] - fn test_generate_enum_is_already_implemented() { - check_assist_not_applicable( - generate_enum_is_method, - r#" -enum Variant { - Undefined, - Minor$0, - Major, -} - -impl Variant { - fn is_minor(&self) -> bool { - matches!(self, Self::Minor) - } -}"#, - ); - } - - #[test] - fn test_generate_enum_is_from_tuple_variant() { - check_assist( - generate_enum_is_method, - 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] - 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 { .. }) - } -}"#, - ); - } - - #[test] - fn test_generate_enum_is_from_variant_with_one_variant() { - check_assist( - generate_enum_is_method, - r#"enum Variant { Undefi$0ned }"#, - r#" -enum Variant { Undefined } - -impl Variant { - /// Returns `true` if the variant is [`Undefined`]. - fn is_undefined(&self) -> bool { - matches!(self, Self::Undefined) - } -}"#, - ); - } - - #[test] - fn test_generate_enum_is_from_variant_with_visibility_marker() { - check_assist( - generate_enum_is_method, - 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`]. - pub(crate) fn is_minor(&self) -> bool { - matches!(self, Self::Minor) - } -}"#, - ); - } - - #[test] - 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) - } -}"#, - ); - } -} diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 4a7cd5848..c5137df96 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs @@ -128,7 +128,7 @@ mod handlers { mod flip_trait_bound; mod generate_default_from_enum_variant; mod generate_derive; - mod generate_enum_match_method; + mod generate_enum_is_method; mod generate_enum_projection_method; mod generate_from_impl_for_enum; mod generate_function; @@ -190,7 +190,7 @@ mod handlers { flip_trait_bound::flip_trait_bound, generate_default_from_enum_variant::generate_default_from_enum_variant, generate_derive::generate_derive, - generate_enum_match_method::generate_enum_is_method, + generate_enum_is_method::generate_enum_is_method, generate_enum_projection_method::generate_enum_into_method, generate_enum_projection_method::generate_enum_as_method, generate_from_impl_for_enum::generate_from_impl_for_enum, -- cgit v1.2.3