diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/adt.rs | 32 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/query_definitions.rs | 20 |
3 files changed, 31 insertions, 25 deletions
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 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::ast::{self, NameOwner, StructFlavor}; | 3 | use ra_db::Cancelable; |
4 | use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode}; | ||
4 | 5 | ||
5 | use crate::{ | 6 | use crate::{ |
6 | DefId, Name, AsName, Struct, Enum, VariantData, StructField, | 7 | DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind, |
7 | type_ref::TypeRef, | 8 | type_ref::TypeRef, |
8 | }; | 9 | }; |
9 | 10 | ||
@@ -20,12 +21,24 @@ pub struct StructData { | |||
20 | } | 21 | } |
21 | 22 | ||
22 | impl StructData { | 23 | impl StructData { |
23 | pub(crate) fn new(struct_def: &ast::StructDef) -> StructData { | 24 | fn new(struct_def: &ast::StructDef) -> StructData { |
24 | let name = struct_def.name().map(|n| n.as_name()); | 25 | let name = struct_def.name().map(|n| n.as_name()); |
25 | let variant_data = VariantData::new(struct_def.flavor()); | 26 | let variant_data = VariantData::new(struct_def.flavor()); |
26 | let variant_data = Arc::new(variant_data); | 27 | let variant_data = Arc::new(variant_data); |
27 | StructData { name, variant_data } | 28 | StructData { name, variant_data } |
28 | } | 29 | } |
30 | |||
31 | pub(crate) fn struct_data_query( | ||
32 | db: &impl HirDatabase, | ||
33 | def_id: DefId, | ||
34 | ) -> Cancelable<Arc<StructData>> { | ||
35 | let def_loc = def_id.loc(db); | ||
36 | assert!(def_loc.kind == DefKind::Struct); | ||
37 | let syntax = db.file_item(def_loc.source_item_id); | ||
38 | let struct_def = | ||
39 | ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); | ||
40 | Ok(Arc::new(StructData::new(struct_def))) | ||
41 | } | ||
29 | } | 42 | } |
30 | 43 | ||
31 | impl Enum { | 44 | impl Enum { |
@@ -41,7 +54,7 @@ pub struct EnumData { | |||
41 | } | 54 | } |
42 | 55 | ||
43 | impl EnumData { | 56 | impl EnumData { |
44 | pub(crate) fn new(enum_def: &ast::EnumDef) -> Self { | 57 | fn new(enum_def: &ast::EnumDef) -> Self { |
45 | let name = enum_def.name().map(|n| n.as_name()); | 58 | let name = enum_def.name().map(|n| n.as_name()); |
46 | let variants = if let Some(evl) = enum_def.variant_list() { | 59 | let variants = if let Some(evl) = enum_def.variant_list() { |
47 | evl.variants() | 60 | evl.variants() |
@@ -57,6 +70,17 @@ impl EnumData { | |||
57 | }; | 70 | }; |
58 | EnumData { name, variants } | 71 | EnumData { name, variants } |
59 | } | 72 | } |
73 | |||
74 | pub(crate) fn enum_data_query( | ||
75 | db: &impl HirDatabase, | ||
76 | def_id: DefId, | ||
77 | ) -> Cancelable<Arc<EnumData>> { | ||
78 | let def_loc = def_id.loc(db); | ||
79 | assert!(def_loc.kind == DefKind::Enum); | ||
80 | let syntax = db.file_item(def_loc.source_item_id); | ||
81 | let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); | ||
82 | Ok(Arc::new(EnumData::new(enum_def))) | ||
83 | } | ||
60 | } | 84 | } |
61 | 85 | ||
62 | impl VariantData { | 86 | 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 | |||
38 | 38 | ||
39 | fn struct_data(def_id: DefId) -> Cancelable<Arc<StructData>> { | 39 | fn struct_data(def_id: DefId) -> Cancelable<Arc<StructData>> { |
40 | type StructDataQuery; | 40 | type StructDataQuery; |
41 | use fn query_definitions::struct_data; | 41 | use fn crate::adt::StructData::struct_data_query; |
42 | } | 42 | } |
43 | 43 | ||
44 | fn enum_data(def_id: DefId) -> Cancelable<Arc<EnumData>> { | 44 | fn enum_data(def_id: DefId) -> Cancelable<Arc<EnumData>> { |
45 | type EnumDataQuery; | 45 | type EnumDataQuery; |
46 | use fn query_definitions::enum_data; | 46 | use fn crate::adt::EnumData::enum_data_query; |
47 | } | 47 | } |
48 | 48 | ||
49 | fn infer(def_id: DefId) -> Cancelable<Arc<InferenceResult>> { | 49 | fn infer(def_id: DefId) -> Cancelable<Arc<InferenceResult>> { |
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::{ | |||
11 | use ra_db::{SourceRootId, Cancelable,}; | 11 | use ra_db::{SourceRootId, Cancelable,}; |
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | SourceFileItems, SourceItemId, DefKind, DefId, HirFileId, ModuleSource, | 14 | SourceFileItems, SourceItemId, DefId, HirFileId, ModuleSource, |
15 | MacroCallLoc, | 15 | MacroCallLoc, |
16 | db::HirDatabase, | 16 | db::HirDatabase, |
17 | function::FnScopes, | 17 | function::FnScopes, |
18 | module_tree::ModuleId, | 18 | module_tree::ModuleId, |
19 | nameres::{InputModuleItems, ItemMap, Resolver}, | 19 | nameres::{InputModuleItems, ItemMap, Resolver}, |
20 | adt::{StructData, EnumData}, | ||
21 | }; | 20 | }; |
22 | 21 | ||
23 | pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<FnScopes>> { | 22 | pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<FnScopes>> { |
@@ -26,23 +25,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc< | |||
26 | Ok(Arc::new(res)) | 25 | Ok(Arc::new(res)) |
27 | } | 26 | } |
28 | 27 | ||
29 | pub(super) fn struct_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<StructData>> { | ||
30 | let def_loc = def_id.loc(db); | ||
31 | assert!(def_loc.kind == DefKind::Struct); | ||
32 | let syntax = db.file_item(def_loc.source_item_id); | ||
33 | let struct_def = | ||
34 | ast::StructDef::cast(&syntax).expect("struct def should point to StructDef node"); | ||
35 | Ok(Arc::new(StructData::new(struct_def))) | ||
36 | } | ||
37 | |||
38 | pub(super) fn enum_data(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<EnumData>> { | ||
39 | let def_loc = def_id.loc(db); | ||
40 | assert!(def_loc.kind == DefKind::Enum); | ||
41 | let syntax = db.file_item(def_loc.source_item_id); | ||
42 | let enum_def = ast::EnumDef::cast(&syntax).expect("enum def should point to EnumDef node"); | ||
43 | Ok(Arc::new(EnumData::new(enum_def))) | ||
44 | } | ||
45 | |||
46 | pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> { | 28 | pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> { |
47 | let source_file = db.hir_source_file(file_id); | 29 | let source_file = db.hir_source_file(file_id); |
48 | let res = SourceFileItems::new(file_id, &source_file); | 30 | let res = SourceFileItems::new(file_id, &source_file); |