Skip to content

Commit 6427869

Browse files
committed
deduplicate some
1 parent 2dcd5d7 commit 6427869

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

crates/ide_assists/src/handlers/generate_enum_match_method.rs

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use itertools::Itertools;
2-
use stdx::{format_to, to_lower_snake_case};
2+
use stdx::to_lower_snake_case;
33
use syntax::ast::VisibilityOwner;
44
use syntax::ast::{self, AstNode, NameOwner};
55

6-
use crate::{
7-
utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
8-
AssistContext, AssistId, AssistKind, Assists,
9-
};
6+
use crate::{AssistContext, AssistId, AssistKind, Assists, assist_context::AssistBuilder, utils::{find_impl_block_end, find_struct_impl, generate_impl_text}};
107

118
// Assist: generate_enum_is_method
129
//
@@ -56,15 +53,8 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
5653
"Generate an `is_` method for an enum variant",
5754
target,
5855
|builder| {
59-
let mut buf = String::with_capacity(512);
60-
61-
if impl_def.is_some() {
62-
buf.push('\n');
63-
}
64-
6556
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
66-
format_to!(
67-
buf,
57+
let method = format!(
6858
" /// Returns `true` if the {} is [`{}`].
6959
{}fn {}(&self) -> bool {{
7060
matches!(self, Self::{}{})
@@ -77,14 +67,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
7767
variant_kind.pattern_suffix(),
7868
);
7969

80-
let start_offset = impl_def
81-
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
82-
.unwrap_or_else(|| {
83-
buf = generate_impl_text(&parent_enum, &buf);
84-
parent_enum.syntax().text_range().end()
85-
});
86-
87-
builder.insert(start_offset, buf);
70+
add_method_to_adt(builder, &parent_enum, impl_def, &method);
8871
},
8972
)
9073
}
@@ -140,15 +123,8 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
140123
"Generate an `into_` method for an enum variant",
141124
target,
142125
|builder| {
143-
let mut buf = String::with_capacity(512);
144-
145-
if impl_def.is_some() {
146-
buf.push('\n');
147-
}
148-
149126
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
150-
format_to!(
151-
buf,
127+
let method = format!(
152128
" {}fn {}(self) -> Option<{}> {{
153129
if let Self::{}{} = self {{
154130
Some({})
@@ -164,18 +140,33 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
164140
bound_name,
165141
);
166142

167-
let start_offset = impl_def
168-
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
169-
.unwrap_or_else(|| {
170-
buf = generate_impl_text(&parent_enum, &buf);
171-
parent_enum.syntax().text_range().end()
172-
});
173-
174-
builder.insert(start_offset, buf);
143+
add_method_to_adt(builder, &parent_enum, impl_def, &method);
175144
},
176145
)
177146
}
178147

148+
fn add_method_to_adt(
149+
builder: &mut AssistBuilder,
150+
adt: &ast::Adt,
151+
impl_def: Option<ast::Impl>,
152+
method: &str,
153+
) {
154+
let mut buf = String::with_capacity(method.len() + 2);
155+
if impl_def.is_some() {
156+
buf.push('\n');
157+
}
158+
buf.push_str(method);
159+
160+
let start_offset = impl_def
161+
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
162+
.unwrap_or_else(|| {
163+
buf = generate_impl_text(&adt, &buf);
164+
adt.syntax().text_range().end()
165+
});
166+
167+
builder.insert(start_offset, buf);
168+
}
169+
179170
enum VariantKind {
180171
Unit,
181172
/// Tuple with a single field

0 commit comments

Comments
 (0)