From 56710f119b7114efac237ac36ea21730b8bd5311 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 15:11:57 +0100 Subject: Move enum&union to new loc --- crates/ra_hir/src/code_model.rs | 10 +++++----- crates/ra_hir/src/from_source.rs | 29 ++++++++++------------------- crates/ra_hir/src/has_source.rs | 6 +++--- 3 files changed, 18 insertions(+), 27 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 8588aa5a3..c705d1630 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -11,7 +11,7 @@ use hir_def::{ per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, - AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, + AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, }; @@ -309,11 +309,11 @@ impl Union { } pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.module(db).krate, self.id) + Type::from_def(db, self.id.lookup(db).container.krate, self.id) } pub fn fields(self, db: &impl HirDatabase) -> Vec { @@ -337,7 +337,7 @@ pub struct Enum { impl Enum { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -357,7 +357,7 @@ impl Enum { } pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.module(db).krate, self.id) + Type::from_def(db, self.id.lookup(db).container.krate, self.id) } } diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 978326c74..f7411c5cf 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir_def::{ - child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, - EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId, + child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, EnumVariantId, + GenericDefId, ModuleId, VariantId, }; use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; use ra_syntax::{ @@ -32,15 +32,19 @@ impl FromSource for Struct { impl FromSource for Union { type Ast = ast::UnionDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Union { id }) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::UNION] + .get(&src) + .copied() + .map(Union::from) } } impl FromSource for Enum { type Ast = ast::EnumDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Enum { id }) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::ENUM] + .get(&src) + .copied() + .map(Enum::from) } } impl FromSource for Trait { @@ -250,19 +254,6 @@ impl Module { } } -fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option -where - N: AstNode, - DEF: AstItemDef, -{ - let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); - let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; - let ctx = LocationCtx::new(db, module.id, src.file_id); - let items = db.ast_id_map(src.file_id); - let item_id = items.ast_id(&src.value); - Some(DEF::from_ast_id(ctx, item_id)) -} - fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { _analyze_container(db, src).unwrap_or_default() } diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 8f35a3fbb..72afecf26 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -4,7 +4,7 @@ use either::Either; use hir_def::{ nameres::ModuleSource, src::{HasChildSource, HasSource as _}, - AstItemDef, Lookup, VariantId, + Lookup, VariantId, }; use ra_syntax::ast; @@ -57,13 +57,13 @@ impl HasSource for Struct { impl HasSource for Union { type Ast = ast::UnionDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for Enum { type Ast = ast::EnumDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for EnumVariant { -- cgit v1.2.3