From ac959b82b3408fafd22f4fbb59e10383a18c545f Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Fri, 12 Feb 2021 11:48:43 +0100 Subject: Add `find_impl_block_end` assist helper --- .../src/handlers/generate_enum_match_method.rs | 41 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'crates/assists/src/handlers/generate_enum_match_method.rs') diff --git a/crates/assists/src/handlers/generate_enum_match_method.rs b/crates/assists/src/handlers/generate_enum_match_method.rs index c3ff38b66..aeb887e71 100644 --- a/crates/assists/src/handlers/generate_enum_match_method.rs +++ b/crates/assists/src/handlers/generate_enum_match_method.rs @@ -4,7 +4,7 @@ use syntax::ast::{self, AstNode, NameOwner}; use test_utils::mark; use crate::{ - utils::{find_impl_block, find_struct_impl, generate_impl_text}, + utils::{find_impl_block_end, find_struct_impl, generate_impl_text}, AssistContext, AssistId, AssistKind, Assists, }; @@ -80,7 +80,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) ); let start_offset = impl_def - .and_then(|impl_def| find_impl_block(impl_def, &mut buf)) + .and_then(|impl_def| find_impl_block_end(impl_def, &mut buf)) .unwrap_or_else(|| { buf = generate_impl_text(&ast::Adt::Enum(parent_enum.clone()), &buf); parent_enum.syntax().text_range().end() @@ -197,6 +197,43 @@ impl Variant { pub(crate) fn is_minor(&self) -> bool { matches!(self, Self::Minor) } +}"#, + ); + } + + #[test] + fn test_multiple_generate_enum_match_from_variant() { + check_assist( + generate_enum_match_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) + } }"#, ); } -- cgit v1.2.3