make documenation a query
This commit is contained in:
parent
33026c654e
commit
2c28f5245d
3 changed files with 62 additions and 66 deletions
|
@ -196,7 +196,7 @@ impl Module {
|
|||
/// `None` for the crate root.
|
||||
pub fn declaration_source(
|
||||
self,
|
||||
db: &impl HirDatabase,
|
||||
db: &(impl DefDatabase + AstDatabase),
|
||||
) -> Option<(HirFileId, TreeArc<ast::Module>)> {
|
||||
let def_map = db.crate_def_map(self.krate);
|
||||
let decl = def_map[self.module_id].declaration?;
|
||||
|
|
|
@ -127,6 +127,9 @@ pub trait DefDatabase: SourceDatabase {
|
|||
|
||||
#[salsa::invoke(crate::lang_item::LangItems::lang_item_query)]
|
||||
fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>;
|
||||
|
||||
#[salsa::invoke(crate::docs::documentation_query)]
|
||||
fn documentation(&self, def: crate::docs::DocDef) -> Option<crate::docs::Documentation>;
|
||||
}
|
||||
|
||||
#[salsa::query_group(HirDatabaseStorage)]
|
||||
|
|
|
@ -1,10 +1,44 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::ast;
|
||||
|
||||
use crate::{HirDatabase, Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource};
|
||||
use crate::{
|
||||
HirDatabase, DefDatabase, AstDatabase,
|
||||
Module, StructField, Struct, Enum, EnumVariant, Static, Const, Function, Union, Trait, TypeAlias, FieldSource
|
||||
};
|
||||
|
||||
#[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),
|
||||
}
|
||||
|
||||
impl_froms!(
|
||||
DocDef: Module,
|
||||
StructField,
|
||||
Struct,
|
||||
Enum,
|
||||
EnumVariant,
|
||||
Static,
|
||||
Const,
|
||||
Function,
|
||||
Union,
|
||||
Trait,
|
||||
TypeAlias
|
||||
);
|
||||
|
||||
/// Holds documentation
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Documentation(String);
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Documentation(Arc<str>);
|
||||
|
||||
impl Documentation {
|
||||
fn new(s: &str) -> Documentation {
|
||||
|
@ -12,13 +46,13 @@ impl Documentation {
|
|||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
&*self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<String> for Documentation {
|
||||
fn into(self) -> String {
|
||||
self.0.clone()
|
||||
self.as_str().to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,71 +64,30 @@ pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documen
|
|||
node.doc_comment_text().map(|it| Documentation::new(&it))
|
||||
}
|
||||
|
||||
impl Docs for Module {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
self.declaration_source(db).and_then(|it| docs_from_ast(&*it.1))
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for StructField {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
match self.source(db).1 {
|
||||
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 {
|
||||
FieldSource::Named(named) => docs_from_ast(&*named),
|
||||
FieldSource::Pos(..) => return None,
|
||||
}
|
||||
},
|
||||
DocDef::Struct(it) => docs_from_ast(&*it.source(db).1),
|
||||
DocDef::Enum(it) => docs_from_ast(&*it.source(db).1),
|
||||
DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).1),
|
||||
DocDef::Static(it) => docs_from_ast(&*it.source(db).1),
|
||||
DocDef::Const(it) => docs_from_ast(&*it.source(db).1),
|
||||
DocDef::Function(it) => docs_from_ast(&*it.source(db).1),
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Struct {
|
||||
impl<T: Into<DocDef> + Copy> Docs for T {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Union {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Enum {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for EnumVariant {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Function {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Const {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Static {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for Trait {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Docs for TypeAlias {
|
||||
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||
docs_from_ast(&*self.source(db).1)
|
||||
db.documentation((*self).into())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue