diff --git a/crates/ide-assists/src/handlers/generate_trait_from_impl.rs b/crates/ide-assists/src/handlers/generate_trait_from_impl.rs index 92a4bd35b3e7..db0b6a3fc76a 100644 --- a/crates/ide-assists/src/handlers/generate_trait_from_impl.rs +++ b/crates/ide-assists/src/handlers/generate_trait_from_impl.rs @@ -49,7 +49,7 @@ use syntax::{ // }; // } // -// trait ${0:NewTrait} { +// trait ${0:Create} { // // Used as an associated constant. // const CONST_ASSOC: usize = N * 4; // @@ -58,7 +58,7 @@ use syntax::{ // const_maker! {i32, 7} // } // -// impl ${0:NewTrait} for Foo { +// impl ${0:Create} for Foo { // // Used as an associated constant. // const CONST_ASSOC: usize = N * 4; // @@ -115,7 +115,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_ let trait_ast = make::trait_( false, - "NewTrait", + &trait_name(&impl_items).text(), impl_ast.generic_param_list(), impl_ast.where_clause(), trait_items, @@ -161,6 +161,14 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_ Some(()) } +fn trait_name(items: &ast::AssocItemList) -> ast::Name { + items + .assoc_items() + .find_map(|x| if let ast::AssocItem::Fn(f) = x { f.name() } else { None }) + .map(|name| make::name(&stdx::to_camel_case(&name.text()))) + .unwrap_or_else(|| make::name("NewTrait")) +} + /// `E0449` Trait items always share the visibility of their trait fn remove_items_visibility(item: &ast::AssocItem) { if let Some(has_vis) = ast::AnyHasVisibility::cast(item.syntax().clone()) { @@ -225,11 +233,11 @@ impl F$0oo { r#" struct Foo(f64); -trait NewTrait { +trait Add { fn add(&mut self, x: f64); } -impl NewTrait for Foo { +impl Add for Foo { fn add(&mut self, x: f64) { self.0 += x; } @@ -338,11 +346,11 @@ impl F$0oo { r#" struct Foo; -trait NewTrait { +trait AFunc { fn a_func() -> Option<()>; } -impl NewTrait for Foo { +impl AFunc for Foo { fn a_func() -> Option<()> { Some(()) } @@ -372,11 +380,11 @@ mod a { }"#, r#" mod a { - trait NewTrait { + trait Foo { fn foo(); } - impl NewTrait for S { + impl Foo for S { fn foo() {} } }"#, diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs index fc1c6928ff31..564e3a6f0293 100644 --- a/crates/ide-assists/src/tests/generated.rs +++ b/crates/ide-assists/src/tests/generated.rs @@ -2055,7 +2055,7 @@ macro_rules! const_maker { }; } -trait ${0:NewTrait} { +trait ${0:Create} { // Used as an associated constant. const CONST_ASSOC: usize = N * 4; @@ -2064,7 +2064,7 @@ trait ${0:NewTrait} { const_maker! {i32, 7} } -impl ${0:NewTrait} for Foo { +impl ${0:Create} for Foo { // Used as an associated constant. const CONST_ASSOC: usize = N * 4;