Skip to content

Add auto trait name for generate_trait_from_impl #20299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions crates/ide-assists/src/handlers/generate_trait_from_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use syntax::{
// };
// }
//
// trait ${0:NewTrait}<const N: usize> {
// trait ${0:Create}<const N: usize> {
// // Used as an associated constant.
// const CONST_ASSOC: usize = N * 4;
//
Expand All @@ -58,7 +58,7 @@ use syntax::{
// const_maker! {i32, 7}
// }
//
// impl<const N: usize> ${0:NewTrait}<N> for Foo<N> {
// impl<const N: usize> ${0:Create}<N> for Foo<N> {
// // Used as an associated constant.
// const CONST_ASSOC: usize = N * 4;
//
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -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() {}
}
}"#,
Expand Down
4 changes: 2 additions & 2 deletions crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,7 @@ macro_rules! const_maker {
};
}

trait ${0:NewTrait}<const N: usize> {
trait ${0:Create}<const N: usize> {
// Used as an associated constant.
const CONST_ASSOC: usize = N * 4;

Expand All @@ -2064,7 +2064,7 @@ trait ${0:NewTrait}<const N: usize> {
const_maker! {i32, 7}
}

impl<const N: usize> ${0:NewTrait}<N> for Foo<N> {
impl<const N: usize> ${0:Create}<N> for Foo<N> {
// Used as an associated constant.
const CONST_ASSOC: usize = N * 4;

Expand Down
Loading