aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-03-08 09:51:40 +0000
committerFlorian Diebold <[email protected]>2020-03-08 09:51:40 +0000
commitd9c77c54534fcde7c432c6e11746d636d972a20b (patch)
treecdf44c613f55bdd99ab08490b32c185e9915c7bd /crates/ra_ide/src/completion
parent734e68da4ceb1b15b3430302f233d4700d694728 (diff)
Handle visibility for path completion (not in all cases yet)
Diffstat (limited to 'crates/ra_ide/src/completion')
-rw-r--r--crates/ra_ide/src/completion/complete_path.rs44
1 files changed, 40 insertions, 4 deletions
diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs
index 1a9699466..f99b1c2c4 100644
--- a/crates/ra_ide/src/completion/complete_path.rs
+++ b/crates/ra_ide/src/completion/complete_path.rs
@@ -1,6 +1,6 @@
1//! Completion of paths, including when writing a single name. 1//! Completion of paths, including when writing a single name.
2 2
3use hir::{Adt, PathResolution, ScopeDef}; 3use hir::{Adt, PathResolution, ScopeDef, HasVisibility};
4use ra_syntax::AstNode; 4use ra_syntax::AstNode;
5use test_utils::tested_by; 5use test_utils::tested_by;
6 6
@@ -15,9 +15,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
15 Some(PathResolution::Def(def)) => def, 15 Some(PathResolution::Def(def)) => def,
16 _ => return, 16 _ => return,
17 }; 17 };
18 let context_module = ctx.scope().module();
18 match def { 19 match def {
19 hir::ModuleDef::Module(module) => { 20 hir::ModuleDef::Module(module) => {
20 let module_scope = module.scope(ctx.db); 21 let module_scope = module.scope(ctx.db, context_module);
21 for (name, def) in module_scope { 22 for (name, def) in module_scope {
22 if ctx.use_item_syntax.is_some() { 23 if ctx.use_item_syntax.is_some() {
23 if let ScopeDef::Unknown = def { 24 if let ScopeDef::Unknown = def {
@@ -53,7 +54,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
53 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { 54 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
54 match item { 55 match item {
55 hir::AssocItem::Function(func) => { 56 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)) {
57 acc.add_function(ctx, func); 58 acc.add_function(ctx, func);
58 } 59 }
59 } 60 }
@@ -170,6 +171,41 @@ mod tests {
170 } 171 }
171 172
172 #[test] 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
208 #[test]
173 fn completes_use_item_starting_with_self() { 209 fn completes_use_item_starting_with_self() {
174 assert_debug_snapshot!( 210 assert_debug_snapshot!(
175 do_reference_completion( 211 do_reference_completion(
@@ -177,7 +213,7 @@ mod tests {
177 use self::m::<|>; 213 use self::m::<|>;
178 214
179 mod m { 215 mod m {
180 struct Bar; 216 pub struct Bar;
181 } 217 }
182 " 218 "
183 ), 219 ),