aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/from_source.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/from_source.rs')
-rw-r--r--crates/ra_hir/src/from_source.rs59
1 files changed, 3 insertions, 56 deletions
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index caaff012a..c766c3f0b 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -1,66 +1,13 @@
1//! Finds a corresponding hir data structure for a syntax node in a specific 1//! Finds a corresponding hir data structure for a syntax node in a specific
2//! file. 2//! file.
3 3
4use hir_def::{ 4use hir_def::{nameres::ModuleSource, ModuleId};
5 child_by_source::ChildBySource, keys, nameres::ModuleSource, GenericDefId, ModuleId,
6};
7use hir_expand::name::AsName; 5use hir_expand::name::AsName;
8use ra_db::FileId; 6use ra_db::FileId;
9use ra_prof::profile; 7use ra_prof::profile;
10use ra_syntax::{ 8use ra_syntax::ast::{self, AstNode, NameOwner};
11 ast::{self, AstNode, NameOwner},
12 match_ast,
13};
14 9
15use crate::{ 10use crate::{db::DefDatabase, InFile, Module};
16 db::{DefDatabase, HirDatabase},
17 DefWithBody, InFile, Local, Module, SourceBinder, TypeParam,
18};
19
20impl Local {
21 pub fn from_source(db: &impl HirDatabase, src: InFile<ast::BindPat>) -> Option<Self> {
22 let mut sb = SourceBinder::new(db);
23 let file_id = src.file_id;
24 let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
25 let res = match_ast! {
26 match it {
27 ast::ConstDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
28 ast::StaticDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
29 ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
30 _ => return None,
31 }
32 };
33 Some(res)
34 })?;
35 let (_body, source_map) = db.body_with_source_map(parent.into());
36 let src = src.map(ast::Pat::from);
37 let pat_id = source_map.node_pat(src.as_ref())?;
38 Some(Local { parent, pat_id })
39 }
40}
41
42impl TypeParam {
43 pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> {
44 let mut sb = SourceBinder::new(db);
45 let file_id = src.file_id;
46 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
47 let res = match_ast! {
48 match it {
49 ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
50 ast::StructDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
51 ast::EnumDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
52 ast::TraitDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
53 ast::TypeAliasDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
54 ast::ImplBlock(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
55 _ => return None,
56 }
57 };
58 Some(res)
59 })?;
60 let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?;
61 Some(TypeParam { id })
62 }
63}
64 11
65impl Module { 12impl Module {
66 pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { 13 pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {