mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
Generate the impl block via generate_trait_impl_text
This commit is contained in:
parent
87ef340c19
commit
3f0222565d
1 changed files with 45 additions and 2 deletions
|
@ -7,6 +7,7 @@ use syntax::{
|
|||
ast::{self, Impl, NameOwner},
|
||||
AstNode,
|
||||
};
|
||||
use crate::utils::generate_trait_impl_text;
|
||||
|
||||
// Assist: generate_default_from_new
|
||||
//
|
||||
|
@ -59,13 +60,21 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
|
|||
}
|
||||
|
||||
let insert_location = impl_.syntax().text_range();
|
||||
|
||||
let code = match ast::Struct::cast(impl_.self_ty().unwrap().syntax().clone()){
|
||||
None => {
|
||||
default_fn_node_for_new(impl_)
|
||||
}
|
||||
Some(strukt) => {
|
||||
generate_trait_impl_text(&ast::Adt::Struct(strukt),"core:default:Default"," fn default() -> Self {{
|
||||
Self::new()
|
||||
}}")
|
||||
}
|
||||
};
|
||||
acc.add(
|
||||
AssistId("generate_default_from_new", crate::AssistKind::Generate),
|
||||
"Generate a Default impl from a new fn",
|
||||
insert_location,
|
||||
move |builder| {
|
||||
let code = default_fn_node_for_new(impl_);
|
||||
builder.insert(insert_location.end(), code);
|
||||
},
|
||||
)
|
||||
|
@ -175,6 +184,40 @@ impl Default for Test {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generate_default3() {
|
||||
check_pass(
|
||||
r#"
|
||||
pub struct Foo<T> {
|
||||
_bar: *mut T,
|
||||
}
|
||||
|
||||
impl<T> Foo<T> {
|
||||
pub fn ne$0w() -> Self {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
pub struct Foo<T> {
|
||||
_bar: *mut T,
|
||||
}
|
||||
|
||||
impl<T> Foo<T> {
|
||||
pub fn new() -> Self {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Foo<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_function_with_parameters() {
|
||||
cov_mark::check!(new_function_with_parameters);
|
||||
|
|
Loading…
Reference in a new issue