Move docs to hir_def

This commit is contained in:
Aleksey Kladov 2019-11-23 14:43:38 +03:00
parent 8692977d27
commit 958862093e
11 changed files with 118 additions and 134 deletions

View File

@ -9,7 +9,6 @@
ra_db::SourceDatabaseStorage,
hir::db::InternDatabaseStorage,
hir::db::AstDatabaseStorage,
hir::db::DefDatabaseStorage,
hir::db::DefDatabase2Storage,
hir::db::HirDatabaseStorage
)]

View File

@ -1,7 +1,6 @@
//! FIXME: write short doc here
pub(crate) mod src;
pub(crate) mod docs;
use std::sync::Arc;
@ -9,11 +8,12 @@
adt::VariantData,
body::scope::ExprScopes,
builtin_type::BuiltinType,
docs::Documentation,
nameres::per_ns::PerNs,
resolver::{HasResolver, TypeNs},
type_ref::TypeRef,
AdtId, ContainerId, CrateModuleId, EnumVariantId, HasModule, ImplId, LocalEnumVariantId,
LocalStructFieldId, Lookup, ModuleId, StructFieldId, UnionId,
ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
ModuleId, UnionId,
};
use hir_expand::{
diagnostics::DiagnosticSink,
@ -1024,18 +1024,17 @@ pub trait HasAttrs {
impl<T: Into<AttrDef>> HasAttrs for T {
fn attrs(self, db: &impl DefDatabase) -> Attrs {
let def = self.into();
match def {
AttrDef::Module(it) => db.attrs(it.id.into()),
AttrDef::StructField(it) => db.attrs(StructFieldId::from(it).into()),
AttrDef::Adt(it) => db.attrs(AdtId::from(it).into()),
AttrDef::Function(it) => db.attrs(it.id.into()),
AttrDef::EnumVariant(it) => db.attrs(EnumVariantId::from(it).into()),
AttrDef::Static(it) => db.attrs(it.id.into()),
AttrDef::Const(it) => db.attrs(it.id.into()),
AttrDef::Trait(it) => db.attrs(it.id.into()),
AttrDef::TypeAlias(it) => db.attrs(it.id.into()),
AttrDef::MacroDef(it) => db.attrs(it.id.into()),
}
let def: AttrDef = self.into();
db.attrs(def.into())
}
}
pub trait Docs {
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>;
}
impl<T: Into<AttrDef> + Copy> Docs for T {
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
let def: AttrDef = (*self).into();
db.documentation(def.into())
}
}

View File

@ -1,97 +0,0 @@
//! FIXME: write short doc here
use std::sync::Arc;
use ra_syntax::ast;
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
Struct, StructField, Trait, TypeAlias, Union,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DocDef {
Module(Module),
StructField(StructField),
Adt(Adt),
EnumVariant(EnumVariant),
Static(Static),
Const(Const),
Function(Function),
Trait(Trait),
TypeAlias(TypeAlias),
MacroDef(MacroDef),
}
impl_froms!(
DocDef: Module,
StructField,
Adt(Struct, Enum, Union),
EnumVariant,
Static,
Const,
Function,
Trait,
TypeAlias,
MacroDef
);
/// Holds documentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);
impl Documentation {
fn new(s: &str) -> Documentation {
Documentation(s.into())
}
pub fn as_str(&self) -> &str {
&*self.0
}
}
impl Into<String> for Documentation {
fn into(self) -> String {
self.as_str().to_owned()
}
}
pub trait Docs {
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>;
}
pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
node.doc_comment_text().map(|it| Documentation::new(&it))
}
pub(crate) fn documentation_query(
db: &(impl DefDatabase + AstDatabase),
def: DocDef,
) -> Option<Documentation> {
match def {
DocDef::Module(it) => docs_from_ast(&it.declaration_source(db)?.value),
DocDef::StructField(it) => match it.source(db).value {
FieldSource::Named(named) => docs_from_ast(&named),
FieldSource::Pos(..) => None,
},
DocDef::Adt(it) => match it {
Adt::Struct(it) => docs_from_ast(&it.source(db).value),
Adt::Enum(it) => docs_from_ast(&it.source(db).value),
Adt::Union(it) => docs_from_ast(&it.source(db).value),
},
DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).value),
DocDef::Static(it) => docs_from_ast(&it.source(db).value),
DocDef::Const(it) => docs_from_ast(&it.source(db).value),
DocDef::Function(it) => docs_from_ast(&it.source(db).value),
DocDef::Trait(it) => docs_from_ast(&it.source(db).value),
DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).value),
DocDef::MacroDef(it) => docs_from_ast(&it.source(db).value),
}
}
impl<T: Into<DocDef> + Copy> Docs for T {
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
db.documentation((*self).into())
}
}

View File

