2019-11-23 05:43:38 -06:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use hir_expand::either::Either;
|
|
|
|
use ra_syntax::ast;
|
|
|
|
|
2019-11-23 05:44:43 -06:00
|
|
|
use crate::{db::DefDatabase, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, 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);
|
|
|
|
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))
|
|
|
|
}
|