From b3652ef2886e01f772559aa90df4c45e7c7fb1fd Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 7 Dec 2020 18:06:46 +0100 Subject: Remove documentation query --- crates/hir/src/attrs.rs | 2 +- crates/hir/src/db.rs | 12 +++++----- crates/hir_def/src/attr.rs | 16 +++++++++++++ crates/hir_def/src/db.rs | 6 ----- crates/hir_def/src/docs.rs | 48 +++------------------------------------ crates/ide_db/src/apply_change.rs | 1 - 6 files changed, 26 insertions(+), 59 deletions(-) diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs index c3e820d89..fb2631b3e 100644 --- a/crates/hir/src/attrs.rs +++ b/crates/hir/src/attrs.rs @@ -38,7 +38,7 @@ macro_rules! impl_has_attrs { } fn docs(self, db: &dyn HirDatabase) -> Option { let def = AttrDefId::$def_id(self.into()); - db.documentation(def) + db.attrs(def).docs() } fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option) -> Option { let def = AttrDefId::$def_id(self.into()); diff --git a/crates/hir/src/db.rs b/crates/hir/src/db.rs index 8c767b249..8d949b264 100644 --- a/crates/hir/src/db.rs +++ b/crates/hir/src/db.rs @@ -2,12 +2,12 @@ pub use hir_def::db::{ AttrsQuery, BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQueryQuery, - CrateLangItemsQuery, DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, - ExprScopesQuery, FunctionDataQuery, GenericParamsQuery, ImplDataQuery, ImportMapQuery, - InternConstQuery, InternDatabase, InternDatabaseStorage, InternEnumQuery, InternFunctionQuery, - InternImplQuery, InternStaticQuery, InternStructQuery, InternTraitQuery, InternTypeAliasQuery, - InternUnionQuery, ItemTreeQuery, LangItemQuery, ModuleLangItemsQuery, StaticDataQuery, - StructDataQuery, TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, + CrateLangItemsQuery, DefDatabase, DefDatabaseStorage, EnumDataQuery, ExprScopesQuery, + FunctionDataQuery, GenericParamsQuery, ImplDataQuery, ImportMapQuery, InternConstQuery, + InternDatabase, InternDatabaseStorage, InternEnumQuery, InternFunctionQuery, InternImplQuery, + InternStaticQuery, InternStructQuery, InternTraitQuery, InternTypeAliasQuery, InternUnionQuery, + ItemTreeQuery, LangItemQuery, ModuleLangItemsQuery, StaticDataQuery, StructDataQuery, + TraitDataQuery, TypeAliasDataQuery, UnionDataQuery, }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index b2ce7ca3c..7825290e6 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -5,6 +5,7 @@ use std::{ops, sync::Arc}; use cfg::{CfgExpr, CfgOptions}; use either::Either; use hir_expand::{hygiene::Hygiene, AstId, InFile}; +use itertools::Itertools; use mbe::ast_to_token_tree; use syntax::{ ast::{self, AstNode, AttrsOwner}, @@ -14,6 +15,7 @@ use tt::Subtree; use crate::{ db::DefDatabase, + docs::Documentation, item_tree::{ItemTreeId, ItemTreeNode}, nameres::ModuleSource, path::ModPath, @@ -140,6 +142,20 @@ impl Attrs { Some(cfg) => cfg_options.check(&cfg) != Some(false), } } + + pub fn docs(&self) -> Option { + let mut docs = String::new(); + self.by_key("doc") + .attrs() + .flat_map(|attr| match attr.input.as_ref()? { + AttrInput::Literal(s) => Some(s), + AttrInput::TokenTree(_) => None, + }) + .intersperse(&SmolStr::new_inline("\n")) + // No FromIterator for String + .for_each(|s| docs.push_str(s.as_str())); + if docs.is_empty() { None } else { Some(docs) }.map(|it| Documentation::new(&it)) + } } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs index 6d694de11..7f250da33 100644 --- a/crates/hir_def/src/db.rs +++ b/crates/hir_def/src/db.rs @@ -10,7 +10,6 @@ use crate::{ attr::Attrs, body::{scope::ExprScopes, Body, BodySourceMap}, data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData}, - docs::Documentation, generics::GenericParams, import_map::ImportMap, item_tree::ItemTree, @@ -105,11 +104,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast { #[salsa::invoke(LangItems::lang_item_query)] fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option; - // FIXME(https://github.com/rust-analyzer/rust-analyzer/issues/2148#issuecomment-550519102) - // Remove this query completely, in favor of `Attrs::docs` method - #[salsa::invoke(Documentation::documentation_query)] - fn documentation(&self, def: AttrDefId) -> Option; - #[salsa::invoke(ImportMap::import_map_query)] fn import_map(&self, krate: CrateId) -> Arc; } diff --git a/crates/hir_def/src/docs.rs b/crates/hir_def/src/docs.rs index 3e59a8f47..6a27effef 100644 --- a/crates/hir_def/src/docs.rs +++ b/crates/hir_def/src/docs.rs @@ -5,16 +5,9 @@ use std::sync::Arc; -use either::Either; use itertools::Itertools; use syntax::{ast, SmolStr}; -use crate::{ - db::DefDatabase, - src::{HasChildSource, HasSource}, - AdtId, AttrDefId, Lookup, -}; - /// Holds documentation #[derive(Debug, Clone, PartialEq, Eq)] pub struct Documentation(Arc); @@ -26,7 +19,7 @@ impl Into for Documentation { } impl Documentation { - fn new(s: &str) -> Documentation { + pub fn new(s: &str) -> Documentation { Documentation(s.into()) } @@ -40,42 +33,6 @@ impl Documentation { pub fn as_str(&self) -> &str { &*self.0 } - - pub(crate) fn documentation_query( - db: &dyn DefDatabase, - def: AttrDefId, - ) -> Option { - match def { - AttrDefId::ModuleId(module) => { - let def_map = db.crate_def_map(module.krate); - let src = def_map[module.local_id].declaration_source(db)?; - docs_from_ast(&src.value) - } - AttrDefId::FieldId(it) => { - let src = it.parent.child_source(db); - match &src.value[it.local_id] { - Either::Left(_tuple) => None, - Either::Right(record) => docs_from_ast(record), - } - } - AttrDefId::AdtId(it) => match it { - AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value), - }, - AttrDefId::EnumVariantId(it) => { - let src = it.parent.child_source(db); - docs_from_ast(&src.value[it.local_id]) - } - AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db.upcast())), - AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value), - AttrDefId::ImplId(_) => None, - } - } } pub(crate) fn docs_from_ast(node: &N) -> Option @@ -94,7 +51,8 @@ fn merge_doc_comments_and_attrs( ) -> Option { match (doc_comment_text, doc_attr_text) { (Some(mut comment_text), Some(attr_text)) => { - comment_text.push_str("\n"); + comment_text.reserve(attr_text.len() + 1); + comment_text.push('\n'); comment_text.push_str(&attr_text); Some(comment_text) } diff --git a/crates/ide_db/src/apply_change.rs b/crates/ide_db/src/apply_change.rs index 987191fe3..e2251f2b7 100644 --- a/crates/ide_db/src/apply_change.rs +++ b/crates/ide_db/src/apply_change.rs @@ -166,7 +166,6 @@ impl RootDatabase { hir::db::ModuleLangItemsQuery hir::db::CrateLangItemsQuery hir::db::LangItemQuery - hir::db::DocumentationQuery hir::db::ImportMapQuery // HirDatabase -- cgit v1.2.3