aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-16 15:27:21 +0000
committerAleksey Kladov <[email protected]>2020-01-16 15:27:21 +0000
commita3d6ddbe694498a1bf69c6253422efb89431164e (patch)
treeced08cf46f2f822abc630b6935ec5ecf4ee4dc0d /crates/ra_hir
parent8691ae8ac04ef9dc089a377770da86a952b0e4c1 (diff)
More natural trait setup
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/from_source.rs21
-rw-r--r--crates/ra_hir/src/source_binder.rs45
2 files changed, 44 insertions, 22 deletions
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 30e818892..caaff012a 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -14,8 +14,7 @@ use ra_syntax::{
14 14
15use crate::{ 15use crate::{
16 db::{DefDatabase, HirDatabase}, 16 db::{DefDatabase, HirDatabase},
17 Const, DefWithBody, Enum, Function, ImplBlock, InFile, Local, Module, SourceBinder, Static, 17 DefWithBody, InFile, Local, Module, SourceBinder, TypeParam,
18 Struct, Trait, TypeAlias, TypeParam,
19}; 18};
20 19
21impl Local { 20impl Local {
@@ -25,9 +24,9 @@ impl Local {
25 let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { 24 let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
26 let res = match_ast! { 25 let res = match_ast! {
27 match it { 26 match it {
28 ast::ConstDef(value) => { sb.to_def::<Const, _>(InFile { value, file_id})?.into() }, 27 ast::ConstDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
29 ast::StaticDef(value) => { sb.to_def::<Static, _>(InFile { value, file_id})?.into() }, 28 ast::StaticDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
30 ast::FnDef(value) => { sb.to_def::<Function, _>(InFile { value, file_id})?.into() }, 29 ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
31 _ => return None, 30 _ => return None,
32 } 31 }
33 }; 32 };
@@ -47,12 +46,12 @@ impl TypeParam {
47 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { 46 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
48 let res = match_ast! { 47 let res = match_ast! {
49 match it { 48 match it {
50 ast::FnDef(value) => { sb.to_def::<Function, _>(InFile { value, file_id})?.id.into() }, 49 ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
51 ast::StructDef(value) => { sb.to_def::<Struct, _>(InFile { value, file_id})?.id.into() }, 50 ast::StructDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
52 ast::EnumDef(value) => { sb.to_def::<Enum, _>(InFile { value, file_id})?.id.into() }, 51 ast::EnumDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
53 ast::TraitDef(value) => { sb.to_def::<Trait, _>(InFile { value, file_id})?.id.into() }, 52 ast::TraitDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
54 ast::TypeAliasDef(value) => { sb.to_def::<TypeAlias, _>(InFile { value, file_id})?.id.into() }, 53 ast::TypeAliasDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
55 ast::ImplBlock(value) => { sb.to_def::<ImplBlock, _>(InFile { value, file_id})?.id.into() }, 54 ast::ImplBlock(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
56 _ => return None, 55 _ => return None,
57 } 56 }
58 }; 57 };
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 66930e492..c02175c06 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -52,11 +52,7 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
52 SourceAnalyzer::new_for_resolver(resolver, src) 52 SourceAnalyzer::new_for_resolver(resolver, src)
53 } 53 }
54 54
55 pub fn to_def<D, T>(&mut self, src: InFile<T>) -> Option<D> 55 pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
56 where
57 D: From<T::ID>,
58 T: ToId,
59 {
60 let id: T::ID = self.to_id(src)?; 56 let id: T::ID = self.to_id(src)?;
61 Some(id.into()) 57 Some(id.into())
62 } 58 }
@@ -114,6 +110,39 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
114 } 110 }
115} 111}
116 112
113pub trait ToId: Sized + AstNode + 'static {
114 type ID: Sized + Copy + 'static;
115 fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
116 -> Option<Self::ID>;
117}
118
119pub trait ToDef: ToId {
120 type Def: From<Self::ID>;
121}
122
123macro_rules! to_def_impls {
124 ($(($def:path, $ast:path)),* ,) => {$(
125 impl ToDef for $ast {
126 type Def = $def;
127 }
128 )*}
129}
130
131to_def_impls![
132 (crate::Struct, ast::StructDef),
133 (crate::Enum, ast::EnumDef),
134 (crate::Union, ast::UnionDef),
135 (crate::Trait, ast::TraitDef),
136 (crate::ImplBlock, ast::ImplBlock),
137 (crate::TypeAlias, ast::TypeAliasDef),
138 (crate::Const, ast::ConstDef),
139 (crate::Static, ast::StaticDef),
140 (crate::Function, ast::FnDef),
141 (crate::StructField, ast::RecordFieldDef),
142 (crate::EnumVariant, ast::EnumVariant),
143 (crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
144];
145
117#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 146#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
118enum ChildContainer { 147enum ChildContainer {
119 DefWithBodyId(DefWithBodyId), 148 DefWithBodyId(DefWithBodyId),
@@ -133,12 +162,6 @@ impl_froms! {
133 VariantId, 162 VariantId,
134} 163}
135 164
136pub trait ToId: Sized + AstNode + 'static {
137 type ID: Sized + Copy + 'static;
138 fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
139 -> Option<Self::ID>;
140}
141
142pub trait ToIdByKey: Sized + AstNode + 'static { 165pub trait ToIdByKey: Sized + AstNode + 'static {
143 type ID: Sized + Copy + 'static; 166 type ID: Sized + Copy + 'static;
144 const KEY: Key<Self, Self::ID>; 167 const KEY: Key<Self, Self::ID>;