Skip to content

Commit d9c77c5

Browse files
committed
Handle visibility for path completion (not in all cases yet)
1 parent 734e68d commit d9c77c5

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,20 @@ impl Module {
204204
}
205205

206206
/// Returns a `ModuleScope`: a set of items, visible in this module.
207-
pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> {
207+
pub fn scope(self, db: &impl HirDatabase, visible_from: Option<Module>) -> Vec<(Name, ScopeDef)> {
208208
db.crate_def_map(self.id.krate)[self.id.local_id]
209209
.scope
210210
.entries()
211+
.filter_map(|(name, def)| if let Some(m) = visible_from {
212+
let filtered = def.filter_visibility(|vis| vis.is_visible_from(db, m.id));
213+
if filtered.is_none() && !def.is_none() {
214+
None
215+
} else {
216+
Some((name, filtered))
217+
}
218+
} else {
219+
Some((name, def))
220+
})
211221
.map(|(name, def)| (name.clone(), def.into()))
212222
.collect()
213223
}

crates/ra_ide/src/completion/complete_path.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Completion of paths, including when writing a single name.
22
3-
use hir::{Adt, PathResolution, ScopeDef};
3+
use hir::{Adt, PathResolution, ScopeDef, HasVisibility};
44
use ra_syntax::AstNode;
55
use test_utils::tested_by;
66

@@ -15,9 +15,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
1515
Some(PathResolution::Def(def)) => def,
1616
_ => return,
1717
};
18+
let context_module = ctx.scope().module();
1819
match def {
1920
hir::ModuleDef::Module(module) => {
20-
let module_scope = module.scope(ctx.db);
21+
let module_scope = module.scope(ctx.db, context_module);
2122
for (name, def) in module_scope {
2223
if ctx.use_item_syntax.is_some() {
2324
if let ScopeDef::Unknown = def {
@@ -53,7 +54,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
5354
ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
5455
match item {
5556
hir::AssocItem::Function(func) => {
56-
if !func.has_self_param(ctx.db) {
57+
if !func.has_self_param(ctx.db) && context_module.map_or(true, |m| func.is_visible_from(ctx.db, m)) {
5758
acc.add_function(ctx, func);
5859
}
5960
}
@@ -169,6 +170,41 @@ mod tests {
169170
);
170171
}
171172

173+
#[test]
174+
fn path_visibility() {
175+
assert_debug_snapshot!(
176+
do_reference_completion(
177+
r"
178+
use self::my::<|>;
179+
180+
mod my {
181+
struct Bar;
182+
pub struct Foo;
183+
pub use Bar as PublicBar;
184+
}
185+
"
186+
),
187+
@r###"
188+
[
189+
CompletionItem {
190+
label: "Foo",
191+
source_range: [31; 31),
192+
delete: [31; 31),
193+
insert: "Foo",
194+
kind: Struct,
195+
},
196+
CompletionItem {
197+
label: "PublicBar",
198+
source_range: [31; 31),
199+
delete: [31; 31),
200+
insert: "PublicBar",
201+
kind: Struct,
202+
},
203+
]
204+
"###
205+
);
206+
}
207+
172208
#[test]
173209
fn completes_use_item_starting_with_self() {
174210
assert_debug_snapshot!(
@@ -177,7 +213,7 @@ mod tests {
177213
use self::m::<|>;
178214
179215
mod m {
180-
struct Bar;
216+
pub struct Bar;
181217
}
182218
"
183219
),

0 commit comments

Comments
 (0)