aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/Cargo.toml1
-rw-r--r--crates/ra_ide_db/src/change.rs12
-rw-r--r--crates/ra_ide_db/src/defs.rs31
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs8
-rw-r--r--crates/ra_ide_db/src/search.rs66
5 files changed, 55 insertions, 63 deletions
diff --git a/crates/ra_ide_db/Cargo.toml b/crates/ra_ide_db/Cargo.toml
index de4f5bce0..c3921bd40 100644
--- a/crates/ra_ide_db/Cargo.toml
+++ b/crates/ra_ide_db/Cargo.toml
@@ -17,6 +17,7 @@ fst = { version = "0.4", default-features = false }
17rustc-hash = "1.1.0" 17rustc-hash = "1.1.0"
18superslice = "1.0.0" 18superslice = "1.0.0"
19once_cell = "1.3.1" 19once_cell = "1.3.1"
20either = "1.5.3"
20 21
21ra_syntax = { path = "../ra_syntax" } 22ra_syntax = { path = "../ra_syntax" }
22ra_text_edit = { path = "../ra_text_edit" } 23ra_text_edit = { path = "../ra_text_edit" }
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs
index 628cf6416..8446ef88e 100644
--- a/crates/ra_ide_db/src/change.rs
+++ b/crates/ra_ide_db/src/change.rs
@@ -311,6 +311,7 @@ impl RootDatabase {
311 hir::db::MacroDefQuery 311 hir::db::MacroDefQuery
312 hir::db::ParseMacroQuery 312 hir::db::ParseMacroQuery
313 hir::db::MacroExpandQuery 313 hir::db::MacroExpandQuery
314 hir::db::InternEagerExpansionQuery
314 315
315 // DefDatabase 316 // DefDatabase
316 hir::db::RawItemsQuery 317 hir::db::RawItemsQuery
@@ -359,14 +360,21 @@ impl RootDatabase {
359 hir::db::ImplsInCrateQuery 360 hir::db::ImplsInCrateQuery
360 hir::db::ImplsForTraitQuery 361 hir::db::ImplsForTraitQuery
361 hir::db::InternTypeCtorQuery 362 hir::db::InternTypeCtorQuery
363 hir::db::InternTypeParamIdQuery
362 hir::db::InternChalkImplQuery 364 hir::db::InternChalkImplQuery
363 hir::db::InternAssocTyValueQuery 365 hir::db::InternAssocTyValueQuery
364 hir::db::AssociatedTyDataQuery 366 hir::db::AssociatedTyDataQuery
365 hir::db::AssociatedTyValueQuery
366 hir::db::TraitSolveQuery
367 hir::db::TraitDatumQuery 367 hir::db::TraitDatumQuery
368 hir::db::StructDatumQuery 368 hir::db::StructDatumQuery
369 hir::db::ImplDatumQuery 369 hir::db::ImplDatumQuery
370 hir::db::AssociatedTyValueQuery
371 hir::db::TraitSolveQuery
372
373 // SymbolsDatabase
374 crate::symbol_index::FileSymbolsQuery
375
376 // LineIndexDatabase
377 crate::LineIndexQuery
370 ]; 378 ];
371 acc.sort_by_key(|it| std::cmp::Reverse(it.1)); 379 acc.sort_by_key(|it| std::cmp::Reverse(it.1));
372 acc 380 acc
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
8use hir::{ 8use 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};
12use ra_prof::profile; 12use ra_prof::profile;
13use ra_syntax::{ 13use ra_syntax::{
14 ast::{self, AstNode, VisibilityOwner}, 14 ast::{self, AstNode},
15 match_ast, 15 match_ast,
16}; 16};
17use test_utils::tested_by; 17use 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/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs
index c96351982..bf0d8db60 100644
--- a/crates/ra_ide_db/src/imports_locator.rs
+++ b/crates/ra_ide_db/src/imports_locator.rs
@@ -1,7 +1,7 @@
1//! This module contains an import search funcionality that is provided to the ra_assists module. 1//! This module contains an import search funcionality that is provided to the ra_assists module.
2//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. 2//! Later, this should be moved away to a separate crate that is accessible from the ra_assists module.
3 3
4use hir::{ModuleDef, Semantics}; 4use hir::{MacroDef, ModuleDef, Semantics};
5use ra_prof::profile; 5use ra_prof::profile;
6use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; 6use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
7 7
@@ -10,6 +10,7 @@ use crate::{
10 symbol_index::{self, FileSymbol, Query}, 10 symbol_index::{self, FileSymbol, Query},
11 RootDatabase, 11 RootDatabase,
12}; 12};
13use either::Either;
13 14
14pub struct ImportsLocator<'a> { 15pub struct ImportsLocator<'a> {
15 sema: Semantics<'a, RootDatabase>, 16 sema: Semantics<'a, RootDatabase>,
@@ -20,7 +21,7 @@ impl<'a> ImportsLocator<'a> {
20 Self { sema: Semantics::new(db) } 21 Self { sema: Semantics::new(db) }
21 } 22 }
22 23
23 pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { 24 pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> {
24 let _p = profile("search_for_imports"); 25 let _p = profile("search_for_imports");
25 let db = self.sema.db; 26 let db = self.sema.db;
26 27
@@ -43,7 +44,8 @@ impl<'a> ImportsLocator<'a> {
43 .chain(lib_results.into_iter()) 44 .chain(lib_results.into_iter())
44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) 45 .filter_map(|import_candidate| self.get_name_definition(&import_candidate))
45 .filter_map(|name_definition_to_import| match name_definition_to_import { 46 .filter_map(|name_definition_to_import| match name_definition_to_import {
46 Definition::ModuleDef(module_def) => Some(module_def), 47 Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
48 Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
47 _ => None, 49 _ => None,
48 }) 50 })
49 .collect() 51 .collect()
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
7use std::mem; 7use std::mem;
8 8
9use hir::{DefWithBody, HasSource, ModuleSource, Semantics}; 9use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility};
10use once_cell::unsync::Lazy; 10use once_cell::unsync::Lazy;
11use ra_db::{FileId, FileRange, SourceDatabaseExt}; 11use ra_db::{FileId, FileRange, SourceDatabaseExt};
12use ra_prof::profile; 12use 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();