From e42f9627664cc3c44094e1c4f985270fbfd592b1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 11:27:47 +0300 Subject: Encapsulate Attrs --- crates/ra_hir/src/code_model/attrs.rs | 16 +++++++++------- crates/ra_hir/src/db.rs | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir/src') 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::{ use hir_def::attr::Attr; use hir_expand::hygiene::Hygiene; use ra_syntax::ast; -use std::sync::Arc; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum AttrDef { @@ -38,16 +37,19 @@ impl_froms!( ); pub trait Attrs { - fn attrs(&self, db: &impl HirDatabase) -> Option>; + fn attrs(&self, db: &impl HirDatabase) -> hir_def::attr::Attrs; } pub(crate) fn attributes_query( db: &(impl DefDatabase + AstDatabase), def: AttrDef, -) -> Option> { +) -> hir_def::attr::Attrs { match def { AttrDef::Module(it) => { - let src = it.declaration_source(db)?; + let src = match it.declaration_source(db) { + Some(it) => it, + None => return hir_def::attr::Attrs::default(), + }; let hygiene = Hygiene::new(db, src.file_id); Attr::from_attrs_owner(&src.value, &hygiene) } @@ -57,7 +59,7 @@ pub(crate) fn attributes_query( let hygiene = Hygiene::new(db, src.file_id); Attr::from_attrs_owner(&named, &hygiene) } - FieldSource::Pos(..) => None, + FieldSource::Pos(..) => hir_def::attr::Attrs::default(), }, AttrDef::Adt(it) => match it { Adt::Struct(it) => attrs_from_ast(it, db), @@ -74,7 +76,7 @@ pub(crate) fn attributes_query( } } -fn attrs_from_ast(node: T, db: &D) -> Option> +fn attrs_from_ast(node: T, db: &D) -> hir_def::attr::Attrs where T: HasSource, T::Ast: ast::AttrsOwner, @@ -86,7 +88,7 @@ where } impl + Copy> Attrs for T { - fn attrs(&self, db: &impl HirDatabase) -> Option> { + fn attrs(&self, db: &impl HirDatabase) -> hir_def::attr::Attrs { db.attrs((*self).into()) } } 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 @@ use std::sync::Arc; -use hir_def::attr::Attr; +use hir_def::attr::Attrs; use ra_db::salsa; use ra_syntax::SmolStr; @@ -61,7 +61,7 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { fn documentation(&self, def: crate::DocDef) -> Option; #[salsa::invoke(crate::code_model::attrs::attributes_query)] - fn attrs(&self, def: crate::AttrDef) -> Option>; + fn attrs(&self, def: crate::AttrDef) -> Attrs; } #[salsa::query_group(HirDatabaseStorage)] -- cgit v1.2.3 From a87e9145a67634a5ea8a893ab8b52b3c07108a13 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 11:36:14 +0300 Subject: Rename Atts trait --- crates/ra_hir/src/code_model/attrs.rs | 21 +++++++++------------ crates/ra_hir/src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model/attrs.rs b/crates/ra_hir/src/code_model/attrs.rs index 9923cde81..96da8c88c 100644 --- a/crates/ra_hir/src/code_model/attrs.rs +++ b/crates/ra_hir/src/code_model/attrs.rs @@ -5,7 +5,7 @@ use crate::{ Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, }; -use hir_def::attr::Attr; +use hir_def::attr::{Attr, Attrs}; use hir_expand::hygiene::Hygiene; use ra_syntax::ast; @@ -36,19 +36,16 @@ impl_froms!( MacroDef ); -pub trait Attrs { - fn attrs(&self, db: &impl HirDatabase) -> hir_def::attr::Attrs; +pub trait HasAttrs { + fn attrs(&self, db: &impl HirDatabase) -> Attrs; } -pub(crate) fn attributes_query( - db: &(impl DefDatabase + AstDatabase), - def: AttrDef, -) -> hir_def::attr::Attrs { +pub(crate) fn attributes_query(db: &(impl DefDatabase + AstDatabase), def: AttrDef) -> Attrs { match def { AttrDef::Module(it) => { let src = match it.declaration_source(db) { Some(it) => it, - None => return hir_def::attr::Attrs::default(), + None => return Attrs::default(), }; let hygiene = Hygiene::new(db, src.file_id); Attr::from_attrs_owner(&src.value, &hygiene) @@ -59,7 +56,7 @@ pub(crate) fn attributes_query( let hygiene = Hygiene::new(db, src.file_id); Attr::from_attrs_owner(&named, &hygiene) } - FieldSource::Pos(..) => hir_def::attr::Attrs::default(), + FieldSource::Pos(..) => Attrs::default(), }, AttrDef::Adt(it) => match it { Adt::Struct(it) => attrs_from_ast(it, db), @@ -76,7 +73,7 @@ pub(crate) fn attributes_query( } } -fn attrs_from_ast(node: T, db: &D) -> hir_def::attr::Attrs +fn attrs_from_ast(node: T, db: &D) -> Attrs where T: HasSource, T::Ast: ast::AttrsOwner, @@ -87,8 +84,8 @@ where Attr::from_attrs_owner(&src.value, &hygiene) } -impl + Copy> Attrs for T { - fn attrs(&self, db: &impl HirDatabase) -> hir_def::attr::Attrs { +impl + Copy> HasAttrs for T { + fn attrs(&self, db: &impl HirDatabase) -> Attrs { db.attrs((*self).into()) } } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 8c6834392..915ca46fb 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -52,7 +52,7 @@ mod marks; pub use crate::{ code_model::{ - attrs::{AttrDef, Attrs}, + attrs::{AttrDef, HasAttrs}, docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource}, Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, -- cgit v1.2.3