@ -5,7 +5,6 @@
use ra_db::salsa;
use crate::{
debug::HirDebugDatabase,
ids,
ty::{
method_resolution::CrateImplBlocks,
@ -18,23 +17,17 @@
pub use hir_def::db::{
BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
DefDatabase2, DefDatabase2Storage, EnumDataQuery, ExprScopesQuery, FunctionDataQuery,
GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, LangItemQuery,
ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, StaticDataQuery,
StructDataQuery, TraitDataQuery, TypeAliasDataQuery,
DefDatabase2, DefDatabase2Storage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage,
LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery,
StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery,
};
pub use hir_expand::db::{
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
ParseMacroQuery,
};
// This database uses `AstDatabase` internally,
#[salsa::query_group(DefDatabaseStorage)]
#[salsa::requires(AstDatabase)]
pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
#[salsa::invoke(crate::code_model::docs::documentation_query)]
fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
}
pub use DefDatabase2 as DefDatabase;
#[salsa::query_group(HirDatabaseStorage)]
#[salsa::requires(salsa::Database)]

View File

@ -4,14 +4,14 @@
//! are splitting the hir.
use hir_def::{
AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId,
AdtId, AssocItemId, AttrDefId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
GenericDefId, ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId,
};
use crate::{
ty::{CallableDef, TypableDef},
Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef,
Static, StructField, TypeAlias, VariantDef,
Adt, AssocItem, AttrDef, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef,
ModuleDef, Static, StructField, TypeAlias, VariantDef,
};
impl From<ra_db::CrateId> for Crate {
@ -240,3 +240,20 @@ fn from(def: StructField) -> Self {
StructFieldId { parent: def.parent.into(), local_id: def.id }
}
}
impl From<AttrDef> for AttrDefId {
fn from(def: AttrDef) -> Self {
match def {
AttrDef::Module(it) => AttrDefId::ModuleId(it.id),
AttrDef::StructField(it) => AttrDefId::StructFieldId(it.into()),
AttrDef::Adt(it) => AttrDefId::AdtId(it.into()),
AttrDef::Function(it) => AttrDefId::FunctionId(it.id),
AttrDef::EnumVariant(it) => AttrDefId::EnumVariantId(it.into()),
AttrDef::Static(it) => AttrDefId::StaticId(it.id),
AttrDef::Const(it) => AttrDefId::ConstId(it.id),
AttrDef::Trait(it) => AttrDefId::TraitId(it.id),
AttrDef::TypeAlias(it) => AttrDefId::TypeAliasId(it.id),
AttrDef::MacroDef(it) => AttrDefId::MacroDefId(it.id),
}
}
}

View File

@ -50,9 +50,8 @@ fn from(it: $sv) -> $e {
pub use crate::{
code_model::{
docs::{DocDef, Docs, Documentation},
src::{HasBodySource, HasSource},
Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Enum,
Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum,
EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock,
Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField,
Trait, TypeAlias, Union, VariantDef,
@ -70,6 +69,7 @@ fn from(it: $sv) -> $e {
pub use hir_def::{
builtin_type::BuiltinType,
docs::Documentation,
nameres::{per_ns::PerNs, raw::ImportId},
path::{Path, PathKind},
type_ref::Mutability,

View File

@ -14,7 +14,6 @@
ra_db::SourceDatabaseStorage,
db::InternDatabaseStorage,
db::AstDatabaseStorage,
db::DefDatabaseStorage,
db::DefDatabase2Storage,
db::HirDatabaseStorage
)]

View File

@ -10,6 +10,7 @@
attr::Attrs,
body::{scope::ExprScopes, Body, BodySourceMap},
data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData},
docs::Documentation,
generics::GenericParams,
lang_item::{LangItemTarget, LangItems},
nameres::{
@ -101,4 +102,9 @@ fn raw_items_with_source_map(
#[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>;
}

View File

@ -0,0 +1,68 @@
//! FIXME: write short doc here
use std::sync::Arc;
use hir_expand::either::Either;
use ra_syntax::ast;
use crate::{db::DefDatabase2, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup};
/// Holds documentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);
impl Into<String> for Documentation {
fn into(self) -> String {
self.as_str().to_owned()
}
}
impl Documentation {
fn new(s: &str) -> Documentation {
Documentation(s.into())
}
pub fn as_str(&self) -> &str {
&*self.0
}
pub(crate) fn documentation_query(
db: &impl DefDatabase2,
def: AttrDefId,
) -> Option<Documentation> {
match def {
AttrDefId::ModuleId(module) => {
let def_map = db.crate_def_map(module.krate);
let src = def_map[module.module_id].declaration_source(db)?;
docs_from_ast(&src.value)
}
AttrDefId::StructFieldId(it) => {
let src = it.parent.child_source(db);
match &src.value[it.local_id] {
Either::A(_tuple) => None,
Either::B(record) => docs_from_ast(record),
}
}
AttrDefId::AdtId(it) => match it {
AdtId::StructId(it) => docs_from_ast(&it.0.source(db).value),
AdtId::EnumId(it) => docs_from_ast(&it.source(db).value),
AdtId::UnionId(it) => docs_from_ast(&it.0.source(db).value),
},
AttrDefId::EnumVariantId(it) => {
let src = it.parent.child_source(db);
docs_from_ast(&src.value[it.local_id])
}
AttrDefId::StaticId(it) => docs_from_ast(&it.source(db).value),
AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value),
AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id.to_node(db)),
AttrDefId::ConstId(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: &impl ast::DocCommentsOwner) -> Option<Documentation> {
node.doc_comment_text().map(|it| Documentation::new(&it))
}

View File

@ -20,6 +20,7 @@
pub mod resolver;
pub mod data;
pub mod lang_item;
pub mod docs;
mod trace;

View File

@ -21,7 +21,6 @@
symbol_index::SymbolsDatabaseStorage,
hir::db::InternDatabaseStorage,
hir::db::AstDatabaseStorage,
hir::db::DefDatabaseStorage,
hir::db::DefDatabase2Storage,
hir::db::HirDatabaseStorage
)]