2023-04-04 06:57:02 +00:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2023-10-09 15:30:34 +00:00
|
|
|
use hir::{self, HasCrate, HasVisibility};
|
2023-10-03 05:04:59 +00:00
|
|
|
use ide_db::path_transform::PathTransform;
|
2023-06-25 04:06:30 +00:00
|
|
|
use syntax::{
|
|
|
|
ast::{
|
2023-07-07 01:43:34 +00:00
|
|
|
self, edit_in_place::Indent, make, AstNode, HasGenericParams, HasName, HasVisibility as _,
|
2023-06-25 04:06:30 +00:00
|
|
|
},
|
|
|
|
ted,
|
|
|
|
};
|
2021-10-13 13:08:40 +00:00
|
|
|
|
|
|
|
use crate::{
|
2023-06-25 04:06:30 +00:00
|
|
|
utils::{convert_param_list_to_arg_list, find_struct_impl},
|
2021-10-13 13:08:40 +00:00
|
|
|
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
|
|
|
|
};
|
|
|
|
|
2021-10-14 11:52:31 +00:00
|
|
|
// Assist: generate_delegate_methods
|
2021-10-13 13:08:40 +00:00
|
|
|
//
|
2021-10-14 11:52:31 +00:00
|
|
|
// Generate delegate methods.
|
2021-10-13 13:08:40 +00:00
|
|
|
//
|
|
|
|
// ```
|
2021-10-14 11:23:46 +00:00
|
|
|
// struct Age(u8);
|
|
|
|
// impl Age {
|
|
|
|
// fn age(&self) -> u8 {
|
|
|
|
// self.0
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2021-10-13 13:08:40 +00:00
|
|
|
// struct Person {
|
2021-10-14 11:23:46 +00:00
|
|
|
// ag$0e: Age,
|
2021-10-13 13:08:40 +00:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2021-10-14 11:23:46 +00:00
|
|
|
// struct Age(u8);
|
|
|
|
// impl Age {
|
|
|
|
// fn age(&self) -> u8 {
|
|
|
|
// self.0
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
2021-10-13 13:08:40 +00:00
|
|
|
// struct Person {
|
2021-10-14 11:23:46 +00:00
|
|
|
// age: Age,
|
2021-10-13 13:08:40 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// impl Person {
|
2021-10-14 11:23:46 +00:00
|
|
|
// $0fn age(&self) -> u8 {
|
|
|
|
// self.age.age()
|
2021-10-13 13:08:40 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 13:02:08 +00:00
|
|
|
pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-10-13 13:08:40 +00:00
|
|
|
let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
|
2021-10-13 21:59:23 +00:00
|
|
|
let strukt_name = strukt.name()?;
|
2022-03-31 09:12:08 +00:00
|
|
|
let current_module = ctx.sema.scope(strukt.syntax())?.module();
|
2021-10-13 13:08:40 +00:00
|
|
|
|
2021-12-13 15:32:57 +00:00
|
|
|
let (field_name, field_ty, target) = match ctx.find_node_at_offset::<ast::RecordField>() {
|
2021-10-14 12:18:12 +00:00
|
|
|
Some(field) => {
|
|
|
|
let field_name = field.name()?;
|
|
|
|
let field_ty = field.ty()?;
|
2022-10-07 19:24:57 +00:00
|
|
|
(field_name.to_string(), field_ty, field.syntax().text_range())
|
2021-10-14 12:18:12 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let field = ctx.find_node_at_offset::<ast::TupleField>()?;
|
|
|
|
let field_list = ctx.find_node_at_offset::<ast::TupleFieldList>()?;
|
2021-10-14 16:19:20 +00:00
|
|
|
let field_list_index = field_list.fields().position(|it| it == field)?;
|
2021-10-14 12:18:12 +00:00
|
|
|
let field_ty = field.ty()?;
|
2022-10-07 19:24:57 +00:00
|
|
|
(field_list_index.to_string(), field_ty, field.syntax().text_range())
|
2021-10-14 12:18:12 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-13 13:08:40 +00:00
|
|
|
|
|
|
|
let sema_field_ty = ctx.sema.resolve_type(&field_ty)?;
|
|
|
|
let mut methods = vec![];
|
2023-04-04 06:57:02 +00:00
|
|
|
let mut seen_names = HashSet::new();
|
2023-04-04 06:50:20 +00:00
|
|
|
|
|
|
|
for ty in sema_field_ty.autoderef(ctx.db()) {
|
2023-04-04 06:54:26 +00:00
|
|
|
let krate = ty.krate(ctx.db());
|
2023-04-04 06:50:20 +00:00
|
|
|
ty.iterate_assoc_items(ctx.db(), krate, |item| {
|
|
|
|
if let hir::AssocItem::Function(f) = item {
|
2023-06-16 16:41:25 +00:00
|
|
|
let name = f.name(ctx.db());
|
2023-04-04 06:57:02 +00:00
|
|
|
if f.self_param(ctx.db()).is_some()
|
|
|
|
&& f.is_visible_from(ctx.db(), current_module)
|
2023-06-16 16:41:25 +00:00
|
|
|
&& seen_names.insert(name.clone())
|
2023-04-04 06:57:02 +00:00
|
|
|
{
|
2023-06-16 16:41:25 +00:00
|
|
|
methods.push((name, f))
|
2023-04-04 06:50:20 +00:00
|
|
|
}
|
2021-10-13 13:08:40 +00:00
|
|
|
}
|
2023-04-04 06:50:20 +00:00
|
|
|
Option::<()>::None
|
|
|
|
});
|
|
|
|
}
|
2023-06-16 16:41:25 +00:00
|
|
|
methods.sort_by(|(a, _), (b, _)| a.cmp(b));
|
|
|
|
for (name, method) in methods {
|
2021-10-14 16:19:20 +00:00
|
|
|
let adt = ast::Adt::Struct(strukt.clone());
|
2023-06-16 16:41:25 +00:00
|
|
|
let name = name.display(ctx.db()).to_string();
|
2023-05-02 03:36:01 +00:00
|
|
|
// if `find_struct_impl` returns None, that means that a function named `name` already exists.
|
2023-07-03 18:34:09 +00:00
|
|
|
let Some(impl_def) = find_struct_impl(ctx, &adt, std::slice::from_ref(&name)) else {
|
|
|
|
continue;
|
|
|
|
};
|
2023-08-09 21:42:52 +00:00
|
|
|
|
|
|
|
let field = make::ext::field_from_idents(["self", &field_name])?;
|
|
|
|
|
2021-10-13 13:08:40 +00:00
|
|
|
acc.add_group(
|
2021-10-14 11:52:31 +00:00
|
|
|
&GroupLabel("Generate delegate methods…".to_owned()),
|
|
|
|
AssistId("generate_delegate_methods", AssistKind::Generate),
|
2023-06-16 16:41:25 +00:00
|
|
|
format!("Generate delegate for `{field_name}.{name}()`",),
|
2021-10-13 13:08:40 +00:00
|
|
|
target,
|
2023-06-25 04:06:30 +00:00
|
|
|
|edit| {
|
2021-10-14 11:23:46 +00:00
|
|
|
// Create the function
|
2023-10-09 15:30:34 +00:00
|
|
|
let method_source = match ctx.sema.source(method) {
|
|
|
|
Some(source) => source.value,
|
2021-10-14 10:34:31 +00:00
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let vis = method_source.visibility();
|
2023-06-16 16:41:25 +00:00
|
|
|
let fn_name = make::name(&name);
|
2021-10-14 11:23:46 +00:00
|
|
|
let params =
|
|
|
|
method_source.param_list().unwrap_or_else(|| make::param_list(None, []));
|
2021-10-14 16:19:20 +00:00
|
|
|
let type_params = method_source.generic_param_list();
|
|
|
|
let arg_list = match method_source.param_list() {
|
|
|
|
Some(list) => convert_param_list_to_arg_list(list),
|
|
|
|
None => make::arg_list([]),
|
|
|
|
};
|
2023-08-09 21:42:52 +00:00
|
|
|
let tail_expr = make::expr_method_call(field, make::name_ref(&name), arg_list);
|
2021-10-14 16:33:29 +00:00
|
|
|
let ret_type = method_source.ret_type();
|
2021-10-14 11:23:46 +00:00
|
|
|
let is_async = method_source.async_token().is_some();
|
2023-04-16 21:41:08 +00:00
|
|
|
let is_const = method_source.const_token().is_some();
|
|
|
|
let is_unsafe = method_source.unsafe_token().is_some();
|
2022-12-25 00:47:16 +00:00
|
|
|
let tail_expr_finished =
|
|
|
|
if is_async { make::expr_await(tail_expr) } else { tail_expr };
|
|
|
|
let body = make::block_expr([], Some(tail_expr_finished));
|
2023-04-16 21:41:08 +00:00
|
|
|
let f = make::fn_(
|
|
|
|
vis,
|
2023-06-16 16:41:25 +00:00
|
|
|
fn_name,
|
2023-04-16 21:41:08 +00:00
|
|
|
type_params,
|
2023-10-05 19:45:26 +00:00
|
|
|
method_source.where_clause(),
|
2023-04-16 21:41:08 +00:00
|
|
|
params,
|
|
|
|
body,
|
|
|
|
ret_type,
|
|
|
|
is_async,
|
|
|
|
is_const,
|
|
|
|
is_unsafe,
|
|
|
|
)
|
|
|
|
.clone_for_update();
|
2021-10-13 18:13:36 +00:00
|
|
|
|
2023-07-07 02:31:28 +00:00
|
|
|
// Get the impl to update, or create one if we need to.
|
|
|
|
let impl_def = match impl_def {
|
|
|
|
Some(impl_def) => edit.make_mut(impl_def),
|
2021-10-13 21:59:23 +00:00
|
|
|
None => {
|
|
|
|
let name = &strukt_name.to_string();
|
2021-10-14 11:23:46 +00:00
|
|
|
let params = strukt.generic_param_list();
|
|
|
|
let ty_params = params.clone();
|
2023-04-16 21:41:08 +00:00
|
|
|
let where_clause = strukt.where_clause();
|
|
|
|
|
|
|
|
let impl_def = make::impl_(
|
|
|
|
ty_params,
|
|
|
|
None,
|
|
|
|
make::ty_path(make::ext::ident_path(name)),
|
|
|
|
where_clause,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.clone_for_update();
|
2023-06-25 04:06:30 +00:00
|
|
|
|
2023-07-07 01:43:34 +00:00
|
|
|
// Fixup impl_def indentation
|
|
|
|
let indent = strukt.indent_level();
|
|
|
|
impl_def.reindent_to(indent);
|
|
|
|
|
2021-10-14 10:34:31 +00:00
|
|
|
// Insert the impl block.
|
2023-06-25 04:06:30 +00:00
|
|
|
let strukt = edit.make_mut(strukt.clone());
|
|
|
|
ted::insert_all(
|
|
|
|
ted::Position::after(strukt.syntax()),
|
|
|
|
vec![
|
2023-07-07 01:43:34 +00:00
|
|
|
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
|
2023-06-25 04:06:30 +00:00
|
|
|
impl_def.syntax().clone().into(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2023-07-07 02:31:28 +00:00
|
|
|
impl_def
|
2021-10-13 21:59:23 +00:00
|
|
|
}
|
2023-07-07 02:31:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Fixup function indentation.
|
|
|
|
// FIXME: Should really be handled by `AssocItemList::add_item`
|
|
|
|
f.reindent_to(impl_def.indent_level() + 1);
|
|
|
|
|
|
|
|
let assoc_items = impl_def.get_or_create_assoc_item_list();
|
|
|
|
assoc_items.add_item(f.clone().into());
|
|
|
|
|
2023-10-09 15:30:34 +00:00
|
|
|
if let Some((target, source)) =
|
|
|
|
ctx.sema.scope(strukt.syntax()).zip(ctx.sema.scope(method_source.syntax()))
|
|
|
|
{
|
|
|
|
PathTransform::generic_transformation(&target, &source).apply(f.syntax());
|
|
|
|
}
|
2023-10-03 05:04:59 +00:00
|
|
|
|
2023-07-07 02:31:28 +00:00
|
|
|
if let Some(cap) = ctx.config.snippet_cap {
|
|
|
|
edit.add_tabstop_before(cap, f)
|
2021-10-13 21:59:23 +00:00
|
|
|
}
|
2021-10-13 13:08:40 +00:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-10-16 10:09:53 +00:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2021-10-13 13:08:40 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-10-14 10:34:31 +00:00
|
|
|
fn test_generate_delegate_create_impl_block() {
|
|
|
|
check_assist(
|
2021-10-14 11:52:31 +00:00
|
|
|
generate_delegate_methods,
|
2021-10-14 10:34:31 +00:00
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
ag$0e: Age,
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
age: Age,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {
|
|
|
|
$0fn age(&self) -> u8 {
|
|
|
|
self.age.age()
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-07 01:43:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_delegate_create_impl_block_match_indent() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
mod indent {
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
ag$0e: Age,
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
mod indent {
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
age: Age,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {
|
|
|
|
$0fn age(&self) -> u8 {
|
|
|
|
self.age.age()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-14 10:34:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_delegate_update_impl_block() {
|
2021-10-13 13:08:40 +00:00
|
|
|
check_assist(
|
2021-10-14 11:52:31 +00:00
|
|
|
generate_delegate_methods,
|
2021-10-13 13:08:40 +00:00
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
ag$0e: Age,
|
|
|
|
}
|
2021-10-13 21:59:23 +00:00
|
|
|
|
2021-10-14 10:34:31 +00:00
|
|
|
impl Person {}"#,
|
2021-10-13 13:08:40 +00:00
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
2021-10-14 10:34:31 +00:00
|
|
|
fn age(&self) -> u8 {
|
2021-10-13 13:08:40 +00:00
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
age: Age,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {
|
2021-10-14 10:34:31 +00:00
|
|
|
$0fn age(&self) -> u8 {
|
|
|
|
self.age.age()
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2021-10-14 11:23:46 +00:00
|
|
|
|
2023-07-07 01:43:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_delegate_update_impl_block_match_indent() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
mod indent {
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
ag$0e: Age,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
mod indent {
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
age: Age,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Person {
|
|
|
|
$0fn age(&self) -> u8 {
|
|
|
|
self.age.age()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-14 12:18:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_delegate_tuple_struct() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person(A$0ge);"#,
|
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person(Age);
|
|
|
|
|
|
|
|
impl Person {
|
|
|
|
$0fn age(&self) -> u8 {
|
|
|
|
self.0.age()
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-14 11:23:46 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_delegate_enable_all_attributes() {
|
|
|
|
check_assist(
|
2021-10-14 11:52:31 +00:00
|
|
|
generate_delegate_methods,
|
2021-10-14 11:23:46 +00:00
|
|
|
r#"
|
|
|
|
struct Age<T>(T);
|
|
|
|
impl<T> Age<T> {
|
|
|
|
pub(crate) async fn age<J, 'a>(&'a mut self, ty: T, arg: J) -> T {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person<T> {
|
|
|
|
ag$0e: Age<T>,
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct Age<T>(T);
|
|
|
|
impl<T> Age<T> {
|
|
|
|
pub(crate) async fn age<J, 'a>(&'a mut self, ty: T, arg: J) -> T {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person<T> {
|
|
|
|
age: Age<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Person<T> {
|
2021-10-14 16:33:29 +00:00
|
|
|
$0pub(crate) async fn age<J, 'a>(&'a mut self, ty: T, arg: J) -> T {
|
2022-12-25 00:47:16 +00:00
|
|
|
self.age.age(ty, arg).await
|
2021-10-13 13:08:40 +00:00
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2021-10-16 10:09:53 +00:00
|
|
|
|
2023-04-04 06:50:20 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generates_delegate_autoderef() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
//- minicore: deref
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct AgeDeref(Age);
|
|
|
|
impl core::ops::Deref for AgeDeref { type Target = Age; }
|
|
|
|
struct Person {
|
|
|
|
ag$0e: AgeDeref,
|
|
|
|
}
|
|
|
|
impl Person {}"#,
|
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
struct AgeDeref(Age);
|
|
|
|
impl core::ops::Deref for AgeDeref { type Target = Age; }
|
|
|
|
struct Person {
|
|
|
|
age: AgeDeref,
|
|
|
|
}
|
|
|
|
impl Person {
|
|
|
|
$0fn age(&self) -> u8 {
|
|
|
|
self.age.age()
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-05 19:45:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_preserve_where_clause() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
struct Inner<T>(T);
|
|
|
|
impl<T> Inner<T> {
|
|
|
|
fn get(&self) -> T
|
|
|
|
where
|
|
|
|
T: Copy,
|
|
|
|
T: PartialEq,
|
|
|
|
{
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Struct<T> {
|
|
|
|
$0field: Inner<T>,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Inner<T>(T);
|
|
|
|
impl<T> Inner<T> {
|
|
|
|
fn get(&self) -> T
|
|
|
|
where
|
|
|
|
T: Copy,
|
|
|
|
T: PartialEq,
|
|
|
|
{
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Struct<T> {
|
|
|
|
field: Inner<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Struct<T> {
|
|
|
|
$0fn get(&self) -> T where
|
|
|
|
T: Copy,
|
|
|
|
T: PartialEq, {
|
|
|
|
self.field.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-03 05:04:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fixes_basic_self_references() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
field: $0Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn bar(&self, other: Self) -> Self {
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
field: Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
$0fn bar(&self, other: Bar) -> Bar {
|
|
|
|
self.field.bar(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn bar(&self, other: Self) -> Self {
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fixes_nested_self_references() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
field: $0Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn bar(&mut self, a: (Self, [Self; 4]), b: Vec<Self>) {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
field: Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
$0fn bar(&mut self, a: (Bar, [Bar; 4]), b: Vec<Bar>) {
|
|
|
|
self.field.bar(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn bar(&mut self, a: (Self, [Self; 4]), b: Vec<Self>) {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fixes_self_references_with_lifetimes_and_generics() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
struct Foo<'a, T> {
|
|
|
|
$0field: Bar<'a, T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar<'a, T>(&'a T);
|
|
|
|
|
|
|
|
impl<'a, T> Bar<'a, T> {
|
|
|
|
fn bar(self, mut b: Vec<&'a Self>) -> &'a Self {
|
|
|
|
b.pop().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo<'a, T> {
|
|
|
|
field: Bar<'a, T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> Foo<'a, T> {
|
|
|
|
$0fn bar(self, mut b: Vec<&'a Bar<'_, T>>) -> &'a Bar<'_, T> {
|
|
|
|
self.field.bar(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Bar<'a, T>(&'a T);
|
|
|
|
|
|
|
|
impl<'a, T> Bar<'a, T> {
|
|
|
|
fn bar(self, mut b: Vec<&'a Self>) -> &'a Self {
|
|
|
|
b.pop().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fixes_self_references_across_macros() {
|
|
|
|
check_assist(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
//- /bar.rs
|
|
|
|
macro_rules! test_method {
|
|
|
|
() => {
|
|
|
|
pub fn test(self, b: Bar) -> Self {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Bar;
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
test_method!();
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /main.rs
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
$0bar: bar::Bar,
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
struct Foo {
|
|
|
|
bar: bar::Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
$0pub fn test(self,b:bar::Bar) ->bar::Bar {
|
|
|
|
self.bar.test(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-16 10:09:53 +00:00
|
|
|
#[test]
|
|
|
|
fn test_generate_delegate_visibility() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
mod m {
|
|
|
|
pub struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
ag$0e: m::Age,
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2023-05-02 03:36:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generate_not_eligible_if_fn_exists() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
generate_delegate_methods,
|
|
|
|
r#"
|
|
|
|
struct Age(u8);
|
|
|
|
impl Age {
|
|
|
|
fn age(&self) -> u8 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Person {
|
|
|
|
ag$0e: Age,
|
|
|
|
}
|
|
|
|
impl Person {
|
|
|
|
fn age(&self) -> u8 { 0 }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2021-10-13 13:08:40 +00:00
|
|
|
}
|