From 121aa35f12d282066651d906ea9a8b2da8209605 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Fri, 4 Oct 2019 03:20:14 +0300 Subject: return Declaration from classify_name_ref --- crates/ra_hir/src/from_source.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index df61c227a..e09414ca3 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -12,7 +12,8 @@ use crate::{ ids::{AstItemDef, LocationCtx}, name::AsName, AssocItem, Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, - Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, + Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, + VariantDef, }; pub trait FromSource: Sized { @@ -147,6 +148,43 @@ impl FromSource for AssocItem { } } +// not fully matched +impl FromSource for ModuleDef { + type Ast = ast::ModuleItem; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + macro_rules! def { + ($kind:ident, $ast:ident) => { + $kind::from_source(db, Source { file_id: src.file_id, ast: $ast }) + .and_then(|it| Some(ModuleDef::from(it))) + }; + } + + match src.ast { + ast::ModuleItem::FnDef(f) => def!(Function, f), + ast::ModuleItem::ConstDef(c) => def!(Const, c), + ast::ModuleItem::TypeAliasDef(a) => def!(TypeAlias, a), + ast::ModuleItem::TraitDef(t) => def!(Trait, t), + ast::ModuleItem::StaticDef(s) => def!(Static, s), + ast::ModuleItem::StructDef(s) => { + let src = Source { file_id: src.file_id, ast: s }; + let s = Struct::from_source(db, src)?; + Some(ModuleDef::Adt(s.into())) + } + ast::ModuleItem::EnumDef(e) => { + let src = Source { file_id: src.file_id, ast: e }; + let e = Enum::from_source(db, src)?; + Some(ModuleDef::Adt(e.into())) + } + ast::ModuleItem::Module(ref m) if !m.has_semi() => { + let src = Source { file_id: src.file_id, ast: ModuleSource::Module(m.clone()) }; + let module = Module::from_definition(db, src)?; + Some(ModuleDef::Module(module)) + } + _ => None, + } + } +} + // FIXME: simplify it impl ModuleSource { pub fn from_position( -- cgit v1.2.3