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.rs23
1 files changed, 20 insertions, 3 deletions
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
2use hir_def::{ 2use 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};
6use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 6use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
7use ra_syntax::{ 7use ra_syntax::{
@@ -11,8 +11,8 @@ use ra_syntax::{
11 11
12use crate::{ 12use 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
18pub trait FromSource: Sized { 18pub trait FromSource: Sized {
@@ -177,6 +177,23 @@ impl Local {
177 } 177 }
178} 178}
179 179
180impl 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
180impl Module { 197impl 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);