aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorunexge <[email protected]>2020-08-17 18:22:14 +0100
committerunexge <[email protected]>2020-08-20 19:34:53 +0100
commit5cff4b60bedf4d0b2e30d08aa19686584202a560 (patch)
tree25d71346b0105a525b857e6be21e8335f5eaeaa3 /crates/assists
parent5d28dec7b93aad54866c80213358fb3eb28153be (diff)
Fix importing private modules in expand glob import
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/expand_glob_import.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/expand_glob_import.rs b/crates/assists/src/handlers/expand_glob_import.rs
index 194fae63e..eb6dd68bb 100644
--- a/crates/assists/src/handlers/expand_glob_import.rs
+++ b/crates/assists/src/handlers/expand_glob_import.rs
@@ -162,11 +162,28 @@ fn find_refs_in_mod(
162 module: Module, 162 module: Module,
163 visible_from: Option<Module>, 163 visible_from: Option<Module>,
164) -> Option<Refs> { 164) -> Option<Refs> {
165 if let Some(from) = visible_from {
166 if !is_mod_visible_from(ctx, module, from) {
167 return None;
168 }
169 }
170
165 let module_scope = module.scope(ctx.db(), visible_from); 171 let module_scope = module.scope(ctx.db(), visible_from);
166 let refs = module_scope.into_iter().filter_map(|(n, d)| Ref::from_scope_def(n, d)).collect(); 172 let refs = module_scope.into_iter().filter_map(|(n, d)| Ref::from_scope_def(n, d)).collect();
167 Some(Refs(refs)) 173 Some(Refs(refs))
168} 174}
169 175
176fn is_mod_visible_from(ctx: &AssistContext, module: Module, from: Module) -> bool {
177 match module.parent(ctx.db()) {
178 Some(parent) => {
179 parent.visibility_of(ctx.db(), &ModuleDef::Module(module)).map_or(true, |vis| {
180 vis.is_visible_from(ctx.db(), from.into()) && is_mod_visible_from(ctx, parent, from)
181 })
182 }
183 None => true,
184 }
185}
186
170// looks for name refs in parent use block's siblings 187// looks for name refs in parent use block's siblings
171// 188//
172// mod bar { 189// mod bar {
@@ -816,6 +833,41 @@ fn main() {
816 } 833 }
817 834
818 #[test] 835 #[test]
836 fn expanding_is_not_applicable_if_target_module_is_not_accessible_from_current_scope() {
837 check_assist_not_applicable(
838 expand_glob_import,
839 r"
840mod foo {
841 mod bar {
842 pub struct Bar;
843 }
844}
845
846use foo::bar::*<|>;
847
848fn baz(bar: Bar) {}
849",
850 );
851
852 check_assist_not_applicable(
853 expand_glob_import,
854 r"
855mod foo {
856 mod bar {
857 pub mod baz {
858 pub struct Baz;
859 }
860 }
861}
862
863use foo::bar::baz::*<|>;
864
865fn qux(baz: Baz) {}
866",
867 );
868 }
869
870 #[test]
819 fn expanding_is_not_applicable_if_cursor_is_not_in_star_token() { 871 fn expanding_is_not_applicable_if_cursor_is_not_in_star_token() {
820 check_assist_not_applicable( 872 check_assist_not_applicable(
821 expand_glob_import, 873 expand_glob_import,