From 5edcf313f6b265c626b75f92c11fe62385ab9556 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:38:29 +0300 Subject: move query impls to adt --- crates/ra_hir/src/adt.rs | 32 ++++++++++++++++++++++++++++---- crates/ra_hir/src/db.rs | 4 ++-- crates/ra_hir/src/query_definitions.rs | 20 +------------------- 3 files changed, 31 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs index d56570754..8639bbd5c 100644 --- a/crates/ra_hir/src/adt.rs +++ b/crates/ra_hir/src/adt.rs @@ -1,9 +1,10 @@ use std::sync::Arc; -use ra_syntax::ast::{self, NameOwner, StructFlavor}; +use ra_db::Cancelable; +use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode}; use crate::{ - DefId, Name, AsName, Struct, Enum, VariantData, StructField, + DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind, type_ref::TypeRef, }; @@ -20,12 +21,24 @@ pub struct StructData { } impl StructData { - pub(crate) fn new(struct_def: &ast::StructDef) -> StructData { + fn new(struct_def: &ast::StructDef) -> StructData { let name = struct_def.name().map(|n| n.as_name()); let variant_data = VariantData::new(struct_def.flavor()); let variant_data = Arc::new(variant_data); StructData { name, variant_data } } + + pub(crate) fn struct_data_query( + db: &impl HirDatabase, + def_id: DefId, + ) -> Cancelable> { + let def_loc = def_id.loc(db); + assert!(def_loc.kind == DefKind::Struct); + let syntax = db.file_item(def_loc.source_item_id); + let struct_def = + ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); + Ok(Arc::new(StructData::new(struct_def))) + } } impl Enum { @@ -41,7 +54,7 @@ pub struct EnumData { } impl EnumData { - pub(crate) fn new(enum_def: &ast::EnumDef) -> Self { + fn new(enum_def: &ast::EnumDef) -> Self { let name = enum_def.name().map(|n| n.as_name()); let variants = if let Some(evl) = enum_def.variant_list() { evl.variants() @@ -57,6 +70,17 @@ impl EnumData { }; EnumData { name, variants } } + + pub(crate) fn enum_data_query( + db: &impl HirDatabase, + def_id: DefId, + ) -> Cancelable> { + let def_loc = def_id.loc(db); + assert!(def_loc.kind == DefKind::Enum); + let syntax = db.file_item(def_loc.source_item_id); + let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); + Ok(Arc::new(EnumData::new(enum_def))) + } } impl VariantData { diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 03e65387d..bb4fb3d66 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -38,12 +38,12 @@ pub trait HirDatabase: SyntaxDatabase fn struct_data(def_id: DefId) -> Cancelable> { type StructDataQuery; - use fn query_definitions::struct_data; + use fn crate::adt::StructData::struct_data_query; } fn enum_data(def_id: DefId) -> Cancelable> { type EnumDataQuery; - use fn query_definitions::enum_data; + use fn crate::adt::EnumData::enum_data_query; } fn infer(def_id: DefId) -> Cancelable> { diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 380ea5410..ab4e6e629 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs @@ -11,13 +11,12 @@ use ra_syntax::{ use ra_db::{SourceRootId, Cancelable,}; use crate::{ - SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, ModuleSource, + SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource, MacroCallLoc, db::HirDatabase, function::FnScopes, module_tree::ModuleId, nameres::{InputModuleItems, ItemMap, Resolver}, - adt::{StructData, EnumData}, }; pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable> { @@ -26,23 +25,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable Cancelable> { - let def_loc = def_id.loc(db); - assert!(def_loc.kind == DefKind::Struct); - let syntax = db.file_item(def_loc.source_item_id); - let struct_def = - ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); - Ok(Arc::new(StructData::new(struct_def))) -} - -pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable> { - let def_loc = def_id.loc(db); - assert!(def_loc.kind == DefKind::Enum); - let syntax = db.file_item(def_loc.source_item_id); - let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); - Ok(Arc::new(EnumData::new(enum_def))) -} - pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc { let source_file = db.hir_source_file(file_id); let res = SourceFileItems::new(file_id, &source_file); -- cgit v1.2.3