Make Attrs::from_attrs_owner private

This commit is contained in:
Jonas Schievink 2020-12-17 15:45:26 +01:00
parent 3f6077ffe7
commit 9231821c03
4 changed files with 13 additions and 40 deletions

View file

@ -1325,6 +1325,7 @@ impl Impl {
let item = src.file_id.is_builtin_derive(db.upcast())?; let item = src.file_id.is_builtin_derive(db.upcast())?;
let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id);
// FIXME: handle `cfg_attr`
let attr = item let attr = item
.value .value
.attrs() .attrs()

View file

@ -104,7 +104,7 @@ impl Attrs {
} }
} }
pub fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Attrs { fn from_attrs_owner(db: &dyn DefDatabase, owner: InFile<&dyn AttrsOwner>) -> Attrs {
let hygiene = Hygiene::new(db.upcast(), owner.file_id); let hygiene = Hygiene::new(db.upcast(), owner.file_id);
Attrs::new(owner.value, &hygiene) Attrs::new(owner.value, &hygiene)
} }

View file

@ -1,9 +1,7 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use either::Either; use either::Either;
use hir::{ use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource};
AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirFileId, InFile, ModuleSource,
};
use ide_db::base_db::{FileId, SourceDatabase}; use ide_db::base_db::{FileId, SourceDatabase};
use ide_db::{defs::Definition, RootDatabase}; use ide_db::{defs::Definition, RootDatabase};
use syntax::{ use syntax::{
@ -168,7 +166,7 @@ impl ToNav for FileSymbol {
focus_range: self.name_range, focus_range: self.name_range,
container_name: self.container_name.clone(), container_name: self.container_name.clone(),
description: description_from_symbol(db, self), description: description_from_symbol(db, self),
docs: docs_from_symbol(db, self), docs: None,
} }
} }
} }
@ -394,30 +392,6 @@ impl ToNav for hir::LifetimeParam {
} }
} }
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<Documentation> {
let parse = db.parse(symbol.file_id);
let node = symbol.ptr.to_node(parse.tree().syntax());
let file_id = HirFileId::from(symbol.file_id);
let it = match_ast! {
match node {
ast::Fn(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Struct(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Enum(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Trait(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Module(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::TypeAlias(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Const(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Static(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::RecordField(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::Variant(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
ast::MacroCall(it) => hir::Attrs::from_attrs_owner(db, InFile::new(file_id, &it)),
_ => return None,
}
};
it.docs()
}
/// Get a description of a symbol. /// Get a description of a symbol.
/// ///
/// e.g. `struct Name`, `enum Name`, `fn Name` /// e.g. `struct Name`, `enum Name`, `fn Name`

View file

@ -2,7 +2,7 @@ use std::fmt;
use assists::utils::test_related_attribute; use assists::utils::test_related_attribute;
use cfg::CfgExpr; use cfg::CfgExpr;
use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics}; use hir::{AsAssocItem, HasAttrs, InFile, Semantics};
use ide_db::RootDatabase; use ide_db::RootDatabase;
use itertools::Itertools; use itertools::Itertools;
use syntax::{ use syntax::{
@ -105,7 +105,7 @@ pub(crate) fn runnable(
match item { match item {
ast::Struct(it) => runnable_struct(sema, it, file_id), ast::Struct(it) => runnable_struct(sema, it, file_id),
ast::Fn(it) => runnable_fn(sema, it, file_id), ast::Fn(it) => runnable_fn(sema, it, file_id),
ast::Module(it) => runnable_mod(sema, it, file_id), ast::Module(it) => runnable_mod(sema, it),
_ => None, _ => None,
} }
} }
@ -116,9 +116,10 @@ fn runnable_fn(
fn_def: ast::Fn, fn_def: ast::Fn,
file_id: FileId, file_id: FileId,
) -> Option<Runnable> { ) -> Option<Runnable> {
let def = sema.to_def(&fn_def)?;
let name_string = fn_def.name()?.text().to_string(); let name_string = fn_def.name()?.text().to_string();
let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &fn_def)); let attrs = def.attrs(sema.db);
let kind = if name_string == "main" { let kind = if name_string == "main" {
RunnableKind::Bin RunnableKind::Bin
} else { } else {
@ -189,10 +190,10 @@ fn runnable_struct(
struct_def: ast::Struct, struct_def: ast::Struct,
file_id: FileId, file_id: FileId,
) -> Option<Runnable> { ) -> Option<Runnable> {
let def = sema.to_def(&struct_def)?;
let name_string = struct_def.name()?.text().to_string(); let name_string = struct_def.name()?.text().to_string();
let attrs = let attrs = def.attrs(sema.db);
Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &struct_def));
if !has_runnable_doc_test(&attrs) { if !has_runnable_doc_test(&attrs) {
return None; return None;
} }
@ -262,11 +263,7 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool {
}) })
} }
fn runnable_mod( fn runnable_mod(sema: &Semantics<RootDatabase>, module: ast::Module) -> Option<Runnable> {
sema: &Semantics<RootDatabase>,
module: ast::Module,
file_id: FileId,
) -> Option<Runnable> {
if !has_test_function_or_multiple_test_submodules(&module) { if !has_test_function_or_multiple_test_submodules(&module) {
return None; return None;
} }
@ -279,7 +276,8 @@ fn runnable_mod(
.filter_map(|it| it.name(sema.db)) .filter_map(|it| it.name(sema.db))
.join("::"); .join("::");
let attrs = Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &module)); let def = sema.to_def(&module)?;
let attrs = def.attrs(sema.db);
let cfg = attrs.cfg(); let cfg = attrs.cfg();
let nav = module_def.to_nav(sema.db); let nav = module_def.to_nav(sema.db);
Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg })