aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/lib.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-23 14:06:04 +0000
committerAleksey Kladov <[email protected]>2019-11-23 14:12:49 +0000
commitffc2325d194d2523456484a7dec1f175c729c1b5 (patch)
tree48d78ab757f8df969e334ad9b7f1336f7ecc5f89 /crates/ra_hir_def/src/lib.rs
parent6bdd5fa461ba0f3f3697339ffb560c577e3b0cc6 (diff)
Move ModuleSource back to hir
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r--crates/ra_hir_def/src/lib.rs62
1 files changed, 2 insertions, 60 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 11ae9cdc0..1d195d65d 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -35,69 +35,11 @@ use std::hash::{Hash, Hasher};
35 35
36use 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};
37use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; 37use ra_arena::{impl_arena_id, map::ArenaMap, RawId};
38use ra_db::{salsa, CrateId, FileId}; 38use ra_db::{salsa, CrateId};
39use ra_syntax::{ast, AstNode, SyntaxNode}; 39use ra_syntax::{ast, AstNode};
40 40
41use crate::{builtin_type::BuiltinType, db::InternDatabase}; 41use crate::{builtin_type::BuiltinType, db::InternDatabase};
42 42
43pub enum ModuleSource {
44 SourceFile(ast::SourceFile),
45 Module(ast::Module),
46}
47
48impl ModuleSource {
49 pub fn new(
50 db: &impl db::DefDatabase,
51 file_id: Option<FileId>,
52 decl_id: Option<AstId<ast::Module>>,
53 ) -> ModuleSource {
54 match (file_id, decl_id) {
55 (Some(file_id), _) => {
56 let source_file = db.parse(file_id).tree();
57 ModuleSource::SourceFile(source_file)
58 }
59 (None, Some(item_id)) => {
60 let module = item_id.to_node(db);
61 assert!(module.item_list().is_some(), "expected inline module");
62 ModuleSource::Module(module)
63 }
64 (None, None) => panic!(),
65 }
66 }
67
68 // FIXME: this methods do not belong here
69 pub fn from_position(db: &impl db::DefDatabase, position: ra_db::FilePosition) -> ModuleSource {
70 let parse = db.parse(position.file_id);
71 match &ra_syntax::algo::find_node_at_offset::<ast::Module>(
72 parse.tree().syntax(),
73 position.offset,
74 ) {
75 Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()),
76 _ => {
77 let source_file = parse.tree();
78 ModuleSource::SourceFile(source_file)
79 }
80 }
81 }
82
83 pub fn from_child_node(db: &impl db::DefDatabase, child: Source<&SyntaxNode>) -> ModuleSource {
84 if let Some(m) =
85 child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi())
86 {
87 ModuleSource::Module(m)
88 } else {
89 let file_id = child.file_id.original_file(db);
90 let source_file = db.parse(file_id).tree();
91 ModuleSource::SourceFile(source_file)
92 }
93 }
94
95 pub fn from_file_id(db: &impl db::DefDatabase, file_id: FileId) -> ModuleSource {
96 let source_file = db.parse(file_id).tree();
97 ModuleSource::SourceFile(source_file)
98 }
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
102pub struct LocalImportId(RawId); 44pub struct LocalImportId(RawId);
103impl_arena_id!(LocalImportId); 45impl_arena_id!(LocalImportId);