Skip to content

Commit c992237

Browse files
committed
Add auto trait name for generate_trait_from_impl
Auto generate trait name from first method. Input: ```rust impl S$0 { fn foo() {} } ``` Old output: ```rust trait NewTrait { fn foo(); } impl NewTrait for S { fn foo() {} } ``` This PR output: ```rust trait Foo { fn foo(); } impl Foo for S { fn foo() {} } ```
1 parent b5b10fb commit c992237

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

crates/ide-assists/src/handlers/generate_trait_from_impl.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use syntax::{
4949
// };
5050
// }
5151
//
52-
// trait ${0:NewTrait}<const N: usize> {
52+
// trait ${0:Create}<const N: usize> {
5353
// // Used as an associated constant.
5454
// const CONST_ASSOC: usize = N * 4;
5555
//
@@ -58,7 +58,7 @@ use syntax::{
5858
// const_maker! {i32, 7}
5959
// }
6060
//
61-
// impl<const N: usize> ${0:NewTrait}<N> for Foo<N> {
61+
// impl<const N: usize> ${0:Create}<N> for Foo<N> {
6262
// // Used as an associated constant.
6363
// const CONST_ASSOC: usize = N * 4;
6464
//
@@ -115,7 +115,7 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
115115

116116
let trait_ast = make::trait_(
117117
false,
118-
"NewTrait",
118+
&trait_name(&impl_items).text(),
119119
impl_ast.generic_param_list(),
120120
impl_ast.where_clause(),
121121
trait_items,
@@ -161,6 +161,14 @@ pub(crate) fn generate_trait_from_impl(acc: &mut Assists, ctx: &AssistContext<'_
161161
Some(())
162162
}
163163

164+
fn trait_name(items: &ast::AssocItemList) -> ast::Name {
165+
items
166+
.assoc_items()
167+
.find_map(|x| if let ast::AssocItem::Fn(f) = x { f.name() } else { None })
168+
.map(|name| make::name(&stdx::to_camel_case(&name.text())))
169+
.unwrap_or_else(|| make::name("NewTrait"))
170+
}
171+
164172
/// `E0449` Trait items always share the visibility of their trait
165173
fn remove_items_visibility(item: &ast::AssocItem) {
166174
if let Some(has_vis) = ast::AnyHasVisibility::cast(item.syntax().clone()) {
@@ -219,11 +227,11 @@ impl F$0oo {
219227
r#"
220228
struct Foo(f64);
221229
222-
trait NewTrait {
230+
trait Add {
223231
fn add(&mut self, x: f64);
224232
}
225233
226-
impl NewTrait for Foo {
234+
impl Add for Foo {
227235
fn add(&mut self, x: f64) {
228236
self.0 += x;
229237
}
@@ -332,11 +340,11 @@ impl F$0oo {
332340
r#"
333341
struct Foo;
334342
335-
trait NewTrait {
343+
trait AFunc {
336344
fn a_func() -> Option<()>;
337345
}
338346
339-
impl NewTrait for Foo {
347+
impl AFunc for Foo {
340348
fn a_func() -> Option<()> {
341349
Some(())
342350
}
@@ -366,11 +374,11 @@ mod a {
366374
}"#,
367375
r#"
368376
mod a {
369-
trait NewTrait {
377+
trait Foo {
370378
fn foo();
371379
}
372380
373-
impl NewTrait for S {
381+
impl Foo for S {
374382
fn foo() {}
375383
}
376384
}"#,

crates/ide-assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,7 @@ macro_rules! const_maker {
20552055
};
20562056
}
20572057
2058-
trait ${0:NewTrait}<const N: usize> {
2058+
trait ${0:Create}<const N: usize> {
20592059
// Used as an associated constant.
20602060
const CONST_ASSOC: usize = N * 4;
20612061
@@ -2064,7 +2064,7 @@ trait ${0:NewTrait}<const N: usize> {
20642064
const_maker! {i32, 7}
20652065
}
20662066
2067-
impl<const N: usize> ${0:NewTrait}<N> for Foo<N> {
2067+
impl<const N: usize> ${0:Create}<N> for Foo<N> {
20682068
// Used as an associated constant.
20692069
const CONST_ASSOC: usize = N * 4;
20702070

0 commit comments

Comments
 (0)