Remove documentation query

This commit is contained in:
Lukas Wirth 2020-12-07 18:06:46 +01:00
parent 03b886de53
commit b3652ef288
6 changed files with 26 additions and 59 deletions

View File

@ -38,7 +38,7 @@ macro_rules! impl_has_attrs {
}
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
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<Namespace>) -> Option<ModuleDef> {
let def = AttrDefId::$def_id(self.into());

View File

@ -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,

View File

@ -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<Documentation> {
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<SmolStr> 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)]

View File

@ -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<dyn AstDatabase> {
#[salsa::invoke(LangItems::lang_item_query)]
fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>;
// 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<Documentation>;
#[salsa::invoke(ImportMap::import_map_query)]
fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
}

View File

@ -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<str>);
@ -26,7 +19,7 @@ impl Into<String> 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<Documentation> {
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<N>(node: &N) -> Option<Documentation>
@ -94,7 +51,8 @@ fn merge_doc_comments_and_attrs(
) -> Option<String> {
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)
}

View File

@ -166,7 +166,6 @@ impl RootDatabase {
hir::db::ModuleLangItemsQuery
hir::db::CrateLangItemsQuery
hir::db::LangItemQuery
hir::db::DocumentationQuery
hir::db::ImportMapQuery
// HirDatabase