mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Merge #2179
2179: Use HirDatabase to compute `is_deprecated` r=matklad a=martskins This PR fixes #2167 by introducing `attributes_query` and adding `fn attrs(&self, def: crate::AttrDef) -> Option<Arc<[Attr]>>;` to the `DefDatabase` trait. I'm a little concerned about the two spots in `attributes_query` where code is repeated, but I couldn't figure out a way to avoid that, so.. I welcome suggestions 😄 Co-authored-by: Martin Asquino <martin.asquino@gmail.com>
This commit is contained in:
commit
14e19c0df1
5 changed files with 111 additions and 14 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
pub(crate) mod src;
|
pub(crate) mod src;
|
||||||
pub(crate) mod docs;
|
pub(crate) mod docs;
|
||||||
|
pub(crate) mod attrs;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
92
crates/ra_hir/src/code_model/attrs.rs
Normal file
92
crates/ra_hir/src/code_model/attrs.rs
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||||
|
Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
|
||||||
|
Struct, StructField, Trait, TypeAlias, Union,
|
||||||
|
};
|
||||||
|
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 {
|
||||||
|
Module(Module),
|
||||||
|
StructField(StructField),
|
||||||
|
Adt(Adt),
|
||||||
|
Function(Function),
|
||||||
|
EnumVariant(EnumVariant),
|
||||||
|
Static(Static),
|
||||||
|
Const(Const),
|
||||||
|
Trait(Trait),
|
||||||
|
TypeAlias(TypeAlias),
|
||||||
|
MacroDef(MacroDef),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_froms!(
|
||||||
|
AttrDef: Module,
|
||||||
|
StructField,
|
||||||
|
Adt(Struct, Enum, Union),
|
||||||
|
EnumVariant,
|
||||||
|
Static,
|
||||||
|
Const,
|
||||||
|
Function,
|
||||||
|
Trait,
|
||||||
|
TypeAlias,
|
||||||
|
MacroDef
|
||||||
|
);
|
||||||
|
|
||||||
|
pub trait Attrs {
|
||||||
|
fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn attributes_query(
|
||||||
|
db: &(impl DefDatabase + AstDatabase),
|
||||||
|
def: AttrDef,
|
||||||
|
) -> Option<Arc<[Attr]>> {
|
||||||
|
match def {
|
||||||
|
AttrDef::Module(it) => {
|
||||||
|
let src = it.declaration_source(db)?;
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id);
|
||||||
|
Attr::from_attrs_owner(&src.ast, &hygiene)
|
||||||
|
}
|
||||||
|
AttrDef::StructField(it) => match it.source(db).ast {
|
||||||
|
FieldSource::Named(named) => {
|
||||||
|
let src = it.source(db);
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id);
|
||||||
|
Attr::from_attrs_owner(&named, &hygiene)
|
||||||
|
}
|
||||||
|
FieldSource::Pos(..) => None,
|
||||||
|
},
|
||||||
|
AttrDef::Adt(it) => match it {
|
||||||
|
Adt::Struct(it) => attrs_from_ast(it, db),
|
||||||
|
Adt::Enum(it) => attrs_from_ast(it, db),
|
||||||
|
Adt::Union(it) => attrs_from_ast(it, db),
|
||||||
|
},
|
||||||
|
AttrDef::EnumVariant(it) => attrs_from_ast(it, db),
|
||||||
|
AttrDef::Static(it) => attrs_from_ast(it, db),
|
||||||
|
AttrDef::Const(it) => attrs_from_ast(it, db),
|
||||||
|
AttrDef::Function(it) => attrs_from_ast(it, db),
|
||||||
|
AttrDef::Trait(it) => attrs_from_ast(it, db),
|
||||||
|
AttrDef::TypeAlias(it) => attrs_from_ast(it, db),
|
||||||
|
AttrDef::MacroDef(it) => attrs_from_ast(it, db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn attrs_from_ast<T, D>(node: T, db: &D) -> Option<Arc<[Attr]>>
|
||||||
|
where
|
||||||
|
T: HasSource,
|
||||||
|
T::Ast: ast::AttrsOwner,
|
||||||
|
D: DefDatabase + AstDatabase,
|
||||||
|
{
|
||||||
|
let src = node.source(db);
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id);
|
||||||
|
Attr::from_attrs_owner(&src.ast, &hygiene)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Into<AttrDef> + Copy> Attrs for T {
|
||||||
|
fn attrs(&self, db: &impl HirDatabase) -> Option<Arc<[Attr]>> {
|
||||||
|
db.attrs((*self).into())
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use hir_def::attr::Attr;
|
||||||
use ra_db::salsa;
|
use ra_db::salsa;
|
||||||
use ra_syntax::SmolStr;
|
use ra_syntax::SmolStr;
|
||||||
|
|
||||||
|
@ -75,6 +76,9 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
|
||||||
|
|
||||||
#[salsa::invoke(crate::code_model::docs::documentation_query)]
|
#[salsa::invoke(crate::code_model::docs::documentation_query)]
|
||||||
fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
|
fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
|
||||||
|
|
||||||
|
#[salsa::invoke(crate::code_model::attrs::attributes_query)]
|
||||||
|
fn attrs(&self, def: crate::AttrDef) -> Option<Arc<[Attr]>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[salsa::query_group(HirDatabaseStorage)]
|
#[salsa::query_group(HirDatabaseStorage)]
|
||||||
|
|
|
@ -61,6 +61,7 @@ use crate::{ids::MacroFileKind, resolve::Resolver};
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
adt::VariantDef,
|
adt::VariantDef,
|
||||||
code_model::{
|
code_model::{
|
||||||
|
attrs::{AttrDef, Attrs},
|
||||||
docs::{DocDef, Docs, Documentation},
|
docs::{DocDef, Docs, Documentation},
|
||||||
src::{HasBodySource, HasSource},
|
src::{HasBodySource, HasSource},
|
||||||
Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum,
|
Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum,
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
//! This modules takes care of rendering various definitions as completion items.
|
//! This modules takes care of rendering various definitions as completion items.
|
||||||
|
|
||||||
use hir::{db::HirDatabase, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk};
|
use hir::{db::HirDatabase, Attrs, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk};
|
||||||
use join_to_string::join;
|
use join_to_string::join;
|
||||||
use ra_syntax::ast::{AttrsOwner, NameOwner};
|
use ra_syntax::ast::NameOwner;
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::completion::{
|
use crate::completion::{
|
||||||
|
@ -18,11 +18,7 @@ impl Completions {
|
||||||
field: hir::StructField,
|
field: hir::StructField,
|
||||||
substs: &hir::Substs,
|
substs: &hir::Substs,
|
||||||
) {
|
) {
|
||||||
let ast_node = field.source(ctx.db).ast;
|
let is_deprecated = is_deprecated(field, ctx.db);
|
||||||
let is_deprecated = match ast_node {
|
|
||||||
hir::FieldSource::Named(m) => is_deprecated(m),
|
|
||||||
hir::FieldSource::Pos(m) => is_deprecated(m),
|
|
||||||
};
|
|
||||||
CompletionItem::new(
|
CompletionItem::new(
|
||||||
CompletionKind::Reference,
|
CompletionKind::Reference,
|
||||||
ctx.source_range(),
|
ctx.source_range(),
|
||||||
|
@ -185,7 +181,7 @@ impl Completions {
|
||||||
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), ¯o_declaration)
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), ¯o_declaration)
|
||||||
.kind(CompletionItemKind::Macro)
|
.kind(CompletionItemKind::Macro)
|
||||||
.set_documentation(docs.clone())
|
.set_documentation(docs.clone())
|
||||||
.set_deprecated(is_deprecated(ast_node))
|
.set_deprecated(is_deprecated(macro_, ctx.db))
|
||||||
.detail(detail);
|
.detail(detail);
|
||||||
|
|
||||||
builder = if ctx.use_item_syntax.is_some() {
|
builder = if ctx.use_item_syntax.is_some() {
|
||||||
|
@ -218,7 +214,7 @@ impl Completions {
|
||||||
CompletionItemKind::Function
|
CompletionItemKind::Function
|
||||||
})
|
})
|
||||||
.set_documentation(func.docs(ctx.db))
|
.set_documentation(func.docs(ctx.db))
|
||||||
.set_deprecated(is_deprecated(ast_node))
|
.set_deprecated(is_deprecated(func, ctx.db))
|
||||||
.detail(detail);
|
.detail(detail);
|
||||||
|
|
||||||
// Add `<>` for generic types
|
// Add `<>` for generic types
|
||||||
|
@ -250,7 +246,7 @@ impl Completions {
|
||||||
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
|
||||||
.kind(CompletionItemKind::Const)
|
.kind(CompletionItemKind::Const)
|
||||||
.set_documentation(constant.docs(ctx.db))
|
.set_documentation(constant.docs(ctx.db))
|
||||||
.set_deprecated(is_deprecated(ast_node))
|
.set_deprecated(is_deprecated(constant, ctx.db))
|
||||||
.detail(detail)
|
.detail(detail)
|
||||||
.add_to(self);
|
.add_to(self);
|
||||||
}
|
}
|
||||||
|
@ -266,13 +262,13 @@ impl Completions {
|
||||||
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
|
||||||
.kind(CompletionItemKind::TypeAlias)
|
.kind(CompletionItemKind::TypeAlias)
|
||||||
.set_documentation(type_alias.docs(ctx.db))
|
.set_documentation(type_alias.docs(ctx.db))
|
||||||
.set_deprecated(is_deprecated(type_def))
|
.set_deprecated(is_deprecated(type_alias, ctx.db))
|
||||||
.detail(detail)
|
.detail(detail)
|
||||||
.add_to(self);
|
.add_to(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) {
|
pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) {
|
||||||
let is_deprecated = is_deprecated(variant.source(ctx.db).ast);
|
let is_deprecated = is_deprecated(variant, ctx.db);
|
||||||
let name = match variant.name(ctx.db) {
|
let name = match variant.name(ctx.db) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
|
@ -291,8 +287,11 @@ impl Completions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_deprecated(node: impl AttrsOwner) -> bool {
|
fn is_deprecated(node: impl Attrs, db: &impl HirDatabase) -> bool {
|
||||||
node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated")
|
match node.attrs(db) {
|
||||||
|
None => false,
|
||||||
|
Some(attrs) => attrs.iter().any(|x| x.is_simple_atom("deprecated")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool {
|
fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue