mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
deduplicate some
This commit is contained in:
parent
2dcd5d7a7c
commit
642786986f
1 changed files with 28 additions and 37 deletions
|
@ -1,12 +1,9 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use stdx::{format_to, to_lower_snake_case};
|
use stdx::to_lower_snake_case;
|
||||||
use syntax::ast::VisibilityOwner;
|
use syntax::ast::VisibilityOwner;
|
||||||
use syntax::ast::{self, AstNode, NameOwner};
|
use syntax::ast::{self, AstNode, NameOwner};
|
||||||
|
|
||||||
use crate::{
|
use crate::{AssistContext, AssistId, AssistKind, Assists, assist_context::AssistBuilder, utils::{find_impl_block_end, find_struct_impl, generate_impl_text}};
|
||||||
utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
|
|
||||||
AssistContext, AssistId, AssistKind, Assists,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Assist: generate_enum_is_method
|
// Assist: generate_enum_is_method
|
||||||
//
|
//
|
||||||
|
@ -56,15 +53,8 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
|
||||||
"Generate an `is_` method for an enum variant",
|
"Generate an `is_` method for an enum variant",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
let mut buf = String::with_capacity(512);
|
|
||||||
|
|
||||||
if impl_def.is_some() {
|
|
||||||
buf.push('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
|
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
|
||||||
format_to!(
|
let method = format!(
|
||||||
buf,
|
|
||||||
" /// Returns `true` if the {} is [`{}`].
|
" /// Returns `true` if the {} is [`{}`].
|
||||||
{}fn {}(&self) -> bool {{
|
{}fn {}(&self) -> bool {{
|
||||||
matches!(self, Self::{}{})
|
matches!(self, Self::{}{})
|
||||||
|
@ -77,14 +67,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext) ->
|
||||||
variant_kind.pattern_suffix(),
|
variant_kind.pattern_suffix(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let start_offset = impl_def
|
add_method_to_adt(builder, &parent_enum, impl_def, &method);
|
||||||
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
buf = generate_impl_text(&parent_enum, &buf);
|
|
||||||
parent_enum.syntax().text_range().end()
|
|
||||||
});
|
|
||||||
|
|
||||||
builder.insert(start_offset, buf);
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -140,15 +123,8 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
|
||||||
"Generate an `into_` method for an enum variant",
|
"Generate an `into_` method for an enum variant",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
let mut buf = String::with_capacity(512);
|
|
||||||
|
|
||||||
if impl_def.is_some() {
|
|
||||||
buf.push('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
|
let vis = parent_enum.visibility().map_or(String::new(), |v| format!("{} ", v));
|
||||||
format_to!(
|
let method = format!(
|
||||||
buf,
|
|
||||||
" {}fn {}(self) -> Option<{}> {{
|
" {}fn {}(self) -> Option<{}> {{
|
||||||
if let Self::{}{} = self {{
|
if let Self::{}{} = self {{
|
||||||
Some({})
|
Some({})
|
||||||
|
@ -164,18 +140,33 @@ pub(crate) fn generate_enum_into_method(acc: &mut Assists, ctx: &AssistContext)
|
||||||
bound_name,
|
bound_name,
|
||||||
);
|
);
|
||||||
|
|
||||||
let start_offset = impl_def
|
add_method_to_adt(builder, &parent_enum, impl_def, &method);
|
||||||
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
buf = generate_impl_text(&parent_enum, &buf);
|
|
||||||
parent_enum.syntax().text_range().end()
|
|
||||||
});
|
|
||||||
|
|
||||||
builder.insert(start_offset, buf);
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_method_to_adt(
|
||||||
|
builder: &mut AssistBuilder,
|
||||||
|
adt: &ast::Adt,
|
||||||
|
impl_def: Option<ast::Impl>,
|
||||||
|
method: &str,
|
||||||
|
) {
|
||||||
|
let mut buf = String::with_capacity(method.len() + 2);
|
||||||
|
if impl_def.is_some() {
|
||||||
|
buf.push('\n');
|
||||||
|
}
|
||||||
|
buf.push_str(method);
|
||||||
|
|
||||||
|
let start_offset = impl_def
|
||||||
|
.and_then(|impl_def| find_impl_block_end(impl_def, &mut buf))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
buf = generate_impl_text(&adt, &buf);
|
||||||
|
adt.syntax().text_range().end()
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.insert(start_offset, buf);
|
||||||
|
}
|
||||||
|
|
||||||
enum VariantKind {
|
enum VariantKind {
|
||||||
Unit,
|
Unit,
|
||||||
/// Tuple with a single field
|
/// Tuple with a single field
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue