From 958862093e83083b188427246323047a2c9e7bab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:43:38 +0300 Subject: Move docs to hir_def --- crates/ra_hir_def/src/db.rs | 6 ++++ crates/ra_hir_def/src/docs.rs | 68 +++++++++++++++++++++++++++++++++++++++++++ crates/ra_hir_def/src/lib.rs | 1 + 3 files changed, 75 insertions(+) create mode 100644 crates/ra_hir_def/src/docs.rs (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index e87bd525a..1481868d0 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -10,6 +10,7 @@ use crate::{ 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 @@ pub trait DefDatabase2: InternDatabase + AstDatabase { #[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; } diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs new file mode 100644 index 000000000..1b5c85437 --- /dev/null +++ b/crates/ra_hir_def/src/docs.rs @@ -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); + +impl Into 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 { + 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 { + node.doc_comment_text().map(|it| Documentation::new(&it)) +} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 899510be4..1ba7c7ee3 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -20,6 +20,7 @@ pub mod generics; pub mod resolver; pub mod data; pub mod lang_item; +pub mod docs; mod trace; -- cgit v1.2.3