diff options
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r-- | crates/ra_ide_db/src/defs.rs | 31 | ||||
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 66 |
2 files changed, 39 insertions, 58 deletions
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(); |