From 9231821c031ddada08d37547664b276d5e631f62 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 17 Dec 2020 15:45:26 +0100 Subject: Make `Attrs::from_attrs_owner` private --- crates/hir/src/code_model.rs | 1 + crates/hir_def/src/attr.rs | 2 +- crates/ide/src/display/navigation_target.rs | 30 ++--------------------------- crates/ide/src/runnables.rs | 20 +++++++++---------- 4 files changed, 13 insertions(+), 40 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 84e8c8047..2c0e0eae0 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -1325,6 +1325,7 @@ impl Impl { let item = src.file_id.is_builtin_derive(db.upcast())?; let hygenic = hir_expand::hygiene::Hygiene::new(db.upcast(), item.file_id); + // FIXME: handle `cfg_attr` let attr = item .value .attrs() diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index c64b78445..45313f335 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -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); Attrs::new(owner.value, &hygiene) } diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 54b33a98e..8410bf5a2 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -1,9 +1,7 @@ //! FIXME: write short doc here use either::Either; -use hir::{ - AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirFileId, InFile, ModuleSource, -}; +use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource}; use ide_db::base_db::{FileId, SourceDatabase}; use ide_db::{defs::Definition, RootDatabase}; use syntax::{ @@ -168,7 +166,7 @@ impl ToNav for FileSymbol { focus_range: self.name_range, container_name: self.container_name.clone(), 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 { - 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. /// /// e.g. `struct Name`, `enum Name`, `fn Name` diff --git a/crates/ide/src/runnables.rs b/crates/ide/src/runnables.rs index 96462a7b0..a2a0ad43d 100644 --- a/crates/ide/src/runnables.rs +++ b/crates/ide/src/runnables.rs @@ -2,7 +2,7 @@ use std::fmt; use assists::utils::test_related_attribute; use cfg::CfgExpr; -use hir::{AsAssocItem, Attrs, HirFileId, InFile, Semantics}; +use hir::{AsAssocItem, HasAttrs, InFile, Semantics}; use ide_db::RootDatabase; use itertools::Itertools; use syntax::{ @@ -105,7 +105,7 @@ pub(crate) fn runnable( match item { ast::Struct(it) => runnable_struct(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, } } @@ -116,9 +116,10 @@ fn runnable_fn( fn_def: ast::Fn, file_id: FileId, ) -> Option { + let def = sema.to_def(&fn_def)?; 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" { RunnableKind::Bin } else { @@ -189,10 +190,10 @@ fn runnable_struct( struct_def: ast::Struct, file_id: FileId, ) -> Option { + let def = sema.to_def(&struct_def)?; let name_string = struct_def.name()?.text().to_string(); - let attrs = - Attrs::from_attrs_owner(sema.db, InFile::new(HirFileId::from(file_id), &struct_def)); + let attrs = def.attrs(sema.db); if !has_runnable_doc_test(&attrs) { return None; } @@ -262,11 +263,7 @@ fn has_runnable_doc_test(attrs: &hir::Attrs) -> bool { }) } -fn runnable_mod( - sema: &Semantics, - module: ast::Module, - file_id: FileId, -) -> Option { +fn runnable_mod(sema: &Semantics, module: ast::Module) -> Option { if !has_test_function_or_multiple_test_submodules(&module) { return None; } @@ -279,7 +276,8 @@ fn runnable_mod( .filter_map(|it| it.name(sema.db)) .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 nav = module_def.to_nav(sema.db); Some(Runnable { nav, kind: RunnableKind::TestMod { path }, cfg }) -- cgit v1.2.3