11//! Completion of paths, including when writing a single name.
22
3- use hir:: { Adt , PathResolution , ScopeDef } ;
3+ use hir:: { Adt , PathResolution , ScopeDef , HasVisibility } ;
44use ra_syntax:: AstNode ;
55use 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