rust/crates/ra_hir/src/docs.rs

97 lines
2.5 KiB
Rust
Raw Normal View History

2019-06-08 14:36:39 +03:00
use std::sync::Arc;
use ra_syntax::ast;
2019-06-08 14:36:39 +03:00
use crate::{
HirDatabase, DefDatabase, AstDatabase,
2019-06-08 14:48:56 +03:00
Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource, MacroDef,
2019-06-08 14:36:39 +03:00
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DocDef {
Module(Module),
StructField(StructField),
Struct(Struct),
Enum(Enum),
EnumVariant(EnumVariant),
Static(Static),
Const(Const),
Function(Function),
Union(Union),
Trait(Trait),
TypeAlias(TypeAlias),
2019-06-08 14:48:56 +03:00
MacroDef(MacroDef),
2019-06-08 14:36:39 +03:00
}
impl_froms!(
DocDef: Module,
StructField,
Struct,
Enum,
EnumVariant,
Static,
Const,
Function,
Union,
Trait,
2019-06-08 14:48:56 +03:00
TypeAlias,
MacroDef
2019-06-08 14:36:39 +03:00
);
2019-01-23 20:13:36 -05:00
/// Holds documentation
2019-06-08 14:36:39 +03:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);
impl Documentation {
2019-06-08 14:16:05 +03:00
fn new(s: &str) -> Documentation {
Documentation(s.into())
}
2019-06-08 14:16:05 +03:00
pub fn as_str(&self) -> &str {
2019-06-08 14:36:39 +03:00
&*self.0
}
}
impl Into<String> for Documentation {
fn into(self) -> String {
2019-06-08 14:36:39 +03:00
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> {
2019-01-26 10:35:23 -05:00
node.doc_comment_text().map(|it| Documentation::new(&it))
}
2019-06-08 13:51:18 +03:00
2019-06-08 14:36:39 +03:00
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)?.1),
DocDef::StructField(it) => match it.source(db).1 {
2019-06-08 13:51:18 +03:00
FieldSource::Named(named) => docs_from_ast(&*named),
FieldSource::Pos(..) => return None,
2019-06-08 14:36:39 +03:00
},
2019-06-11 02:34:34 +03:00
DocDef::Struct(it) => docs_from_ast(&*it.source(db).ast),
2019-06-11 03:17:29 +03:00
DocDef::Enum(it) => docs_from_ast(&*it.source(db).ast),
DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).ast),
2019-06-08 14:36:39 +03:00
DocDef::Static(it) => docs_from_ast(&*it.source(db).1),
DocDef::Const(it) => docs_from_ast(&*it.source(db).1),
2019-06-11 16:49:56 +03:00
DocDef::Function(it) => docs_from_ast(&*it.source(db).ast),
2019-06-08 14:36:39 +03:00
DocDef::Union(it) => docs_from_ast(&*it.source(db).1),
DocDef::Trait(it) => docs_from_ast(&*it.source(db).1),
DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1),
2019-06-08 14:48:56 +03:00
DocDef::MacroDef(it) => docs_from_ast(&*it.source(db).1),
2019-06-08 13:51:18 +03:00
}
}
2019-06-08 14:36:39 +03:00
impl<T: Into<DocDef> + Copy> Docs for T {
2019-06-08 13:51:18 +03:00
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
2019-06-08 14:36:39 +03:00
db.documentation((*self).into())
2019-06-08 13:51:18 +03:00
}
}