From 11577288c23b1f2de1aaba0137c9c2d6344b9a92 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sat, 14 Sep 2019 14:38:10 +0300 Subject: initial classify_name --- crates/ra_hir/src/from_source.rs | 60 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index f80d8eb5f..e09414ca3 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -11,8 +11,9 @@ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, ids::{AstItemDef, LocationCtx}, name::AsName, - Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, Module, - ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, + AssocItem, Const, Crate, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, + Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, + VariantDef, }; pub trait FromSource: Sized { @@ -129,6 +130,61 @@ impl FromSource for StructField { } } +impl FromSource for AssocItem { + type Ast = ast::ImplItem; + 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(AssocItem::from(it))) + }; + } + + match src.ast { + ast::ImplItem::FnDef(f) => def!(Function, f), + ast::ImplItem::ConstDef(c) => def!(Const, c), + ast::ImplItem::TypeAliasDef(a) => def!(TypeAlias, a), + } + } +} + +// 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