@@ -2,7 +2,7 @@ use crate::{
22 assist_context:: { AssistContext , Assists } ,
33 AssistId ,
44} ;
5- use hir :: TypeRef ;
5+ use ide_db :: helpers :: FamousDefs ;
66use syntax:: {
77 ast:: { self , Impl , NameOwner } ,
88 AstNode ,
@@ -53,7 +53,8 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
5353 }
5454
5555 let impl_ = fn_node. syntax ( ) . ancestors ( ) . into_iter ( ) . find_map ( ast:: Impl :: cast) ?;
56- if is_default_implemented ( ctx, & impl_) . is_some ( ) {
56+ let implements_default = is_default_implemented ( ctx, & impl_) ?;
57+ if implements_default {
5758 return None ;
5859 }
5960
@@ -85,29 +86,25 @@ impl Default for {} {{
8586
8687fn is_default_implemented ( ctx : & AssistContext , impl_ : & Impl ) -> Option < bool > {
8788 let db = ctx. sema . db ;
88- let module = impl_. syntax ( ) . parent ( ) ?;
89- let sema_scope = ctx. sema . scope ( & module) ;
90- let impls = sema_scope. module ( ) ?. impl_defs ( db) ;
91- let mut name = None ;
92- for i in impls {
93- if let Some ( TypeRef :: Path ( p) ) = i. target_trait ( db) {
94- name = p. segments ( ) . iter ( ) . map ( |s| s. name . to_string ( ) ) . find ( |n| n == "Default" ) ;
95- }
96- }
97-
98- name. map ( |n| !n. is_empty ( ) )
89+ let impl_def = ctx. sema . to_def ( impl_) ?;
90+ let ty = impl_def. target_ty ( db) ;
91+ let krate = impl_def. module ( db) . krate ( ) ;
92+ let default_trait = FamousDefs ( & ctx. sema , Some ( krate) ) . core_default_Default ( ) ?;
93+ let implements_default = ty. impls_trait ( db, default_trait, & [ ] ) ;
94+ Some ( implements_default)
9995}
10096
10197#[ cfg( test) ]
10298mod tests {
99+ use ide_db:: helpers:: FamousDefs ;
100+
103101 use crate :: tests:: { check_assist, check_assist_not_applicable} ;
104102
105103 use super :: * ;
106104
107105 #[ test]
108106 fn generate_default ( ) {
109- check_assist (
110- generate_default_from_new,
107+ check_pass (
111108 r#"
112109struct Example { _inner: () }
113110
@@ -141,8 +138,7 @@ fn main() {}
141138
142139 #[ test]
143140 fn generate_default2 ( ) {
144- check_assist (
145- generate_default_from_new,
141+ check_pass (
146142 r#"
147143struct Test { value: u32 }
148144
@@ -173,8 +169,7 @@ impl Default for Test {
173169 #[ test]
174170 fn new_function_with_parameters ( ) {
175171 mark:: check!( new_function_with_parameters) ;
176- check_assist_not_applicable (
177- generate_default_from_new,
172+ check_not_applicable (
178173 r#"
179174struct Example { _inner: () }
180175
@@ -190,8 +185,7 @@ impl Example {
190185 #[ test]
191186 fn other_function_than_new ( ) {
192187 mark:: check!( other_function_than_new) ;
193- check_assist_not_applicable (
194- generate_default_from_new,
188+ check_not_applicable (
195189 r#"
196190struct Example { _inner: () }
197191
@@ -207,8 +201,7 @@ impl Exmaple {
207201
208202 #[ test]
209203 fn default_block_is_already_present ( ) {
210- check_assist_not_applicable (
211- generate_default_from_new,
204+ check_not_applicable (
212205 r#"
213206struct Example { _inner: () }
214207
@@ -229,8 +222,7 @@ impl Default for Example {
229222
230223 #[ test]
231224 fn standalone_new_function ( ) {
232- check_assist_not_applicable (
233- generate_default_from_new,
225+ check_not_applicable (
234226 r#"
235227fn n$0ew() -> u32 {
236228 0
@@ -241,8 +233,7 @@ fn n$0ew() -> u32 {
241233
242234 #[ test]
243235 fn multiple_struct_blocks ( ) {
244- check_assist (
245- generate_default_from_new,
236+ check_pass (
246237 r#"
247238struct Example { _inner: () }
248239struct Test { value: u32 }
@@ -274,8 +265,7 @@ impl Default for Example {
274265
275266 #[ test]
276267 fn when_struct_is_after_impl ( ) {
277- check_assist (
278- generate_default_from_new,
268+ check_pass (
279269 r#"
280270impl Example {
281271 pub fn $0new() -> Self {
@@ -305,8 +295,7 @@ struct Example { _inner: () }
305295
306296 #[ test]
307297 fn struct_in_module ( ) {
308- check_assist (
309- generate_default_from_new,
298+ check_pass (
310299 r#"
311300mod test {
312301 struct Example { _inner: () }
@@ -340,8 +329,7 @@ impl Default for Example {
340329
341330 #[ test]
342331 fn struct_in_module_with_default ( ) {
343- check_assist_not_applicable (
344- generate_default_from_new,
332+ check_not_applicable (
345333 r#"
346334mod test {
347335 struct Example { _inner: () }
@@ -361,4 +349,14 @@ mod test {
361349"# ,
362350 ) ;
363351 }
352+
353+ fn check_pass ( before : & str , after : & str ) {
354+ let before = & format ! ( "//- /main.rs crate:main deps:core{}{}" , before, FamousDefs :: FIXTURE ) ;
355+ check_assist ( generate_default_from_new, before, after) ;
356+ }
357+
358+ fn check_not_applicable ( before : & str ) {
359+ let before = & format ! ( "//- /main.rs crate:main deps:core{}{}" , before, FamousDefs :: FIXTURE ) ;
360+ check_assist_not_applicable ( generate_default_from_new, before) ;
361+ }
364362}
0 commit comments