aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assists/add_new.rs4
-rw-r--r--crates/ra_hir/src/from_source.rs21
-rw-r--r--crates/ra_hir/src/source_binder.rs45
-rw-r--r--crates/ra_ide/src/impls.rs6
4 files changed, 49 insertions, 27 deletions
diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs
index aedcd6286..8db63f762 100644
--- a/crates/ra_assists/src/assists/add_new.rs
+++ b/crates/ra_assists/src/assists/add_new.rs
@@ -140,12 +140,12 @@ fn find_struct_impl(
140 140
141 let struct_ty = { 141 let struct_ty = {
142 let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() }; 142 let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() };
143 sb.to_def::<hir::Struct, _>(src)?.ty(db) 143 sb.to_def(src)?.ty(db)
144 }; 144 };
145 145
146 let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| { 146 let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| {
147 let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() }; 147 let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() };
148 let blk = sb.to_def::<hir::ImplBlock, _>(src)?; 148 let blk = sb.to_def(src)?;
149 149
150 let same_ty = blk.target_ty(db) == struct_ty; 150 let same_ty = blk.target_ty(db) == struct_ty;
151 let not_trait_impl = blk.target_trait(db).is_none(); 151 let not_trait_impl = blk.target_trait(db).is_none();
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>;
diff --git a/crates/ra_ide/src/impls.rs b/crates/ra_ide/src/impls.rs
index bd14a6d13..fb9396195 100644
--- a/crates/ra_ide/src/impls.rs
+++ b/crates/ra_ide/src/impls.rs
@@ -44,15 +44,15 @@ fn impls_for_def(
44 let ty = match node { 44 let ty = match node {
45 ast::NominalDef::StructDef(def) => { 45 ast::NominalDef::StructDef(def) => {
46 let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() }; 46 let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
47 sb.to_def::<hir::Struct, _>(src)?.ty(sb.db) 47 sb.to_def(src)?.ty(sb.db)
48 } 48 }
49 ast::NominalDef::EnumDef(def) => { 49 ast::NominalDef::EnumDef(def) => {
50 let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() }; 50 let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
51 sb.to_def::<hir::Enum, _>(src)?.ty(sb.db) 51 sb.to_def(src)?.ty(sb.db)
52 } 52 }
53 ast::NominalDef::UnionDef(def) => { 53 ast::NominalDef::UnionDef(def) => {
54 let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() }; 54 let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
55 sb.to_def::<hir::Union, _>(src)?.ty(sb.db) 55 sb.to_def(src)?.ty(sb.db)
56 } 56 }
57 }; 57 };
58 58