diff options
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 75 |
1 files changed, 10 insertions, 65 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 1b2bc6f45..1d195d65d 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -21,95 +21,40 @@ pub mod resolver; | |||
21 | pub mod data; | 21 | pub mod data; |
22 | pub mod lang_item; | 22 | pub mod lang_item; |
23 | pub mod docs; | 23 | pub mod docs; |
24 | pub mod per_ns; | ||
24 | 25 | ||
25 | mod trace; | 26 | mod trace; |
27 | mod nameres; | ||
26 | 28 | ||
27 | #[cfg(test)] | 29 | #[cfg(test)] |
28 | mod test_db; | 30 | mod test_db; |
29 | #[cfg(test)] | 31 | #[cfg(test)] |
30 | mod marks; | 32 | mod marks; |
31 | 33 | ||
32 | // FIXME: this should be private | ||
33 | pub mod nameres; | ||
34 | |||
35 | use std::hash::{Hash, Hasher}; | 34 | use std::hash::{Hash, Hasher}; |
36 | 35 | ||
37 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source}; | 36 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source}; |
38 | use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; | 37 | use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; |
39 | use ra_db::{salsa, CrateId, FileId}; | 38 | use ra_db::{salsa, CrateId}; |
40 | use ra_syntax::{ast, AstNode, SyntaxNode}; | 39 | use ra_syntax::{ast, AstNode}; |
41 | 40 | ||
42 | use crate::{builtin_type::BuiltinType, db::InternDatabase}; | 41 | use crate::{builtin_type::BuiltinType, db::InternDatabase}; |
43 | 42 | ||
44 | pub enum ModuleSource { | 43 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
45 | SourceFile(ast::SourceFile), | 44 | pub struct LocalImportId(RawId); |
46 | Module(ast::Module), | 45 | impl_arena_id!(LocalImportId); |
47 | } | ||
48 | |||
49 | impl ModuleSource { | ||
50 | pub fn new( | ||
51 | db: &impl db::DefDatabase, | ||
52 | file_id: Option<FileId>, | ||
53 | decl_id: Option<AstId<ast::Module>>, | ||
54 | ) -> ModuleSource { | ||
55 | match (file_id, decl_id) { | ||
56 | (Some(file_id), _) => { | ||
57 | let source_file = db.parse(file_id).tree(); | ||
58 | ModuleSource::SourceFile(source_file) | ||
59 | } | ||
60 | (None, Some(item_id)) => { | ||
61 | let module = item_id.to_node(db); | ||
62 | assert!(module.item_list().is_some(), "expected inline module"); | ||
63 | ModuleSource::Module(module) | ||
64 | } | ||
65 | (None, None) => panic!(), | ||
66 | } | ||
67 | } | ||
68 | |||
69 | // FIXME: this methods do not belong here | ||
70 | pub fn from_position(db: &impl db::DefDatabase, position: ra_db::FilePosition) -> ModuleSource { | ||
71 | let parse = db.parse(position.file_id); | ||
72 | match &ra_syntax::algo::find_node_at_offset::<ast::Module>( | ||
73 | parse.tree().syntax(), | ||
74 | position.offset, | ||
75 | ) { | ||
76 | Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), | ||
77 | _ => { | ||
78 | let source_file = parse.tree(); | ||
79 | ModuleSource::SourceFile(source_file) | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | pub fn from_child_node(db: &impl db::DefDatabase, child: Source<&SyntaxNode>) -> ModuleSource { | ||
85 | if let Some(m) = | ||
86 | child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) | ||
87 | { | ||
88 | ModuleSource::Module(m) | ||
89 | } else { | ||
90 | let file_id = child.file_id.original_file(db); | ||
91 | let source_file = db.parse(file_id).tree(); | ||
92 | ModuleSource::SourceFile(source_file) | ||
93 | } | ||
94 | } | ||
95 | |||
96 | pub fn from_file_id(db: &impl db::DefDatabase, file_id: FileId) -> ModuleSource { | ||
97 | let source_file = db.parse(file_id).tree(); | ||
98 | ModuleSource::SourceFile(source_file) | ||
99 | } | ||
100 | } | ||
101 | 46 | ||
102 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 47 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
103 | pub struct ModuleId { | 48 | pub struct ModuleId { |
104 | pub krate: CrateId, | 49 | pub krate: CrateId, |
105 | pub module_id: CrateModuleId, | 50 | pub module_id: LocalModuleId, |
106 | } | 51 | } |
107 | 52 | ||
108 | /// An ID of a module, **local** to a specific crate | 53 | /// An ID of a module, **local** to a specific crate |
109 | // FIXME: rename to `LocalModuleId`. | 54 | // FIXME: rename to `LocalModuleId`. |
110 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 55 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
111 | pub struct CrateModuleId(RawId); | 56 | pub struct LocalModuleId(RawId); |
112 | impl_arena_id!(CrateModuleId); | 57 | impl_arena_id!(LocalModuleId); |
113 | 58 | ||
114 | macro_rules! impl_intern_key { | 59 | macro_rules! impl_intern_key { |
115 | ($name:ident) => { | 60 | ($name:ident) => { |