From 2cf161266941eac300ae66a633ead26f5109ea16 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Fri, 5 Feb 2021 16:32:34 +0100 Subject: Add `find_or_create_impl_block` to assist utils --- .../src/handlers/generate_enum_match_method.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 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 270b438b7..2345a61c1 100644 --- a/crates/assists/src/handlers/generate_enum_match_method.rs +++ b/crates/assists/src/handlers/generate_enum_match_method.rs @@ -1,9 +1,12 @@ use stdx::{format_to, to_lower_snake_case}; +use syntax::ast::VisibilityOwner; use syntax::ast::{self, AstNode, NameOwner}; -use syntax::{ast::VisibilityOwner, T}; use test_utils::mark; -use crate::{utils::find_struct_impl, AssistContext, AssistId, AssistKind, Assists}; +use crate::{ + utils::{find_impl_block, find_struct_impl}, + AssistContext, AssistId, AssistKind, Assists, +}; // Assist: generate_enum_match_method // @@ -61,7 +64,6 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) } let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); - format_to!( buf, " {}fn is_{}(&self) -> bool {{ @@ -73,17 +75,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext) ); let start_offset = impl_def - .and_then(|impl_def| { - buf.push('\n'); - let start = impl_def - .syntax() - .descendants_with_tokens() - .find(|t| t.kind() == T!['{'])? - .text_range() - .end(); - - Some(start) - }) + .and_then(|impl_def| find_impl_block(impl_def, &mut buf)) .unwrap_or_else(|| { buf = generate_impl_text(&parent_enum, &buf); parent_enum.syntax().text_range().end() -- cgit v1.2.3