rust/crates/ra_hir/src/code_model/docs.rs

98 lines
2.6 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2019-06-08 11:36:39 +00:00
use std::sync::Arc;
use ra_syntax::ast;
2019-06-08 11:36:39 +00:00
use crate::{
2019-09-08 06:53:49 +00:00
db::{AstDatabase, DefDatabase, HirDatabase},
2019-10-08 17:33:43 +00:00
Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
Struct, StructField, Trait, TypeAlias, Union,
2019-06-08 11:36:39 +00:00
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DocDef {
Module(Module),
StructField(StructField),
2019-10-08 17:33:43 +00:00
Adt(Adt),
2019-06-08 11:36:39 +00:00
EnumVariant(EnumVariant),
Static(Static),
Const(Const),
Function(Function),
Trait(Trait),
TypeAlias(TypeAlias),
2019-06-08 11:48:56 +00:00
MacroDef(MacroDef),
2019-06-08 11:36:39 +00:00
}
impl_froms!(
DocDef: Module,
StructField,
2019-10-08 17:33:43 +00:00
Adt(Struct, Enum, Union),
2019-06-08 11:36:39 +00:00
EnumVariant,
Static,
Const,
Function,
Trait,
2019-06-08 11:48:56 +00:00
TypeAlias,
MacroDef
2019-06-08 11:36:39 +00:00
);
2019-01-24 01:13:36 +00:00
/// Holds documentation
2019-06-08 11:36:39 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);
impl Documentation {
2019-06-08 11:16:05 +00:00
fn new(s: &str) -> Documentation {
Documentation(s.into())
}
2019-06-08 11:16:05 +00:00
pub fn as_str(&self) -> &str {
2019-06-08 11:36:39 +00:00
&*self.0
}
}
impl Into<String> for Documentation {
fn into(self) -> String {
2019-06-08 11:36:39 +00: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 15:35:23 +00:00
node.doc_comment_text().map(|it| Documentation::new(&it))
}
2019-06-08 10:51:18 +00:00
2019-06-08 11:36:39 +00:00
pub(crate) fn documentation_query(
db: &(impl DefDatabase + AstDatabase),
def: DocDef,
) -> Option<Documentation> {
match def {
2019-11-20 06:40:36 +00:00
DocDef::Module(it) => docs_from_ast(&it.declaration_source(db)?.value),
DocDef::StructField(it) => match it.source(db).value {
2019-07-19 07:43:01 +00:00
FieldSource::Named(named) => docs_from_ast(&named),
2019-07-04 17:26:44 +00:00
FieldSource::Pos(..) => None,
2019-06-08 11:36:39 +00:00
},
2019-10-08 17:33:43 +00:00
DocDef::Adt(it) => match it {
2019-11-20 06:40:36 +00:00
Adt::Struct(it) => docs_from_ast(&it.source(db).value),
Adt::Enum(it) => docs_from_ast(&it.source(db).value),
Adt::Union(it) => docs_from_ast(&it.source(db).value),
2019-10-08 17:33:43 +00:00
},
2019-11-20 06:40:36 +00:00
DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).value),
DocDef::Static(it) => docs_from_ast(&it.source(db).value),
DocDef::Const(it) => docs_from_ast(&it.source(db).value),
DocDef::Function(it) => docs_from_ast(&it.source(db).value),
DocDef::Trait(it) => docs_from_ast(&it.source(db).value),
DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).value),
DocDef::MacroDef(it) => docs_from_ast(&it.source(db).value),
2019-06-08 10:51:18 +00:00
}
}
2019-06-08 11:36:39 +00:00
impl<T: Into<DocDef> + Copy> Docs for T {
2019-06-08 10:51:18 +00:00
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
2019-06-08 11:36:39 +00:00
db.documentation((*self).into())
2019-06-08 10:51:18 +00:00
}
}