aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-22 08:27:47 +0000
committerAleksey Kladov <[email protected]>2019-11-22 08:27:47 +0000
commite42f9627664cc3c44094e1c4f985270fbfd592b1 (patch)
tree81603da9c34f301cf82e1ceae80313b8eb9e10c4 /crates/ra_hir/src
parenta1346bba5c457d1aa0a35f44231bed8b494b7d60 (diff)
Encapsulate Attrs
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model/attrs.rs16
-rw-r--r--crates/ra_hir/src/db.rs4
2 files changed, 11 insertions, 9 deletions
diff --git a/crates/ra_hir/src/code_model/attrs.rs b/crates/ra_hir/src/code_model/attrs.rs
index 9e304217c..9923cde81 100644
--- a/crates/ra_hir/src/code_model/attrs.rs
+++ b/crates/ra_hir/src/code_model/attrs.rs
@@ -8,7 +8,6 @@ use crate::{
8use hir_def::attr::Attr; 8use hir_def::attr::Attr;
9use hir_expand::hygiene::Hygiene; 9use hir_expand::hygiene::Hygiene;
10use ra_syntax::ast; 10use ra_syntax::ast;
11use std::sync::Arc;
12 11
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum AttrDef { 13pub enum AttrDef {
@@ -38,16 +37,19 @@ impl_froms!(
38); 37);
39 38
40pub trait Attrs { 39pub trait Attrs {
41 fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>>; 40 fn attrs(&self, db: &impl HirDatabase) -> hir_def::attr::Attrs;
42} 41}
43 42
44pub(crate) fn attributes_query( 43pub(crate) fn attributes_query(
45 db: &(impl DefDatabase + AstDatabase), 44 db: &(impl DefDatabase + AstDatabase),
46 def: AttrDef, 45 def: AttrDef,
47) -> Option<Arc<[Attr]>> { 46) -> hir_def::attr::Attrs {
48 match def { 47 match def {
49 AttrDef::Module(it) => { 48 AttrDef::Module(it) => {
50 let src = it.declaration_source(db)?; 49 let src = match it.declaration_source(db) {
50 Some(it) => it,
51 None => return hir_def::attr::Attrs::default(),
52 };
51 let hygiene = Hygiene::new(db, src.file_id); 53 let hygiene = Hygiene::new(db, src.file_id);
52 Attr::from_attrs_owner(&src.value, &hygiene) 54 Attr::from_attrs_owner(&src.value, &hygiene)
53 } 55 }
@@ -57,7 +59,7 @@ pub(crate) fn attributes_query(
57 let hygiene = Hygiene::new(db, src.file_id); 59 let hygiene = Hygiene::new(db, src.file_id);
58 Attr::from_attrs_owner(&named, &hygiene) 60 Attr::from_attrs_owner(&named, &hygiene)
59 } 61 }
60 FieldSource::Pos(..) => None, 62 FieldSource::Pos(..) => hir_def::attr::Attrs::default(),
61 }, 63 },
62 AttrDef::Adt(it) => match it { 64 AttrDef::Adt(it) => match it {
63 Adt::Struct(it) => attrs_from_ast(it, db), 65 Adt::Struct(it) => attrs_from_ast(it, db),
@@ -74,7 +76,7 @@ pub(crate) fn attributes_query(
74 } 76 }
75} 77}
76 78
77fn attrs_from_ast<T, D>(node: T, db: &D) -> Option<Arc<[Attr]>> 79fn attrs_from_ast<T, D>(node: T, db: &D) -> hir_def::attr::Attrs
78where 80where
79 T: HasSource, 81 T: HasSource,
80 T::Ast: ast::AttrsOwner, 82 T::Ast: ast::AttrsOwner,
@@ -86,7 +88,7 @@ where
86} 88}
87 89
88impl<T: Into<AttrDef> + Copy> Attrs for T { 90impl<T: Into<AttrDef> + Copy> Attrs for T {
89 fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>> { 91 fn attrs(&self, db: &impl HirDatabase) -> hir_def::attr::Attrs {
90 db.attrs((*self).into()) 92 db.attrs((*self).into())
91 } 93 }
92} 94}
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index ed0d68001..1f63be3b9 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -2,7 +2,7 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_def::attr::Attr; 5use hir_def::attr::Attrs;
6use ra_db::salsa; 6use ra_db::salsa;
7use ra_syntax::SmolStr; 7use ra_syntax::SmolStr;
8 8
@@ -61,7 +61,7 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
61 fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>; 61 fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
62 62
63 #[salsa::invoke(crate::code_model::attrs::attributes_query)] 63 #[salsa::invoke(crate::code_model::attrs::attributes_query)]
64 fn attrs(&self, def: crate::AttrDef) -> Option<Arc<[Attr]>>; 64 fn attrs(&self, def: crate::AttrDef) -> Attrs;
65} 65}
66 66
67#[salsa::query_group(HirDatabaseStorage)] 67#[salsa::query_group(HirDatabaseStorage)]