aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists')
-rw-r--r--crates/ra_assists/src/add_missing_impl_members.rs61
-rw-r--r--crates/ra_assists/src/lib.rs1
2 files changed, 59 insertions, 3 deletions
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};
9 9
10use itertools::Itertools; 10use itertools::Itertools;
11 11
12pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 12enum AddMissingImplMembersMode {
13 DefaultMethodsOnly,
14 NoDefaultMethods,
15}
16
17pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
18 add_missing_impl_members_inner(
19 ctx,
20 AddMissingImplMembersMode::NoDefaultMethods,
21 "add_impl_missing_members",
22 "add missing impl members",
23 )
24}
25
26pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
27 add_missing_impl_members_inner(
28 ctx,
29 AddMissingImplMembersMode::DefaultMethodsOnly,
30 "add_impl_default_members",
31 "add impl default members",
32 )
33}
34
35fn add_missing_impl_members_inner(
36 mut ctx: AssistCtx<impl HirDatabase>,
37 mode: AddMissingImplMembersMode,
38 assist_id: &'static str,
39 label: &'static str,
40) -> Option<Assist> {
13 let impl_node = ctx.node_at_offset::<ast::ImplBlock>()?; 41 let impl_node = ctx.node_at_offset::<ast::ImplBlock>()?;
14 let impl_item_list = impl_node.item_list()?; 42 let impl_item_list = impl_node.item_list()?;
15 43
@@ -35,7 +63,10 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) ->
35 trait_fns 63 trait_fns
36 .into_iter() 64 .into_iter()
37 .filter(|t| def_name(t).is_some()) 65 .filter(|t| def_name(t).is_some())
38 .filter(|t| t.body().is_none()) 66 .filter(|t| match mode {
67 AddMissingImplMembersMode::DefaultMethodsOnly => t.body().is_some(),
68 AddMissingImplMembersMode::NoDefaultMethods => t.body().is_none(),
69 })
39 .filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t))) 70 .filter(|t| impl_fns.iter().all(|i| def_name(i) != def_name(t)))
40 .collect() 71 .collect()
41 }; 72 };
@@ -43,7 +74,7 @@ pub(crate) fn add_missing_impl_members(mut ctx: AssistCtx<impl HirDatabase>) ->
43 return None; 74 return None;
44 } 75 }
45 76
46 ctx.add_action(AssistId("add_impl_missing_members"), "add missing impl members", |edit| { 77 ctx.add_action(AssistId(assist_id), label, |edit| {
47 let (parent_indent, indent) = { 78 let (parent_indent, indent) = {
48 // FIXME: Find a way to get the indent already used in the file. 79 // FIXME: Find a way to get the indent already used in the file.
49 // Now, we copy the indent of first item or indent with 4 spaces relative to impl block 80 // 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 {
300}"#, 331}"#,
301 ) 332 )
302 } 333 }
334
335 #[test]
336 fn test_default_methods() {
337 check_assist(
338 add_missing_default_members,
339 "
340trait Foo {
341 fn valid(some: u32) -> bool { false }
342 fn foo(some: u32) -> bool;
343}
344struct S;
345impl Foo for S { <|> }",
346 "
347trait Foo {
348 fn valid(some: u32) -> bool { false }
349 fn foo(some: u32) -> bool;
350}
351struct S;
352impl Foo for S {
353 fn valid(some: u32) -> bool { false }<|>
354}",
355 )
356 }
357
303} 358}
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<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
112 remove_dbg::remove_dbg, 112 remove_dbg::remove_dbg,
113 auto_import::auto_import, 113 auto_import::auto_import,
114 add_missing_impl_members::add_missing_impl_members, 114 add_missing_impl_members::add_missing_impl_members,
115 add_missing_impl_members::add_missing_default_members,
115 ] 116 ]
116} 117}
117 118