From d99ae9ba56ac3ca2a07dc5b587b5a3c0dd504762 Mon Sep 17 00:00:00 2001 From: gfreezy Date: Sat, 23 Mar 2019 22:49:20 +0800 Subject: Add impl members assist shold not copy docstrings, attrs and default methods. --- crates/ra_assists/src/add_missing_impl_members.rs | 64 +++++++++++++++-------- 1 file changed, 42 insertions(+), 22 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index 4435c4b5d..b09478d30 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -35,6 +35,7 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx) -> trait_fns .into_iter() .filter(|t| def_name(t).is_some()) + .filter(|t| t.body().is_none()) .filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t))) .collect() }; @@ -103,11 +104,13 @@ fn build_func_body(def: &ast::FnDef) -> String { let mut buf = String::new(); for child in def.syntax().children() { - if child.kind() == SyntaxKind::SEMI { - buf.push_str(" { unimplemented!() }") - } else { - child.text().push_to(&mut buf); - } + match (child.prev_sibling().map(|c| c.kind()), child.kind()) { + (_, SyntaxKind::SEMI) => buf.push_str(" { unimplemented!() }"), + (_, SyntaxKind::ATTR) | (_, SyntaxKind::COMMENT) => {} + (Some(SyntaxKind::ATTR), SyntaxKind::WHITESPACE) + | (Some(SyntaxKind::COMMENT), SyntaxKind::WHITESPACE) => {} + _ => child.text().push_to(&mut buf), + }; } buf.trim_end().to_string() @@ -180,8 +183,7 @@ struct S; impl Foo for S { fn bar(&self) {} - fn foo(&self) { unimplemented!() } - fn baz(&self) -> u32 { 42 }<|> + fn foo(&self) { unimplemented!() }<|> }", ); } @@ -193,7 +195,7 @@ impl Foo for S { " trait Foo { fn foo(&self); } struct S; -impl Foo for S {<|>}", +impl Foo for S { <|> }", " trait Foo { fn foo(&self); } struct S; @@ -232,8 +234,8 @@ impl Foo for S { <|> }", } #[test] - fn test_ignore_unnamed_trait_members() { - check_assist( + fn test_ignore_unnamed_trait_members_and_default_methods() { + check_assist_not_applicable( add_missing_impl_members, " trait Foo { @@ -242,15 +244,6 @@ trait Foo { } struct S; impl Foo for S { <|> }", - " -trait Foo { - fn (arg: u32); - fn valid(some: u32) -> bool { false } -} -struct S; -impl Foo for S { - fn valid(some: u32) -> bool { false }<|> -}", ) } @@ -260,7 +253,7 @@ impl Foo for S { add_missing_impl_members, " trait Foo { - fn valid(some: u32) -> bool { false } + fn valid(some: u32) -> bool; } struct S; @@ -269,15 +262,42 @@ mod my_mod { }", " trait Foo { - fn valid(some: u32) -> bool { false } + fn valid(some: u32) -> bool; } struct S; mod my_mod { impl crate::Foo for S { - fn valid(some: u32) -> bool { false }<|> + fn valid(some: u32) -> bool { unimplemented!() }<|> } }", ) } + + #[test] + fn test_with_docstring_and_attrs() { + check_assist( + add_missing_impl_members, + r#" +#[doc(alias = "test alias")] +trait Foo { + /// doc string + #[must_use] + fn foo(&self); +} +struct S; +impl Foo for S {}<|>"#, + r#" +#[doc(alias = "test alias")] +trait Foo { + /// doc string + #[must_use] + fn foo(&self); +} +struct S; +impl Foo for S { + fn foo(&self) { unimplemented!() }<|> +}"#, + ) + } } -- cgit v1.2.3 From 02383b91d51c160f4f91aa9da7d2a7c11482b76e Mon Sep 17 00:00:00 2001 From: gfreezy Date: Sat, 23 Mar 2019 23:06:25 +0800 Subject: Add assist for adding default methods --- crates/ra_assists/src/add_missing_impl_members.rs | 61 +++++++++++++++++++++-- crates/ra_assists/src/lib.rs | 1 + 2 files changed, 59 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index b09478d30..e13f54c4f 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -9,7 +9,35 @@ use ra_fmt::{leading_indent, reindent}; use itertools::Itertools; -pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx) -> Option { +enum AddMissingImplMembersMode { + DefaultMethodsOnly, + NoDefaultMethods, +} + +pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option { + add_missing_impl_members_inner( + ctx, + AddMissingImplMembersMode::NoDefaultMethods, + "add_impl_missing_members", + "add missing impl members", + ) +} + +pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option { + add_missing_impl_members_inner( + ctx, + AddMissingImplMembersMode::DefaultMethodsOnly, + "add_impl_default_members", + "add impl default members", + ) +} + +fn add_missing_impl_members_inner( + mut ctx: AssistCtx, + mode: AddMissingImplMembersMode, + assist_id: &'static str, + label: &'static str, +) -> Option { let impl_node = ctx.node_at_offset::()?; let impl_item_list = impl_node.item_list()?; @@ -35,7 +63,10 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx) -> trait_fns .into_iter() .filter(|t| def_name(t).is_some()) - .filter(|t| t.body().is_none()) + .filter(|t| match mode { + AddMissingImplMembersMode::DefaultMethodsOnly => t.body().is_some(), + AddMissingImplMembersMode::NoDefaultMethods => t.body().is_none(), + }) .filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t))) .collect() }; @@ -43,7 +74,7 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx) -> return None; } - ctx.add_action(AssistId("add_impl_missing_members"), "add missing impl members", |edit| { + ctx.add_action(AssistId(assist_id), label, |edit| { let (parent_indent, indent) = { // FIXME: Find a way to get the indent already used in the file. // Now, we copy the indent of first item or indent with 4 spaces relative to impl block @@ -300,4 +331,28 @@ impl Foo for S { }"#, ) } + + #[test] + fn test_default_methods() { + check_assist( + add_missing_default_members, + " +trait Foo { + fn valid(some: u32) -> bool { false } + fn foo(some: u32) -> bool; +} +struct S; +impl Foo for S { <|> }", + " +trait Foo { + fn valid(some: u32) -> bool { false } + fn foo(some: u32) -> bool; +} +struct S; +impl Foo for S { + fn valid(some: u32) -> bool { false }<|> +}", + ) + } + } diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 871b37f58..fc36e8cc9 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -112,6 +112,7 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option