diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-25 08:09:26 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-25 08:09:26 +0000 |
commit | 8a73a8937d67db97072a11a40f0c3ed963e18e1b (patch) | |
tree | 7bfffa2750b044f47b67d7b9e89c14c1a1089505 | |
parent | 6ad1a0711631d8017791a6dfe85bbe205d6c7414 (diff) | |
parent | d606521723d2635a21e6dd821b314f8e3127cbf9 (diff) |
Merge #3707
3707: Add ItemScope::visibility_of r=matklad a=edwin0cheng
~This PR implements `HasVisibility` for various constructs and change `Definition::search_scope` to use `Visibility` directly instead of depends on ad-hoc string parsing.~
This PR added `visibility_of` in `ItemScope` and `Module` and use it directly directly instead of depends on ad-hoc string parsing.
And also add a FIXME to indicate that there is a bug which do not search child-submodules in other files recursively in `Definition::search_scope`.
I will submit another PR to fix that bug after this is merged.
cc @flodiebold
Co-authored-by: Edwin Cheng <[email protected]>
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/item_scope.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_db/src/defs.rs | 31 | ||||
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 66 |
5 files changed, 50 insertions, 59 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c5cfd875f..3f0ebca0d 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -234,6 +234,10 @@ impl Module { | |||
234 | .collect() | 234 | .collect() |
235 | } | 235 | } |
236 | 236 | ||
237 | pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option<Visibility> { | ||
238 | db.crate_def_map(self.id.krate)[self.id.local_id].scope.visbility_of(def.clone().into()) | ||
239 | } | ||
240 | |||
237 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 241 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
238 | let _p = profile("Module::diagnostics"); | 242 | let _p = profile("Module::diagnostics"); |
239 | let crate_def_map = db.crate_def_map(self.id.krate); | 243 | let crate_def_map = db.crate_def_map(self.id.krate); |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 713d45f48..5af7e5d6d 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -54,7 +54,7 @@ pub use crate::{ | |||
54 | Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency, | 54 | Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency, |
55 | DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, | 55 | DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, |
56 | HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, | 56 | HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, |
57 | StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, | 57 | StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, |
58 | }, | 58 | }, |
59 | has_source::HasSource, | 59 | has_source::HasSource, |
60 | semantics::{original_range, PathResolution, Semantics, SemanticsScope}, | 60 | semantics::{original_range, PathResolution, Semantics, SemanticsScope}, |
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 5e943b780..ef1aaba6f 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs | |||
@@ -68,6 +68,12 @@ impl ItemScope { | |||
68 | self.impls.iter().copied() | 68 | self.impls.iter().copied() |
69 | } | 69 | } |
70 | 70 | ||
71 | pub fn visbility_of(&self, def: ModuleDefId) -> Option<Visibility> { | ||
72 | self.name_of(ItemInNs::Types(def)) | ||
73 | .or_else(|| self.name_of(ItemInNs::Values(def))) | ||
74 | .map(|(_, v)| v) | ||
75 | } | ||
76 | |||
71 | /// Iterate over all module scoped macros | 77 | /// Iterate over all module scoped macros |
72 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { | 78 | pub(crate) fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { |
73 | self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) | 79 | self.visible.iter().filter_map(|(name, def)| def.take_macros().map(|macro_| (name, macro_))) |
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index 97961bb6d..e9934844e 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs | |||
@@ -6,12 +6,12 @@ | |||
6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). | 6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). |
7 | 7 | ||
8 | use hir::{ | 8 | use hir::{ |
9 | Adt, FieldSource, HasSource, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, | 9 | HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, Semantics, |
10 | Semantics, StructField, TypeParam, | 10 | StructField, TypeParam, Visibility, |
11 | }; | 11 | }; |
12 | use ra_prof::profile; | 12 | use ra_prof::profile; |
13 | use ra_syntax::{ | 13 | use ra_syntax::{ |
14 | ast::{self, AstNode, VisibilityOwner}, | 14 | ast::{self, AstNode}, |
15 | match_ast, | 15 | match_ast, |
16 | }; | 16 | }; |
17 | use test_utils::tested_by; | 17 | use test_utils::tested_by; |
@@ -41,28 +41,13 @@ impl Definition { | |||
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | pub fn visibility(&self, db: &RootDatabase) -> Option<ast::Visibility> { | 44 | pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> { |
45 | let module = self.module(db); | ||
46 | |||
45 | match self { | 47 | match self { |
46 | Definition::Macro(_) => None, | 48 | Definition::Macro(_) => None, |
47 | Definition::StructField(sf) => match sf.source(db).value { | 49 | Definition::StructField(sf) => Some(sf.visibility(db)), |
48 | FieldSource::Named(it) => it.visibility(), | 50 | Definition::ModuleDef(def) => module?.visibility_of(db, def), |
49 | FieldSource::Pos(it) => it.visibility(), | ||
50 | }, | ||
51 | Definition::ModuleDef(def) => match def { | ||
52 | ModuleDef::Module(it) => it.declaration_source(db)?.value.visibility(), | ||
53 | ModuleDef::Function(it) => it.source(db).value.visibility(), | ||
54 | ModuleDef::Adt(adt) => match adt { | ||
55 | Adt::Struct(it) => it.source(db).value.visibility(), | ||
56 | Adt::Union(it) => it.source(db).value.visibility(), | ||
57 | Adt::Enum(it) => it.source(db).value.visibility(), | ||
58 | }, | ||
59 | ModuleDef::Const(it) => it.source(db).value.visibility(), | ||
60 | ModuleDef::Static(it) => it.source(db).value.visibility(), | ||
61 | ModuleDef::Trait(it) => it.source(db).value.visibility(), | ||
62 | ModuleDef::TypeAlias(it) => it.source(db).value.visibility(), | ||
63 | ModuleDef::EnumVariant(_) => None, | ||
64 | ModuleDef::BuiltinType(_) => None, | ||
65 | }, | ||
66 | Definition::SelfType(_) => None, | 51 | Definition::SelfType(_) => None, |
67 | Definition::Local(_) => None, | 52 | Definition::Local(_) => None, |
68 | Definition::TypeParam(_) => None, | 53 | Definition::TypeParam(_) => None, |
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 117454695..05a0eed30 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs | |||
@@ -6,7 +6,7 @@ | |||
6 | 6 | ||
7 | use std::mem; | 7 | use std::mem; |
8 | 8 | ||
9 | use hir::{DefWithBody, HasSource, ModuleSource, Semantics}; | 9 | use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; |
10 | use once_cell::unsync::Lazy; | 10 | use once_cell::unsync::Lazy; |
11 | use ra_db::{FileId, FileRange, SourceDatabaseExt}; | 11 | use ra_db::{FileId, FileRange, SourceDatabaseExt}; |
12 | use ra_prof::profile; | 12 | use ra_prof::profile; |
@@ -123,51 +123,47 @@ impl Definition { | |||
123 | return SearchScope::new(res); | 123 | return SearchScope::new(res); |
124 | } | 124 | } |
125 | 125 | ||
126 | let vis = self.visibility(db).as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); | 126 | let vis = self.visibility(db); |
127 | 127 | ||
128 | if vis.as_str() == "pub(super)" { | 128 | // FIXME: |
129 | if let Some(parent_module) = module.parent(db) { | 129 | // The following logic are wrong that it does not search |
130 | let mut res = FxHashMap::default(); | 130 | // for submodules within other files recursively. |
131 | let parent_src = parent_module.definition_source(db); | ||
132 | let file_id = parent_src.file_id.original_file(db); | ||
133 | 131 | ||
134 | match parent_src.value { | 132 | if let Some(Visibility::Module(module)) = vis.and_then(|it| it.into()) { |
135 | ModuleSource::Module(m) => { | 133 | let module: Module = module.into(); |
136 | let range = Some(m.syntax().text_range()); | 134 | let mut res = FxHashMap::default(); |
137 | res.insert(file_id, range); | 135 | let src = module.definition_source(db); |
138 | } | 136 | let file_id = src.file_id.original_file(db); |
139 | ModuleSource::SourceFile(_) => { | 137 | |
140 | res.insert(file_id, None); | 138 | match src.value { |
141 | res.extend(parent_module.children(db).map(|m| { | 139 | ModuleSource::Module(m) => { |
142 | let src = m.definition_source(db); | 140 | let range = Some(m.syntax().text_range()); |
143 | (src.file_id.original_file(db), None) | 141 | res.insert(file_id, range); |
144 | })); | 142 | } |
145 | } | 143 | ModuleSource::SourceFile(_) => { |
144 | res.insert(file_id, None); | ||
145 | res.extend(module.children(db).map(|m| { | ||
146 | let src = m.definition_source(db); | ||
147 | (src.file_id.original_file(db), None) | ||
148 | })); | ||
146 | } | 149 | } |
147 | return SearchScope::new(res); | ||
148 | } | 150 | } |
151 | return SearchScope::new(res); | ||
149 | } | 152 | } |
150 | 153 | ||
151 | if vis.as_str() != "" { | 154 | if let Some(Visibility::Public) = vis { |
152 | let source_root_id = db.file_source_root(file_id); | 155 | let source_root_id = db.file_source_root(file_id); |
153 | let source_root = db.source_root(source_root_id); | 156 | let source_root = db.source_root(source_root_id); |
154 | let mut res = source_root.walk().map(|id| (id, None)).collect::<FxHashMap<_, _>>(); | 157 | let mut res = source_root.walk().map(|id| (id, None)).collect::<FxHashMap<_, _>>(); |
155 | 158 | ||
156 | // FIXME: add "pub(in path)" | 159 | let krate = module.krate(); |
157 | 160 | for rev_dep in krate.reverse_dependencies(db) { | |
158 | if vis.as_str() == "pub(crate)" { | 161 | let root_file = rev_dep.root_file(db); |
159 | return SearchScope::new(res); | 162 | let source_root_id = db.file_source_root(root_file); |
160 | } | 163 | let source_root = db.source_root(source_root_id); |
161 | if vis.as_str() == "pub" { | 164 | res.extend(source_root.walk().map(|id| (id, None))); |
162 | let krate = module.krate(); | ||
163 | for rev_dep in krate.reverse_dependencies(db) { | ||
164 | let root_file = rev_dep.root_file(db); | ||
165 | let source_root_id = db.file_source_root(root_file); | ||
166 | let source_root = db.source_root(source_root_id); | ||
167 | res.extend(source_root.walk().map(|id| (id, None))); | ||
168 | } | ||
169 | return SearchScope::new(res); | ||
170 | } | 165 | } |
166 | return SearchScope::new(res); | ||
171 | } | 167 | } |
172 | 168 | ||
173 | let mut res = FxHashMap::default(); | 169 | let mut res = FxHashMap::default(); |