aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-06 17:01:49 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-06 17:01:49 +0000
commit0d59422b18de8dce416d792b9e7dbe9b8d5aa30a (patch)
tree2032c28d8a72b162abe9f7b8dddffef7f036b91d /crates/ra_hir/src/source_binder.rs
parentcf0ce14351af03c620aca784ee2c03aad86b866e (diff)
parent8a3b489c2f57bdf8f6241e69276efa48b5ed4a98 (diff)
Merge #445
445: kill module source r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs64
1 files changed, 32 insertions, 32 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index b7e3ff9b0..4c14650c0 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -14,15 +14,15 @@ use ra_syntax::{
14 14
15use crate::{ 15use crate::{
16 HirDatabase, Function, SourceItemId, 16 HirDatabase, Function, SourceItemId,
17 module_tree::ModuleSource, 17 DefKind, DefLoc, AsName, Module,
18 DefKind, DefLoc, AsName,
19}; 18};
20 19
21use crate::code_model_api::Module;
22
23/// Locates the module by `FileId`. Picks topmost module in the file. 20/// Locates the module by `FileId`. Picks topmost module in the file.
24pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> { 21pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> {
25 let module_source = ModuleSource::new_file(file_id.into()); 22 let module_source = SourceItemId {
23 file_id: file_id.into(),
24 item_id: None,
25 };
26 module_from_source(db, module_source) 26 module_from_source(db, module_source)
27} 27}
28 28
@@ -51,11 +51,26 @@ pub fn module_from_position(
51 position: FilePosition, 51 position: FilePosition,
52) -> Cancelable<Option<Module>> { 52) -> Cancelable<Option<Module>> {
53 let file = db.source_file(position.file_id); 53 let file = db.source_file(position.file_id);
54 let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) { 54 match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) {
55 Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id.into(), m), 55 Some(m) if !m.has_semi() => module_from_inline(db, position.file_id.into(), m),
56 _ => ModuleSource::new_file(position.file_id.into()), 56 _ => module_from_file_id(db, position.file_id.into()),
57 }
58}
59
60fn module_from_inline(
61 db: &impl HirDatabase,
62 file_id: FileId,
63 module: ast::Module,
64) -> Cancelable<Option<Module>> {
65 assert!(!module.has_semi());
66 let file_id = file_id.into();
67 let file_items = db.file_items(file_id);
68 let item_id = file_items.id_of(file_id, module.syntax());
69 let source = SourceItemId {
70 file_id,
71 item_id: Some(item_id),
57 }; 72 };
58 module_from_source(db, module_source) 73 module_from_source(db, source)
59} 74}
60 75
61/// Locates the module by child syntax element within the module 76/// Locates the module by child syntax element within the module
@@ -64,37 +79,22 @@ pub fn module_from_child_node(
64 file_id: FileId, 79 file_id: FileId,
65 child: SyntaxNodeRef, 80 child: SyntaxNodeRef,
66) -> Cancelable<Option<Module>> { 81) -> Cancelable<Option<Module>> {
67 let module_source = if let Some(m) = child 82 if let Some(m) = child
68 .ancestors() 83 .ancestors()
69 .filter_map(ast::Module::cast) 84 .filter_map(ast::Module::cast)
70 .find(|it| !it.has_semi()) 85 .find(|it| !it.has_semi())
71 { 86 {
72 ModuleSource::new_inline(db, file_id.into(), m) 87 module_from_inline(db, file_id.into(), m)
73 } else { 88 } else {
74 ModuleSource::new_file(file_id.into()) 89 module_from_file_id(db, file_id.into())
75 }; 90 }
76 module_from_source(db, module_source)
77} 91}
78 92
79fn module_from_source( 93fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Cancelable<Option<Module>> {
80 db: &impl HirDatabase, 94 let source_root_id = db.file_source_root(source.file_id.as_original_file());
81 module_source: ModuleSource,
82) -> Cancelable<Option<Module>> {
83 let source_root_id = db.file_source_root(module_source.file_id().as_original_file());
84 let module_tree = db.module_tree(source_root_id)?; 95 let module_tree = db.module_tree(source_root_id)?;
85 let m = module_tree 96 let module_id = ctry!(module_tree.find_module_by_source(source));
86 .modules_with_sources() 97 Ok(Some(Module::from_module_id(db, source_root_id, module_id)?))
87 .find(|(_id, src)| src == &module_source);
88 let module_id = ctry!(m).0;
89 let def_loc = DefLoc {
90 kind: DefKind::Module,
91 source_root_id,
92 module_id,
93 source_item_id: module_source.0,
94 };
95 let def_id = def_loc.id(db);
96
97 Ok(Some(Module::new(def_id)))
98} 98}
99 99
100pub fn function_from_position( 100pub fn function_from_position(