aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDomantas Jadenkus <[email protected]>2021-02-14 10:15:20 +0000
committerDomantas Jadenkus <[email protected]>2021-02-27 10:05:59 +0000
commit642786986ff21f33d3a08191d7c19cccf97d25e2 (patch)
tree1715dfa5b3c864eee804ce091161e1fbd18b320b
parent2dcd5d7a7c41407478360bb2b77b3bfa857cca09 (diff)
deduplicate some
-rw-r--r--crates/ide_assists/src/handlers/generate_enum_match_method.rs65
1 files changed, 28 insertions, 37 deletions
diff --git a/crates/ide_assists/src/handlers/generate_enum_match_method.rs b/crates/ide_assists/src/handlers/generate_enum_match_method.rs
index 25565d4cc..b271b48b6 100644
--- a/crates/ide_assists/src/handlers/generate_enum_match_method.rs
+++ b/crates/ide_assists/src/handlers/generate_enum_match_method.rs
@@ -1,12 +1,9 @@
1use itertools::Itertools; 1use itertools::Itertools;
2use stdx::{format_to, to_lower_snake_case}; 2use stdx::to_lower_snake_case;
3use syntax::ast::VisibilityOwner; 3use syntax::ast::VisibilityOwner;
4use syntax::ast::{self, AstNode, NameOwner}; 4use syntax::ast::{self, AstNode, NameOwner};
5 5
6use crate::{ 6use crate::{AssistContext, AssistId, AssistKind, Assists, assist_context::AssistBuilder, utils::{find_impl_block_end, find_struct_impl, generate_impl_text}};
7 utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
8 AssistContext, AssistId, AssistKind, Assists,
9};
10 7
11// Assist: generate_enum_is_method 8// Assist: generate_enum_is_method
12// 9//
@@ -56,15 +53,8 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
56 "Generate an `is_` method for an enum variant", 53 "Generate an `is_` method for an enum variant",
57 target, 54 target,
58 |builder| { 55 |builder| {
59 let mut buf = String::with_capacity(512);
60
61 if impl_def.is_some() {
62 buf.push('\n');
63 }
64
65 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); 56 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
66 format_to!( 57 let method = format!(
67 buf,
68 " /// Returns `true` if the {} is [`{}`]. 58 " /// Returns `true` if the {} is [`{}`].
69 {}fn {}(&self) -> bool {{ 59 {}fn {}(&self) -> bool {{
70 matches!(self, Self::{}{}) 60 matches!(self, Self::{}{})
@@ -77,14 +67,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
77 variant_kind.pattern_suffix(), 67 variant_kind.pattern_suffix(),
78 ); 68 );
79 69
80 let start_offset = impl_def 70 add_method_to_adt(builder, &parent_enum, impl_def, &method);
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);
88 }, 71 },
89 ) 72 )
90} 73}
@@ -140,15 +123,8 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
140 "Generate an `into_` method for an enum variant", 123 "Generate an `into_` method for an enum variant",
141 target, 124 target,
142 |builder| { 125 |builder| {
143 let mut buf = String::with_capacity(512);
144
145 if impl_def.is_some() {
146 buf.push('\n');
147 }
148
149 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v)); 126 let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
150 format_to!( 127 let method = format!(
151 buf,
152 " {}fn {}(self) -> Option<{}> {{ 128 " {}fn {}(self) -> Option<{}> {{
153 if let Self::{}{} = self {{ 129 if let Self::{}{} = self {{
154 Some({}) 130 Some({})
@@ -164,18 +140,33 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
164 bound_name, 140 bound_name,
165 ); 141 );
166 142
167 let start_offset = impl_def 143 add_method_to_adt(builder, &parent_enum, impl_def, &method);
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);
175 }, 144 },
176 ) 145 )
177} 146}
178 147
148fn 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
179enum VariantKind { 170enum VariantKind {
180 Unit, 171 Unit,
181 /// Tuple with a single field 172 /// Tuple with a single field