aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model/attrs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model/attrs.rs')
-rw-r--r--crates/ra_hir/src/code_model/attrs.rs25
1 files changed, 12 insertions, 13 deletions
diff --git a/crates/ra_hir/src/code_model/attrs.rs b/crates/ra_hir/src/code_model/attrs.rs
index 9e304217c..96da8c88c 100644
--- a/crates/ra_hir/src/code_model/attrs.rs
+++ b/crates/ra_hir/src/code_model/attrs.rs
@@ -5,10 +5,9 @@ use crate::{
5 Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, 5 Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
6 Struct, StructField, Trait, TypeAlias, Union, 6 Struct, StructField, Trait, TypeAlias, Union,
7}; 7};
8use hir_def::attr::Attr; 8use hir_def::attr::{Attr, Attrs};
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 {
@@ -37,17 +36,17 @@ impl_froms!(
37 MacroDef 36 MacroDef
38); 37);
39 38
40pub trait Attrs { 39pub trait HasAttrs {
41 fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>>; 40 fn attrs(&self, db: &impl HirDatabase) -> Attrs;
42} 41}
43 42
44pub(crate) fn attributes_query( 43pub(crate) fn attributes_query(db: &(impl DefDatabase + AstDatabase), def: AttrDef) -> Attrs {
45 db: &(impl DefDatabase + AstDatabase),
46 def: AttrDef,
47) -> Option<Arc<[Attr]>> {
48 match def { 44 match def {
49 AttrDef::Module(it) => { 45 AttrDef::Module(it) => {
50 let src = it.declaration_source(db)?; 46 let src = match it.declaration_source(db) {
47 Some(it) => it,
48 None => return Attrs::default(),
49 };
51 let hygiene = Hygiene::new(db, src.file_id); 50 let hygiene = Hygiene::new(db, src.file_id);
52 Attr::from_attrs_owner(&src.value, &hygiene) 51 Attr::from_attrs_owner(&src.value, &hygiene)
53 } 52 }
@@ -57,7 +56,7 @@ pub(crate) fn attributes_query(
57 let hygiene = Hygiene::new(db, src.file_id); 56 let hygiene = Hygiene::new(db, src.file_id);
58 Attr::from_attrs_owner(&named, &hygiene) 57 Attr::from_attrs_owner(&named, &hygiene)
59 } 58 }
60 FieldSource::Pos(..) => None, 59 FieldSource::Pos(..) => Attrs::default(),
61 }, 60 },
62 AttrDef::Adt(it) => match it { 61 AttrDef::Adt(it) => match it {
63 Adt::Struct(it) => attrs_from_ast(it, db), 62 Adt::Struct(it) => attrs_from_ast(it, db),
@@ -74,7 +73,7 @@ pub(crate) fn attributes_query(
74 } 73 }
75} 74}
76 75
77fn attrs_from_ast<T, D>(node: T, db: &D) -> Option<Arc<[Attr]>> 76fn attrs_from_ast<T, D>(node: T, db: &D) -> Attrs
78where 77where
79 T: HasSource, 78 T: HasSource,
80 T::Ast: ast::AttrsOwner, 79 T::Ast: ast::AttrsOwner,
@@ -85,8 +84,8 @@ where
85 Attr::from_attrs_owner(&src.value, &hygiene) 84 Attr::from_attrs_owner(&src.value, &hygiene)
86} 85}
87 86
88impl<T: Into<AttrDef> + Copy> Attrs for T { 87impl<T: Into<AttrDef> + Copy> HasAttrs for T {
89 fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>> { 88 fn attrs(&self, db: &impl HirDatabase) -> Attrs {
90 db.attrs((*self).into()) 89 db.attrs((*self).into())
91 } 90 }
92} 91}