Skip to content

Commit 6240b2d

Browse files
committed
generate method adds pub keyword
1 parent d38f36e commit 6240b2d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<
105105
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
106106
let ty = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
107107

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+
108116
let (impl_, file) = match ty {
109117
hir::Adt::Struct(strukt) => get_impl(strukt.source(ctx.sema.db)?.syntax(), &fn_name, ctx),
110118
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<
117125
&fn_name,
118126
&impl_,
119127
file,
120-
ty.module(ctx.sema.db),
128+
target_module,
129+
current_module,
121130
)?;
122131
let target = call.syntax().text_range();
123132

@@ -261,6 +270,7 @@ impl FunctionBuilder {
261270
impl_: &Option<ast::Impl>,
262271
file: FileId,
263272
target_module: Module,
273+
current_module: Module,
264274
) -> Option<Self> {
265275
// let mut file = ctx.frange.file_id;
266276
// let target_module = ctx.sema.scope(call.syntax()).module()?;
@@ -274,8 +284,8 @@ impl FunctionBuilder {
274284
.1
275285
}
276286
};
287+
let needs_pub = !module_is_descendant(&current_module, &target_module, ctx);
277288

278-
let needs_pub = false;
279289
let fn_name = make::name(&name.text());
280290
let (type_params, params) = fn_args(ctx, target_module, FuncExpr::Method(call))?;
281291

@@ -564,6 +574,18 @@ fn next_space_for_fn_in_impl(impl_: &ast::Impl) -> Option<GeneratedFunctionTarge
564574
}
565575
}
566576

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+
567589
#[cfg(test)]
568590
mod tests {
569591
use crate::tests::{check_assist, check_assist_not_applicable};

0 commit comments

Comments
 (0)