Skip to content
Open
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
158 changes: 153 additions & 5 deletions crates/ide-assists/src/handlers/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ impl GeneratedFunctionTarget {
function_builder,
adt,
position,
item,
indent,
indent,
cap,
Expand All @@ -603,6 +604,7 @@ impl GeneratedFunctionTarget {
function_builder,
adt,
position,
item_list,
indent,
leading_indent,
cap,
Expand Down Expand Up @@ -723,6 +725,7 @@ fn insert_rendered_impl(
function_builder: &FunctionBuilder,
adt: Adt,
position: Position,
target_node: &SyntaxNode,
impl_indent: IndentLevel,
leading_ws_indent: IndentLevel,
cap: Option<SnippetCap>,
Expand All @@ -734,10 +737,45 @@ fn insert_rendered_impl(
adt.name(ctx.db()).display(ctx.db(), function_builder.target_edition)
)));

// FIXME: adt may have generic params.
let adt_src = adt.source(ctx.sema.db);
let (adt_generic_params, adt_where_clause) = adt_src
.as_ref()
.map(|src| match &src.value {
ast::Adt::Struct(it) => (it.generic_param_list(), it.where_clause()),
ast::Adt::Union(it) => (it.generic_param_list(), it.where_clause()),
ast::Adt::Enum(it) => (it.generic_param_list(), it.where_clause()),
})
.unwrap_or_default();

let source_scope = adt_src.as_ref().and_then(|src| ctx.sema.scope(src.value.syntax()));
let target_scope = source_scope.as_ref().and_then(|_| ctx.sema.scope(target_node));
let (adt_generic_params, adt_where_clause) = if let Some(source_scope) = source_scope
&& let Some(target_scope) = target_scope
&& source_scope.module() != target_scope.module()
{
let transform = PathTransform::generic_transformation(&target_scope, &source_scope);
let transformed_params = adt_generic_params.as_ref().map(|p| {
ast::GenericParamList::cast(transform.apply(p.syntax())).unwrap_or_else(|| p.clone())
});
let transformed_where = adt_where_clause.as_ref().map(|w| {
ast::WhereClause::cast(transform.apply(w.syntax())).unwrap_or_else(|| w.clone())
});
(transformed_params, transformed_where)
} else {
(adt_generic_params, adt_where_clause)
};

let generic_args = adt_generic_params.as_ref().map(|p| p.to_generic_args(make));

let fn_ = function_builder.render(make).indent(IndentLevel(1));
let impl_ =
make.impl_(None, None, None, name.into(), None, Some(make.assoc_item_list([fn_.into()])));
let impl_ = make.impl_(
None,
adt_generic_params,
generic_args,
name.into(),
adt_where_clause,
Some(make.assoc_item_list([fn_.into()])),
);
let impl_ = impl_.indent(impl_indent);
if let Some(fn_) = impl_.syntax().descendants().find_map(ast::Fn::cast) {
add_generated_fn_annotation(editor, edit, function_builder, &fn_, cap);
Expand Down Expand Up @@ -1901,7 +1939,7 @@ fn bar<T, U>(t: T, u: U) {

#[test]
fn generic_param_in_receiver_type() {
// FIXME: Generic parameter `T` should be part of impl, not method.
// FIXME: Generic parameter `T` from the impl should not also appear in the method's generics.
check_assist(
generate_function,
r"
Expand All @@ -1910,7 +1948,7 @@ fn foo<T, U>(s: S<T>, u: U) { s.$0foo(u) }
",
r"
struct S<T>(T);
impl S {
impl<T> S<T> {
fn foo<T, U>(&self, u: U) {
${0:todo!()}
}
Expand Down Expand Up @@ -2565,6 +2603,116 @@ impl<S> Foo<S> {
)
}

#[test]
fn create_method_on_generic_struct_no_existing_impl() {
check_assist(
generate_function,
r#"
struct Generic<T: Send>(T);

fn foo() {
Generic("test").do_a_thing$0()
}
"#,
r#"
struct Generic<T: Send>(T);
impl<T: Send> Generic<T> {
fn do_a_thing(&self) {
${0:todo!()}
}
}

fn foo() {
Generic("test").do_a_thing()
}
"#,
)
}

#[test]
fn create_method_on_generic_struct_with_where_clause() {
check_assist(
generate_function,
r#"
struct Generic<T>(T) where T: Send;

fn foo() {
Generic("test").do_a_thing$0()
}
"#,
r#"
struct Generic<T>(T) where T: Send;
impl<T> Generic<T>
where T: Send
{
fn do_a_thing(&self) {
${0:todo!()}
}
}

fn foo() {
Generic("test").do_a_thing()
}
"#,
)
}

#[test]
fn create_method_on_generic_struct_in_different_module() {
check_assist(
generate_function,
r#"
mod foo {
pub struct Generic<T: Send>(T);
}

fn bar() {
foo::Generic("test").do_a_thing$0()
}
"#,
r#"
mod foo {
pub struct Generic<T: Send>(T);
impl<T: Send> Generic<T> {
pub(crate) fn do_a_thing(&self) {
${0:todo!()}
}
}
}

fn bar() {
foo::Generic("test").do_a_thing()
}
"#,
)
}

#[test]
fn create_method_on_generic_enum_no_existing_impl() {
check_assist(
generate_function,
r#"
enum MyEnum<T> { A(T), B }

fn foo() {
MyEnum::B.do_a_thing$0()
}
"#,
r#"
enum MyEnum<T> { A(T), B }
impl<T> MyEnum<T> {
fn do_a_thing(&self) {
${0:todo!()}
}
}

fn foo() {
MyEnum::B.do_a_thing()
}
"#,
)
}

#[test]
fn create_function_with_async() {
check_assist(
Expand Down
Loading