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.rs64
1 files changed, 36 insertions, 28 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 01f51ba5d..152bc71bd 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -12,7 +12,7 @@ use hir_expand::name::AsName;
12use ra_db::FileId; 12use ra_db::FileId;
13use ra_syntax::{ 13use ra_syntax::{
14 ast::{self, AstNode}, 14 ast::{self, AstNode},
15 AstPtr, 15 match_ast, AstPtr,
16 SyntaxKind::*, 16 SyntaxKind::*,
17 SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, 17 SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
18}; 18};
@@ -37,24 +37,34 @@ fn try_get_resolver_for_node(
37 file_id: FileId, 37 file_id: FileId,
38 node: &SyntaxNode, 38 node: &SyntaxNode,
39) -> Option<Resolver> { 39) -> Option<Resolver> {
40 if let Some(module) = ast::Module::cast(node.clone()) { 40 match_ast! {
41 let src = crate::Source { file_id: file_id.into(), ast: module }; 41 match node {
42 Some(crate::Module::from_declaration(db, src)?.resolver(db)) 42 ast::Module(it) => {
43 } else if let Some(file) = ast::SourceFile::cast(node.clone()) { 43 let src = crate::Source { file_id: file_id.into(), ast: it };
44 let src = 44 Some(crate::Module::from_declaration(db, src)?.resolver(db))
45 crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(file) }; 45 },
46 Some(crate::Module::from_definition(db, src)?.resolver(db)) 46 ast::SourceFile(it) => {
47 } else if let Some(s) = ast::StructDef::cast(node.clone()) { 47 let src =
48 let src = crate::Source { file_id: file_id.into(), ast: s }; 48 crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(it) };
49 Some(Struct::from_source(db, src)?.resolver(db)) 49 Some(crate::Module::from_definition(db, src)?.resolver(db))
50 } else if let Some(e) = ast::EnumDef::cast(node.clone()) { 50 },
51 let src = crate::Source { file_id: file_id.into(), ast: e }; 51 ast::StructDef(it) => {
52 Some(Enum::from_source(db, src)?.resolver(db)) 52 let src = crate::Source { file_id: file_id.into(), ast: it };
53 } else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { 53 Some(Struct::from_source(db, src)?.resolver(db))
54 Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db)) 54 },
55 } else { 55 ast::EnumDef(it) => {
56 // FIXME add missing cases 56 let src = crate::Source { file_id: file_id.into(), ast: it };
57 None 57 Some(Enum::from_source(db, src)?.resolver(db))
58 },
59 _ => {
60 if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF {
61 Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db))
62 } else {
63 // FIXME add missing cases
64 None
65 }
66 },
67 }
58 } 68 }
59} 69}
60 70
@@ -68,16 +78,14 @@ fn def_with_body_from_child_node(
68 let ctx = LocationCtx::new(db, module.id, file_id.into()); 78 let ctx = LocationCtx::new(db, module.id, file_id.into());
69 79
70 node.ancestors().find_map(|node| { 80 node.ancestors().find_map(|node| {
71 if let Some(def) = ast::FnDef::cast(node.clone()) { 81 match_ast! {
72 return Some(Function { id: ctx.to_def(&def) }.into()); 82 match node {
73 } 83 ast::FnDef(def) => { Some(Function {id: ctx.to_def(&def) }.into()) },
74 if let Some(def) = ast::ConstDef::cast(node.clone()) { 84 ast::ConstDef(def) => { Some(Const { id: ctx.to_def(&def) }.into()) },
75 return Some(Const { id: ctx.to_def(&def) }.into()); 85 ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) },
76 } 86 _ => { None },
77 if let Some(def) = ast::StaticDef::cast(node) { 87 }
78 return Some(Static { id: ctx.to_def(&def) }.into());
79 } 88 }
80 None
81 }) 89 })
82} 90}
83 91