diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 23 |
2 files changed, 24 insertions, 3 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 8833750c8..0295eb948 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -865,6 +865,10 @@ impl GenericParam { | |||
865 | let params = db.generic_params(self.id.parent); | 865 | let params = db.generic_params(self.id.parent); |
866 | params.params[self.id.local_id].name.clone() | 866 | params.params[self.id.local_id].name.clone() |
867 | } | 867 | } |
868 | |||
869 | pub fn module(self, db: &impl HirDatabase) -> Module { | ||
870 | self.id.parent.module(db).into() | ||
871 | } | ||
868 | } | 872 | } |
869 | 873 | ||
870 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 874 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 437f800c1..686ab1d79 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | use hir_def::{ | 2 | use hir_def::{ |
3 | child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, | 3 | child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, |
4 | EnumVariantId, LocationCtx, ModuleId, VariantId, | 4 | EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId, |
5 | }; | 5 | }; |
6 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; | 6 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
@@ -11,8 +11,8 @@ use ra_syntax::{ | |||
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | db::{AstDatabase, DefDatabase, HirDatabase}, | 13 | db::{AstDatabase, DefDatabase, HirDatabase}, |
14 | Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, | 14 | Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, GenericParam, ImplBlock, InFile, |
15 | MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, | 15 | Local, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, |
16 | }; | 16 | }; |
17 | 17 | ||
18 | pub trait FromSource: Sized { | 18 | pub trait FromSource: Sized { |
@@ -177,6 +177,23 @@ impl Local { | |||
177 | } | 177 | } |
178 | } | 178 | } |
179 | 179 | ||
180 | impl GenericParam { | ||
181 | pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> { | ||
182 | let file_id = src.file_id; | ||
183 | let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { | ||
184 | let res = match_ast! { | ||
185 | match it { | ||
186 | ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.id.into() }, | ||
187 | _ => return None, | ||
188 | } | ||
189 | }; | ||
190 | Some(res) | ||
191 | })?; | ||
192 | let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?; | ||
193 | Some(GenericParam { id }) | ||
194 | } | ||
195 | } | ||
196 | |||
180 | impl Module { | 197 | impl Module { |
181 | pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { | 198 | pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { |
182 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); | 199 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); |