@@ -105,6 +105,14 @@ pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<
105
105
let call: ast:: MethodCallExpr = ctx. find_node_at_offset ( ) ?;
106
106
let ty = ctx. sema . type_of_expr ( & call. receiver ( ) ?) ?. original ( ) . strip_references ( ) . as_adt ( ) ?;
107
107
108
+ let current_module =
109
+ ctx. sema . scope ( ctx. find_node_at_offset :: < ast:: MethodCallExpr > ( ) ?. syntax ( ) ) . module ( ) ?;
110
+ let target_module = ty. module ( ctx. sema . db ) ;
111
+
112
+ if current_module. krate ( ) != target_module. krate ( ) {
113
+ return None ;
114
+ }
115
+
108
116
let ( impl_, file) = match ty {
109
117
hir:: Adt :: Struct ( strukt) => get_impl ( strukt. source ( ctx. sema . db ) ?. syntax ( ) , & fn_name, ctx) ,
110
118
hir:: Adt :: Enum ( en) => get_impl ( en. source ( ctx. sema . db ) ?. syntax ( ) , & fn_name, ctx) ,
@@ -117,7 +125,8 @@ pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<
117
125
& fn_name,
118
126
& impl_,
119
127
file,
120
- ty. module ( ctx. sema . db ) ,
128
+ target_module,
129
+ current_module,
121
130
) ?;
122
131
let target = call. syntax ( ) . text_range ( ) ;
123
132
@@ -261,6 +270,7 @@ impl FunctionBuilder {
261
270
impl_ : & Option < ast:: Impl > ,
262
271
file : FileId ,
263
272
target_module : Module ,
273
+ current_module : Module ,
264
274
) -> Option < Self > {
265
275
// let mut file = ctx.frange.file_id;
266
276
// let target_module = ctx.sema.scope(call.syntax()).module()?;
@@ -274,8 +284,8 @@ impl FunctionBuilder {
274
284
. 1
275
285
}
276
286
} ;
287
+ let needs_pub = !module_is_descendant ( & current_module, & target_module, ctx) ;
277
288
278
- let needs_pub = false ;
279
289
let fn_name = make:: name ( & name. text ( ) ) ;
280
290
let ( type_params, params) = fn_args ( ctx, target_module, FuncExpr :: Method ( call) ) ?;
281
291
@@ -564,6 +574,18 @@ fn next_space_for_fn_in_impl(impl_: &ast::Impl) -> Option<GeneratedFunctionTarge
564
574
}
565
575
}
566
576
577
+ fn module_is_descendant ( module : & hir:: Module , ans : & hir:: Module , ctx : & AssistContext ) -> bool {
578
+ if module == ans {
579
+ return true ;
580
+ }
581
+ for c in ans. children ( ctx. sema . db ) {
582
+ if module_is_descendant ( module, & c, ctx) {
583
+ return true ;
584
+ }
585
+ }
586
+ false
587
+ }
588
+
567
589
#[ cfg( test) ]
568
590
mod tests {
569
591
use crate :: tests:: { check_assist, check_assist_not_applicable} ;
0 commit comments