diff options
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 76 |
1 files changed, 22 insertions, 54 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index c3cb17882..476cf117f 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -89,27 +89,6 @@ fn module_from_source( | |||
89 | ) | 89 | ) |
90 | } | 90 | } |
91 | 91 | ||
92 | pub fn const_from_source( | ||
93 | db: &impl HirDatabase, | ||
94 | file_id: FileId, | ||
95 | const_def: &ast::ConstDef, | ||
96 | ) -> Option<Const> { | ||
97 | let module = module_from_child_node(db, file_id, const_def.syntax())?; | ||
98 | let res = const_from_module(db, module, const_def); | ||
99 | Some(res) | ||
100 | } | ||
101 | |||
102 | pub fn const_from_module( | ||
103 | db: &impl HirDatabase, | ||
104 | module: Module, | ||
105 | const_def: &ast::ConstDef, | ||
106 | ) -> Const { | ||
107 | let (file_id, _) = module.definition_source(db); | ||
108 | let file_id = file_id.into(); | ||
109 | let ctx = LocationCtx::new(db, module, file_id); | ||
110 | Const { id: ctx.to_def(const_def) } | ||
111 | } | ||
112 | |||
113 | pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { | 92 | pub fn function_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Function> { |
114 | let file = db.parse(position.file_id); | 93 | let file = db.parse(position.file_id); |
115 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?; | 94 | let fn_def = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)?; |
@@ -157,27 +136,6 @@ pub fn struct_from_module( | |||
157 | Struct { id: ctx.to_def(struct_def) } | 136 | Struct { id: ctx.to_def(struct_def) } |
158 | } | 137 | } |
159 | 138 | ||
160 | pub fn static_from_source( | ||
161 | db: &impl HirDatabase, | ||
162 | file_id: FileId, | ||
163 | static_def: &ast::StaticDef, | ||
164 | ) -> Option<Static> { | ||
165 | let module = module_from_child_node(db, file_id, static_def.syntax())?; | ||
166 | let res = static_from_module(db, module, static_def); | ||
167 | Some(res) | ||
168 | } | ||
169 | |||
170 | pub fn static_from_module( | ||
171 | db: &impl HirDatabase, | ||
172 | module: Module, | ||
173 | static_def: &ast::StaticDef, | ||
174 | ) -> Static { | ||
175 | let (file_id, _) = module.definition_source(db); | ||
176 | let file_id = file_id.into(); | ||
177 | let ctx = LocationCtx::new(db, module, file_id); | ||
178 | Static { id: ctx.to_def(static_def) } | ||
179 | } | ||
180 | |||
181 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { | 139 | pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum { |
182 | let (file_id, _) = module.definition_source(db); | 140 | let (file_id, _) = module.definition_source(db); |
183 | let file_id = file_id.into(); | 141 | let file_id = file_id.into(); |
@@ -246,6 +204,27 @@ fn try_get_resolver_for_node( | |||
246 | } | 204 | } |
247 | } | 205 | } |
248 | 206 | ||
207 | pub fn def_with_body_from_child_node( | ||
208 | db: &impl HirDatabase, | ||
209 | file_id: FileId, | ||
210 | node: &SyntaxNode, | ||
211 | ) -> Option<DefWithBody> { | ||
212 | let module = module_from_child_node(db, file_id, node)?; | ||
213 | let ctx = LocationCtx::new(db, module, file_id.into()); | ||
214 | node.ancestors().find_map(|node| { | ||
215 | if let Some(def) = ast::FnDef::cast(node) { | ||
216 | return Some(Function { id: ctx.to_def(def) }.into()); | ||
217 | } | ||
218 | if let Some(def) = ast::ConstDef::cast(node) { | ||
219 | return Some(Const { id: ctx.to_def(def) }.into()); | ||
220 | } | ||
221 | if let Some(def) = ast::StaticDef::cast(node) { | ||
222 | return Some(Static { id: ctx.to_def(def) }.into()); | ||
223 | } | ||
224 | None | ||
225 | }) | ||
226 | } | ||
227 | |||
249 | /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of | 228 | /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of |
250 | /// original source files. It should not be used inside the HIR itself. | 229 | /// original source files. It should not be used inside the HIR itself. |
251 | #[derive(Debug)] | 230 | #[derive(Debug)] |
@@ -274,18 +253,7 @@ impl SourceAnalyzer { | |||
274 | node: &SyntaxNode, | 253 | node: &SyntaxNode, |
275 | offset: Option<TextUnit>, | 254 | offset: Option<TextUnit>, |
276 | ) -> SourceAnalyzer { | 255 | ) -> SourceAnalyzer { |
277 | let def_with_body = node.ancestors().find_map(|node| { | 256 | let def_with_body = def_with_body_from_child_node(db, file_id, node); |
278 | if let Some(src) = ast::FnDef::cast(node) { | ||
279 | return function_from_source(db, file_id, src).map(DefWithBody::from); | ||
280 | } | ||
281 | if let Some(src) = ast::StaticDef::cast(node) { | ||
282 | return static_from_source(db, file_id, src).map(DefWithBody::from); | ||
283 | } | ||
284 | if let Some(src) = ast::ConstDef::cast(node) { | ||
285 | return const_from_source(db, file_id, src).map(DefWithBody::from); | ||
286 | } | ||
287 | None | ||
288 | }); | ||
289 | SourceAnalyzer { | 257 | SourceAnalyzer { |
290 | resolver: resolver_for_node(db, file_id, node, offset), | 258 | resolver: resolver_for_node(db, file_id, node, offset), |
291 | body_source_map: def_with_body.map(|it| it.body_source_map(db)), | 259 | body_source_map: def_with_body.map(|it| it.body_source_map(db)), |