aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/generate_enum_match_method.rs
diff options
context:
space:
mode:
authorYoshua Wuyts <[email protected]>2021-02-05 15:32:34 +0000
committerYoshua Wuyts <[email protected]>2021-02-05 16:19:31 +0000
commit2cf161266941eac300ae66a633ead26f5109ea16 (patch)
treef7909b18efed14e2a6bd5f410b50310a2167b72d /crates/assists/src/handlers/generate_enum_match_method.rs
parentae7bee70a10d01f0fdebfc12c70aca4f765826b3 (diff)
Add `find_or_create_impl_block` to assist utils
Diffstat (limited to 'crates/assists/src/handlers/generate_enum_match_method.rs')
-rw-r--r--crates/assists/src/handlers/generate_enum_match_method.rs20
1 files changed, 6 insertions, 14 deletions
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 @@
1use stdx::{format_to, to_lower_snake_case}; 1use stdx::{format_to, to_lower_snake_case};
2use syntax::ast::VisibilityOwner;
2use syntax::ast::{self, AstNode, NameOwner}; 3use syntax::ast::{self, AstNode, NameOwner};
3use syntax::{ast::VisibilityOwner, T};
4use test_utils::mark; 4use test_utils::mark;
5 5
6use crate::{utils::find_struct_impl, AssistContext, AssistId, AssistKind, Assists}; 6use crate::{
7 utils::{find_impl_block, find_struct_impl},
8 AssistContext, AssistId, AssistKind, Assists,
9};
7 10
8// Assist: generate_enum_match_method 11// Assist: generate_enum_match_method
9// 12//
@@ -61,7 +64,6 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
61 } 64 }
62 65
63 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); 66 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
64
65 format_to!( 67 format_to!(
66 buf, 68 buf,
67 " {}fn is_{}(&self) -> bool {{ 69 " {}fn is_{}(&self) -> bool {{
@@ -73,17 +75,7 @@ pub(crate) fn generate_enum_match_method(acc: &mut Assists, ctx: &AssistContext)
73 ); 75 );
74 76
75 let start_offset = impl_def 77 let start_offset = impl_def
76 .and_then(|impl_def| { 78 .and_then(|impl_def| find_impl_block(impl_def, &mut buf))
77 buf.push('\n');
78 let start = impl_def
79 .syntax()
80 .descendants_with_tokens()
81 .find(|t| t.kind() == T!['{'])?
82 .text_range()
83 .end();
84
85 Some(start)
86 })
87 .unwrap_or_else(|| { 79 .unwrap_or_else(|| {
88 buf = generate_impl_text(&parent_enum, &buf); 80 buf = generate_impl_text(&parent_enum, &buf);
89 parent_enum.syntax().text_range().end() 81 parent_enum.syntax().text_range().end()