aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs51
1 files changed, 25 insertions, 26 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index bde0be37b..dbe040805 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -13,8 +13,9 @@ use ra_syntax::{
13}; 13};
14 14
15use crate::{ 15use crate::{
16 HirDatabase, Function, SourceItemId, 16 HirDatabase, Function, SourceItemId, ModuleDef,
17 DefKind, DefLoc, AsName, Module, 17 AsName, Module,
18 ids::LocationCtx,
18}; 19};
19 20
20/// Locates the module by `FileId`. Picks topmost module in the file. 21/// Locates the module by `FileId`. Picks topmost module in the file.
@@ -84,9 +85,13 @@ pub fn module_from_child_node(
84 85
85fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Option<Module> { 86fn module_from_source(db: &impl HirDatabase, source: SourceItemId) -> Option<Module> {
86 let source_root_id = db.file_source_root(source.file_id.as_original_file()); 87 let source_root_id = db.file_source_root(source.file_id.as_original_file());
87 let module_tree = db.module_tree(source_root_id); 88 db.source_root_crates(source_root_id)
88 let module_id = module_tree.find_module_by_source(source)?; 89 .iter()
89 Some(Module::from_module_id(db, source_root_id, module_id)) 90 .find_map(|&krate| {
91 let module_tree = db.module_tree(krate);
92 let module_id = module_tree.find_module_by_source(source)?;
93 Some(Module { krate, module_id })
94 })
90} 95}
91 96
92pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { 97pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> {
@@ -101,30 +106,21 @@ pub fn function_from_source(
101 fn_def: &ast::FnDef, 106 fn_def: &ast::FnDef,
102) -> Option<Function> { 107) -> Option<Function> {
103 let module = module_from_child_node(db, file_id, fn_def.syntax())?; 108 let module = module_from_child_node(db, file_id, fn_def.syntax())?;
104 let res = function_from_module(db, &module, fn_def); 109 let res = function_from_module(db, module, fn_def);
105 Some(res) 110 Some(res)
106} 111}
107 112
108pub fn function_from_module( 113pub fn function_from_module(
109 db: &impl HirDatabase, 114 db: &impl HirDatabase,
110 module: &Module, 115 module: Module,
111 fn_def: &ast::FnDef, 116 fn_def: &ast::FnDef,
112) -> Function { 117) -> Function {
113 let loc = module.def_id.loc(db); 118 let (file_id, _) = module.definition_source(db);
114 let file_id = loc.source_item_id.file_id; 119 let file_id = file_id.into();
115 let file_items = db.file_items(file_id); 120 let ctx = LocationCtx::new(db, module, file_id);
116 let item_id = file_items.id_of(file_id, fn_def.syntax()); 121 Function {
117 let source_item_id = SourceItemId { 122 id: ctx.to_def(fn_def),
118 file_id, 123 }
119 item_id: Some(item_id),
120 };
121 let def_loc = DefLoc {
122 kind: DefKind::Function,
123 source_root_id: loc.source_root_id,
124 module_id: loc.module_id,
125 source_item_id,
126 };
127 Function::new(def_loc.id(db))
128} 124}
129 125
130pub fn function_from_child_node( 126pub fn function_from_child_node(
@@ -141,15 +137,18 @@ pub fn macro_symbols(db: &impl HirDatabase, file_id: FileId) -> Vec<(SmolStr, Te
141 Some(it) => it, 137 Some(it) => it,
142 None => return Vec::new(), 138 None => return Vec::new(),
143 }; 139 };
144 let loc = module.def_id.loc(db); 140 let items = db.lower_module_module(module);
145 let items = db.lower_module_module(loc.source_root_id, loc.module_id);
146 let mut res = Vec::new(); 141 let mut res = Vec::new();
147 142
148 for macro_call_id in items 143 for macro_call_id in items
149 .declarations 144 .declarations
150 .iter() 145 .iter()
151 .filter_map(|(_, it)| it.take_types()) 146 .filter_map(|(_, it)| it.clone().take_types())
152 .filter_map(|it| it.loc(db).source_item_id.file_id.as_macro_call_id()) 147 .filter_map(|it| match it {
148 ModuleDef::Trait(it) => Some(it),
149 _ => None,
150 })
151 .filter_map(|it| it.source(db).0.as_macro_call_id())
153 { 152 {
154 if let Some(exp) = db.expand_macro_invocation(macro_call_id) { 153 if let Some(exp) = db.expand_macro_invocation(macro_call_id) {
155 let loc = macro_call_id.loc(db); 154 let loc = macro_call_id.loc(db);