diff options
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/module.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir/src/either.rs | 18 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/raw.rs | 33 |
6 files changed, 40 insertions, 44 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 9e6170440..f13a6b37a 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -4,7 +4,7 @@ use ra_db::{CrateId, SourceRootId, Edition}; | |||
4 | use ra_syntax::{ast::self, TreeArc}; | 4 | use ra_syntax::{ast::self, TreeArc}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | Name, ScopesWithSourceMap, Ty, HirFileId, ImportSource, | 7 | Name, ScopesWithSourceMap, Ty, HirFileId, Either, |
8 | HirDatabase, DefDatabase, | 8 | HirDatabase, DefDatabase, |
9 | type_ref::TypeRef, | 9 | type_ref::TypeRef, |
10 | nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, | 10 | nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, |
@@ -117,8 +117,14 @@ impl Module { | |||
117 | } | 117 | } |
118 | 118 | ||
119 | /// Returns the syntax of the last path segment corresponding to this import | 119 | /// Returns the syntax of the last path segment corresponding to this import |
120 | pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> ImportSource { | 120 | pub fn import_source( |
121 | self.import_source_impl(db, import) | 121 | &self, |
122 | db: &impl HirDatabase, | ||
123 | import: ImportId, | ||
124 | ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> { | ||
125 | let (file_id, source) = self.definition_source(db); | ||
126 | let (_, source_map) = db.raw_items_with_source_map(file_id); | ||
127 | source_map.get(&source, import) | ||
122 | } | 128 | } |
123 | 129 | ||
124 | /// Returns the crate this module is part of. | 130 | /// Returns the crate this module is part of. |
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 88dee3a69..5c2ea73ce 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs | |||
@@ -3,9 +3,9 @@ use ra_syntax::{ast, TreeArc}; | |||
3 | 3 | ||
4 | use crate::{ | 4 | use crate::{ |
5 | Module, ModuleSource, Name, AstId, | 5 | Module, ModuleSource, Name, AstId, |
6 | nameres::{CrateModuleId, ImportId}, | 6 | nameres::CrateModuleId, |
7 | HirDatabase, DefDatabase, | 7 | HirDatabase, DefDatabase, |
8 | HirFileId, ImportSource, | 8 | HirFileId, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | impl ModuleSource { | 11 | impl ModuleSource { |
@@ -68,16 +68,6 @@ impl Module { | |||
68 | Some((decl.file_id(), ast)) | 68 | Some((decl.file_id(), ast)) |
69 | } | 69 | } |
70 | 70 | ||
71 | pub(crate) fn import_source_impl( | ||
72 | &self, | ||
73 | db: &impl HirDatabase, | ||
74 | import: ImportId, | ||
75 | ) -> ImportSource { | ||
76 | let (file_id, source) = self.definition_source(db); | ||
77 | let (_, source_map) = db.raw_items_with_source_map(file_id); | ||
78 | source_map.get(&source, import) | ||
79 | } | ||
80 | |||
81 | pub(crate) fn crate_root_impl(&self, db: &impl DefDatabase) -> Module { | 71 | pub(crate) fn crate_root_impl(&self, db: &impl DefDatabase) -> Module { |
82 | let def_map = db.crate_def_map(self.krate); | 72 | let def_map = db.crate_def_map(self.krate); |
83 | self.with_module_id(def_map.root()) | 73 | self.with_module_id(def_map.root()) |
diff --git a/crates/ra_hir/src/either.rs b/crates/ra_hir/src/either.rs new file mode 100644 index 000000000..6714529d9 --- /dev/null +++ b/crates/ra_hir/src/either.rs | |||
@@ -0,0 +1,18 @@ | |||
1 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
2 | pub enum Either<A, B> { | ||
3 | A(A), | ||
4 | B(B), | ||
5 | } | ||
6 | |||
7 | impl<A, B> Either<A, B> { | ||
8 | pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V> | ||
9 | where | ||
10 | F1: FnOnce(A) -> U, | ||
11 | F2: FnOnce(B) -> V, | ||
12 | { | ||
13 | match self { | ||
14 | Either::A(a) => Either::A(f1(a)), | ||
15 | Either::B(b) => Either::B(f2(b)), | ||
16 | } | ||
17 | } | ||
18 | } | ||
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4d337d2e3..0881939a2 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -17,6 +17,8 @@ macro_rules! impl_froms { | |||
17 | } | 17 | } |
18 | } | 18 | } |
19 | 19 | ||
20 | mod either; | ||
21 | |||
20 | pub mod db; | 22 | pub mod db; |
21 | #[macro_use] | 23 | #[macro_use] |
22 | pub mod mock; | 24 | pub mod mock; |
@@ -52,11 +54,12 @@ use crate::{ | |||
52 | }; | 54 | }; |
53 | 55 | ||
54 | pub use self::{ | 56 | pub use self::{ |
57 | either::Either, | ||
55 | path::{Path, PathKind}, | 58 | path::{Path, PathKind}, |
56 | name::Name, | 59 | name::Name, |
57 | source_id::{AstIdMap, ErasedFileAstId}, | 60 | source_id::{AstIdMap, ErasedFileAstId}, |
58 | ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc}, | 61 | ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc}, |
59 | nameres::{PerNs, Namespace, ImportId, ImportSource}, | 62 | nameres::{PerNs, Namespace, ImportId}, |
60 | ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay}, | 63 | ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay}, |
61 | impl_block::{ImplBlock, ImplItem}, | 64 | impl_block::{ImplBlock, ImplItem}, |
62 | docs::{Docs, Documentation}, | 65 | docs::{Docs, Documentation}, |
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 4ae04514a..0eddfab12 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs | |||
@@ -75,7 +75,7 @@ pub(crate) use self::raw::{RawItems, ImportSourceMap}; | |||
75 | 75 | ||
76 | pub use self::{ | 76 | pub use self::{ |
77 | per_ns::{PerNs, Namespace}, | 77 | per_ns::{PerNs, Namespace}, |
78 | raw::{ImportId, ImportSource}, | 78 | raw::ImportId, |
79 | }; | 79 | }; |
80 | 80 | ||
81 | /// Contans all top-level defs from a macro-expanded crate | 81 | /// Contans all top-level defs from a macro-expanded crate |
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index b7416ede6..43c97a0bf 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs | |||
@@ -12,7 +12,7 @@ use ra_syntax::{ | |||
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, | 14 | DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, |
15 | AstIdMap, FileAstId, | 15 | AstIdMap, FileAstId, Either, |
16 | }; | 16 | }; |
17 | 17 | ||
18 | /// `RawItems` is a set of top-level items in a file (except for impls). | 18 | /// `RawItems` is a set of top-level items in a file (except for impls). |
@@ -34,28 +34,15 @@ pub struct ImportSourceMap { | |||
34 | map: ArenaMap<ImportId, ImportSourcePtr>, | 34 | map: ArenaMap<ImportId, ImportSourcePtr>, |
35 | } | 35 | } |
36 | 36 | ||
37 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 37 | type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; |
38 | enum ImportSourcePtr { | 38 | type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>; |
39 | UseTree(AstPtr<ast::UseTree>), | ||
40 | ExternCrate(AstPtr<ast::ExternCrateItem>), | ||
41 | } | ||
42 | 39 | ||
43 | impl ImportSourcePtr { | 40 | impl ImportSourcePtr { |
44 | fn to_node(self, file: &SourceFile) -> ImportSource { | 41 | fn to_node(self, file: &SourceFile) -> ImportSource { |
45 | match self { | 42 | self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned()) |
46 | ImportSourcePtr::UseTree(ptr) => ImportSource::UseTree(ptr.to_node(file).to_owned()), | ||
47 | ImportSourcePtr::ExternCrate(ptr) => { | ||
48 | ImportSource::ExternCrate(ptr.to_node(file).to_owned()) | ||
49 | } | ||
50 | } | ||
51 | } | 43 | } |
52 | } | 44 | } |
53 | 45 | ||
54 | pub enum ImportSource { | ||
55 | UseTree(TreeArc<ast::UseTree>), | ||
56 | ExternCrate(TreeArc<ast::ExternCrateItem>), | ||
57 | } | ||
58 | |||
59 | impl ImportSourceMap { | 46 | impl ImportSourceMap { |
60 | fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { | 47 | fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { |
61 | self.map.insert(import, ptr) | 48 | self.map.insert(import, ptr) |
@@ -281,11 +268,7 @@ impl RawItemsCollector { | |||
281 | Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| { | 268 | Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| { |
282 | let import_data = | 269 | let import_data = |
283 | ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false }; | 270 | ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false }; |
284 | self.push_import( | 271 | self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree))); |
285 | current_module, | ||
286 | import_data, | ||
287 | ImportSourcePtr::UseTree(AstPtr::new(use_tree)), | ||
288 | ); | ||
289 | }) | 272 | }) |
290 | } | 273 | } |
291 | 274 | ||
@@ -304,11 +287,7 @@ impl RawItemsCollector { | |||
304 | is_prelude: false, | 287 | is_prelude: false, |
305 | is_extern_crate: true, | 288 | is_extern_crate: true, |
306 | }; | 289 | }; |
307 | self.push_import( | 290 | self.push_import(current_module, import_data, Either::B(AstPtr::new(extern_crate))); |
308 | current_module, | ||
309 | import_data, | ||
310 | ImportSourcePtr::ExternCrate(AstPtr::new(extern_crate)), | ||
311 | ); | ||
312 | } | 291 | } |
313 | } | 292 | } |
314 | 293 | ||