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;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast;
|
2019-11-23 05:43:38 -06:00
|
|
|
|
2019-11-28 09:05:28 -06:00
|
|
|
use crate::{
|
|
|
|
db::DefDatabase,
|
|
|
|
src::{HasChildSource, HasSource},
|
2019-12-12 08:11:57 -06:00
|
|
|
AdtId, AttrDefId, Lookup,
|
2019-11-28 09:05:28 -06:00
|
|
|
};
|
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())
|
|
|
|
}
|
|
|
|
|
2020-06-03 05:14:56 -05:00
|
|
|
pub fn from_ast<N>(node: &N) -> Option<Documentation>
|
|
|
|
where
|
|
|
|
N: ast::DocCommentsOwner + ast::AttrsOwner,
|
|
|
|
{
|
|
|
|
docs_from_ast(node)
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:43:38 -06:00
|
|
|
pub fn as_str(&self) -> &str {
|
|
|
|
&*self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn documentation_query(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn 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)
|
|
|
|
}
|
2020-04-25 07:23:34 -05:00
|
|
|
AttrDefId::FieldId(it) => {
|
2019-11-23 05:43:38 -06:00
|
|
|
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-12-12 08:11:57 -06:00
|
|
|
AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value),
|
|
|
|
AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).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),
|
2020-03-13 10:05:46 -05:00
|
|
|
AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db.upcast())),
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 13:21:06 -05:00
|
|
|
pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation>
|
|
|
|
where
|
|
|
|
N: ast::DocCommentsOwner + ast::AttrsOwner,
|
|
|
|
{
|
|
|
|
let doc_comment_text = node.doc_comment_text();
|
|
|
|
let doc_attr_text = expand_doc_attrs(node);
|
|
|
|
let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
|
|
|
|
docs.map(|it| Documentation::new(&it))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_doc_comments_and_attrs(
|
|
|
|
doc_comment_text: Option<String>,
|
|
|
|
doc_attr_text: Option<String>,
|
|
|
|
) -> Option<String> {
|
|
|
|
match (doc_comment_text, doc_attr_text) {
|
|
|
|
(Some(mut comment_text), Some(attr_text)) => {
|
|
|
|
comment_text.push_str("\n\n");
|
|
|
|
comment_text.push_str(&attr_text);
|
|
|
|
Some(comment_text)
|
|
|
|
}
|
|
|
|
(Some(comment_text), None) => Some(comment_text),
|
|
|
|
(None, Some(attr_text)) => Some(attr_text),
|
|
|
|
(None, None) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
|
|
|
|
let mut docs = String::new();
|
|
|
|
for attr in owner.attrs() {
|
|
|
|
if let Some(("doc", value)) =
|
|
|
|
attr.as_simple_key_value().as_ref().map(|(k, v)| (k.as_str(), v.as_str()))
|
|
|
|
{
|
|
|
|
docs.push_str(value);
|
|
|
|
docs.push_str("\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if docs.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-05-31 10:33:48 -05:00
|
|
|
Some(docs.trim_end_matches("\n\n").to_owned())
|
2020-05-30 13:21:06 -05:00
|
|
|
}
|
2019-11-23 05:43:38 -06:00
|
|
|
}
|