aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-10-31 08:43:20 +0000
committerSeivan Heidari <[email protected]>2019-10-31 08:43:20 +0000
commit8edda0e7b164009d6c03bb3d4be603fb38ad2e2a (patch)
tree744cf81075d394e2f9c06afb07642a2601800dda /crates/ra_hir/src/source_binder.rs
parent49562d36b97ddde34cf7585a8c2e8f232519b657 (diff)
parentd067afb064a7fa67b172abf561b7d80740cd6f18 (diff)
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs73
1 files changed, 41 insertions, 32 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index a907d6a9f..152bc71bd 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -7,10 +7,12 @@
7//! purely for "IDE needs". 7//! purely for "IDE needs".
8use std::sync::Arc; 8use std::sync::Arc;
9 9
10use hir_def::path::known;
11use hir_expand::name::AsName;
10use ra_db::FileId; 12use ra_db::FileId;
11use ra_syntax::{ 13use ra_syntax::{
12 ast::{self, AstNode}, 14 ast::{self, AstNode},
13 AstPtr, 15 match_ast, AstPtr,
14 SyntaxKind::*, 16 SyntaxKind::*,
15 SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, 17 SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
16}; 18};
@@ -24,11 +26,10 @@ use crate::{
24 BodySourceMap, 26 BodySourceMap,
25 }, 27 },
26 ids::LocationCtx, 28 ids::LocationCtx,
27 path::known,
28 resolve::{ScopeDef, TypeNs, ValueNs}, 29 resolve::{ScopeDef, TypeNs, ValueNs},
29 ty::method_resolution::implements_trait, 30 ty::method_resolution::implements_trait,
30 AsName, Const, DefWithBody, Either, Enum, FromSource, Function, HasBody, HirFileId, MacroDef, 31 Const, DefWithBody, Either, Enum, FromSource, Function, HasBody, HirFileId, MacroDef, Module,
31 Module, Name, Path, Resolver, Static, Struct, Ty, 32 Name, Path, Resolver, Static, Struct, Ty,
32}; 33};
33 34
34fn try_get_resolver_for_node( 35fn try_get_resolver_for_node(
@@ -36,24 +37,34 @@ fn try_get_resolver_for_node(
36 file_id: FileId, 37 file_id: FileId,
37 node: &SyntaxNode, 38 node: &SyntaxNode,
38) -> Option<Resolver> { 39) -> Option<Resolver> {
39 if let Some(module) = ast::Module::cast(node.clone()) { 40 match_ast! {
40 let src = crate::Source { file_id: file_id.into(), ast: module }; 41 match node {
41 Some(crate::Module::from_declaration(db, src)?.resolver(db)) 42 ast::Module(it) => {
42 } else if let Some(file) = ast::SourceFile::cast(node.clone()) { 43 let src = crate::Source { file_id: file_id.into(), ast: it };
43 let src = 44 Some(crate::Module::from_declaration(db, src)?.resolver(db))
44 crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(file) }; 45 },
45 Some(crate::Module::from_definition(db, src)?.resolver(db)) 46 ast::SourceFile(it) => {
46 } else if let Some(s) = ast::StructDef::cast(node.clone()) { 47 let src =
47 let src = crate::Source { file_id: file_id.into(), ast: s }; 48 crate::Source { file_id: file_id.into(), ast: crate::ModuleSource::SourceFile(it) };
48 Some(Struct::from_source(db, src)?.resolver(db)) 49 Some(crate::Module::from_definition(db, src)?.resolver(db))
49 } else if let Some(e) = ast::EnumDef::cast(node.clone()) { 50 },
50 let src = crate::Source { file_id: file_id.into(), ast: e }; 51 ast::StructDef(it) => {
51 Some(Enum::from_source(db, src)?.resolver(db)) 52 let src = crate::Source { file_id: file_id.into(), ast: it };
52 } else if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { 53 Some(Struct::from_source(db, src)?.resolver(db))
53 Some(def_with_body_from_child_node(db, file_id, node)?.resolver(db)) 54 },
54 } else { 55 ast::EnumDef(it) => {
55 // FIXME add missing cases 56 let src = crate::Source { file_id: file_id.into(), ast: it };
56 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 }
57 } 68 }
58} 69}
59 70
@@ -64,19 +75,17 @@ fn def_with_body_from_child_node(
64) -> Option<DefWithBody> { 75) -> Option<DefWithBody> {
65 let src = crate::ModuleSource::from_child_node(db, file_id, node); 76 let src = crate::ModuleSource::from_child_node(db, file_id, node);
66 let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?; 77 let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?;
67 let ctx = LocationCtx::new(db, module, file_id.into()); 78 let ctx = LocationCtx::new(db, module.id, file_id.into());
68 79
69 node.ancestors().find_map(|node| { 80 node.ancestors().find_map(|node| {
70 if let Some(def) = ast::FnDef::cast(node.clone()) { 81 match_ast! {
71 return Some(Function { id: ctx.to_def(&def) }.into()); 82 match node {
72 } 83 ast::FnDef(def) => { Some(Function {id: ctx.to_def(&def) }.into()) },
73 if let Some(def) = ast::ConstDef::cast(node.clone()) { 84 ast::ConstDef(def) => { Some(Const { id: ctx.to_def(&def) }.into()) },
74 return Some(Const { id: ctx.to_def(&def) }.into()); 85 ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) },
75 } 86 _ => { None },
76 if let Some(def) = ast::StaticDef::cast(node) { 87 }
77 return Some(Static { id: ctx.to_def(&def) }.into());
78 } 88 }
79 None
80 }) 89 })
81} 90}
82 91