2019-11-24 08:34:36 -06:00
|
|
|
//! Defines hir documentation.
|
|
|
|
//!
|
|
|
|
//! This really shouldn't exist, instead, we should deshugar doc comments into attributes, see
|
|
|
|
//! https://github.com/rust-analyzer/rust-analyzer/issues/2148#issuecomment-550519102
|
2019-11-23 05:43:38 -06:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2019-12-03 10:07:56 -06:00
|
|
|
use either::Either;
|
2019-11-23 05:43:38 -06:00
|
|
|
use ra_syntax::ast;
|
|
|
|
|
2019-11-28 09:05:28 -06:00
|
|
|
use crate::{
|
|
|
|
db::DefDatabase,
|
|
|
|
src::{HasChildSource, HasSource},
|
|
|
|
AdtId, AstItemDef, AttrDefId, Lookup,
|
|
|
|
};
|
2019-11-23 05:43:38 -06:00
|
|
|
|
|
|
|
/// 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(
|
2019-11-23 05:44:43 -06:00
|
|
|
db: &impl DefDatabase,
|
2019-11-23 05:43:38 -06:00
|
|
|
def: AttrDefId,
|
|
|
|
) -> Option<Documentation> {
|
|
|
|
match def {
|
|
|
|
AttrDefId::ModuleId(module) => {
|
|
|
|
let def_map = db.crate_def_map(module.krate);
|
2019-11-27 12:31:51 -06:00
|
|
|
let src = def_map[module.local_id].declaration_source(db)?;
|
2019-11-23 05:43:38 -06:00
|
|
|
docs_from_ast(&src.value)
|
|
|
|
}
|
|
|
|
AttrDefId::StructFieldId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
|
|
|
match &src.value[it.local_id] {
|
2019-12-03 10:07:56 -06:00
|
|
|
Either::Left(_tuple) => None,
|
|
|
|
Either::Right(record) => docs_from_ast(record),
|
2019-11-23 05:43:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AttrDefId::AdtId(it) => match it {
|
2019-12-12 07:58:04 -06:00
|
|
|
AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value),
|
2019-11-23 05:43:38 -06:00
|
|
|
AdtId::EnumId(it) => docs_from_ast(&it.source(db).value),
|
2019-11-25 08:30:50 -06:00
|
|
|
AdtId::UnionId(it) => docs_from_ast(&it.source(db).value),
|
2019-11-23 05:43:38 -06:00
|
|
|
},
|
|
|
|
AttrDefId::EnumVariantId(it) => {
|
|
|
|
let src = it.parent.child_source(db);
|
|
|
|
docs_from_ast(&src.value[it.local_id])
|
|
|
|
}
|
2019-12-12 07:34:03 -06:00
|
|
|
AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value),
|
2019-12-05 08:10:33 -06:00
|
|
|
AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)),
|
2019-11-23 05:43:38 -06:00
|
|
|
AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
|
2019-11-24 06:13:56 -06:00
|
|
|
AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
|
2019-11-23 05:43:38 -06:00
|
|
|
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))
|
|
|
|
}
|