aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r--crates/ra_hir_def/src/lib.rs75
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;
21pub mod data; 21pub mod data;
22pub mod lang_item; 22pub mod lang_item;
23pub mod docs; 23pub mod docs;
24pub mod per_ns;
24 25
25mod trace; 26mod trace;
27mod nameres;
26 28
27#[cfg(test)] 29#[cfg(test)]
28mod test_db; 30mod test_db;
29#[cfg(test)] 31#[cfg(test)]
30mod marks; 32mod marks;
31 33
32// FIXME: this should be private
33pub mod nameres;
34
35use std::hash::{Hash, Hasher}; 34use std::hash::{Hash, Hasher};
36 35
37use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source}; 36use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source};
38use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; 37use ra_arena::{impl_arena_id, map::ArenaMap, RawId};
39use ra_db::{salsa, CrateId, FileId}; 38use ra_db::{salsa, CrateId};
40use ra_syntax::{ast, AstNode, SyntaxNode}; 39use ra_syntax::{ast, AstNode};
41 40
42use crate::{builtin_type::BuiltinType, db::InternDatabase}; 41use crate::{builtin_type::BuiltinType, db::InternDatabase};
43 42
44pub enum ModuleSource { 43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45 SourceFile(ast::SourceFile), 44pub struct LocalImportId(RawId);
46 Module(ast::Module), 45impl_arena_id!(LocalImportId);
47}
48
49impl 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)]
103pub struct ModuleId { 48pub 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)]
111pub struct CrateModuleId(RawId); 56pub struct LocalModuleId(RawId);
112impl_arena_id!(CrateModuleId); 57impl_arena_id!(LocalModuleId);
113 58
114macro_rules! impl_intern_key { 59macro_rules! impl_intern_key {
115 ($name:ident) => { 60 ($name:ident) => {