Skip to content

Commit 734e68d

Browse files
committed
Handle visibility in method call completion
1 parent aff82cf commit 734e68d

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,14 @@ impl Function {
571571
}
572572
}
573573

574+
impl HasVisibility for Function {
575+
fn visibility(&self, db: &impl HirDatabase) -> Visibility {
576+
let function_data = db.function_data(self.id);
577+
let visibility = &function_data.visibility;
578+
visibility.resolve(db, &self.id.resolver(db))
579+
}
580+
}
581+
574582
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
575583
pub struct Const {
576584
pub(crate) id: ConstId,

crates/ra_hir_def/src/data.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ use hir_expand::{
77
AstId, InFile,
88
};
99
use ra_prof::profile;
10-
use ra_syntax::ast::{self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner};
10+
use ra_syntax::ast::{
11+
self, AstNode, ImplItem, ModuleItemOwner, NameOwner, TypeAscriptionOwner, VisibilityOwner,
12+
};
1113

1214
use crate::{
1315
db::DefDatabase,
1416
path::{path, GenericArgs, Path},
1517
src::HasSource,
1618
type_ref::{Mutability, TypeBound, TypeRef},
19+
visibility::RawVisibility,
1720
AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule,
1821
ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
1922
};
@@ -26,6 +29,7 @@ pub struct FunctionData {
2629
/// True if the first param is `self`. This is relevant to decide whether this
2730
/// can be called as a method.
2831
pub has_self_param: bool,
32+
pub visibility: RawVisibility,
2933
}
3034

3135
impl FunctionData {
@@ -72,7 +76,9 @@ impl FunctionData {
7276
ret_type
7377
};
7478

75-
let sig = FunctionData { name, params, ret_type, has_self_param };
79+
let visibility = RawVisibility::from_ast(db, src.map(|s| s.visibility()));
80+
81+
let sig = FunctionData { name, params, ret_type, has_self_param, visibility };
7682
Arc::new(sig)
7783
}
7884
}
@@ -230,7 +236,7 @@ impl ConstData {
230236
Arc::new(ConstData::new(&node))
231237
}
232238

233-
fn new<N: NameOwner + TypeAscriptionOwner>(node: &N) -> ConstData {
239+
fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>(node: &N) -> ConstData {
234240
let name = node.name().map(|n| n.as_name());
235241
let type_ref = TypeRef::from_ast_opt(node.ascribed_type());
236242
ConstData { name, type_ref }

crates/ra_ide/src/completion/complete_dot.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T
5757
let mut seen_methods = FxHashSet::default();
5858
let traits_in_scope = ctx.scope().traits_in_scope();
5959
receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
60-
if func.has_self_param(ctx.db) && seen_methods.insert(func.name(ctx.db)) {
60+
if func.has_self_param(ctx.db)
61+
&& ctx.scope().module().map_or(true, |m| func.is_visible_from(ctx.db, m))
62+
&& seen_methods.insert(func.name(ctx.db))
63+
{
6164
acc.add_function(ctx, func);
6265
}
6366
None::<()>
@@ -307,6 +310,39 @@ mod tests {
307310
);
308311
}
309312

313+
#[test]
314+
fn test_method_completion_private() {
315+
assert_debug_snapshot!(
316+
do_ref_completion(
317+
r"
318+
struct A {}
319+
mod m {
320+
impl super::A {
321+
fn private_method(&self) {}
322+
pub(super) fn the_method(&self) {}
323+
}
324+
}
325+
fn foo(a: A) {
326+
a.<|>
327+
}
328+
",
329+
),
330+
@r###"
331+
[
332+
CompletionItem {
333+
label: "the_method()",
334+
source_range: [256; 256),
335+
delete: [256; 256),
336+
insert: "the_method()$0",
337+
kind: Method,
338+
lookup: "the_method",
339+
detail: "pub(super) fn the_method(&self)",
340+
},
341+
]
342+
"###
343+
);
344+
}
345+
310346
#[test]
311347
fn test_trait_method_completion() {
312348
assert_debug_snapshot!(

0 commit comments

Comments
 (0)