2020-11-05 16:34:50 -06:00
|
|
|
use ide_db::traits::resolve_target_trait;
|
|
|
|
use syntax::ast::{self, AstNode};
|
2019-08-22 13:31:21 -05:00
|
|
|
|
2020-01-10 11:26:18 -06:00
|
|
|
use crate::{
|
2020-05-06 11:45:35 -05:00
|
|
|
assist_context::{AssistContext, Assists},
|
2020-11-05 16:34:50 -06:00
|
|
|
utils::add_trait_assoc_items_to_impl,
|
|
|
|
utils::DefaultMethods,
|
|
|
|
utils::{filter_assoc_items, render_snippet, Cursor},
|
2020-06-28 17:36:05 -05:00
|
|
|
AssistId, AssistKind,
|
2020-01-10 11:26:18 -06:00
|
|
|
};
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: add_impl_missing_members
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-26 11:08:13 -05:00
|
|
|
// Adds scaffold for required impl members.
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait<T> {
|
2020-12-25 11:15:31 -06:00
|
|
|
// type X;
|
2019-12-24 09:44:32 -06:00
|
|
|
// fn foo(&self) -> T;
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2021-01-06 14:15:48 -06:00
|
|
|
// impl Trait<u32> for () {$0
|
2019-10-25 15:38:15 -05:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait<T> {
|
2020-12-25 11:15:31 -06:00
|
|
|
// type X;
|
2019-12-24 09:44:32 -06:00
|
|
|
// fn foo(&self) -> T;
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait<u32> for () {
|
2020-12-25 11:15:31 -06:00
|
|
|
// $0type X;
|
|
|
|
//
|
2020-05-19 19:07:21 -05:00
|
|
|
// fn foo(&self) -> u32 {
|
2020-12-25 11:15:31 -06:00
|
|
|
// todo!()
|
2020-05-02 04:50:43 -05:00
|
|
|
// }
|
2019-10-25 15:38:15 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2019-03-23 10:06:25 -05:00
|
|
|
add_missing_impl_members_inner(
|
2020-05-06 11:45:35 -05:00
|
|
|
acc,
|
2019-03-23 10:06:25 -05:00
|
|
|
ctx,
|
2020-11-05 16:34:50 -06:00
|
|
|
DefaultMethods::No,
|
2019-03-23 10:06:25 -05:00
|
|
|
"add_impl_missing_members",
|
2020-01-14 11:32:26 -06:00
|
|
|
"Implement missing members",
|
2019-03-23 10:06:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: add_impl_default_members
|
2019-10-26 11:08:13 -05:00
|
|
|
//
|
|
|
|
// Adds scaffold for overriding default impl members.
|
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait {
|
2020-12-25 11:15:31 -06:00
|
|
|
// type X;
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn foo(&self);
|
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait for () {
|
2020-12-25 11:15:31 -06:00
|
|
|
// type X = ();
|
2021-01-06 14:15:48 -06:00
|
|
|
// fn foo(&self) {}$0
|
2019-10-25 15:38:15 -05:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait {
|
2020-12-25 11:15:31 -06:00
|
|
|
// type X;
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn foo(&self);
|
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait for () {
|
2020-12-25 11:15:31 -06:00
|
|
|
// type X = ();
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn foo(&self) {}
|
|
|
|
//
|
2020-08-14 08:10:52 -05:00
|
|
|
// $0fn bar(&self) {}
|
2019-10-25 15:38:15 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2019-03-23 10:06:25 -05:00
|
|
|
add_missing_impl_members_inner(
|
2020-05-06 11:45:35 -05:00
|
|
|
acc,
|
2019-03-23 10:06:25 -05:00
|
|
|
ctx,
|
2020-11-05 16:34:50 -06:00
|
|
|
DefaultMethods::Only,
|
2019-03-23 10:06:25 -05:00
|
|
|
"add_impl_default_members",
|
2020-01-14 11:32:26 -06:00
|
|
|
"Implement default members",
|
2019-03-23 10:06:25 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_missing_impl_members_inner(
|
2020-05-06 11:45:35 -05:00
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
2020-11-05 16:34:50 -06:00
|
|
|
mode: DefaultMethods,
|
2019-03-23 10:06:25 -05:00
|
|
|
assist_id: &'static str,
|
|
|
|
label: &'static str,
|
2020-05-06 11:45:35 -05:00
|
|
|
) -> Option<()> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("add_missing_impl_members_inner");
|
2020-07-30 11:28:28 -05:00
|
|
|
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?;
|
2020-05-06 05:51:28 -05:00
|
|
|
let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2020-11-05 16:34:50 -06:00
|
|
|
let missing_items = filter_assoc_items(
|
|
|
|
ctx.db(),
|
|
|
|
&ide_db::traits::get_missing_assoc_items(&ctx.sema, &impl_def),
|
|
|
|
mode,
|
|
|
|
);
|
2020-02-11 06:57:26 -06:00
|
|
|
|
2019-07-03 08:17:18 -05:00
|
|
|
if missing_items.is_empty() {
|
2019-03-06 18:48:31 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = impl_def.syntax().text_range();
|
2020-07-02 16:48:35 -05:00
|
|
|
acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
|
2020-09-20 18:42:27 -05:00
|
|
|
let target_scope = ctx.sema.scope(impl_def.syntax());
|
2020-11-05 16:34:50 -06:00
|
|
|
let (new_impl_def, first_new_item) =
|
|
|
|
add_trait_assoc_items_to_impl(&ctx.sema, missing_items, trait_, impl_def, target_scope);
|
2020-05-19 18:53:21 -05:00
|
|
|
match ctx.config.snippet_cap {
|
2020-09-20 18:42:27 -05:00
|
|
|
None => builder.replace(target, new_impl_def.to_string()),
|
2020-05-19 19:07:21 -05:00
|
|
|
Some(cap) => {
|
|
|
|
let mut cursor = Cursor::Before(first_new_item.syntax());
|
|
|
|
let placeholder;
|
2020-07-30 07:51:08 -05:00
|
|
|
if let ast::AssocItem::Fn(func) = &first_new_item {
|
2020-05-19 19:07:21 -05:00
|
|
|
if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) {
|
|
|
|
if m.syntax().text() == "todo!()" {
|
|
|
|
placeholder = m;
|
|
|
|
cursor = Cursor::Replace(placeholder.syntax());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.replace_snippet(
|
2020-05-19 18:53:21 -05:00
|
|
|
cap,
|
2020-09-20 18:42:27 -05:00
|
|
|
target,
|
|
|
|
render_snippet(cap, new_impl_def.syntax(), cursor),
|
2020-05-19 19:07:21 -05:00
|
|
|
)
|
|
|
|
}
|
2019-09-28 12:09:57 -05:00
|
|
|
};
|
2019-10-27 09:35:37 -05:00
|
|
|
})
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2019-03-06 07:41:22 -06:00
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use super::*;
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_add_missing_impl_members() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-06 07:41:22 -06:00
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
fn foo(&self);
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self);
|
2019-03-07 05:02:53 -06:00
|
|
|
fn baz(&self);
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self) {}
|
2021-01-06 14:15:48 -06:00
|
|
|
$0
|
2020-05-02 04:50:43 -05:00
|
|
|
}"#,
|
|
|
|
r#"
|
2019-03-06 07:41:22 -06:00
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
fn foo(&self);
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self);
|
2019-03-07 05:02:53 -06:00
|
|
|
fn baz(&self);
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self) {}
|
2020-08-14 08:10:52 -05:00
|
|
|
|
2020-05-19 18:53:21 -05:00
|
|
|
$0type Output;
|
2020-08-14 08:10:52 -05:00
|
|
|
|
2019-07-03 08:17:18 -05:00
|
|
|
const CONST: usize = 42;
|
2020-08-14 08:10:52 -05:00
|
|
|
|
2020-05-02 04:50:43 -05:00
|
|
|
fn foo(&self) {
|
|
|
|
todo!()
|
|
|
|
}
|
2020-08-14 08:10:52 -05:00
|
|
|
|
2020-05-02 04:50:43 -05:00
|
|
|
fn baz(&self) {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}"#,
|
2019-03-06 07:41:22 -06:00
|
|
|
);
|
|
|
|
}
|
2019-03-07 06:21:56 -06:00
|
|
|
|
2019-03-07 06:44:01 -06:00
|
|
|
#[test]
|
|
|
|
fn test_copied_overriden_members() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-07 06:44:01 -06:00
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
|
|
|
fn bar(&self) -> bool { true }
|
|
|
|
fn baz(&self) -> u32 { 42 }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
|
|
|
fn bar(&self) {}
|
2021-01-06 14:15:48 -06:00
|
|
|
$0
|
2020-05-02 04:50:43 -05:00
|
|
|
}"#,
|
|
|
|
r#"
|
2019-03-07 06:44:01 -06:00
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
|
|
|
fn bar(&self) -> bool { true }
|
|
|
|
fn baz(&self) -> u32 { 42 }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
|
|
|
fn bar(&self) {}
|
2020-08-14 08:10:52 -05:00
|
|
|
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2019-03-07 06:44:01 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:21:56 -06:00
|
|
|
#[test]
|
2020-02-29 14:24:40 -06:00
|
|
|
fn test_empty_impl_def() {
|
2019-03-07 06:21:56 -06:00
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-07 06:21:56 -06:00
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-07 06:21:56 -06:00
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
2020-09-20 18:43:03 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_def_without_braces() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S$0"#,
|
2020-09-20 18:43:03 -05:00
|
|
|
r#"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
|
|
|
fn foo(&self) {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
2020-05-02 04:50:43 -05:00
|
|
|
}"#,
|
2019-03-07 06:21:56 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:44:32 -06:00
|
|
|
#[test]
|
|
|
|
fn fill_in_type_params_1() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-12-24 09:44:32 -06:00
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo<u32> for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-12-24 09:44:32 -06:00
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
|
|
|
impl Foo<u32> for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, t: u32) -> &u32 {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2019-12-24 09:44:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fill_in_type_params_2() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-12-24 09:44:32 -06:00
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl<U> Foo<U> for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-12-24 09:44:32 -06:00
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
|
|
|
impl<U> Foo<U> for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, t: U) -> &U {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2019-12-24 09:44:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:21:56 -06:00
|
|
|
#[test]
|
2020-02-29 14:24:40 -06:00
|
|
|
fn test_cursor_after_empty_impl_def() {
|
2019-03-16 17:19:14 -05:00
|
|
|
check_assist(
|
2019-03-07 06:21:56 -06:00
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-07 06:21:56 -06:00
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S {}$0"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-16 17:19:14 -05:00
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2019-03-07 06:21:56 -06:00
|
|
|
)
|
|
|
|
}
|
2019-03-07 06:44:01 -06:00
|
|
|
|
2019-12-30 06:53:43 -06:00
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_1() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-12-30 06:53:43 -06:00
|
|
|
mod foo {
|
2019-12-31 09:17:08 -06:00
|
|
|
pub struct Bar;
|
2019-12-30 06:53:43 -06:00
|
|
|
trait Foo { fn foo(&self, bar: Bar); }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-12-30 06:53:43 -06:00
|
|
|
mod foo {
|
2019-12-31 09:17:08 -06:00
|
|
|
pub struct Bar;
|
2019-12-30 06:53:43 -06:00
|
|
|
trait Foo { fn foo(&self, bar: Bar); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: foo::Bar) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2019-12-30 06:53:43 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-25 08:19:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_2() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub trait Foo { fn foo(&self, bar: Bar); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::bar;
|
|
|
|
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl bar::Foo for S { $0 }"#,
|
2020-09-25 08:19:22 -05:00
|
|
|
r#"
|
|
|
|
mod foo {
|
|
|
|
pub mod bar {
|
|
|
|
pub struct Bar;
|
|
|
|
pub trait Foo { fn foo(&self, bar: Bar); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
use foo::bar;
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
impl bar::Foo for S {
|
|
|
|
fn foo(&self, bar: bar::Bar) {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-01 16:08:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_generic() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>); }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: foo::Bar<u32>) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2020-01-01 16:08:22 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_and_substitute_param() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo<T> { fn foo(&self, bar: Bar<T>); }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo<u32> for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo<T> { fn foo(&self, bar: Bar<T>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo<u32> for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: foo::Bar<u32>) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2020-01-01 16:08:22 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:05:58 -06:00
|
|
|
#[test]
|
|
|
|
fn test_substitute_param_no_qualify() {
|
|
|
|
// when substituting params, the substituted param should not be qualified!
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-03 16:05:58 -06:00
|
|
|
mod foo {
|
|
|
|
trait Foo<T> { fn foo(&self, bar: T); }
|
|
|
|
pub struct Param;
|
|
|
|
}
|
|
|
|
struct Param;
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo<Param> for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-03 16:05:58 -06:00
|
|
|
mod foo {
|
|
|
|
trait Foo<T> { fn foo(&self, bar: T); }
|
|
|
|
pub struct Param;
|
|
|
|
}
|
|
|
|
struct Param;
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo<Param> for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: Param) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2020-01-03 16:05:58 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-01 16:08:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_associated_item() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
impl Bar<T> { type Assoc = u32; }
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
2020-01-03 12:58:56 -06:00
|
|
|
impl Bar<T> { type Assoc = u32; }
|
2020-01-01 16:08:22 -06:00
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: foo::Bar<u32>::Assoc) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2020-01-01 16:08:22 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_nested() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
pub struct Baz;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<Baz>); }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
pub struct Baz;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<Baz>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: foo::Bar<foo::Baz>) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2020-01-01 16:08:22 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_fn_trait_notation() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub trait Fn<Args> { type Output; }
|
|
|
|
trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl foo::Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2020-01-01 16:08:22 -06:00
|
|
|
mod foo {
|
|
|
|
pub trait Fn<Args> { type Output; }
|
|
|
|
trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn foo(&self, bar: dyn Fn(u32) -> i32) {
|
|
|
|
${0:todo!()}
|
2020-05-02 04:50:43 -05:00
|
|
|
}
|
|
|
|
}"#,
|
2020-01-01 16:08:22 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:44:01 -06:00
|
|
|
#[test]
|
|
|
|
fn test_empty_trait() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-07 06:44:01 -06:00
|
|
|
trait Foo;
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S { $0 }"#,
|
2019-03-07 06:44:01 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-23 09:49:20 -05:00
|
|
|
fn test_ignore_unnamed_trait_members_and_default_methods() {
|
|
|
|
check_assist_not_applicable(
|
2019-03-07 06:44:01 -06:00
|
|
|
add_missing_impl_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-07 06:44:01 -06:00
|
|
|
trait Foo {
|
|
|
|
fn (arg: u32);
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S { $0 }"#,
|
2019-03-07 09:10:21 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-23 09:49:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_with_docstring_and_attrs() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
#[doc(alias = "test alias")]
|
|
|
|
trait Foo {
|
|
|
|
/// doc string
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
2019-03-23 09:49:20 -05:00
|
|
|
#[must_use]
|
|
|
|
fn foo(&self);
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S {}$0"#,
|
2019-03-23 09:49:20 -05:00
|
|
|
r#"
|
|
|
|
#[doc(alias = "test alias")]
|
|
|
|
trait Foo {
|
|
|
|
/// doc string
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
2019-03-23 09:49:20 -05:00
|
|
|
#[must_use]
|
|
|
|
fn foo(&self);
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2020-05-19 18:53:21 -05:00
|
|
|
$0type Output;
|
2020-08-14 08:10:52 -05:00
|
|
|
|
2020-05-02 04:50:43 -05:00
|
|
|
fn foo(&self) {
|
|
|
|
todo!()
|
|
|
|
}
|
2019-03-23 09:49:20 -05:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2019-03-23 10:06:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_methods() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_default_members,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-23 10:06:25 -05:00
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-23 10:06:25 -05:00
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
fn foo(some: u32) -> bool;
|
|
|
|
}
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S { $0 }"#,
|
2020-05-02 04:50:43 -05:00
|
|
|
r#"
|
2019-03-23 10:06:25 -05:00
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-23 10:06:25 -05:00
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
fn foo(some: u32) -> bool;
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2020-05-19 18:53:21 -05:00
|
|
|
$0fn valid(some: u32) -> bool { false }
|
2020-05-13 08:06:42 -05:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generic_single_default_parameter() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Foo<T = Self> {
|
|
|
|
fn bar(&self, other: &T);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo for S { $0 }"#,
|
2020-05-13 08:06:42 -05:00
|
|
|
r#"
|
|
|
|
trait Foo<T = Self> {
|
|
|
|
fn bar(&self, other: &T);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn bar(&self, other: &Self) {
|
|
|
|
${0:todo!()}
|
2020-05-13 08:06:42 -05:00
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generic_default_parameter_is_second() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Foo<T1, T2 = Self> {
|
|
|
|
fn bar(&self, this: &T1, that: &T2);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S<T>;
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Foo<T> for S<T> { $0 }"#,
|
2020-05-13 08:06:42 -05:00
|
|
|
r#"
|
|
|
|
trait Foo<T1, T2 = Self> {
|
|
|
|
fn bar(&self, this: &T1, that: &T2);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S<T>;
|
|
|
|
impl Foo<T> for S<T> {
|
2020-05-19 19:07:21 -05:00
|
|
|
fn bar(&self, this: &T, that: &Self) {
|
|
|
|
${0:todo!()}
|
2020-05-13 08:06:42 -05:00
|
|
|
}
|
2020-07-14 06:12:16 -05:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_assoc_type_bounds_are_removed() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
type Ty: Copy + 'static;
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Tr for ()$0 {
|
2020-07-14 06:12:16 -05:00
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
type Ty: Copy + 'static;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Tr for () {
|
|
|
|
$0type Ty;
|
2020-08-14 08:10:52 -05:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_whitespace_fixup_preserves_bad_tokens() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
fn foo();
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Tr for ()$0 {
|
2020-08-14 08:10:52 -05:00
|
|
|
+++
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
fn foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Tr for () {
|
|
|
|
fn foo() {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
|
|
|
+++
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_whitespace_fixup_preserves_comments() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
fn foo();
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
impl Tr for ()$0 {
|
2020-08-14 08:10:52 -05:00
|
|
|
// very important
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
trait Tr {
|
|
|
|
fn foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Tr for () {
|
|
|
|
fn foo() {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
|
|
|
// very important
|
2020-05-02 04:50:43 -05:00
|
|
|
}"#,
|
2019-03-23 10:06:25 -05:00
|
|
|
)
|
|
|
|
}
|
2020-10-06 09:19:18 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn weird_path() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn foo(&self, x: crate)
|
|
|
|
}
|
|
|
|
impl Test for () {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0
|
2020-10-06 09:19:18 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Test {
|
|
|
|
fn foo(&self, x: crate)
|
|
|
|
}
|
|
|
|
impl Test for () {
|
|
|
|
fn foo(&self, x: crate) {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 11:18:19 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_generic_type() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
trait Foo<BAR> {
|
|
|
|
fn foo(&self, bar: BAR);
|
|
|
|
}
|
|
|
|
impl Foo for () {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0
|
2020-12-12 11:18:19 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<BAR> {
|
|
|
|
fn foo(&self, bar: BAR);
|
|
|
|
}
|
|
|
|
impl Foo for () {
|
|
|
|
fn foo(&self, bar: BAR) {
|
|
|
|
${0:todo!()}
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 09:19:18 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|