diff options
author | Aleksey Kladov <[email protected]> | 2019-11-23 08:14:10 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-23 08:14:40 +0000 |
commit | 552ba868afc8f72202ac834d07bbeb330aca007d (patch) | |
tree | cd57fc21ce442e84bdc41241a70ece9995d826af /crates/ra_hir/src/code_model | |
parent | 81bfbd26bef1a63ccbeba33430e6b07a53c8e7d9 (diff) |
Move attrs query to hir_def
Diffstat (limited to 'crates/ra_hir/src/code_model')
-rw-r--r-- | crates/ra_hir/src/code_model/attrs.rs | 91 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 19 |
2 files changed, 9 insertions, 101 deletions
diff --git a/crates/ra_hir/src/code_model/attrs.rs b/crates/ra_hir/src/code_model/attrs.rs deleted file mode 100644 index 96da8c88c..000000000 --- a/crates/ra_hir/src/code_model/attrs.rs +++ /dev/null | |||
@@ -1,91 +0,0 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use crate::{ | ||
4 | db::{AstDatabase, DefDatabase, HirDatabase}, | ||
5 | Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, | ||
6 | Struct, StructField, Trait, TypeAlias, Union, | ||
7 | }; | ||
8 | use hir_def::attr::{Attr, Attrs}; | ||
9 | use hir_expand::hygiene::Hygiene; | ||
10 | use ra_syntax::ast; | ||
11 | |||
12 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
13 | pub enum AttrDef { | ||
14 | Module(Module), | ||
15 | StructField(StructField), | ||
16 | Adt(Adt), | ||
17 | Function(Function), | ||
18 | EnumVariant(EnumVariant), | ||
19 | Static(Static), | ||
20 | Const(Const), | ||
21 | Trait(Trait), | ||
22 | TypeAlias(TypeAlias), | ||
23 | MacroDef(MacroDef), | ||
24 | } | ||
25 | |||
26 | impl_froms!( | ||
27 | AttrDef: Module, | ||
28 | StructField, | ||
29 | Adt(Struct, Enum, Union), | ||
30 | EnumVariant, | ||
31 | Static, | ||
32 | Const, | ||
33 | Function, | ||
34 | Trait, | ||
35 | TypeAlias, | ||
36 | MacroDef | ||
37 | ); | ||
38 | |||
39 | pub trait HasAttrs { | ||
40 | fn attrs(&self, db: &impl HirDatabase) -> Attrs; | ||
41 | } | ||
42 | |||
43 | pub(crate) fn attributes_query(db: &(impl DefDatabase + AstDatabase), def: AttrDef) -> Attrs { | ||
44 | match def { | ||
45 | AttrDef::Module(it) => { | ||
46 | let src = match it.declaration_source(db) { | ||
47 | Some(it) => it, | ||
48 | None => return Attrs::default(), | ||
49 | }; | ||
50 | let hygiene = Hygiene::new(db, src.file_id); | ||
51 | Attr::from_attrs_owner(&src.value, &hygiene) | ||
52 | } | ||
53 | AttrDef::StructField(it) => match it.source(db).value { | ||
54 | FieldSource::Named(named) => { | ||
55 | let src = it.source(db); | ||
56 | let hygiene = Hygiene::new(db, src.file_id); | ||
57 | Attr::from_attrs_owner(&named, &hygiene) | ||
58 | } | ||
59 | FieldSource::Pos(..) => Attrs::default(), | ||
60 | }, | ||
61 | AttrDef::Adt(it) => match it { | ||
62 | Adt::Struct(it) => attrs_from_ast(it, db), | ||
63 | Adt::Enum(it) => attrs_from_ast(it, db), | ||
64 | Adt::Union(it) => attrs_from_ast(it, db), | ||
65 | }, | ||
66 | AttrDef::EnumVariant(it) => attrs_from_ast(it, db), | ||
67 | AttrDef::Static(it) => attrs_from_ast(it, db), | ||
68 | AttrDef::Const(it) => attrs_from_ast(it, db), | ||
69 | AttrDef::Function(it) => attrs_from_ast(it, db), | ||
70 | AttrDef::Trait(it) => attrs_from_ast(it, db), | ||
71 | AttrDef::TypeAlias(it) => attrs_from_ast(it, db), | ||
72 | AttrDef::MacroDef(it) => attrs_from_ast(it, db), | ||
73 | } | ||
74 | } | ||
75 | |||
76 | fn attrs_from_ast<T, D>(node: T, db: &D) -> Attrs | ||
77 | where | ||
78 | T: HasSource, | ||
79 | T::Ast: ast::AttrsOwner, | ||
80 | D: DefDatabase + AstDatabase, | ||
81 | { | ||
82 | let src = node.source(db); | ||
83 | let hygiene = Hygiene::new(db, src.file_id); | ||
84 | Attr::from_attrs_owner(&src.value, &hygiene) | ||
85 | } | ||
86 | |||
87 | impl<T: Into<AttrDef> + Copy> HasAttrs for T { | ||
88 | fn attrs(&self, db: &impl HirDatabase) -> Attrs { | ||
89 | db.attrs((*self).into()) | ||
90 | } | ||
91 | } | ||
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index a45c062bf..09bacf579 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs | |||
@@ -1,13 +1,14 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; | 3 | use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; |
4 | use hir_expand::either::Either; | ||
4 | use ra_syntax::ast::{self, AstNode}; | 5 | use ra_syntax::ast::{self, AstNode}; |
5 | 6 | ||
6 | use crate::{ | 7 | use crate::{ |
7 | db::{AstDatabase, DefDatabase, HirDatabase}, | 8 | db::{AstDatabase, DefDatabase, HirDatabase}, |
8 | ids::AstItemDef, | 9 | ids::AstItemDef, |
9 | Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, | 10 | Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, |
10 | ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, | 11 | Static, Struct, StructField, Trait, TypeAlias, Union, |
11 | }; | 12 | }; |
12 | 13 | ||
13 | pub use hir_expand::Source; | 14 | pub use hir_expand::Source; |
@@ -23,11 +24,11 @@ impl Module { | |||
23 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 24 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
24 | pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> { | 25 | pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> { |
25 | let def_map = db.crate_def_map(self.id.krate); | 26 | let def_map = db.crate_def_map(self.id.krate); |
26 | let decl_id = def_map[self.id.module_id].declaration; | 27 | let src = def_map[self.id.module_id].definition_source(db); |
27 | let file_id = def_map[self.id.module_id].definition; | 28 | src.map(|it| match it { |
28 | let value = ModuleSource::new(db, file_id, decl_id); | 29 | Either::A(it) => ModuleSource::SourceFile(it), |
29 | let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); | 30 | Either::B(it) => ModuleSource::Module(it), |
30 | Source { file_id, value } | 31 | }) |
31 | } | 32 | } |
32 | 33 | ||
33 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 34 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
@@ -37,9 +38,7 @@ impl Module { | |||
37 | db: &(impl DefDatabase + AstDatabase), | 38 | db: &(impl DefDatabase + AstDatabase), |
38 | ) -> Option<Source<ast::Module>> { | 39 | ) -> Option<Source<ast::Module>> { |
39 | let def_map = db.crate_def_map(self.id.krate); | 40 | let def_map = db.crate_def_map(self.id.krate); |
40 | let decl = def_map[self.id.module_id].declaration?; | 41 | def_map[self.id.module_id].declaration_source(db) |
41 | let value = decl.to_node(db); | ||
42 | Some(Source { file_id: decl.file_id(), value }) | ||
43 | } | 42 | } |
44 | } | 43 | } |
45 | 44 | ||