diff options
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 1fdd7d087..998158b3e 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -9,13 +9,14 @@ use ra_db::{FileId, FilePosition}; | |||
9 | use ra_syntax::{ | 9 | use ra_syntax::{ |
10 | SmolStr, TextRange, SyntaxNode, | 10 | SmolStr, TextRange, SyntaxNode, |
11 | ast::{self, AstNode, NameOwner}, | 11 | ast::{self, AstNode, NameOwner}, |
12 | algo::find_node_at_offset, | 12 | algo::{find_node_at_offset, find_leaf_at_offset}, |
13 | }; | 13 | }; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | HirDatabase, Function, ModuleDef, Struct, Enum, | 16 | HirDatabase, Function, ModuleDef, Struct, Enum, |
17 | AsName, Module, HirFileId, Crate, Trait, Resolver, | 17 | AsName, Module, HirFileId, Crate, Trait, Resolver, |
18 | ids::{LocationCtx, SourceFileItemId}, | 18 | ids::{LocationCtx, SourceFileItemId}, |
19 | expr | ||
19 | }; | 20 | }; |
20 | 21 | ||
21 | /// Locates the module by `FileId`. Picks topmost module in the file. | 22 | /// Locates the module by `FileId`. Picks topmost module in the file. |
@@ -202,7 +203,29 @@ pub fn macro_symbols(db: &impl HirDatabase, file_id: FileId) -> Vec<(SmolStr, Te | |||
202 | res | 203 | res |
203 | } | 204 | } |
204 | 205 | ||
205 | #[allow(unused_variables)] | 206 | pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> Resolver<'static> { |
206 | pub fn resolver_for_position(db: &impl HirDatabase, position: FilePosition) -> Resolver { | 207 | let file = db.parse(position.file_id); |
207 | unimplemented!() | 208 | find_leaf_at_offset(file.syntax(), position.offset) |
209 | .find_map(|node| { | ||
210 | node.ancestors().find_map(|node| { | ||
211 | if ast::Expr::cast(node).is_some() || ast::Block::cast(node).is_some() { | ||
212 | if let Some(func) = function_from_child_node(db, position.file_id, node) { | ||
213 | let scopes = func.scopes(db); | ||
214 | let scope = scopes.scope_for_offset(position.offset); | ||
215 | Some(expr::resolver_for_scope(func.body(db), db, scope)) | ||
216 | } else { | ||
217 | // TODO const/static/array length | ||
218 | None | ||
219 | } | ||
220 | } else if let Some(module) = ast::Module::cast(node) { | ||
221 | Some(module_from_declaration(db, position.file_id, module)?.resolver(db)) | ||
222 | } else if let Some(_) = ast::SourceFile::cast(node) { | ||
223 | Some(module_from_source(db, position.file_id.into(), None)?.resolver(db)) | ||
224 | } else { | ||
225 | // TODO add missing cases | ||
226 | None | ||
227 | } | ||
228 | }) | ||
229 | }) | ||
230 | .unwrap_or_default() | ||
208 | } | 231 | } |